simulate_markov: Simulate Markov chains

View source: R/sequences.R

simulate_markovR Documentation

Simulate Markov chains

Description

Simulate Markov chains

Usage

simulate_markov(
  n,
  transition,
  chain_length,
  initial = NULL,
  states = NULL,
  trim_state = NULL,
  id = "id",
  period = "period",
  state = "state",
  seed = NULL,
  batch = NULL
)

Arguments

n

Number of chains.

transition

Square transition matrix, or a tidy transition table with from, to, and probability columns. Every row of the implied matrix must sum to one.

chain_length

Chain length.

initial

Starting-state probabilities or a single fixed start state. When NULL, every chain starts in the first state.

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 batch times and returns a plain list of batch results, each exactly what the same call without batch returns. With a seed, every dataset gets its own seed drawn from seed, so the whole batch is reproducible; without one the datasets are consecutive draws from the session's random-number stream. The default NULL returns a single result.

Value

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, ...).

Examples

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
))

simulab documentation built on Oct. 15, 2026, 5:08 p.m.