convertToLongFormat: Convert simulation output to long format

View source: R/convert_long.R

convertToLongFormatR Documentation

Convert simulation output to long format

Description

Reshapes the transitions returned by micSim() into one row per episode.

Usage

convertToLongFormat(
  pop,
  migr = FALSE,
  stateSpace = NULL,
  absStates = NULL,
  allTransitions = NULL,
  simHorizon = NULL,
  immigrPop = NULL,
  maxAge = NULL
)

Arguments

pop

The population data frame returned by micSim().

migr

Logical. If TRUE, start immigrants' first episodes at arrival and mark their entries as observed. Requires immigrPop, either supplied explicitly or recorded on pop.

stateSpace

The state space, as passed to buildTransitionMatrix(). Only its column NAMES are used, to name the state-variable columns of the result. When omitted, the names recorded by micSim() are used, which are var1, var2, ... because a transition matrix does not carry them.

absStates

The absorbing states. Taken from pop when omitted.

allTransitions

Optional transient-transition patterns in the format supplied to buildTransitionMatrix(). Only the first column, containing the patterns, is used. See Details for how patterns are matched to output labels.

simHorizon

A two-element vector c(startDate, endDate) with dates in yyyymmdd format. Taken from pop when omitted.

immigrPop

The immigrant population. Taken from pop when omitted. Needed only when migr is TRUE.

maxAge

The maximum age of the simulation, in years. Taken from pop when omitted. If neither is available, final episodes end at the simulation end without an age limit.

Details

Each row describes one uninterrupted spell in one state. Each row records its start and end dates, the transition that ended it, and flags indicating whether entry and exit were observed. Each state variable has its own column. For example, the state "f/DE/1" is split into separate columns for sex, nationality and parity.

The OD column describes how the episode ended. A transition to an absorbing state is labelled with that state's name, such as "dead". An episode still in progress when observation ends is labelled "cens". Other transitions are labelled with the values that changed. For example, "f/DE/0->f/DE/1" becomes "0->1".

Supplying allTransitions lets a SINGLE-VARIABLE pattern be reported under the user's own name. It cannot do more than that: the match is made component by component, so a pattern naming two or more variables can never equal one of the candidates and the changed-variable form is used instead.

The output can be used to select episodes in a relevant origin states and count observed exits (statusExit == 1) to the destination of interest. Episode lengths in can also be calculated Tstop - Tstart.

The population table is the first argument, so the function supports the native R pipe: pop |> convertToLongFormat(stateSpace = stateSpace).

Value

A data frame with the columns ID, birthDate, Tstart, Tstop, statusEntry, statusExit, OD, ns, Episode, and one column per state variable. Dates use yyyymmdd format. ns gives the total number of episodes for the individual; Episode numbers those episodes in start-date order.

Episode boundaries and observation flags

Left truncation. For individuals born before the simulation starts, the first episode begins at the simulation start and has statusEntry = 0: entry into their initial state was not observed during the simulation. For individuals born on or after the start date, the first episode begins at birth and has statusEntry = 1.

Immigrants. With migr = TRUE, an immigrant's first episode begins on their arrival date rather than at the start of observation, and counts as observed. Their earlier life happened elsewhere and is outside the study.

Right censoring. Anyone who has neither entered an absorbing state nor already got a censored row gets one more episode, running from their last transition to the end of observation: the end of the simulation, or the day they reach maxAge if that comes first. The simulation stops putting a person at risk at ⁠maxAge - 0.01 years⁠,

Deprecated

convertToLongFormat() 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: use as_long() on the result of simulate.micsim_model(). Set options(MicSim.show_deprecation = FALSE) to turn the message off.

See Also

micSim(), convertToWideFormat(), MicSim-concepts

Examples

stateSpace <- expand.grid(sex = c("m", "f"), fert = c("0", "1"))
fertRates <- function(age, calTime) ifelse(age > 15 & age < 45, 0.1, 0)
mortRates <- function(age, calTime) 0.00003 * exp(0.1 * age)

tm <- buildTransitionMatrix(
  allTransitions = cbind("f/0->f/1", "fertRates"),
  absTransitions = cbind("dead", "mortRates"),
  stateSpace     = stateSpace
)

set.seed(1)
pop <- micSim(
  initPop = data.frame(ID = 1:20, birthDate = rep("19850101", 20),
                       initState = rep("f/0", 20),
                       stringsAsFactors = FALSE),
  transitionMatrix = tm,
  varInitStates  = rbind(c("m", "0"), c("f", "0")),
  initStatesProb = c(0.515, 0.485),
  maxAge = 100, simHorizon = c(20140101, 20241231),
  fertTr = "f/0->f/1", verbose = FALSE
)
head(convertToLongFormat(pop, stateSpace = stateSpace))

# pipe-friendly:
pop |> convertToLongFormat(stateSpace = stateSpace) |> head()


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