These functions generate wide-format categorical state sequences from
Markov chains, suitable for sequence analysis and TNA model fitting.
simulate_sequences() draws from a (possibly auto-generated)
transition matrix, while simulate_sequences_advanced()
layers stable/unstable transition dynamics on top to mimic habit
formation and disruption.
simulate_sequences()Generate one row per sequence by walking a Markov chain over a set of states, auto-generating a random transition matrix (optionally labelled with learning-state verbs) when none is supplied.
Signature
args(simulate_sequences)
#> function (n_sequences = 1000, seq_length = 20, n_states = 8,
#> states = NULL, use_learning_states = TRUE, categories = "all",
#> smart_select = TRUE, trans_matrix = NULL, init_probs = NULL,
#> na_range = c(0, 0), include_na = TRUE, include_params = FALSE,
#> seed = NULL, num_rows = NULL, max_seq_length = NULL, state_names = NULL,
#> learning_categories = NULL, transition_matrix = NULL, initial_probabilities = NULL,
#> min_na = NULL, max_na = NULL, return_params = NULL)
#> NULLExample
seqs <- simulate_sequences(
n_sequences = 8,
seq_length = 6,
n_states = 4,
include_na = FALSE,
seed = 42
)
dim(seqs)
#> [1] 8 6
head(seqs)Returning the ground-truth parameters alongside the sequences:
res <- simulate_sequences(
n_sequences = 8,
seq_length = 6,
n_states = 4,
include_params = TRUE,
seed = 42
)
names(res)
#> [1] "sequences" "transition_matrix" "initial_probabilities"
#> [4] "state_names"
round(res$transition_matrix, 2)
#> Plan Regulate Discourage Judge
#> Plan 0.22 0.03 0.65 0.10
#> Regulate 0.01 0.51 0.29 0.20
#> Discourage 0.31 0.02 0.07 0.61
#> Judge 0.58 0.19 0.17 0.06Using learning-state verbs as the state labels:
seqs_ls <- simulate_sequences(
n_sequences = 6,
seq_length = 6,
n_states = 4,
use_learning_states = TRUE,
categories = "cognitive",
include_na = FALSE,
seed = 7
)
head(seqs_ls)simulate_sequences_advanced()Generate Markov sequences with explicit stable transition pairs (followed with high probability) and a configurable unstable mode for the remaining draws, producing sequences with realistic persistence and disruption patterns.
Signature
args(simulate_sequences_advanced)
#> function (n_sequences = 1000, seq_length = 20, n_states = 8,
#> states = NULL, use_learning_states = TRUE, categories = "all",
#> trans_matrix = NULL, init_probs = NULL, stable_transitions = NULL,
#> stability_prob = 0.95, unstable_mode = "unlikely_jump", unstable_random_transition_prob = 0.4,
#> unstable_perturb_noise = 0.5, unlikely_prob_threshold = 0.1,
#> na_range = c(0, 0), include_na = TRUE, seed = NULL, transition_matrix = NULL,
#> initial_probabilities = NULL, num_rows = NULL, max_seq_length = NULL,
#> min_na = NULL, max_na = NULL)
#> NULLExample
adv <- simulate_sequences_advanced(
n_sequences = 8,
seq_length = 6,
n_states = 4,
include_na = FALSE,
seed = 42
)
dim(adv)
#> [1] 8 6
head(adv)Forcing stable transitions between specific state pairs:
adv_stable <- simulate_sequences_advanced(
n_sequences = 8,
seq_length = 6,
states = c("A", "B", "C", "D"),
stable_transitions = list(c("A", "B"), c("C", "D")),
stability_prob = 0.9,
include_na = FALSE,
seed = 123
)
head(adv_stable)