Set initial conditions
There are two commonly-used ways to set initial conditions: in
$MAIN and in the initial condition list.
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
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
BASE, you could also write
RESP_0 = BASE;. In these examples, we’re using data items
$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
double RESP_0): it just
appears for you to use.
You can also set initial conditions in the initials list. Most
commonly, this means declaring compartments with
$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
. . Model initial conditions (N=1): . name value . name value . RESP (1) 4 | . ... .
. . Model initial conditions (N=1): . name value . name value . RESP (1) 8 | . ... .
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.
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.
mrgsolvekeeps a base list of compartments and initial conditions that you can update either from
Ror from inside the model specification
- When you use
$CMT, the value in that base list is assumed to be 0 for every compartment
mrgsolvewill by default use the values in that base list when starting the problem
- When only the base list is available, every individual will get the same initial condition
- When you use
- You can override this base list by including code
$MAINto set the initial condition
- Most often, you do this so that the initial is calculated as a function of a parameter
- For example,
$MAIN RESP_0 = KIN/KOUT;when
KOUThave some value in
- This code in
$MAINoverwrites the value in the base list for the current
- For typical PK/PD type models, we most frequently initialize in
- This is equivalent to what you might do in your NONMEM model
- For larger systems models, we often just set the initial value via the base list
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
For this demo
Ainitial condition defaults to 0
Ainitial condition will get set to
IFLAG > 0
Aalways 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; '
Check the initial condition
. . Model initial conditions (N=1): . name value . name value . A (1) 0 | . ... .
- We used
$CMTin the model spec; that implies that the base initial condition for
Ais set to 0
- In this chunk, the code in
$MAINdoesn’t get run because
- So, if we don’t update something in
$MAINthe initial condition is as we set it in the base list
Next, we update the base initial condition for
- The code in
$MAINstill doesn’t get run because
Now, turn on
- Now, that code in
A_0is set to the value of
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
Even when we change
$MAIN gets the final say
. Model: init_long2 . Dim: 25 x 3 . Time: 0 to 24 . ID: 1 . ID time RESP . 1: 1 0 40 . 2: 1 1 40 . 3: 1 2 40 . 4: 1 3 40 . 5: 1 4 40 . 6: 1 5 40 . 7: 1 6 40 . 8: 1 7 40
- It’s a good idea to get in the habit of doing this when things aren’t clear
initfirst takes the base initial condition list, then calls
$MAINand does any calculation you have in there; so the result is the calculated initials
. . Model initial conditions (N=1): . name value . name value . RESP (1) 40 | . ... .
. . Model initial conditions (N=1): . name value . name value . RESP (1) 20 | . ... .
Go back to house model
. . Model initial conditions (N=3): . name value . name value . CENT (2) 0 | RESP (3) 50 . GUT (1) 0 | . ... .
idata(only), include a column with
CMT_0(like you’d do in
- When each ID is simulated, the
idatavalue will override the base initial list for that subject.
- But note that if
CMT_0is set in
$MAIN, that will override the
. ID CENT_0 . 1 1 0 . 2 2 1 . 3 3 2 . 4 4 3 . 5 5 4 . 6 6 5