mrgsolve will automatically calculate time after dose (TAD) for you and include it in the simulated output.
doses <- ev (amt = 100 , ii = 6 , addl = 3 )
mod <- modlib ("pk1" )
. Loading model from cache.
mrgsim (mod, doses, tad = TRUE )
. Model: pk1
. Dim: 26 x 6
. Time: 0 to 24
. ID: 1
. ID time tad EV CENT CP
. 1: 1 0 0 0.0000 0.00 0.000
. 2: 1 0 0 100.0000 0.00 0.000
. 3: 1 1 1 36.7879 61.41 3.070
. 4: 1 2 2 13.5335 81.00 4.050
. 5: 1 3 3 4.9787 85.36 4.268
. 6: 1 4 4 1.8316 84.25 4.213
. 7: 1 5 5 0.6738 81.27 4.063
. 8: 1 6 0 100.2479 77.72 3.886
This works well when you need tad in the output; but what if you’d like to know tad while the model is running. For this case, use the tad plugin.
Simple approach
The self object has a .tad() method that will give you the time since last dose into any compartment.
[ param ] CL = 1 , V = 20
[ pkmodel ] advan = 1
[ error ]
capture tad = self. tad();
This is easy and works well if there is dosing into just one compartment.
mod <- mread ("model/tad-1.solv" )
. Building tad-1_solv ... done.
doses <- ev (amt = 100 , ii = 6 , addl = 3 )
out <- mrgsim (mod, doses, end = 48 , delta = 1 )
head (out, 12 )
. ID time A1 tad
. 1 1 0 0.00000 -1
. 2 1 0 100.00000 0
. 3 1 1 95.12294 1
. 4 1 2 90.48374 2
. 5 1 3 86.07080 3
. 6 1 4 81.87308 4
. 7 1 5 77.88008 5
. 8 1 6 174.08182 0
. 9 1 7 165.59175 1
. 10 1 8 157.51575 2
. 11 1 9 149.83361 3
. 12 1 10 142.52614 4
More customized approach
You can also use the tad plugin to calcuate tad for multiple different dosing compartments.
In the model, we initialize a tadose object in [ error ] using the static keyword. You can take this approach if you will only need to work with tad in [ error ]. Otherwise, you can construct the object in [ global ]. The argument to the constructor is the compartment number for doses you want to track.
Then call obj.tad(self) to get the current time after the most recent dose for the monitored compartment.
[ plugin] tad
[ param ] CL = 1 , V = 20
[ pkmodel ] advan = 1
[ error ]
static mrg:: tadose obj( 1 );
if ( NEWIND <= 1 ) obj. reset();
capture tad = obj. tad( self);
Note that you have to call obj.tad(self) on every record so the object can know when the doses are happening.
mod <- mread ("model/tad-2.solv" )
. Building tad-2_solv ... done.
doses <- ev (amt = 100 , ii = 6 , addl = 3 )
out <- mrgsim (mod, doses, end = 48 , delta = 1 )
head (out, 12 )
. ID time A1 tad
. 1 1 0 0.00000 -1
. 2 1 0 100.00000 0
. 3 1 1 95.12294 1
. 4 1 2 90.48374 2
. 5 1 3 86.07080 3
. 6 1 4 81.87308 4
. 7 1 5 77.88008 5
. 8 1 6 174.08182 0
. 9 1 7 165.59175 1
. 10 1 8 157.51575 2
. 11 1 9 149.83361 3
. 12 1 10 142.52614 4