| micsim_model | R Documentation |
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().
micsim_model(
state_space,
transitions,
population,
horizon,
max_age = 100,
immigrants = NULL,
exits = NULL,
...
)
state_space |
User-defined state variables, from |
transitions |
Allowed transitions, from |
population |
Data frame describing the starting population, with
columns |
horizon |
Two numeric dates in |
max_age |
Maximum age for scheduling transitions, in years; default
|
immigrants |
Data frame with columns |
exits |
Optional named character vector saying what kind of exit
each absorbing state is: |
... |
Not used. |
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.
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).
state_space(), transitions(), add_subsystem(),
validate(), simulate.micsim_model(), MicSim-concepts
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.