Learn R Programming

HMMHSMM (version 0.1.0)

localdecodeHSMM: Local Decoding for Hidden Semi-Markov Models

Description

Computes the most likely hidden state sequence for a univariate time series using a fitted Hidden Semi-Markov Model (HSMM).

Usage

localdecodeHSMM(x, M = NA, HSMM, obsdist, dwelldist)

Value

states

Numeric vector. The most likely hidden state at each observation, computed via local decoding.

Arguments

x

Numeric vector. The observed data sequence.

M

Integer. Maximum dwell time to consider for semi-Markov states. Defaults to min(length(x), 1000) if NA.

HSMM

A list containing estimated HSMM parameters, including observationparameters, dwellparameters, Pi, and delta. Typically returned by findmleHSMM.

obsdist

Character string. Observation distribution. Supported distributions: "norm", "pois", "weibull", "zip", "nbinom", "zinb", "exp", "gamma", "lnorm", "gev", "ZInormal", "ZIgamma".

dwelldist

Character string. Dwell-time distribution. Supported distributions: "pois", "nbinom", "betabinom".

Author

Aimee Cody

Details

This function uses the forward-backward algorithm for HSMMs to compute local state probabilities and returns the most probable state at each time point. It supports a wide range of observation distributions and dwell-time distributions. The user can supply a maximum dwell-time truncation parameter M and optionally use a shift parameter for dwell times.

See Also

findmleHSMM, for estimating HSMM parameters from data. generateHSMM, for simulating HSMM data. localdecodeHMM, for local decoding of a hidden Markov model. globaldecodeHSMM, for finding the most likely sequence of states using global decoding.

Examples

Run this code
J <- 3
Pi <- matrix(c(0.0, 0.6, 0.4,
               0.5, 0.0, 0.5,
               0.3, 0.7, 0.0), nrow = J, byrow = TRUE)

obspar <- list(
  mean = c(-4, 0, 4),
  sd   = c(1, 0.5, 1.5)
)

dwellpar <- list(
  lambda = c(3, 5, 4)
)

# Simulate HSMM data
sim <- generateHSMM(n = 1000, J = J, obsdist = "norm",
                    dwelldist = "pois", obspar = obspar,
                    dwellpar = dwellpar, Pi = Pi)

# Fit HSMM using the true parameters
HSMM_true <- findmleHSMM(x = sim$x, J = J, M = 100,
                          obsdist = "norm", dwelldist = "pois",
                          obspar = obspar, dwellpar = dwellpar,
                          Pi = Pi)

# Decode states using localdecodeHSMM
decoded <- localdecodeHSMM(x = sim$x, HSMM = HSMM_true,
                            obsdist = "norm", dwelldist = "pois",
                            M = 100)
decoded

Run the code above in your browser using DataLab