micsim_model: Define a simulation model

View source: R/class_model.R

micsim_modelR Documentation

Define a simulation model

Description

Builds the object that says everything about a model: its states, its transitions, who starts in it and how long it runs. Mechanisms that ordinary transitions cannot express, such as fertility or a school calendar, are added afterwards with add_subsystem().

Usage

micsim_model(
  state_space,
  transitions,
  population,
  horizon,
  max_age = 100,
  immigrants = NULL,
  exits = NULL,
  ...
)

Arguments

state_space

User-defined state variables, from state_space().

transitions

Allowed transitions, from transitions().

population

Data frame describing the starting population, with columns ID, birthDate and initState, in that order. Use unique whole-number IDs within R's integer range, birth dates in yyyymmdd format (numeric, character, Date), and full transient-state labels.

horizon

Two numeric dates in yyyymmdd format, c(start, end), with start before end.

max_age

Maximum age for scheduling transitions, in years; default 100.

immigrants

Data frame with columns ID, immigrDate, birthDate and immigrInitState, or NULL.

exits

Optional named character vector saying what kind of exit each absorbing state is: "death", "emigration" or "other", e.g. c(deceased = "death", abroad = "emigration"). Absorbing states left out are of unspecified kind. Nothing in core MicSim depends on it, but subsystems that must know whether someone died do.

...

Not used.

Details

Building a model enumerates the state space, resolves transitions and checks population formats, dates and identifiers. It also checks rate argument names and, where short test evaluations succeed, the number of values returned. Subsystem-specific checks occur in add_subsystem(). These checks cannot establish that all rates are suitable for every input encountered during a run. validate() provides an additional screen for missing and negative rate values on a grid.

Absorbing states are the destinations of transitions that are not transient states. Call them anything: "dead", "deceased", "tot" and "abroad" are all fine, and a model may have any number of them, including none (then max_age alone ends each life course). If something must tell a death from another kind of exit, say so in exits.

Nothing here consumes random numbers. A model can be built, printed, saved with saveRDS() and simulated repeatedly, and every run from the same seed gives the same result. A model saved under one MicSim version and simulated under another gives a warning; rebuild it with micsim_model() if the run fails or looks wrong.

Value

A micsim_model containing the supplied inputs and internal codec (state mapping), registry (transition-rate lookup) and micsim_version (the MicSim version that built the model).

See Also

state_space(), transitions(), add_subsystem(), validate(), simulate.micsim_model(), MicSim-concepts

Examples

mortality   <- function(age) 0.00003 * exp(0.1 * age)
first_birth <- function(age) ifelse(age >= 18 & age < 45, 0.10, 0)

# The birth is declared once and reused by the fertility subsystem
birth <- move("parity", from = "0", to = "1", where = sex == "female")

trans <- transitions(
  transition(birth, rate = first_birth),
  transition(from = NA, to = "deceased", rate = mortality)
)

model <- micsim_model(
  state_space = state_space(sex = c("female", "male"), parity = c("0", "1")),
  transitions = trans,
  population = data.frame(
    ID        = 1:20,
    birthDate = rep("19850101", 20),
    initState = rep("female/0", 20)
  ),
  horizon = c(20140101, 20241231),
  max_age = 100,
  exits   = c(deceased = "death")
) |>
  add_subsystem(fertility(
    births  = birth,
    newborn = newborn_rule(sex = c(male = 0.515, female = 0.485), parity = "0")
  ))
model


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