From eabe02f56c7d45852545b84a3741f5161db25fb7 Mon Sep 17 00:00:00 2001 From: Matthew Fidler Date: Tue, 17 Sep 2024 11:40:05 -0500 Subject: [PATCH] Add simulate extra items --- R/rxSolve.R | 2 +- vignettes/articles/rxode2-validate.Rmd | 3 +- vignettes/articles/simulate-extra-items.Rmd | 124 ++++++++++++++++++++ 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 vignettes/articles/simulate-extra-items.Rmd diff --git a/R/rxSolve.R b/R/rxSolve.R index 5d6eb09..12b37ed 100644 --- a/R/rxSolve.R +++ b/R/rxSolve.R @@ -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", diff --git a/vignettes/articles/rxode2-validate.Rmd b/vignettes/articles/rxode2-validate.Rmd index 38495b8..a166acc 100644 --- a/vignettes/articles/rxode2-validate.Rmd +++ b/vignettes/articles/rxode2-validate.Rmd @@ -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) diff --git a/vignettes/articles/simulate-extra-items.Rmd b/vignettes/articles/simulate-extra-items.Rmd new file mode 100644 index 0000000..126bfef --- /dev/null +++ b/vignettes/articles/simulate-extra-items.Rmd @@ -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) +```