Skip to contents

This page shows a simple work-flow for directly simulating a different dosing paradigm than what was modeled taking into account the modeled uncertainty. This workflow is very similar to simply simulating without uncertainty in the parameters themselves.

Step 1: Import the model

library(nonmem2rx)
library(rxode2)
# its best practice to set the seed for the simulations
set.seed(42)
rxSetSeed(42)

# 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
mod <- nonmem2rx(ctlFile, lst=".res", save=FALSE, determineError=FALSE)
#>  getting information from  '/home/runner/work/_temp/Library/nonmem2rx/mods/cpt/runODE032.ctl'
#>  reading in xml file
#>  done
#>  reading in ext file
#>  done
#>  reading in phi file
#>  done
#>  reading in lst file
#>  abbreviated list parsing
#>  done
#>  done
#>  splitting control stream by records
#>  done
#>  Processing record $INPUT
#>  Processing record $MODEL
#>  Processing record $gTHETA
#>  Processing record $OMEGA
#>  Processing record $SIGMA
#>  Processing record $PROBLEM
#>  Processing record $DATA
#>  Processing record $SUBROUTINES
#>  Processing record $PK
#>  Processing record $DES
#>  Processing record $ERROR
#>  Processing record $ESTIMATION
#>  Ignore record $ESTIMATION
#>  Processing record $COVARIANCE
#>  Ignore record $COVARIANCE
#>  Processing record $TABLE
#>  change initial estimate of `theta1` to `1.37034036528946`
#>  change initial estimate of `theta2` to `4.19814911033061`
#>  change initial estimate of `theta3` to `1.38003493562413`
#>  change initial estimate of `theta4` to `3.87657341967489`
#>  change initial estimate of `theta5` to `0.196446108190896`
#>  change initial estimate of `eta1` to `0.101251418415006`
#>  change initial estimate of `eta2` to `0.0993872449483344`
#>  change initial estimate of `eta3` to `0.101302674763154`
#>  change initial estimate of `eta4` to `0.0730497519364148`
#>  read in nonmem input data (for model validation): /home/runner/work/_temp/Library/nonmem2rx/mods/cpt/Bolus_2CPT.csv
#>  ignoring lines that begin with a letter (IGNORE=@)'
#>  applying names specified by $INPUT
#>  subsetting accept/ignore filters code: .data[-which((.data$SD == 0)),]
#>  done
#>  read in nonmem IPRED data (for model validation): /home/runner/work/_temp/Library/nonmem2rx/mods/cpt/runODE032.csv
#>  done
#>  changing most variables to lower case
#>  done
#>  replace theta names
#>  done
#>  replace eta names
#>  done (no labels)
#>  renaming compartments
#>  done
#>  solving ipred problem
#>  done
#>  solving pred problem
#>  done

Step 2: Look at 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.

ev <- et(amt=120000, ii=12, until=24) %>%
  et(c(1:6, seq(8, 24, by=2))) %>%
  et(id=1:100)

Step 3: Solve using the uncertainty in the NONMEM model

To use the uncertainty in the model, it is a simple matter of telling how many times rxode2() should sample with nStud=X. In this case we will use 100.

s <- rxSolve(mod, ev, nStud=100)
#>  using nocb interpolation like NONMEM, specify directly to change
#>  using addlKeepsCov=TRUE like NONMEM, specify directly to change
#>  using addlDropSs=TRUE like NONMEM, specify directly to change
#>  using ssAtDoseTime=TRUE like NONMEM, specify directly to change
#>  using safeZero=FALSE since NONMEM does not use protection by default
#>  using ss2cancelAllPending=FALSE since NONMEM does not cancel pending doses with SS=2
#>  using dfSub=120 from NONMEM
#>  using dfObs=2280 from NONMEM
#>  using thetaMat from NONMEM
#>  using sigma from NONMEM
#>  using NONMEM specified atol=1e-12
#>  using NONMEM specified rtol=1e-06
#>  using NONMEM specified ssAtol=1e-12
#>  thetaMat has too many items, ignored: 'omega.2.1', 'omega.3.1', 'omega.3.2', 'omega.4.1', 'omega.4.2', 'omega.4.3'
#> [====|====|====|====|====|====|====|====|====|====] 0:00:01
#> Warning: corrected 'thetaMat' to be a symmetric, positive definite matrix

s
#> ── Solved rxode2 object ──
#> ── Parameters (x$params): ──
#> # A tibble: 10,000 × 11
#>    sim.id id    theta1 theta2 theta3 theta4   RSV    eta1   eta2       eta3
#>     <int> <fct>  <dbl>  <dbl>  <dbl>  <dbl> <dbl>   <dbl>  <dbl>      <dbl>
#>  1      1 1       1.35   4.20   1.33   3.90 0.194  0.139  -0.277 -0.279    
#>  2      1 2       1.35   4.20   1.33   3.90 0.194 -0.383  -0.502  0.0800   
#>  3      1 3       1.35   4.20   1.33   3.90 0.194 -0.409  -0.636  0.285    
#>  4      1 4       1.35   4.20   1.33   3.90 0.194  0.0952 -0.129  0.653    
#>  5      1 5       1.35   4.20   1.33   3.90 0.194 -0.0307 -0.282 -0.124    
#>  6      1 6       1.35   4.20   1.33   3.90 0.194 -0.349  -0.198  0.287    
#>  7      1 7       1.35   4.20   1.33   3.90 0.194 -0.124  -0.256 -0.0465   
#>  8      1 8       1.35   4.20   1.33   3.90 0.194  0.193  -0.196  0.328    
#>  9      1 9       1.35   4.20   1.33   3.90 0.194  0.391   0.664  0.0000844
#> 10      1 10      1.35   4.20   1.33   3.90 0.194 -0.0423 -0.215 -0.149    
#> # ℹ 9,990 more rows
#> # ℹ 1 more variable: eta4 <dbl>
#> ── Initial Conditions (x$inits): ──
#> CENTRAL    PERI 
#>       0       0 
#> 
#> Simulation with uncertainty in:
#> • parameters (x$thetaMat for changes)
#> • omega matrix (x$omegaList)
#> • sigma matrix (x$sigmaList)
#> 
#> ── First part of data (object): ──
#> # A tibble: 150,000 × 21
#>   sim.id    id  time    cl     v     q    v2    v1 scale1    k21    k12     f
#>    <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl> <dbl>
#> 1      1     1     1  4.44  50.7  2.85  30.8  50.7   50.7 0.0924 0.0563 2056.
#> 2      1     1     2  4.44  50.7  2.85  30.8  50.7   50.7 0.0924 0.0563 1795.
#> 3      1     1     3  4.44  50.7  2.85  30.8  50.7   50.7 0.0924 0.0563 1576.
#> 4      1     1     4  4.44  50.7  2.85  30.8  50.7   50.7 0.0924 0.0563 1392.
#> 5      1     1     5  4.44  50.7  2.85  30.8  50.7   50.7 0.0924 0.0563 1236.
#> 6      1     1     6  4.44  50.7  2.85  30.8  50.7   50.7 0.0924 0.0563 1104.
#> # ℹ 149,994 more rows
#> # ℹ 9 more variables: ipred <dbl>, rescv <dbl>, w <dbl>, ires <dbl>,
#> #   iwres <dbl>, y <dbl>, CENTRAL <dbl>, PERI <dbl>, DV <dbl>

Step 4: Summarize and plot

Since there is a bunch of data, a confidence band of the simulation with uncertainty would be helpful.

One way to do that is to select the interesting components, create a confidence interval and then plot the confidence bands:

sci <- confint(s, parm=c("CENTRAL", "PERI", "sim"))
#> summarizing data...done

sci
#> # A tibble: 90 × 7
#>        p1  time trt        p2.5     p50   p97.5 Percentile
#>     <dbl> <dbl> <fct>     <dbl>   <dbl>   <dbl> <fct>     
#>  1 0.0250     1 CENTRAL  87596.  93715.  98391. 2.5%      
#>  2 0.5        1 CENTRAL 105000. 106690. 108013. 50%       
#>  3 0.975      1 CENTRAL 112152. 113325. 114676. 97.5%     
#>  4 0.0250     2 CENTRAL  65940.  74349.  81703. 2.5%      
#>  5 0.5        2 CENTRAL  92536.  95332.  97680. 50%       
#>  6 0.975      2 CENTRAL 105080. 107200. 109794. 97.5%     
#>  7 0.0250     3 CENTRAL  49880.  60468.  68450. 2.5%      
#>  8 0.5        3 CENTRAL  82182.  85686.  88683. 50%       
#>  9 0.975      3 CENTRAL  98624. 101630. 105311. 97.5%     
#> 10 0.0250     4 CENTRAL  38839.  49728.  58630. 2.5%      
#> # ℹ 80 more rows

plot(sci)


plot(sci, log="y")