---
title: "Infusions"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Infusions}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, results='asis', echo=F, message=F, warning=F}
if (campsis::on_cran()) {
  cat(
    "This vignette was not built on CRAN. Please check out the online version [here](https://calvagone.github.io/campsis.doc/articles/v06_infusions.html)."
  )
  knitr::knit_exit()
}
```

```{r, results='hide', echo=F, message=F, warning=F}
library(campsis)
```

There are 2 ways to implement infusions in Campsis:

* in the model: infusion duration or rate is defined for each compartment
* in the dataset: infusion duration or rate is defined for infusion

In the first case, the simulation engine will take care of the infusion duration or rate (RATE in dataset will be -1 or -2).
In the second case, Campsis will inject specific values in the RATE column of the dataset.

### Infusion duration or rate implemented in model

Let's use a 2-compartment model without absorption compartment to illustrate how this can be achieved.

```{r}
model <- model_suite$nonmem$advan3_trans4
```

For this example, we're going to define a lag time `D1` for this absorption compartment.

First let's create a new parameter `D1`, log-normally distributed with a median of 5 hours and 20% CV.

```{r}
model <- model %>%
  add(Theta(name = "D1", value = 5)) %>%
  add(Omega(name = "D1", value = 20, type = "cv%"))
```

Now, let's add an equation to the drug model to define `D1`.
```{r}
model <- model %>%
  add(Equation("D1", "THETA_D1*exp(ETA_D1)"))
```

Finally, we need to tell Campsis that `D1` corresponds the infusion duration for the first compartment.

```{r}
model <- model %>%
  add(InfusionDuration(compartment = 1, rhs = "D1"))
```

Our persisted drug model would look like this:

```{r}
model
```

Now, let's infuse 1000 mg and run the simulation.

```{r}
ds1 <- Dataset(50) %>%
  add(Infusion(time = 0, amount = 1000)) %>%
  add(Observations(times = seq(0, 24, by = 0.5)))
```


```{r infusion_model , fig.align='center', fig.height=4, fig.width=8, message=F}
results_d1 <- simulate(model = model, dataset = ds1, seed = 1)
shaded_plot(results_d1, "CONC")
```

### Infusion duration or rate implemented in dataset

The same simulation can be performed by defining the infusion duration in the dataset.

For this, we need to sample `D1` values. This can be done as follows:

```{r, results='hide', echo=F, message=F, warning=F}
set.seed(1)
```

```{r}
distribution <- ParameterDistribution(
  model = model,
  theta = "D1",
  omega = "D1"
) %>%
  sample(50L)
```

We can then pass the pre-sampled distribution.

```{r}
ds2 <- Dataset(50) %>%
  add(Infusion(time = 0, amount = 1000, duration = distribution)) %>%
  add(Observations(times = seq(0, 24, by = 0.5)))
```

Here is an overview of the dataset in its table form if we filter on the doses:

```{r}
ds2 %>% export(dest = "rxode2") %>% dosing_only() %>% head()
```

Let's now simulate this dataset using the original model.

```{r infusion_dataset , fig.align='center', fig.height=4, fig.width=8, message=F}
results_d1 <- simulate(
  model = model_suite$nonmem$advan4_trans4,
  dataset = ds2,
  seed = 1
)
shaded_plot(results_d1, "CONC")
```
