Simulate from a homogeneous or non-homogeneous Markov chain
Source:R/fittingFunctions.R
rmarkovchain.RdSimulates trajectories of a homogeneous Markov chain (a
markovchain object) or of a non-homogeneous one (a
markovchainList object, whose \(k\)-th
matrix gives the transition from time \(k - 1\) to time \(k\)).
Usage
rmarkovchain(
n,
object,
what = "data.frame",
useRCpp = TRUE,
parallel = FALSE,
num.cores = NULL,
...
)Arguments
- n
For a
markovchain, the number of steps of the trajectory; for amarkovchainList, the number of trajectories, each as long as the list.- object
A
markovchainormarkovchainListobject.- what
Form of the output for a
markovchainList:"data.frame"(default; columnsiterationandvalues, one row per simulated state),"matrix"(one row per trajectory) or"list"(one character vector per trajectory). Ignored for amarkovchain.- useRCpp
Logical: use the C++ implementation (default)?
- parallel
Logical: simulate the trajectories of a
markovchainListin parallel (defaultFALSE)?- num.cores
Number of threads used when
parallel = TRUE. IfNULL(the default) the thread count is read fromgetOption("RcppParallel.numThreads"), thengetOption("Ncpus"), then the environment variablesRCPP_PARALLEL_NUM_THREADSandOMP_NUM_THREADS, falling back tomin(2, cores)as CRAN policy requires.- ...
Further arguments:
t0(initial state) andinclude.t0(include it in the output), as inmarkovchainSequence().
Value
For a markovchain, a character vector of states. For a
markovchainList, a data frame, matrix or list, according to what.
Details
For a markovchain the function calls markovchainSequence().
For a markovchainList each trajectory starts from t0 (by default a
state drawn at random) and moves once with each matrix of the list.
See also
markovchainFit() and markovchainListFit() for the reverse
operation.
Other simulation:
markovchainSequence()
Examples
statesNames <- c("a", "b", "c")
mcA <- new("markovchain", states = statesNames, transitionMatrix =
matrix(c(0.2, 0.5, 0.3, 0, 0.2, 0.8, 0.1, 0.8, 0.1), nrow = 3,
byrow = TRUE, dimnames = list(statesNames, statesNames)))
rmarkovchain(n = 20, object = mcA, t0 = "a")
#> [1] "b" "b" "c" "b" "c" "b" "c" "b" "c" "b" "c" "b" "c" "b" "b" "c" "b" "b" "b"
#> [20] "c"
# a non-homogeneous chain: a different matrix at each of three steps
mcB <- new("markovchain", states = statesNames, transitionMatrix =
matrix(c(0.5, 0.5, 0, 0.1, 0.8, 0.1, 0, 0.5, 0.5), nrow = 3,
byrow = TRUE, dimnames = list(statesNames, statesNames)))
mclist <- new("markovchainList", markovchains = list(mcA, mcB, mcA))
rmarkovchain(5, mclist, what = "matrix")
#> [,1] [,2] [,3]
#> [1,] "c" "b" "c"
#> [2,] "b" "b" "b"
#> [3,] "c" "b" "c"
#> [4,] "b" "b" "c"
#> [5,] "c" "c" "b"
rmarkovchain(2, mclist, what = "list", t0 = "a", include.t0 = TRUE)
#> [[1]]
#> [1] "a" "c" "b" "c"
#>
#> [[2]]
#> [1] "a" "b" "b" "b"
#>