simulate.micsim_model: Simulate from a model

View source: R/simulate.R

simulate.micsim_modelR Documentation

Simulate from a model

Description

Runs the simulation a micsim_model() describes and returns the result together with the model, the seed and what happened during the run.

Usage

## S3 method for class 'micsim_model'
simulate(
  object,
  nsim = 1,
  seed = NULL,
  cores = 1L,
  verbose = FALSE,
  reestimate = TRUE,
  ...
)

Arguments

object

A micsim_model.

nsim

Number of simulations. Only 1 is supported; run the model again, with a different seed, for more.

seed

Integer seed, or NULL to draw one from R's current random number state. The same seed gives the same result on any number of cores.

cores

Number of processor cores. Splitting the population across cores is exact rather than approximate, because individuals in MicSim's model are independent of one another. The result does not depend on the number of cores. No more workers are started than there are individuals in the starting population.

verbose

Report progress through message().

reestimate

Estimate the transition rates from the simulated population after the run and compare them with the supplied ones (reestimate()).

...

Not used.

Details

This is a method for stats::simulate(), so it has that generic's argument names. With the default seed = NULL, a seed is drawn from R's current random number state (one draw), so set.seed(234) before the call makes a run reproducible. The drawn seed is stored in the result. Apart from that draw, the caller's random number state is left exactly as it was, including the generator kind. Every individual in the simulation draws from their own random-number stream, derived from the seed and their ID (a newborn's from its mother's). A life course therefore does not depend on who else is in the population, on the order of the population's rows or of the transitions, or on the number of cores. Two scenarios simulated with the same seed share their random numbers (common random numbers), i.e. an individual whose rates are the same in both lives the same life in both.

A model can be simulated repeatedly. Nothing is shared between runs: every subsystem's state is created fresh at the start of each run. If the model was built by a different MicSim version, a micsim_version_warning is given.

After the run, every transition rate is estimated from the simulated population and compared with the supplied one (reestimate()). Using plot() on the result shows the comparison. Pass reestimate = FALSE to skip it.

Value

An object of class micsim_result, a list with

pop

the simulated population: one row per transition, one row with NA transition columns for an individual who never moved. as.data.frame() returns it.

model

the micsim_model that was simulated, so the result is self-describing and as_long() needs no arguments.

seed

the seed the run used; when none was supplied, the one drawn from R's random number state.

diagnostics

n_events, n_individuals, n_starting, n_immigrants and n_newborns; n_forced (transitions forced by an Inf rate), n_unreliable (waiting times drawn from a degraded numerical integration); peak_queue (largest number of pending events; for a multi-core run the largest of any core), cores (worker processes used) and seconds (wall time of the simulation).

extensions

a named list with what each subsystem reports, e.g. result$extensions$fertility$births; see fertility() and school_enrolment().

reestimation

a micsim_reestimation (reestimate()), or NULL with reestimate = FALSE.

meta

provenance: micsim_version, r_version, platform, rng_kind (always "L'Ecuyer-CMRG"), seed_drawn (TRUE if the seed was drawn from R's random number state), created (time the run finished) and reestimation_seconds.

See Also

micsim_model(), as_long(), as_wide(), MicSim-concepts

Examples

mortality <- function(age, calTime) 0.00003 * exp(0.1 * age)
model <- micsim_model(
  state_space = state_space(sex = c("m", "f")),
  transitions = transitions(transition(NA, "dead", mortality)),
  population  = data.frame(ID = 1:10, birthDate = rep("19500101", 10),
                           initState = rep("f", 10),
                           stringsAsFactors = FALSE),
  horizon = c(20000101, 20401231), max_age = 120
)
result <- simulate(model, seed = 234)
result


MicSim documentation built on Dec. 2, 2026, 1:09 a.m.