Run a simulation against an nlmixr-format model with rxode2
run_sim_nlmixr.RdInternal companion to run_sim(); called when the input model is a
pharmpy nlmixr-backend model. Uses rxode2::rxSolve() directly so we
can avoid the pharmpy-driven nlmixr fitting/simulation path (which
requires the Python pyreadr package).
Usage
run_sim_nlmixr(
fit = NULL,
data = NULL,
model = NULL,
id = irxutils::get_random_id("sim_"),
path = NULL,
n_iterations = 1,
variables = NULL,
add_pk_variables = FALSE,
output_file = "simtab",
seed = 12345,
verbose = TRUE,
model_code = NULL
)Arguments
- fit
a Pharmpy modelfit object.
- data
a NONMEM-format data.frame to use as the simulation dataset. Typically the output of
create_sim_dataset(). IfNULL, the dataset attached tomodelis used as-is.- model
either a Pharmpy model object, or a filename (for a model with NONMEM model code). If the latter,
run_sim()will attempt to load the model into Pharmpy first.- id
base run id (default a random
sim_*). NONMEM only: nlmixr2 simulations are solved in memory and write no run folders, soiddoes nothing there. Each regimen is run in its own subfolderid/regimen_<i>(<i>= 1-based regimen index), so regimens don't overwrite each other's output. Underuncertainty_engine = "replicates"each draw gets a folder of its own too,id/uncertainty_<r>/regimen_<i>(<r>= 1-based replicate index), so every replicate's NONMEM artifacts can be inspected afterwards and concurrent replicates cannot clobber each other.- path
ignored for the nlmixr2 backend: simulations run via
rxode2::rxSolve()and create no NONMEM-style run folders. Accepted only to keep the signature aligned withrun_sim().- n_iterations
number of iterations of the entire simulation to perform. The dataset for the simulation will stay the same between each iterations.
- variables
vector of variables to output. If
NULL, will output default variablesc("ID", "TIME", "DV", "EVID", "PRED")as well as all variables declared in the NONMEM code.- add_pk_variables
calculate basic PK variables: CMAX_OBS, TMAX_OBS, CMIN_OBS, and (when
CLis in the output table) AUC_SS. AUC_SS is derived as the last dose in the simulation dataset divided by CL.- output_file
TODO
- seed
TODO
- verbose
verbose output?
- model_code
pre-rendered nlmixr2/rxode2 model code (character), used instead of extracting it from
model. This is what lets a simulation run in a worker process: a Pharmpymodelis a Python (reticulate) object and cannot cross a process boundary, but its rendered code can. When supplied,model/fitare not needed anddatais required.
Details
Returns a data.frame in the same shape as the NONMEM-side simulation
output (ID, TIME, DV, IPRED, PRED, EVID, plus declared
variables and a regimen_label column), so downstream example code
that plots simulation results works unchanged.
PRED is the population prediction, obtained from a second solve with the
between-subject random effects zeroed (rxode2::zeroRe()) — rxSolve cannot
emit IPRED and PRED in one call. If that solve is unavailable (older
rxode2 without zeroRe()) or fails, PRED falls back to IPRED.