Introduction to MicSim 4.0.0

Introduction

This vignette introduces MicSim’s model-object interface through an example that simulates first births, school enrolment and deaths. The fertility subsystem adds newborns to the population, and the school enrolment subsystem schedules enrolments on a school calendar. We specify the model components, build and validate the model, then run the simulation.

We use MicSim’s built-in functions to summarise the results, inspect individual transition histories and subsystem records, and compare rates estimated from the simulated life courses with the rates supplied to the model. The two companion vignettes apply the same workflow to migration flows and educational inequalities in depression.

# Loading libraries
library(MicSim)
library(ggplot2)

# Clearing the environment
rm(list = ls())
invisible(gc())

We set a random seed so that the synthetic population below is reproducible. You can change SEED to draw a different population.

# Setting the random seed
SEED <- 1234
set.seed(SEED)

Specify the model components

We begin by setting the simulation period and age limits. We then define the states individuals can occupy, the starting population and the rates of the allowed transitions. Women can have a first child, children become eligible to enrol in primary school at age six, and mortality rates differ between men and women. Death is an absorbing state, so entering it ends an individual’s simulated life course.

Simulation framework

The simulation starts with 1,000 synthetic adults. Change N to adjust the size of the starting population. The simulation horizon runs from 1 January 2000 to 31 December 2050, a period of approximately 51 years. Both dates are supplied in yyyymmdd format.

We set minage to 18 and generate a starting population aged approximately 18 to 90 years. Children born during the simulation enter at age zero.

The maxage setting controls the upper age limit for follow-up. With maxage = 100, MicSim uses an age cutoff of 99.99 years (maxage - 0.01). For anyone still alive at the age cutoff or the end date, whichever comes first, observation ends while their final episode is still in progress. This is called right censoring.

getInDays() converts a yyyymmdd date to the number of days since 1 January 1970. Subtracting two converted dates gives the number of days between them, which we use below to confirm the length of the horizon.

startDate <- 20000101 # yyyymmdd
endDate   <- 20501231 # yyyymmdd
simHorizon <- c(startDate, endDate)

N <- 10000

minage <- 18
maxage <- 100

sprintf("The simulation runs from %d to %d, which is %d days.", 
        startDate, endDate, getInDays(endDate) - getInDays(startDate))
## [1] "The simulation runs from 20000101 to 20501231, which is 18627 days."

State space

state_space() defines the variables that describe an individual’s state and the values each variable can take. This model uses sex with values "m" and "f", fert with values "0" and "1" for zero and one child, and education with values "none" before primary school enrolment and "primary" from enrolment onwards.

Combining these values gives eight possible states. MicSim joins the values with / in the order supplied to state_space(). For example, "f/0/primary" describes a woman with no children who has enrolled in primary school. The model adds the absorbing state "dead" when we use it as a destination in the mortality transitions.

stateSpace <- state_space(
  sex = c("m", "f"),
  fert = c("0", "1"),
  education = c("none", "primary")
)

stateSpace
## $sex
## [1] "m" "f"
## 
## $fert
## [1] "0" "1"
## 
## $education
## [1] "none"    "primary"
## 
## attr(,"class")
## [1] "micsim_state_space"

Initial population

The initial population is a data frame with one row per individual and three columns in this order. ID is a unique whole-number identifier, birthDate is the date of birth in yyyymmdd format, and initState is the individual’s state at the start of the simulation.

Our population is synthetic; in most cases you would likely have your own population to supply. To generate the birth dates, we first use getInDays() to express the simulation start date as a count of days since 1 January 1970. We subtract the day equivalents of 90 years and minage from the start date to obtain the earliest and latest birth dates, using 365.25 days per year. We then draw N values uniformly between these bounds and use getInDateFormat() to convert them to yyyymmdd. Both date helpers follow the Gregorian calendar and adapt Howard Hinnant’s date conversion algorithms.

To construct initState, we sample sex for each individual and randomly assign women to have zero or one child. Men start with fert = "0". We assume all adults in the initial population have already enrolled in primary school, so they start with education = "primary".

birthDates <- runif(N,
  min = getInDays(startDate) - round(90 * 365.25),
  max = getInDays(startDate) - round(minage * 365.25)
) |>
  getInDateFormat()

sex  <- sample(stateSpace$sex, N, replace = TRUE)
fert <- ifelse(sex == "f", sample(stateSpace$fert, N, replace = TRUE), "0")

initPop <- data.frame(
  ID        = seq_len(N),
  birthDate = birthDates,
  initState = paste(sex, fert, "primary", sep = "/")
)

head(initPop)
ID birthDate initState
1 19180310 f/0/primary
2 19541022 f/0/primary
3 19531113 f/0/primary
4 19541119 m/0/primary
5 19711227 f/1/primary
6 19560207 f/1/primary

To plot the age distribution at the start date, we first convert birth dates to ages with getAgeInDays(). A yyyymmdd date is not a number on a continuous scale (19731231 is followed by 19740101), so it should not be plotted directly. Because the birth dates are drawn uniformly, the age distribution is roughly flat.

  initAges <- data.frame(
    age = getAgeInDays(startDate, initPop$birthDate) / 365.25,
    sex = factor(sub("/.*", "", initPop$initState), levels = c("f", "m"),
             labels = c("Female", "Male"))
  )

  ggplot(initAges, aes(x = age, fill = sex)) +
    geom_histogram(binwidth = 5, boundary = minage, position = "dodge") +
    labs(x = "Age at the start of the simulation", y = "Count",
         fill = "Sex", title = "Initial population") +
    theme_minimal()

Transition rates

A transition rate can be constant or depend on age (age), calendar time (calTime), time since the relevant state variable last changed (duration), or any combination of these. All three inputs use years of 365.25 days.

For a constant rate, you can use a function with no arguments. For example, function() 0.1 gives a constant rate of 0.1 per person-year. Functions that take time inputs must be vectorised, returning one rate for each set of input values.

Mortality

The two mortality rates depend only on age. They follow a Gompertz curve, \(\mu(a) = \alpha e^{\beta a}\), where \(a\) is age. Both rates increase with age, and mortality is higher for men than for women. The same curves apply to children born during the simulation, so they face a very low but nonzero risk of dying in childhood.

mortMale   <- function(age) 0.00005 * exp(0.095 * age)
mortFemale <- function(age) 0.00002 * exp(0.098 * age)

# Plot the rates
age <- seq(0, 100, 0.5)
ggplot() +
  geom_line(aes(x = age, y = mortMale(age), color = "Male")) +
  geom_line(aes(x = age, y = mortFemale(age), color = "Female")) +
  labs(x = "Age", y = "Mortality rate", colour = "Sex",
       title = "Mortality rates") +
  theme_minimal()

Fertility

fertRate() gives the rate of a first birth as a function of age and calendar time. It uses a Hadwiger-shaped age profile, \[ f(a) = \frac{b}{c} \left(\frac{c}{a}\right)^{3/2} \exp\left\{-b^2 \left(\frac{c}{a} + \frac{a}{c} - 2\right)\right\}, \] where \(a\) is age, \(c\) sets the location of the peak and \(b\) its sharpness. The rate is zero at ages 15 and below and 45 and up. The function uses \(b = 3.5, c = 28\) when calTime <= 2020 and \(b = 3.0, c = 29\) when calTime > 2020, resulting in first births after 2020 coming slightly later and being more spread out.

fertRate <- function(age, calTime) {
  b <- ifelse(calTime <= 2020, 3.5, 3.0)
  c <- ifelse(calTime <= 2020, 28, 29)
  rate <- (b / c) * (c / age)^(3 / 2) * exp(-b^2 * (c / age + age / c - 2))
  rate[age <= 15 | age >= 45] <- 0
  rate
}

age <- seq(15, 45, 0.25)
ggplot() +
  geom_line(aes(x = age, y = fertRate(age, 2010), colour = "Until 2020")) +
  geom_line(aes(x = age, y = fertRate(age, 2030), colour = "After 2020")) +
  labs(x = "Age", y = "First-birth rate", colour = "Period",
       title = "First-birth rates") +
  theme_minimal()

School enrolment

enrolRate() returns zero before age six and Inf from age six onwards. MicSim treats an infinite rate as a forced transition. The school enrolment subsystem adjusts the candidate enrolment date to the school calendar.

enrolRate <- function(age) ifelse(age == 6, Inf, 0)

Transitions

Each transition() call specifies the origin state (from), the destination state (to) and the rate function (rate). Pass the function directly, as in rate = mortMale.

For mortality, we list the full origin state labels and use "dead" as the destination. Because this label is outside the state space, MicSim recognises it as an absorbing state when it builds the model. All states for the same sex share a mortality rate, so we group those origins in one declaration. If there is only one mortality rate for everyone you can use from = NA to cover all origins in one declaration.

For first birth and school enrolment, we use move() to specify which state variable changes, instead of listing every origin and destination state variable. We pass each selector to transition() and reuse it in fertility() or school_enrolment() to tell the subsystem which declared transitions to act on. MicSim carries the other state values into the destination.

birthEdge changes fert from "0" to "1" for female states at either education level. enrolEdge changes education from "none" to "primary" for all combinations of sex and fertility. In this example, children enrol before they reach first-birth ages. Enrolments therefore occur with fert = "0", and first births move women from "f/0/primary" to "f/1/primary".

The final lines use do.call() to pass each declaration as a separate argument to transitions(), which collects them into one object for micsim_model().

mortTransitions_m <- transition(
  from = c("m/0/none", "m/0/primary"),
  to = "dead",
  rate = mortMale
)

mortTransitions_f <- transition(
  from = c("f/0/none", "f/0/primary", "f/1/none", "f/1/primary"),
  to = "dead",
  rate = mortFemale
)

birthEdge <- move("fert", from = "0", to = "1", where = sex == "f")
enrolEdge <- move("education", from = "none", to = "primary")

fertTransition <- transition(birthEdge, rate = fertRate)
enrolTransition <- transition(enrolEdge, rate = enrolRate)

all_transitions <- transitions(
  fertTransition,
  enrolTransition,
  mortTransitions_m,
  mortTransitions_f
)

Build the model and add the subsystems

micsim_model() brings together the state space, transitions, initial population, simulation horizon and maximum age. It checks that the state labels fit the model, checks the rate functions’ arguments and probes the length of their output. The named vector exits assigns the exit type "death" to the absorbing state "dead".

To set up the fertility subsystem, we use newborn_rule() to define each newborn’s starting state. The rule assigns sex = "m" with probability 0.515 or sex = "f" with probability 0.485. Every newborn starts with fert = "0" and education = "none". We pass the newborn rule and birthEdge to fertility() so MicSim adds a newborn whenever one of the first-birth transitions occurs.

We pass enrolEdge to school_enrolment() and set month = 9L. If the age-six candidate falls from January through September, the subsystem schedules enrolment for 1 September of the same year. If it falls from October through December, it uses 1 September of the following year. A late-September candidate can move to the first of that month, so some children enrol shortly before turning six. An enrolment is recorded if the child survives to the adjusted date and that date falls within the simulation horizon.

We attach both subsystems to the model using add_subsystem().

newborn <- newborn_rule(
  sex       = c(m = 0.515, f = 0.485),
  fert      = "0",
  education = "none"
)

model <- micsim_model(
  state_space = stateSpace,
  transitions = all_transitions,
  population  = initPop,
  horizon     = simHorizon,
  max_age     = maxage,
  exits       = c(dead = "death")
) |>
  add_subsystem(fertility(births = birthEdge, newborn = newborn)) |>
  add_subsystem(school_enrolment(transition = enrolEdge, month = 9L))

model
## <micsim_model>
##   state space : sex x fert x education = 8 transient state(s)
##   absorbing   : dead (death)
##   transitions : 12 (from 4 declaration(s))
##   population  : 10000 individual(s)
##   horizon     : 20000101 to 20501231, max age 100
##   subsystems  :
##     fertility: fertility (2 birth transition(s))
##     school_enrolment: school enrolment (4 transition(s), month 9)

Validate and run the model

validate() evaluates every rate over all ages and calendar years the simulation can reach. It stops if it encounters an error, and lists all rates that fail, return NA or a negative value anywhere (see documentation of validate() for details). Inf is allowed for forced transitions such as school enrolment. This step makes sure that we simulate only well-defined models. It is optional, but we strongly recommend running validate() before simulate() to catch errors early. If there are no errors, validate() returns the model invisibly, so it fits into the pipeline before simulate().

Setting a seed in simulate() lets you reproduce the results with the same inputs, on any number of cores. The seed argument is optional but strongly recommended for both single-core and parallel runs. If you set a seed to generate the initial population, we recommend using a different one for the simulation. Here we use SEED + 1L, where SEED is the seed used to generate the initial population.

result <- model |>
  validate() |>
  simulate(seed = SEED + 1L, cores = 1L)

result
## <micsim_result> MicSim 4.0.0
##   9,383 event(s) among 10,485 individual(s) (485 born during the run)
##   horizon: 20000101 to 20501231, max age 100
##   seed   : 1235
##   NOTE: 4 waiting time(s) were drawn from a degraded numerical
##         integration, typically at a jump of a step-shaped rate.
##         Compare the supplied and recovered rates with plot().
##   extensions  : fertility, school_enrolment (see $extensions)
##   reestimation: 4 transition(s), observed/expected 0.99 to 1.03 (see $reestimation, plot())
##   as.data.frame() for the population, as_long() for episodes

Results

summary() counts the recorded transitions, summarises the ages at which they occurred and shows how many people ended observation in each state.

Most people at the end of the simulation are dead. This is expected, because we only add people through our fertility, which only models first births and not higher parities. Thus, our population shrinks over the 51 year run.

summary(result)
## Simulated 10485 individual(s), 9383 event(s)
## 
## Transitions:
## 
##        m/0/primary -> dead        f/1/primary -> dead 
##                       4455                       2097 
##        f/0/primary -> dead f/0/primary -> f/1/primary 
##                       1871                        485 
##    m/0/none -> m/0/primary    f/0/none -> f/0/primary 
##                        268                        207 
## 
## Age at transition, by dimension of the state space:
##               n  Min. 1st Qu. Median   Mean 3rd Qu.  Max.
## fert        485 16.40   24.62  28.37 28.732   32.13 44.42
## education   475  5.92    6.18   6.43  6.423    6.66  6.92
## exit: dead 8423 10.96   73.12  81.38 79.871   88.25 99.99
## 
## State at the end of observation:
## final_state
##        dead m/0/primary f/1/primary f/0/primary    m/0/none    f/0/none 
##        8423         870         856         326           6           4

The subsystem reports are in result$extensions. The fertility report includes the state assigned to each newborn in childState. The school enrolment report counts completed enrolments and records each child’s age and date of enrolment. Compare drawnDate, the candidate date from the age-six rate, with date, the date after the calendar adjustment. shiftDays gives the difference in days. A negative value means the child enrolled before turning six.

head(result$extensions$fertility$births)
motherID childID date motherAge transition childState
3289 10001 20000101 27.32 f/0/primary -> f/1/primary f/0/none
7265 10002 20000105 19.93 f/0/primary -> f/1/primary f/0/none
6340 10003 20000106 30.36 f/0/primary -> f/1/primary m/0/none
1561 10004 20000112 21.88 f/0/primary -> f/1/primary m/0/none
5427 10005 20000121 41.18 f/0/primary -> f/1/primary m/0/none
2409 10006 20000128 27.32 f/0/primary -> f/1/primary f/0/none
enrolment <- result$extensions$school_enrolment
enrolment$n_enrolments
## [1] 475
head(enrolment$enrolments)
ID date age transition drawnDate shiftDays
10001 20060901 6.67 f/0/none -> f/0/primary 20060101 243
10002 20060901 6.66 f/0/none -> f/0/primary 20060104 240
10003 20060901 6.65 m/0/none -> m/0/primary 20060105 239
10004 20060901 6.64 m/0/none -> m/0/primary 20060111 233
10005 20060901 6.61 m/0/none -> m/0/primary 20060120 224
10006 20060901 6.59 f/0/none -> f/0/primary 20060128 216

as.data.frame() returns one row per transition, and one row for each individual with no transition at all. Those individuals have NA in the transition columns, as never experienced a transition until the end of the simulation was reached and are still in their initial state. They are censored.

pop <- as.data.frame(result)
head(pop)
ID birthDate initState From To transitionTime transitionAge motherID
1 19180310 f/0/primary f/0/primary dead 20091022 91.62 NA
2 19541022 f/0/primary f/0/primary dead 20470730 92.77 NA
3 19531113 f/0/primary f/0/primary dead 20250303 71.30 NA
4 19541119 m/0/primary m/0/primary dead 20220401 67.37 NA
5 19711227 f/1/primary f/1/primary dead 20490411 77.29 NA
6 19560207 f/1/primary f/1/primary dead 20450313 89.10 NA

To calculate mean age at death by sex, we select transitions to "dead" and extract sex from the origin state labels. The resulting means include only individuals who died during the simulation, so they also depend on the age structure of the starting population. Because men have higher mortality rates, we expect their mean age at death to be lower than that of women:

deaths <- pop[pop$To %in% "dead", ]
tapply(deaths$transitionAge, sub("/.*", "", deaths$From), mean)
##        f        m 
## 82.05987 77.92137

Re-estimating the rates

By default, simulate() estimates transition rates from the simulated life courses. Within each age band, it divides the number of transitions by the person-years spent in the relevant origin states. For the first-birth rate, this means dividing the number of first births by the time spent in "f/0". The estimates are stored in result$reestimation.

result$reestimation
## <micsim_reestimation> 4 transition(s)
##   transition                                            events  person-years  expected  obs/exp [95% CI]  
##   fert: 0 -> 1 (where sex == "f")                          485        59,898     481.9  1.006 [0.92, 1.10]
##   education: none -> primary                               475         3,083         -  forced            
##   m/0/none, m/0/primary -> dead                          4,455       131,232   4,481.9  0.994 [0.97, 1.02]
##   f/0/none, f/0/primary, f/1/none, f/1/primary -> dead   3,968       146,459   3,864.6  1.027 [1.00, 1.06]
## Observed / expected near 1 means the simulation reproduces the supplied
## rate; plot() draws each supplied rate beside the estimated one.

Convert the re-estimation to a data frame to inspect the estimates by age band. The code below displays the first bands with at least one event.

bands <- as.data.frame(result$reestimation)
head(bands[bands$events > 0, ])
transition axis group lower upper mid events exposure rate ci_lower ci_upper expected
20 fert: 0 -> 1 (where sex == “f”) age calendar time 2000.0 to 2009.4 19 20 19.5 5 49.92266 0.1001549 0.0325200 0.2337282 2.160449
22 fert: 0 -> 1 (where sex == “f”) age calendar time 2000.0 to 2009.4 21 22 21.5 11 108.63518 0.1012563 0.0505468 0.1811755 8.589096
23 fert: 0 -> 1 (where sex == “f”) age calendar time 2000.0 to 2009.4 22 23 22.5 9 139.60301 0.0644685 0.0294791 0.1223813 13.491015
24 fert: 0 -> 1 (where sex == “f”) age calendar time 2000.0 to 2009.4 23 24 23.5 18 159.56468 0.1128069 0.0668565 0.1782836 17.808131
25 fert: 0 -> 1 (where sex == “f”) age calendar time 2000.0 to 2009.4 24 25 24.5 24 168.69747 0.1422665 0.0911528 0.2116813 20.699577
26 fert: 0 -> 1 (where sex == “f”) age calendar time 2000.0 to 2009.4 25 26 25.5 24 179.79535 0.1334851 0.0855264 0.1986153 23.230934

plot() draws one figure per declared transition. The left panel shows the supplied rate. The right panel shows what was recovered from the simulation:

Mortality rates is defined via exponential function, so the y axis is logarithmic. This can be explicitly set by using the argument log = TRUE in plot().

plot(result)

Use reestimate() to recalculate the rates from the same simulated life courses with different settings. For example, width = 5 groups the estimates into five-year age bands.

reestimate(result, width = 5)
## <micsim_reestimation> 4 transition(s)
##   transition                                            events  person-years  expected  obs/exp [95% CI]  
##   fert: 0 -> 1 (where sex == "f")                          485        59,898     482.0  1.006 [0.92, 1.10]
##   education: none -> primary                               475         3,083         -  forced            
##   m/0/none, m/0/primary -> dead                          4,455       131,232   4,449.7  1.001 [0.97, 1.03]
##   f/0/none, f/0/primary, f/1/none, f/1/primary -> dead   3,968       146,459   3,833.5  1.035 [1.00, 1.07]
## Observed / expected near 1 means the simulation reproduces the supplied
## rate; plot() draws each supplied rate beside the estimated one.

Conclusion

This vignette covered a full MicSim workflow. We went from defining a state space, an initial population and transition rates to building a model with micsim_model() and adding subsystems. Then we validated and simulated our model. Lastly, we checked the results against the rates we supplied initially. The two companion vignettes apply the same workflow to migration flows and to educational inequalities in depression.