---
title: "Absorption models"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Absorption models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Absorption with lag time

Specify the `lagtime` when you create the model using `new_ode_model`. The lag time can either be a number, or a string referring to a parameter (e.g. "ALAG"). If a single number or parameter is specified, the lag time will apply to the default dose compartment only. Alternatively, it is also possible to specify a vector of lag times, with a value for each compartment.


```{r absorption-lagtime-example}
library(PKPDsim)

pk <- new_ode_model(
  code = " dAdt[1] = -KA * A[1]; dAdt[2] = KA*A[1] -(CL/V) * A[2] ",
  obs = list(cmt = 2, scale = "V"),
  dose = list(cmt = 1),
  lagtime = "ALAG"
)

r <- new_regimen(
  amt = c(100, 100, 100),
  times = c(0, 12, 24),
  cmt = 1
)
p <- list(CL = 5, V  = 10, KA = 0.25, ALAG = 2)

res <- sim(
  ode = pk,
  n_ind = 25,
  omega = cv_to_omega(
    par_cv = list("CL" = 0.1, "V" = 0.1, "KA" = .1, "ALAG" = 0.3),
    p
  ),
  parameters = p,
  regimen = r,
  verbose = FALSE,
  only_obs = TRUE
)
```

# Absorption transit compartments

The code below implements the transit compartment model for absorption compartments as described by Savic et al. JPKPD 2007. Please note that this implementation assumes that all drug amount has been absorbed within the dosing interval. Such an assumption might not always hold. Also, for this implementation of dosing, it is essential to set the bioavailability for the dose compartment to 0, since dosing is now implemented using a separate analytical equation.

*Note: a simplified way of coding the transit compartments model will be added soon to PKPDsim.*

```{r transit-compartments-example}
library(PKPDsim)

parameters <- list(CL = 15, V = 50, MTT = 2.5, N = 4, KA = 1)
reg1 <- new_regimen(amt = 100, n = 3, interval = 12) # needs dummy doses

mod <- new_ode_model(
  code = "
  tad = t - t_prv_dose
  KTR = (N+1)/MTT
  LNFAC= log(2.506628)+(N+0.5)*log(N)-N
  dAdt[1] = exp(log(prv_dose) + log(KTR) + N*log(KTR * tad) - KTR * tad - LNFAC) - KA*A[1]
  dAdt[2] = KA*A[1]-CL/V*A[2]
",
  declare_variables = c("LNFAC", "KTR", "tad"),
  parameters = parameters,
  dose = list(cmt = 1, bioav = 0),
  obs = list(cmt = 2, scale = "V")
)

res <- sim(
  ode = mod,
  regimen = reg1,
  parameters = parameters,
  n = 5,
  omega = cv_to_omega(
    list(CL = 0.1, V = 0.1, MTT = 0.2, N = 0.1, KA = 0.1),
    parameters
  ),
  t_obs = seq(0, 36, .5)
)
```

.
