fit_sequence_hmm: Fit a categorical hidden Markov model

View source: R/sequence-latent-models.R

fit_sequence_hmmR Documentation

Fit a categorical hidden Markov model

Description

Fits a finite-state, time-homogeneous categorical HMM by Baum-Welch EM. Latent states are statistical model states only; they are not psychological, diagnostic, or causal constructs.

Usage

fit_sequence_hmm(
  data,
  n_states,
  sequence_id_col = "sequence_id",
  order_col = "sequence_order",
  state_col = "state",
  symbol_levels = NULL,
  state_names = NULL,
  initial_probs = NULL,
  transition_probs = NULL,
  emission_probs = NULL,
  max_iter = 200L,
  tolerance = 1e-06,
  pseudocount = 1e-06,
  seed = 1L,
  keep_posteriors = FALSE
)

Arguments

data

Long-format sequence data.

n_states

Number of latent states.

sequence_id_col, order_col, state_col

Sequence columns.

symbol_levels

Optional observed-symbol ordering.

state_names

Optional latent-state names.

initial_probs, transition_probs, emission_probs

Optional starting values.

max_iter

Maximum EM iterations.

tolerance

Relative log-likelihood tolerance.

pseudocount

Non-negative smoothing count.

seed

Reproducibility seed.

keep_posteriors

Retain final forward-backward results.

Value

An object of class gp3_sequence_hmm containing fitted parameters, log likelihood, convergence diagnostics, symbol coding, and optional posteriors.

Examples

sequences <- data.frame(
  sequence_id = rep(c("s1", "s2", "s3", "s4"), each = 4L),
  sequence_order = rep(1:4, times = 4L),
  state = c("A", "B", "C", "D", "A", "B", "C", "C",
            "D", "C", "B", "A", "D", "C", "A", "A"),
  group = rep(c("g1", "g2"), each = 8L),
  stringsAsFactors = FALSE
)
fit_sequence_hmm(sequences, n_states = 2L, max_iter = 5L, seed = 1L)


gp3sequences documentation built on Aug. 23, 2026, 5:10 p.m.