micSim: Run a continuous-time microsimulation

View source: R/compat.R

micSimR Documentation

Run a continuous-time microsimulation

Description

Simulates individual life courses as sequences of changes between states. This is the classic interface, which specifies transitions and their rate functions in a transition matrix.

Usage

micSim(
  initPop,
  immigrPop = NULL,
  transitionMatrix,
  absStates = NULL,
  fixInitStates = c(),
  varInitStates = c(),
  initStatesProb = c(),
  maxAge = 99,
  simHorizon,
  fertTr = c(),
  monthSchoolEnrol = c(),
  verbose = TRUE
)

Arguments

initPop

Data frame describing the starting population, with columns ID, birthDate and initState, in that order. Birth dates are yyyymmdd.

immigrPop

Data frame describing individuals who enter the population after the simulation starts, with columns ID, immigrDate, birthDate and immigrInitState. NULL when the model has no immigration.

transitionMatrix

Character matrix as produced by buildTransitionMatrix(): rows are transient states, columns are all states, and a cell holds "0" or the name of a rate function.

absStates

The absorbing states. Accepted for backward compatibility but no longer needed: they are read from the matrix's column names, which removes the risk of the two disagreeing.

fixInitStates

Integer positions of the state variables a newborn inherits from its mother.

varInitStates

Matrix of admissible newborn states, one row per candidate and one column per state variable.

initStatesProb

Weights for the rows of varInitStates. They must sum to one within each combination of inherited attributes.

maxAge

Sharp maximum age, in years.

simHorizon

Length-2 vector c(startDate, endDate), yyyymmdd.

fertTr

Transitions that create a newborn, as "origin->destination" patterns. Empty when the model has no fertility.

monthSchoolEnrol

Month, 1 to 12, on whose first day elementary-school enrolment takes place. Empty when the model does not track school enrolment.

verbose

Report progress through message(), which can be suppressed or captured. Pass FALSE for a silent run.

Details

This is the classic interface based on a transition matrix whose cells name rate functions. micsim_model() provides an alternative interface that keeps the model inputs together and accepts functions directly. Both use the same simulation engine.

The state space contains all possible combinations of the model's state variables, with each state written as its values joined by forward slashes: "f/DE/1" for a German woman with one child. The absorbing states are the columns of transitionMatrix after the transient ones; any label works.

Labels with a meaning. This interface keeps three conventions of MicSim 3.0.0, because it has no other way to be told: only states containing "f" can give birth through fertTr; with monthSchoolEnrol, an allowed transition counts as enrolment if it changes one state variable from "no" to "low" and leaves the other values as they were; and buildTransitionMatrix() puts "dead" and "rest" first among the absorbing states.

Rates are functions of age, calTime and duration, in any subset, all in fractional years. They are transition intensities, not probabilities: a rate may exceed one. They must be vectorised, because the simulation integrates them numerically.

Reproducibility. micSim() takes no seed. It draws the seed of the run from the random stream the caller has set up (one draw), so set.seed(234) immediately before the call makes a run reproducible. Every individual then draws from their own random-number stream, as described in simulate.micsim_model().

Value

A data frame with one row per transition, plus one row per individual who never experienced one, and the columns ID, birthDate, initState, From, To, transitionTime and transitionAge, with motherID added when the model has fertility.

Reproducibility across versions

The return value keeps the same columns, row semantics and ordering. Some values differ from earlier releases, most importantly because dates are now computed on the exact civil calendar. Random numbers are now drawn per individual, so the same seed gives a different, equally valid population than in MicSim 3.0.0.

Deprecated

micSim() is deprecated as of MicSim 4.0.0 and will be removed in a future release. It still works, and shows a message once per R session naming its replacement: build the model with micsim_model() (and add_subsystem()) and run it with simulate.micsim_model(). Set options(MicSim.show_deprecation = FALSE) to turn the message off.

References

Zinn, S. (2014). The MicSim Package of R: An Entry-Level Toolkit for Continuous-Time Microsimulation. International Journal of Microsimulation, 7(3), 3-32. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.34196/IJM.00105")}

See Also

buildTransitionMatrix(), MicSim-concepts

Examples

# A mortality-only model: five individuals, one absorbing state.
stateSpace <- c("m", "f")
attr(stateSpace, "name") <- "sex"

mortRates <- function(age, calTime) {
  0.00003 * exp(ifelse(calTime <= 2020, 0.1, 0.097) * age)
}

initPop <- data.frame(
  ID        = 1:5,
  birthDate = c("19301231", "19990403", "19561015", "19911111", "19650101"),
  initState = c("f", "m", "f", "m", "m"),
  stringsAsFactors = FALSE
)

transitionMatrix <- buildTransitionMatrix(
  allTransitions = NULL,
  absTransitions = cbind("dead", "mortRates"),
  stateSpace     = stateSpace
)

set.seed(234)
pop <- micSim(
  initPop          = initPop,
  transitionMatrix = transitionMatrix,
  absStates        = "dead",
  maxAge           = 120,
  simHorizon       = c(20000101, 21001231),
  verbose          = FALSE
)
head(pop)


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