| convertToLongFormat | R Documentation |
Reshapes the transitions returned by micSim() into one row per
episode.
convertToLongFormat(
pop,
migr = FALSE,
stateSpace = NULL,
absStates = NULL,
allTransitions = NULL,
simHorizon = NULL,
immigrPop = NULL,
maxAge = NULL
)
pop |
The population data frame returned by |
migr |
Logical. If |
stateSpace |
The state space, as passed to
|
absStates |
The absorbing states. Taken from |
allTransitions |
Optional transient-transition patterns in the
format supplied to |
simHorizon |
A two-element vector |
immigrPop |
The immigrant population. Taken from |
maxAge |
The maximum age of the simulation, in years. Taken from
|
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).
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.
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,
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.
micSim(), convertToWideFormat(), MicSim-concepts
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.