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

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

This vignette shows how inter-occasion variability (IOV) can be added on a parameter at each administration.

### Treatment IOV on KA

Let's use a 2-compartment model with absorption compartment for this example.

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

We're going to add a term `IOV_KA` on parameter `KA`. This can be done as follows:

```{r}
model <- model %>%
  replace(Equation("KA", "THETA_KA*exp(ETA_KA + IOV_KA)"))
```

This model will not run unless we give some values for `IOV_KA`. This can achieved by adding IOV to the dataset.
The following code will create a simple dataset with IOV.

```{r}
ds_iov <- Dataset(50) %>%
  add(Bolus(time = c(0, 24, 48), amount = 1000, compartment = 1)) %>%
  add(Observations(times = seq(0, 72, by = 0.5))) %>%
  add(IOV("IOV_KA", distribution = NormalDistribution(mean = 0, sd = 1)))
```

To disable IOV on KA, the `replace` method can be used:

```{r}
ds_no_iov <- ds_iov %>%
  replace(IOV("IOV_KA", 0))
```

We can now run the model with IOV on `KA` and without.

```{r lag_time_model , fig.align='center', fig.height=4, fig.width=8, message=F}
results_iov <- simulate(model = model, dataset = ds_iov, seed = 1)
results_no_iov <- simulate(model = model, dataset = ds_no_iov, seed = 1)
gridExtra::grid.arrange(
  shaded_plot(results_iov, "CONC"),
  shaded_plot(results_no_iov, "CONC"),
  nrow = 1
)
```
