| convertToWideFormat | R Documentation |
Reshapes the population table returned by micSim() into one row per
individual, with numbered columns for their recorded transitions.
convertToWideFormat(pop)
pop |
The population data frame returned by |
Each transition is represented by four columns: its origin state,
destination state, date and age. The first transition appears in
From.1, To.1, transitionTime.1 and transitionAge.1; the second
appears in the corresponding .2 columns, and so on.
Transitions are numbered in their existing order within each
individual, so the input rows should already be in chronological
order. Individuals with fewer transitions have NA in the remaining
numbered columns.
ns counts an individual's transitions. Someone who never transitioned
occupies one row of pop, with NA in all four transition columns,
and is reported here with ns = 0.
The output omits motherID. If parent links are needed, retain a
separate table of ID and motherID from pop, then join it to the
wide result by ID.
The function takes a single data frame, so it is pipe-friendly:
pop |> convertToWideFormat().
A data frame with one row per individual and columns ID,
birthDate, initState, ns, and numbered groups of From, To,
transitionTime and transitionAge.
convertToWideFormat() 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_wide() on the result of
simulate.micsim_model(). Set
options(MicSim.show_deprecation = FALSE) to turn the message off.
micSim(), convertToLongFormat(), MicSim-concepts
stateSpace <- c("m", "f")
mortRates <- function(age, calTime) 0.00003 * exp(0.1 * age)
tm <- buildTransitionMatrix(NULL, cbind("dead", "mortRates"), stateSpace)
set.seed(1)
pop <- micSim(
initPop = data.frame(ID = 1:10, birthDate = rep("19500101", 10),
initState = rep("f", 10),
stringsAsFactors = FALSE),
transitionMatrix = tm, maxAge = 120,
simHorizon = c(20000101, 20401231), verbose = FALSE
)
convertToWideFormat(pop)
# convertToWideFormat() is pipe-friendly:
pop |> convertToWideFormat()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.