add_subsystem: Add a subsystem to a model

View source: R/subsystem.R

add_subsystemR Documentation

Add a subsystem to a model

Description

Switches on a mechanism that ordinary transitions cannot express, such as transitions that create a newborn (fertility()) or a calendar rule for when a transition takes place (school_enrolment()).

Usage

add_subsystem(model, subsystem)

Arguments

model

A micsim_model().

subsystem

A subsystem, from either fertility() or school_enrolment().

Details

Subsystems are added one at a time, and the call returns the model. Thus, a workflow can look like this:

model <- micsim_model(...) |>
  add_subsystem(fertility(births = moves_of("parity"), newborn = nb)) |>
  add_subsystem(school_enrolment(move("edu", "none", "primary"), month = 9))

Adding a subsystem compiles it against the model. That means every selector is resolved into the model's declared transitions and every parameter is checked. If there is a mistake, it is reported and names the subsystem. Two subsystems with the same name, or two that claim the same transition for the same purpose, will give an error.

Subsystems take effect in the order they were added. Their per-run state is created fresh at the start of every simulate() call, and whatever they report is returned in result$extensions, under their name.

Value

The model with the subsystem installed.

See Also

fertility(), school_enrolment(), move(), MicSim-concepts

Examples

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

model <- micsim_model(
  state_space = state_space(sex = c("male", "female"), parity = c("0", "1")),
  transitions = transitions(
    transition(move("parity", "0", "1", where = sex == "female"), first_birth),
    transition(NA, "deceased", mortality)
  ),
  population = data.frame(ID = 1:20, birthDate = "19850101",
                          initState = "female/0"),
  horizon = c(20140101, 20241231)
) |>
  add_subsystem(fertility(
    births  = moves_of("parity"),
    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.