Skip to content

Commit

Permalink
Add simulate extra items
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfidler committed Sep 17, 2024
1 parent 29a5345 commit eabe02f
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/rxSolve.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ rxSolve.monolix2rx <- function(object, params = NULL, events = NULL,
maxsteps = 70000L, hmin = 0, hmax = NA_real_, hmaxSd = 0,
hini = 0, maxordn = 12L, maxords = 5L, ..., cores, covsInterpolation = c("locf",
"linear", "nocb", "midpoint"), naInterpolation = c("locf",
"nocb"), keepInterpolation = c("locf", "nocb", "na"),
"nocb"), keepInterpolation = c("na", "locf", "nocb"),
addCov = TRUE, sigma = NULL, sigmaDf = NULL, sigmaLower = -Inf,
sigmaUpper = Inf, nCoresRV = 1L, sigmaIsChol = FALSE, sigmaSeparation = c("auto",
"lkj", "separation"), sigmaXform = c("identity", "variance",
Expand Down
3 changes: 2 additions & 1 deletion vignettes/articles/rxode2-validate.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ library(monolix2rx)
# In this case we will us the theophylline project included in monolix2rx
pkgTheo <- system.file("theo/theophylline_project.mlxtran", package="monolix2rx")
# Note you have to setup monolix2rx to use the model library or save the model as a separate file
# Note you have to setup monolix2rx to use the model library or save
# the model as a separate file
mod <- monolix2rx(pkgTheo)
print(mod)
Expand Down
124 changes: 124 additions & 0 deletions vignettes/articles/simulate-extra-items.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
---
title: "Simulate Derived Variables from imported Monolix model"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Simulate Derived Variables from imported Monolix model}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(rxode2)
setRxThreads(1L)
library(data.table)
setDTthreads(1L)
```

This page shows a simple work-flow for directly simulating a different
dosing paradigm with new derived items, in this case `AUC`.


## Step 1: Import the model

```{r setup}
library(monolix2rx)
library(rxode2)
# First we need the location of the nonmem control stream Since we are running an example, we will use one of the built-in examples in `nonmem2rx`
ctlFile <- system.file("mods/cpt/runODE032.ctl", package="nonmem2rx")
# You can use a control stream or other file. With the development
# version of `babelmixr2`, you can simply point to the listing file
# You use the path to the monolix mlxtran file
# In this case we will us the theophylline project included in monolix2rx
pkgTheo <- system.file("theo/theophylline_project.mlxtran", package="monolix2rx")
# Note you have to setup monolix2rx to use the model library or save
# the model as a separate file
mod <- monolix2rx(pkgTheo)
print(mod)
```

## Step 2: Add AUC calculation

The concentration in this case is the `Cc` from the model, a trick to
get the `AUC` is to have an additional ODE `d/dt(AUC) <- Cc` and use
some reset to get it per dosing period.

However, this additional parameter is not part of the original model.
The calculation of AUC would depend on the number of observations in
your model, and for sparse data wouldn't be terribly accurate.

One thing you can do is to use model piping append `d/dt(AUC) <- Cc` to
the imported model:

```{r modAuc}
modAuc <- mod %>%
model(d/dt(AUC) <- Cc, append=TRUE)
modAuc
```

You can also use `append=NA` to pre-pend or `append=f` to put the ODE
right after the `f` line in the model.

## Step 3: Setup event table to calculate the AUC for a different dosing paradigm:

Lets say that in this case instead of a single dose, we want to see
what the concentration profile is with a single day of BID dosing. In
this case is done by creating a [quick event
table](https://nlmixr2.github.io/rxode2/articles/rxode2-event-table.html).

In this case since we are also wanting `AUC` per dosing period, you
can add a reset dose to the `AUC` compartment every time a dose is given
(so it will only track the AUC of the current dose):

```{r eventTable}
ev <- et(amt=4, ii=12, until=24) %>%
et(amt=0, ii=12, until=24, cmt="AUC", evid=5) %>% # replace AUC with zero at dosing
et(c(0, 4, 8, 11.999, 12, 12.01, 14, 20, 23.999, 24, 24.001, 28, 32, 36)) %>%
et(id=1:10)
```

## Step 4: Solve using `rxode2`

In this step, we solve the model with the new event table for the 10
subjects:

```{r solve}
s <- rxSolve(modAuc, ev)
```

Note that since this derived from a `nonmem2rx` model, the default
solving will match the tolerances and methods specified in your
`NONMEM` model.

## Step 5: Exploring the simulation (by plotting), and summarizing (dplyr)

This solved object acts the same as any other `rxode2` solved object,
so you can use the `plot()` function to see the individual running AUC profiles
you simulated:

```{r plot}
library(ggplot2)
plot(s, AUC) +
ylab("Running AUC")
```

You can also select the points near the dosing to get the AUC for the
interval:

```{r gatherAuc}
library(dplyr)
s %>% filter(time %in% c(11.999, 23.999)) %>%
mutate(time=round(time)) %>%
select(id, time, AUC)
```

0 comments on commit eabe02f

Please sign in to comment.