This function predicts the most likely hidden states from observations. Two methods are implemented: "Viterbi" applies the Viterbi algorithm and predicts the most likely sequence of hidden states, and "FwBw" applies the Forward-Backward algorithm and returns the probability of each state at each time-point.

predict_states_hsmm(
  model,
  X,
  method = "Viterbi",
  ground_truth = data.frame(),
  trust_in_ground_truth = 0.75,
  verbose = FALSE
)

Arguments

model

a hsmm object. The model used to predict the hidden sequence of states.

X

a data.frame. The observation sequences. This data.frame must have the following columns: seq_id (character) provides the sequence id, which allows the prediction of hidden states for several sequence simultaneously, t (numeric) provides the timestamp. Time-points must be separated by the same time-step, ... one column for each variable specified for the model. Additional columns will be ignored.

method

a character specifying the decoding algorithm. "Viterbi" returns the most likely sequence of hidden states. "FwBw" applies the "Forward-Backward" algorithm as described by Guédon, 2003, and returns the "smoothed" (posterior) probability of each state at each time-point.

ground_truth

(optional) a data.frame with three columns (seq_id, t, state) providing the ground-truth, i.e. the actual hidden state, for a given (set of) sequence(s) and time-points.

trust_in_ground_truth

(optional) a double in [0,1] that indicates the reliability of the provided ground-truth. 1 means "full trust", 0 means "no trust". Default value is 0.75.

verbose

logical. Should the function prints additional information?

References

Y. Guédon, Estimating Hidden Semi-Markov Chains from Discrete Sequences. Journal of Computational and Graphical Statistics. 12, 604–639 (2003) https://www.tandfonline.com/doi/abs/10.1198/1061860032030

J. O’Connell, S. Hojsgaard, Hidden semi-Markov models for multiple observation sequences: The mhsmm package for R. Journal of Statistical Software. 39, 1–22 (2011) https://www.jstatsoft.org/article/view/v039i04

Examples

my_model = simple_model Xsim = simulate_hsmm(my_model, n_state_transitions = 20) # viterbi decoding viterbi = predict_states_hsmm(model = my_model, X = Xsim, method = "Viterbi") Xsim$state_viterbi = viterbi$state_seq$state plot_hsmm_seq(X = Xsim, model = my_model)
# forward backward decoding smoothed = predict_states_hsmm(model = my_model, X = Xsim, method = "FwBw") Xsim$state_smoothed = smoothed$state_seq$state plot_hsmm_seq(X = dplyr::select(Xsim, -state_viterbi), model = my_model)
library(ggplot2) ggplot( smoothed$probabilities, aes(x = t, y = state_prob, col = factor(state)) ) + geom_line() + scale_color_manual(values = my_model$state_colors)