buildTransitionMatrix: Build a transition matrix from transition patterns

View source: R/build_transition_matrix.R

buildTransitionMatrixR Documentation

Build a transition matrix from transition patterns

Description

Expands a short description of which transitions a model allows into the full matrix that micSim() expects.

Usage

buildTransitionMatrix(allTransitions, absTransitions, stateSpace)

Arguments

allTransitions

Two-column matrix of transitions between transient states: the pattern "origin->destination" and the name of the rate function. A single transition may be given as a length-2 vector. NULL when the model has none.

absTransitions

Two-column matrix of transitions into absorbing states: the absorbing state's label and the name of the rate function. A single one may be given as a length-2 vector.

stateSpace

User-defined combinations of state-variable values, as produced by base::expand.grid(): one row per transient state and one column per variable. A vector is accepted for a single variable.

Details

Transient transitions are given as "origin->destination". Each side names the state-variable values needed to describe the change. The helper finds origins containing the values on the left and destinations containing the values on the right. Values not mentioned in the pattern must be the same before and after the transition. So in a model of sex, education and parity, "0->1" expands to a first birth for every combination of sex and education, while "f/0->f/1" restricts it to women.

Absorbing transitions are given as a state name rather than a pair, because the origin is "anywhere". "dead" makes every transient state able to reach death; a compound label such as "f/low/dead" restricts it to the states whose other variables match.

The second column of each matrix is the NAME of the function supplying the rate, as a string. Storing names rather than the functions themselves is the convention micSim() expects; it resolves each name once, in the environment it was called from.

Any label can name an absorbing state. "dead", "rest", "abroad" or "institution" are all fine, and a model may have as many as it needs. In a compound label the absorbing part is the component that is not a value of any state variable, so "f/IT/abroad" is the state "abroad", reachable from Italian women.

Value

A character matrix with one row per transient state and one column per state, holding "0" where no transition is possible and otherwise the name of the rate function.

Assumptions

The absorbing states are the columns after the transient ones, and their ORDER is the order of the absorbing states in the state space, which decides which of an individual's random numbers each destination receives. It is "dead" first and "rest" second, where present, then every other absorbing state in the order absTransitions first names it. "rest" conventionally means emigration.

A label that could be read two ways is refused: an absorbing state that is also the value of a state variable, or a compound label with no part, or more than one part, that is not a state value.

A pattern that matches nothing is not an error. It produces a matrix with no cell set, which is usually a typo in a state value; check the result with table(transitionMatrix != "0") if a model behaves unexpectedly.

Deprecated

buildTransitionMatrix() 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: declare the transitions with transition(), move() and transitions() and pass them to micsim_model(). Set options(MicSim.show_deprecation = FALSE) to turn the message off.

See Also

micSim(), MicSim-concepts

Examples

stateSpace <- expand.grid(sex = c("m", "f"), fert = c("0", "1"))
buildTransitionMatrix(
  allTransitions = cbind("f/0->f/1", "fertRates"),
  absTransitions = cbind("dead", "mortRates"),
  stateSpace     = stateSpace
)


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