---
title: "Lag time"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Lag time}
  %\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/v05_lag_time.html)."
  )
  knitr::knit_exit()
}
```

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

There are 2 ways to implement a lag time in Campsis:

* in the model: lag time is defined for each compartment
* in the dataset: lag time is defined for each bolus or infusion

In the first case, the simulation engine will take care of the lag time
In the second case, Campsis will adapt automatically the time of the dose(s)

### Lag time implemented in model

Let's use a 2-compartment model with absorption compartment to illustrate how this can be achieved.

```{r}
model <- model_suite$nonmem$advan4_trans4
```

For this example, we're going to define a lag time `ALAG1` for this absorption compartment.

First let's create a new parameter `ALAG1`, log-normally distributed with a median of 2 hours and 20% CV.

```{r}
model <- model %>%
  add(Theta(name = "ALAG1", value = 2)) %>%
  add(Omega(name = "ALAG1", value = 20, type = "cv%"))
```

Now, let's add an equation to the drug model to define `ALAG1`.
```{r}
model <- model %>%
  add(Equation("ALAG1", "THETA_ALAG1*exp(ETA_ALAG1)"))
```

Finally, we need to tell Campsis that `ALAG1` corresponds to a lag time.

```{r}
model <- model %>%
  add(LagTime(compartment = 1, rhs = "ALAG1"))
```

Our persisted drug model would look like this:

```{r}
model
```

Now, let's now give a simple bolus and simulate with and without `ALAG1`.

```{r}
ds1 <- Dataset(50) %>%
  add(Bolus(time = 0, amount = 1000)) %>%
  add(Observations(times = seq(0, 24, by = 0.5)))
```


```{r lag_time_model , fig.align='center', fig.height=4, fig.width=8, message=F}
results_alag <- simulate(model = model, dataset = ds1, seed = 1)
results_no_alag <- simulate(
  model = model_suite$nonmem$advan4_trans4,
  dataset = ds1,
  seed = 1
)
gridExtra::grid.arrange(
  shaded_plot(results_alag, "CONC"),
  shaded_plot(results_no_alag, "CONC"),
  nrow = 1
)
```

### Lag time implemented in dataset

The same simulation can be performed by defining a lag time to the bolus in the dataset.

For this, we need to sample `ALAG1` 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 = "ALAG1",
  omega = "ALAG1"
) %>%
  sample(50L)
```

We can then pass the pre-sampled distribution.

```{r}
ds2 <- Dataset(50) %>%
  add(Bolus(time = 0, amount = 1000, lag = 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 lag_time_dataset , fig.align='center', fig.height=4, fig.width=8, message=F}
results_alag <- simulate(
  model = model_suite$nonmem$advan4_trans4,
  dataset = ds2,
  seed = 1
)
shaded_plot(results_alag, "CONC")
```
