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

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

There are 2 ways to implement bioavailability in CAMPSIS:

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

In the first case, the simulation engine will take care of the bioavailability.
In the second case, CAMPSIS will adapt automatically the amount injected through the dataset (AMT column).

### Bioavailability 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 bioavailability `F1` for this absorption compartment.

First let's create a new parameter `F1`, log-normally distributed with a median of 0.75 and 10% CV.

```{r}
model <- model %>% add(Theta(name="F1", value=0.75))
model <- model %>% add(Omega(name="F1", value=10, type="cv%"))
```

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

Finally, we need to tell CAMPSIS that `F1` corresponds to a bioavailability.

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

Our persisted drug model would look like this:

```{r}
model
```

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

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


```{r bioavailability_model , fig.align='center', fig.height=4, fig.width=8, message=F}
results_f1 <- model %>% simulate(dataset=ds1, seed=1)
results_no_f1 <- model_suite$nonmem$advan4_trans4 %>% simulate(dataset=ds1, seed=1)
gridExtra::grid.arrange(shadedPlot(results_f1, "CONC"), shadedPlot(results_no_f1, "CONC"), nrow=1)
```

### Bioavailability implemented in dataset

The same simulation can be performed by adapting the column `AMT` in the dataset.

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

We can then pass the pre-sampled distribution.

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

Let's have a look at the dataset, in its table form, and if we look at the doses only:

```{r}
ds2 %>% export(dest="RxODE") %>% dosingOnly() %>% head()
```

Finally, we can simulate the original model using this new dataset.

```{r bioavailability_dataset , fig.align='center', fig.height=4, fig.width=8, message=F}
results_f1 <- model_suite$nonmem$advan4_trans4 %>% simulate(dataset=ds2, seed=1)
shadedPlot(results_f1, "CONC")
```
