Generate a Markov chain for n individuals or units by specifying a transition matrix.
genMarkov(
n,
transMat,
chainLen,
wide = FALSE,
id = "id",
pername = "period",
varname = "state",
widePrefix = "S",
trimvalue = NULL,
startProb = NULL
)
number of individual chains to generate
Square transition matrix where the sum of each row must equal 1. The dimensions of the matrix equal the number of possible states.
Length of each chain that will be generated for each chain; minimum chain length is 2.
Logical variable (TRUE or FALSE) indicating whether the resulting data table should be returned in wide or long format. The wide format includes all elements of a chain on a single row; the long format includes each element of a chain in its own row. The default is wide = FALSE, so the long format is returned by default.
Character string that represents name of "id" field. Defaults to "id".
Character string that represents the variable name of the chain sequence in the long format. Defaults "period",
Character string that represents the variable name of the state in the long format. Defaults to "state".
Character string that represents the variable name prefix for the state fields in the wide format. Defaults to "S".
Integer value indicating end state. If trimvalue is not NULL, all records after the first instance of state = trimvalue will be deleted.
A string that contains the probability distribution of the starting state, separated by a ";". Length of start probabilities must match the number of rows of the transition matrix.
A data table with n rows if in wide format, or n by chainLen rows if in long format.
# Transition matrix P
P <- t(matrix(c(
0.7, 0.2, 0.1,
0.5, 0.3, 0.2,
0.0, 0.1, 0.9
), nrow = 3, ncol = 3))
d1 <- genMarkov(n = 10, transMat = P, chainLen = 5)
d2 <- genMarkov(n = 10, transMat = P, chainLen = 5, wide = TRUE)
d3 <- genMarkov(
n = 10, transMat = P, chainLen = 5,
pername = "seq", varname = "health",
trimvalue = 3
)