Simulate an odin2 model from its initial conditions, once for each of
a collection of parameter sets such as posterior draws or scenarios.
The result has the same structure as the output of
monty::monty_sample(), so it can be passed to tidy_trajectories(),
tidy_pars() and to monty's own tools such as
monty::monty_samples_thin(). Use continue_parameter_sets() to
carry on from the end of a fit or of a previous run.
Usage
run_parameter_sets(
generator,
pars,
times,
packer = NULL,
fixed = NULL,
n_particles = 1,
deterministic = NULL,
n_samples = NULL,
time_start = NULL,
save_trajectories = TRUE,
dt = NULL,
ode_control = NULL,
seed = NULL,
n_threads = 1
)Arguments
- generator
An odin2 model generator, as returned by
odin2::odin(). When rerunning a fitted model, pass the filter or unfilter used for the fit instead: the model, its time step and start time, and whether it is deterministic are then taken from the fit rather than given again.- pars
The parameter sets. One of:
a
monty_samplesobject (requirespacker); chains are kepta data frame with one row per parameter set and one numeric column per parameter. Columns identifying the sample are not treated as parameters:
.chain,.iterationand.draw, as produced byposterior::as_draws_df(), orchain,iterationandsample, as used in this package. The chain column is used to keep chainsa list of parameter lists; without a
packer, elements that are the same in every set are treated as fixeda matrix with one column per parameter set (requires
packer)
- times
Times to save output at, increasing and no earlier than
time_start.- packer
A
monty::monty_packer()used to convert between parameter vectors and the list of parameters the model needs. Required ifparsis amonty_samplesobject or a matrix.- fixed
Optional named list of parameters held constant across all parameter sets, for example
list(N = 1000). These are added to every set, and replace any fixed values inpacker. They may not name a parameter that varies inpars.- n_particles
Number of particles (stochastic realisations) to run for each parameter set.
- deterministic
Logical, indicating that a stochastic model should be run deterministically, replacing random draws with their expectations. Has no effect on ODE models, which are always deterministic. By default
FALSE, or as the fit ifgeneratoris a filter or unfilter.- n_samples
Optional number of parameter sets to select at random from
pars. If given, the result has a single chain.- time_start
The time at which the model starts. By default 0, or as the fit if
generatoris a filter or unfilter.- save_trajectories
Either
TRUE, to save all state variables, or a character vector of variable names to save.- dt, ode_control
Time step for discrete-time models and control for ODE models, passed to
dust2::dust_system_create(). By default those of the fit ifgeneratoris a filter or unfilter, and otherwise the model's defaults.- seed, n_threads
Passed to
dust2::dust_system_create().
Value
An object of class monty_samples, with elements:
pars: parameters x samples x chainsdensity: samples x chains, allNAas no density is computedobservations$trajectories: state x time x samples x chains, or state x particle x time x samples x chains ifn_particles > 1observations$state: the final state, laid out as trajectories without the time dimension
Everything needed to tidy the result or continue the run is stored
as an attribute, so tidy_trajectories() and
continue_parameter_sets() need no further information.
Details
dust2 provides dust2::dust_system_simulate(), which runs a system
that has already been created. This function does the work around
that: turning the parameter sets into the form the model needs,
creating a system with one group per parameter set, running it, and
arranging the output so that each trajectory stays with the
parameters that produced it.
All parameter sets are run together in a single dust2 system, so every set must produce a model with the same state (for example, the same array sizes).
To compare scenarios, call this once per scenario and combine the
tidied results with dplyr::bind_rows(..., .id = "scenario"). For a
counterfactual based on a fit, convert the samples to a data frame
with posterior::as_draws_df(), change the parameters there, and
pass the data frame as pars.