augment_markov: Add a Markov chain to each input row

View source: R/sequences.R

augment_markovR Documentation

Add a Markov chain to each input row

Description

Add a Markov chain to each input row

Usage

augment_markov(
  data,
  transition,
  chain_length,
  initial = NULL,
  states = NULL,
  trim_state = NULL,
  id = "id",
  period = "period",
  state = "state",
  seed = NULL
)

Arguments

data

Base data.frame with a unique identifier.

transition, chain_length, states, trim_state

Markov-chain arguments.

initial

Starting-state probabilities or the name of an input variable containing each row's starting state. When NULL, every chain starts in the first state.

id, period, state

Output variable names.

seed

Optional random seed.

Value

A long-form simulab_sim base data.frame combining the input variables with one row per chain position: id, period, state, followed by the other input columns. A transitions table (from, to, probability) is available through as.data.frame().

Examples

data <- data.frame(id = 1:20, group = rep(c("a", "b"), each = 10))

result <- augment_markov(
  data,
  transition = matrix(c(0.7, 0.3, 0.4, 0.6), nrow = 2, byrow = TRUE),
  chain_length = 10,
  states = c("A", "B"),
  seed = 1
)
head(result)

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