source("global.R") set.seed(1010)
There are two commonly-used ways to set initial conditions: in $MAIN
and in
the initial condition list.
$MAIN
For a compartment called CMT
, there is a variable available to you called
CMT_0
that you can use to set the initial condition of that compartment in
$MAIN
. For example:
code <- ' $PARAM KIN = 200, KOUT = 50 $CMT RESP $MAIN RESP_0 = KIN/KOUT; '
This is the most commonly-used way to set initial conditions: the initial condition for the RESP
compartment is set equal to KIN
divided by KOUT
. If you had a parameter called BASE
, you could also write RESP_0 = BASE;
. In these examples, we're using data items from $PARAM
. But the initial condition could be set to any numeric value in the model, including individual parameters derived from parameters, covariates, and random effects. Note that you should never declare RESP_0
(e.g. double RESP_0
): it just appears for you to use.
init
listYou can also set initial conditions in the initials list. Most commonly, this means declaring compartments with $INIT
rather than $CMT
. For example
code <- ' $INIT RESP = 4 '
This method gets us the same result as the previous example, however the initial condition now is not a derived value, but it is coded as a number. Alternatively, you could declare a compartment via $CMT
and update later (see next).
We can update this value later like this
library(mrgsolve) mod <- mcode("init_up", code) init(mod) init(mod, RESP=8) %>% init
This method is commonly used to set initial conditions in large QSP models where the compartment starts out as some known or assumed steady state value.
Using an initial condition to put a starting dose in a compartment is not recommended. Always use a dosing event for that.
The following is from a wiki post I did on the topic. It's pedantic. But hopefully helpful to learn what mrgsolve
is doing for those who want to know.
mrgsolve
keeps a base list of compartments and initial conditions that you can update either from R
or from inside the model specification$CMT
, the value in that base list is assumed to be 0 for every compartmentmrgsolve
will by default use the values in that base list when starting the problem$MAIN
to set the initial condition$MAIN RESP_0 = KIN/KOUT;
when KIN
and KOUT
have some value in $PARAM
$MAIN
overwrites the value in the base list for the current ID
$MAIN
init
behaviorNote: IFLAG
is my invention only for this demo. The demo
is always responsible for setting and interpreting the value (it is not
reserved in any way and mrgsolve
does not control the value).
For this demo
A
initial condition defaults to 0A
initial condition will get set to BASE
only if IFLAG > 0
A
always stays at the initial condition (the system doesn't advance)code <- ' $PARAM BASE=200, IFLAG = 0 $CMT A $MAIN if(IFLAG > 0) A_0 = BASE; $ODE dxdt_A = 0; '
mod <- mcode("init_long",code) dplot <- function(x) plot(x,ylim=c(0,400))
Check the initial condition
init(mod)
Note:
$CMT
in the model spec; that implies that the base initial condition for A
is set to 0$MAIN
doesn't get run because IFLAG
is 0$MAIN
the initial condition is as we set it in the base listmod %>% mrgsim %>% dplot
Next, we update the base initial condition for A
to 100
Note:
$MAIN
still doesn't get run because IFLAG
is 0mod %>% init(A = 100) %>% mrgsim %>% dplot
Now, turn on IFLAG
Note:
$MAIN
gets runA_0
is set to the value of BASE
mod %>% param(IFLAG=1) %>% mrgsim %>% dplot mod %>% param(IFLAG=1, BASE=300) %>% mrgsim %>% dplot
Just to be clear, there is no need to set any sort of flag to set the initial condition.
code <- ' $PARAM AUC=0, AUC50 = 75, KIN=200, KOUT=5 $CMT RESP $MAIN RESP_0 = KIN/KOUT; $ODE dxdt_RESP = KIN*(1-AUC/(AUC50+AUC)) - KOUT*RESP; '
mod <- mcode("init_long2", code)
The initial condition is set to 40 per the values of KIN
and KOUT
mod %>% mrgsim %>% plot
Even when we change RESP_0
in R
, the calculation in $MAIN
gets the final say
mod %>% init(RESP=1E9) %>% mrgsim
init
will let you check to see what is going oninit
first takes the base initial condition list, then calls $MAIN
and does any calculation
you have in there; so the result is the calculated initialsinit(mod) mod %>% param(KIN=100) %>% init
idata
Go back to house model
mod <- mrgsolve:::house() init(mod)
Notes
idata
(only), include a column with CMT_0
(like you'd
do in $MAIN
).idata
value will override the
base initial list for that subject. CMT_0
is set in $MAIN
, that will override the idata
update. idata <- expand.idata(CENT_0 = seq(0,25,1)) idata %>% head out <- mod %>% idata_set(idata) %>% mrgsim(end=40)
plot(out, CENT~.)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.