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

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

### Simulate with all variabilities

Let's use a simple 1-compartment model with absorption compartment to illustrate the different levels of variabilities.

```{r}
ref_model <- model_suite$nonmem$advan2_trans2
ref_model
```

We're going to use a very basic dataset. 1000 mg QD shall be administered for a week.

```{r}
ds <- Dataset(25) %>%
  add(Bolus(time = 0, amount = 1000, ii = 24, addl = 6)) %>%
  add(Observations(times = seq(0, 24 * 7, by = 4)))
```

All subjects are different due to IIV and RUV.

```{r uncertainties_all_enabled , fig.align='center', fig.height=4, fig.width=8, message=F}
results <- simulate(model = ref_model, dataset = ds, seed = 1)
spaghetti_plot(results, "CONC_ERR")
```

### Simulate without RUV

Disabling RUV is done as follows:

```{r}
model <- ref_model %>%
  disable(c("RUV"))
model@parameters
```

In that case, CONC_ERR (the observed concentration) is identical as CONC (the model-simulated plasma concentration).

```{r uncertainties_no_ruv_y , fig.align='center', fig.height=4, fig.width=8, message=F}
results <- simulate(model = model, dataset = ds, seed = 1)
spaghetti_plot(results, "CONC_ERR")
```

```{r uncertainties_no_ruv_cp , fig.align='center', fig.height=4, fig.width=8, message=F}
spaghetti_plot(results, "CONC")
```

### Simulate without RUV and IIV

Disabling RUV and IIV is done as follows:

```{r}
model <- ref_model %>%
  disable(c("IIV", "RUV"))
model@parameters
```

Now, the typical profile is shown for all subjects.

```{r uncertainties_no_ruv_no_iiv , fig.align='center', fig.height=4, fig.width=8, message=F}
results <- model %>%
  simulate(dataset = ds, seed = 1)
spaghetti_plot(results, "CONC_ERR")
```
