---
title: "Introduction to spleenrbc"
output:
  bookdown::html_document2:
    base_format: rmarkdown::html_vignette
    code_folding: show
    toc: true
    number_sections: false
pkgdown:
  as_is: true
vignette: >
  %\VignetteIndexEntry{Introduction to spleenrbc}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = "center",
  fig.width = 7,
  fig.height = 5,
  fig.asp = NULL,
  out.width = "95%"
)
```

```{r setup}
library(spleenrbc, warn.conflicts = FALSE)
library(dplyr, warn.conflicts = FALSE)
library(tidyr)
library(ggplot2)
```

The `baseline_parameters()` function returns a list of parameter values for the given Plasmodium species.

```{r params}
pf_params <- baseline_parameters(species = "Pf")
pv_params <- baseline_parameters(species = "Pv")
```

You can modify the parameter values to reflect different scenarios.

```{r modified-params}
# Increase the phagocytosis rate for infected RBCs by 10%.
pf_mod_params <- baseline_parameters(species = "Pf")
pf_mod_params$lambdaI.sel <- 1.1 * pf_mod_params$lambdaI.sel

# No initial parasites, so the model should remain at homeostasis.
no_inf_params <- baseline_parameters(species = "Pf")
no_inf_params$I_t_1 <- 0
```

The `run_spleenrbc()` function accepts a parameter list, or a data frame where each row contains the parameters for a separate scenario.
It simulates the model for each scenario, and returns the results as a data frame.

```{r run}
pf_results <- run_spleenrbc(pf_params, days = 50)
pv_results <- run_spleenrbc(pv_params, days = 50)
pf_mod_results <- run_spleenrbc(pf_mod_params, days = 50)
no_inf_results <- run_spleenrbc(no_inf_params, days = 50)

# Collect the results of each scenario into a single data frame.
results <- bind_rows(
  pf_results |>
    mutate(scenario = "Pf (baseline)"),
  pv_results |>
    mutate(scenario = "Pv (baseline)"),
  pf_mod_results |>
    mutate(scenario = "Pf (increased phagocytosis)"),
  no_inf_results |>
    mutate(scenario = "No infection")
) |>
  pivot_longer(! c(scenario, time))
```

You can then plot model quantities, such as the number of uninfected RBCs in the circulation or in the spleen, for each scenario.

```{r plot-uc, class.source = 'fold-hide', fig.cap = 'Circulating uRBC populations for different scenarios.'}
ggplot(
  results |> filter(name == "U_t"),
  aes(time / 24, value, colour = scenario)
) +
  geom_line() +
  scale_colour_hue(name = NULL) +
  xlab("Time (days)") +
  scale_y_log10("Uninfected RBCs in the circulation")
```

```{r plot-ur, class.source = 'fold-hide', fig.cap = 'Splenic uRBC populations for different scenarios.'}
ggplot(
  results |> filter(name == "Ur_t"),
  aes(time / 24, value, colour = scenario)
) +
  geom_line() +
  scale_colour_hue(name = NULL) +
  xlab("Time (days)") +
  scale_y_log10("Uninfected RBCs in the spleen")
```
