| simulate_markov | R Documentation |
Simulate Markov chains
simulate_markov(
n,
transition,
chain_length,
initial = NULL,
states = NULL,
trim_state = NULL,
id = "id",
period = "period",
state = "state",
seed = NULL,
batch = NULL
)
n |
Number of chains. |
transition |
Square transition matrix, or a tidy transition table with
|
chain_length |
Chain length. |
initial |
Starting-state probabilities or a single fixed start state.
When |
states |
Optional state labels. |
trim_state |
Optional terminal state. The first occurrence is kept and all later observations of that chain are removed. |
id, period, state |
Output variable names. |
seed |
Optional random seed. |
batch |
Optional single positive whole number. When given, the
simulator runs |
A long-form simulab_sim base data.frame with columns id,
period, and state (renamed by those arguments), one row per chain
position. as.data.frame(x, what = ) also returns transitions
(from, to, probability), initial_probabilities (state,
probability), and wide (one row per chain, columns id, S1, S2,
...).
transition <- matrix(c(0.7, 0.3, 0.4, 0.6), nrow = 2, byrow = TRUE)
result <- simulate_markov(
n = 50, transition = transition, chain_length = 20,
states = c("A", "B"), seed = 1
)
head(result)
summarize_transitions(result, normalize = TRUE)
# Transitions may also be given as a tidy from/to/probability table.
tidy_transitions <- data.frame(
from = c("A", "A", "B", "B"),
to = c("A", "B", "A", "B"),
probability = c(0.7, 0.3, 0.4, 0.6)
)
head(simulate_markov(
n = 50, transition = tidy_transitions, chain_length = 20, seed = 1
))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.