Learn R Programming

HMMHSMM (version 0.1.0)

findmleHSMM: Maximum Likelihood Estimation for Hidden Semi-Markov Models

Description

Estimates the parameters of a Hidden Semi-Markov Model (HSMM) using the EM algorithm with specified observation and dwell-time distributions.

Usage

findmleHSMM(x, J, M = NA, obsdist, dwelldist, obspar, dwellpar,
            Pi, delta = NULL, maxiter = 100, tol = 1e-05, shift = FALSE,
            verbose = TRUE, seed = NULL)

Value

A list containing:

loglikelihoods

Numeric vector of log-likelihood values across iterations.

AIC

Akaike Information Criteria for the fitted model.

BIC

Bayesian Information Criteria for the fitted model.

delta

Numeric vector. Estimated initial state probabilities.

Pi

Matrix. Estimated J x J transition probability matrix.

dwellparameters

List. Estimated dwell-time distribution parameters.

observationparameters

List. Estimated observation distribution parameters.

Arguments

x

Numeric vector. The observed data sequence.

J

Integer. The number of hidden states in the model. Must be greater than 1.

M

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

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", "geom".

obspar

List. Parameters for the observation distribution. Each parameter must be a vector of length J, with values for each state. Required parameters:

  • norm: mean, sd

  • pois: lambda

  • weibull: shape, scale

  • zip: lambda, pi

  • nbinom: size, mu

  • zinb: size, mu, pi

  • exp: rate

  • gamma: shape, rate

  • lnorm: meanlog, sdlog

  • gev: loc, scale, shape

  • ZInormal: mean, sd, pi

  • ZIgamma: shape, rate, pi

dwellpar

List. Parameters for the dwell-time distribution. Each parameter must be a vector of length J, with values for each state. Required parameters:

  • pois: lambda

  • nbinom: size, mu

  • betabinom: size, prob

  • geom: prob

Pi

Matrix. The J x J transition probability matrix between states. Rows must sum to 1.

delta

Numeric vector of length J. Initial state distribution. If NULL, the stationary distribution of Pi is used.

maxiter

Integer. Maximum number of EM iterations. Defaults to 100.

tol

Numeric. Convergence tolerance for the change in log-likelihood. Defaults to 1e-05.

shift

Logical. Indicates whether the dwell-time distribution includes a user-specified shift parameter. If TRUE, the distribution uses the provided shift parameter. If FALSE, the dwell-time distribution is automatically shifted by 1. Defaults to FALSE.

verbose

Logical. If TRUE, progress messages including iteration numbers and log-likelihood values are printed to the console. Default is TRUE.

seed

Integer or NULL. Random seed for reproducibility. Default is NULL.

Author

Aimee Cody

Details

This function fits a Hidden Semi-Markov Model to a sequence of observations using an EM algorithm. At each iteration, the algorithm:

  • Computes observation probabilities for each state

  • Calculates dwell-time probabilities and survival functions

  • Performs a forward-backward-like procedure to estimate expected sufficient statistics

  • Re-estimates observation and dwell-time parameters

Supported observation distributions include normal, Poisson, Weibull, zero-inflated Poisson (ZIP), negative binomial, zero-inflated negative binomial (ZINB), exponential, gamma, log-normal, generalized extreme value (GEV), zero-inflated normal, and zero-inflated gamma.

Supported dwell-time distributions include Poisson, negative binomial, beta-binomial, and geometric.

When verbose = TRUE, the function displays log-likelihood values at each iteration and reports whether convergence was achieved.

See Also

findmleHMM for Hidden Markov Model estimation. generateHSMM for simulating HSMM data.

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(-2, 0, 3),
  sd   = c(1, 1.5, 2)
)

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

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

fit <- findmleHSMM(x = sim$x, J = J, M = 100,
                   obsdist = "norm", dwelldist = "pois",
                   obspar = obspar, dwellpar = dwellpar,
                   Pi = Pi)

fit$observationparameters
fit$dwellparameters
fit$Pi
fit$delta
fit$loglikelihoods

fit_silent <- findmleHSMM(x = sim$x, J = J, M = 100,
                          obsdist = "norm", dwelldist = "pois",
                          obspar = obspar, dwellpar = dwellpar,
                          Pi = Pi, verbose = FALSE)

Run the code above in your browser using DataLab