library(mrgsolve)
library(dplyr)
options(mrgsolve.soloc="build")
simeta
examplesimeta
with no
arguments until the baseline is between the specified boundsBASE
when
NEWIND <=1
… or whenever we are working on the first
record of an individual. This ensures that we don’t re-simulate
BASE
at every simulation record.i
) that ensures we
only try to resimulate 100 times. If a value for BASE
cannot be generated in 100 tries, we give up.FLAG
for your
problem. I am only using FLAG
here so we can selectively
call the simeta
code to demonstrate how it is working.<- '
code $PARAM TVBASE = 100, FLAG = 0
$CMT RESPONSE
$MAIN
if(NEWIND <=1) {
capture BASE = TVBASE*exp(EBASE);
int i = 0;
if(FLAG > 0) {
while((BASE < 80) || (BASE > 120)) {
if(++i > 100) {
mrg::report("There was a problem simulating BASE.");
break;
}
simeta();
BASE = TVBASE*exp(EBASE);
}
}
RESPONSE_0 = BASE;
}
$OMEGA @labels EBASE
1
$CAPTURE EBASE
'
<- mcode_cache("simeta", code) mod
simeta
system.time({
<- mod %>% mrgsim(nid=100, end=-1)
out <- summary(out)
sum })
. user system elapsed
. 0.004 0.000 0.004
print(sum)
. ID time RESPONSE EBASE
. Min. : 1.00 Min. :0 Min. : 13.45 Min. :-2.00624
. 1st Qu.: 25.75 1st Qu.:0 1st Qu.: 56.49 1st Qu.:-0.57110
. Median : 50.50 Median :0 Median :108.66 Median : 0.08301
. Mean : 50.50 Mean :0 Mean :158.53 Mean : 0.09402
. 3rd Qu.: 75.25 3rd Qu.:0 3rd Qu.:198.08 3rd Qu.: 0.68328
. Max. :100.00 Max. :0 Max. :739.21 Max. : 2.00042
. BASE
. Min. : 13.45
. 1st Qu.: 56.49
. Median :108.66
. Mean :158.53
. 3rd Qu.:198.08
. Max. :739.21
When we simulate with FLAG=0
, the simeta
code isn’t called and we BASE
values all
over the place.
simeta
system.time({
<- mod %>% mrgsim(nid=100, end=-1, param=list(FLAG=1))
out <- summary(out)
sum })
. user system elapsed
. 0.004 0.000 0.003
print(sum)
. ID time RESPONSE EBASE
. Min. : 1.00 Min. :0 Min. : 80.77 Min. :-0.21353
. 1st Qu.: 25.75 1st Qu.:0 1st Qu.: 89.06 1st Qu.:-0.11582
. Median : 50.50 Median :0 Median : 96.87 Median :-0.03180
. Mean : 50.50 Mean :0 Mean : 98.37 Mean :-0.02274
. 3rd Qu.: 75.25 3rd Qu.:0 3rd Qu.:108.52 3rd Qu.: 0.08178
. Max. :100.00 Max. :0 Max. :119.58 Max. : 0.17878
. BASE
. Min. : 80.77
. 1st Qu.: 89.06
. Median : 96.87
. Mean : 98.37
. 3rd Qu.:108.52
. Max. :119.58
When we simulate with FLAG=1
, the simeta
code is called and we BASE
values within
the specified bounds.
simeps
examplesimeta
example,
but we work in $TABLE
this time because we are calculating
CP
.<- '
code $PARAM CL = 1, V = 20, FLAG=0
$SIGMA 50
$PKMODEL cmt="CENT"
$TABLE
capture CP = CENT/V + EPS(1);
int i = 0;
while(CP < 0 && FLAG > 0) {
if(++i > 100) {
mrg::report("Problem simulating positive CP");
break;
}
simeps();
CP = CENT/V + EPS(1);
}
'
<- mcode_cache("simeps", code) mod
. Building simeps ... done.
simeps
system.time({
<- mod %>% ev(amt=100) %>% mrgsim(end=48)
out <- summary(out)
sum })
. user system elapsed
. 0.004 0.000 0.003
print(sum)
. ID time CENT CP
. Min. :1 Min. : 0.00 Min. : 0.00 Min. :-7.4847
. 1st Qu.:1 1st Qu.:11.25 1st Qu.: 15.93 1st Qu.:-0.1713
. Median :1 Median :23.50 Median : 29.38 Median : 3.1417
. Mean :1 Mean :23.52 Mean : 37.47 Mean : 3.2181
. 3rd Qu.:1 3rd Qu.:35.75 3rd Qu.: 54.21 3rd Qu.: 7.3496
. Max. :1 Max. :48.00 Max. :100.00 Max. :13.4523
Negative concentrations are simulated when we
don’t call the simeps
loop.
simeps
system.time({
<- mod %>% ev(amt=100) %>% mrgsim(end=48, param=list(FLAG=1))
out <- summary(out)
sum })
. user system elapsed
. 0.004 0.000 0.003
print(sum)
. ID time CENT CP
. Min. :1 Min. : 0.00 Min. : 0.00 Min. : 0.03374
. 1st Qu.:1 1st Qu.:11.25 1st Qu.: 15.93 1st Qu.: 2.50312
. Median :1 Median :23.50 Median : 29.38 Median : 5.67478
. Mean :1 Mean :23.52 Mean : 37.47 Mean : 6.85330
. 3rd Qu.:1 3rd Qu.:35.75 3rd Qu.: 54.21 3rd Qu.:10.21087
. Max. :1 Max. :48.00 Max. :100.00 Max. :20.74302
Better … all concentrations are positive.
mrgsolve: mrgsolve.github.io | metrum research group: metrumrg.com