hidden_paths: Most Probable Paths of Hidden States

View source: R/hidden_paths.R

hidden_pathsR Documentation

Most Probable Paths of Hidden States

Description

Function hidden_paths computes the most probable path of hidden states of a (mixture) hidden Markov model given the observed sequences.

Usage

hidden_paths(model, ...)

## S3 method for class 'hmm'
hidden_paths(model, as_stslist = FALSE, ...)

## S3 method for class 'mhmm'
hidden_paths(model, as_stslist = FALSE, ...)

## S3 method for class 'nhmm'
hidden_paths(model, as_stslist = FALSE, ...)

## S3 method for class 'mnhmm'
hidden_paths(model, as_stslist = FALSE, ...)

Arguments

model

A hidden Markov model.

...

Ignored.

as_stslist

Logical. If TRUE, the output the is converted to an stslist object. Default is FALSE, which returns a data.table.

Value

The most probable paths of hidden states as an data.table. The log-probability is included as an attribute log_prop.

See Also

hmm_biofam for information on the model used in the example; and ggseqplot::ggseqiplot() and stacked_sequence_plot() for plotting hidden paths.

Examples

# Load a pre-defined HMM
data("hmm_biofam")

# Compute the most probable hidden state paths given the data and the model
mpp <- hidden_paths(hmm_biofam)
head(mpp)
# Plot hidden paths for the first 100 individuals
seqs <- data_to_stslist(mpp, "id", "time", "state")
stacked_sequence_plot(seqs, type = "i", ids = 1:100)

# Because the model structure is so sparse that the posterior probabilities are
# mostly peaked to single state at each time point, the joint probability of
# observations and most probable paths of hidden states is almost identical to
# log-likelihood:

sum(attr(mpp, "log_prob"))
logLik(hmm_biofam)


seqHMM documentation built on June 8, 2025, 10:16 a.m.