---
title: "Complex PK/PD models from literature"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Complex PK/PD models from literature}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, results='asis', echo=F, message=F, warning=F}
if (campsis::onCran()) {
  cat("This vignette was not built on CRAN. Please check out the online version [here](https://calvagone.github.io/campsis.doc/articles/v14_complex_pkpd_models.html).")
  knitr::knit_exit()
}
```

```{r, results='hide', echo=F, message=F, warning=F}
library(campsis)
```

This vignette intends to demonstrate that CAMPSIS can be used to implement almost any PK/PD model, including complex ones.

### Filgrastim PK/PD model

Load the filgrastim PK/PD model from the model library as follows. Please note that this model was translated from NONMEM code. The original model file can be found [here](http://repository.ddmore.eu/model/DDMODEL00000077) on the DDMORE repository. Eventually, this model was updated with the final parameters from the corresponding publication ([Krzyzanski et al., 2010](https://pubmed.ncbi.nlm.nih.gov/20881223/)).

```{r}
pkpd <- model_suite$literature$filgrastim_pkpd_krzyzanski
pkpd
```

Let's create a simple demonstration dataset of 250 subjects:

```{r}
baseDataset <- Dataset(250) %>% 
  add(Covariate("BAS", 0.02)) %>%
  add(Covariate("WT", UniformDistribution(50, 100))) %>%
  add(DoseAdaptation("WT*AMT")) %>% # per kilo dosing
  add(Observations(0:216)) %>%
  add(Covariate("ROUT", 0)) # subcutaneous route (SC)
```

Assume we want to compare the following subcutaneous administrations of filgrastim:

- 2.5 μg/kg QD for a week
- 5 μg/kg QD for a week
- 10 μg/kg QD for a week

We define the following scenarios:

```{r}
scenarios <- Scenarios() %>% 
  add(Scenario("2.5 μg/kg SC", dataset=~.x %>% add(Bolus(time=0, amount=2.5, compartment=1, ii=24, addl=6)))) %>%
  add(Scenario("5 μg/kg SC", dataset=~.x %>% add(Bolus(time=0, amount=5, compartment=1, ii=24, addl=6)))) %>%
  add(Scenario("10 μg/kg SC", dataset=~.x %>% add(Bolus(time=0, amount=10, compartment=1, ii=24, addl=6))))
```

A quick simulation gives us the plasma concentration of filgrastim, as well as the absolute neutrophil count (ANC):

```{r filgrastim_pkpd_model, fig.align='center', fig.height=6, fig.width=8, message=F}
library(ggplot2)

results <- pkpd %>% simulate(dataset=baseDataset, scenarios=scenarios, seed=1)
results <- results %>% dplyr::mutate(SCENARIO=factor(SCENARIO, levels=unique(SCENARIO)))

p1 <- shadedPlot(results, "CP", "SCENARIO") + facet_wrap(~SCENARIO) +
  scale_y_log10(breaks=c(.01,.1,1,10,100)) + ylab("G-CSF Serum Concentration (ng/mL)")
p2 <- shadedPlot(results, "A_14", "SCENARIO") + facet_wrap(~SCENARIO) + 
  ylab("ANC (10^3 cells/μL)")

gridExtra::grid.arrange(p1, p2, nrow=2)
```