Learn R Programming

HMMHSMM (version 0.1.0)

findmleHMM: Maximum Likelihood Estimation for Hidden Markov Models

Description

Estimates the parameters of a Hidden Markov Model (HMM) using either the EM-based semi-Markov parameterisation or direct numerical maximization of the likelihood.

Usage

findmleHMM(J, x, obsdist, obspar, Pi, EM = FALSE, verbose = TRUE, seed = NULL, ...)

Value

A list containing:

estimate

List of estimated HMM parameters, including state-dependent observation parameters and transition probabilities.

loglik

The maximized log-likelihood value.

AIC

The Akaike Information Criterion for the fitted model.

BIC

Bayesian Information Criteria for the fitted model.

hessian

Optional. The Hessian matrix at the maximum likelihood estimates (returned if EM = FALSE).

Arguments

J

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

x

Numeric vector. The observed data sequence.

obsdist

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

obspar

List. Initial parameters for the observation distribution. Required parameters vary by distribution:

  • norm: mean, sd

  • pois: lambda

  • weibull: shape, scale

  • zip: pi, lambda

  • nbinom: size, mu

  • zinb: pi, size, mu

  • exp: rate

  • gamma: shape, rate

  • lnorm: meanlog, sdlog

  • gev: loc, scale, shape

  • ZInormal: mean, sd, pi

  • ZIgamma: shape, rate, pi

Each parameter should be a vector of length J with values for each state.

Pi

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

EM

Logical. If TRUE, uses an EM-based semi-Markov approximation to estimate the HMM parameters. If FALSE, maximizes the likelihood directly using nlm. Defaults to FALSE.

verbose

Logical. If TRUE, progress messages are printed to the console when EM = TRUE. Default is TRUE.

seed

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

...

Further arguments to be passed in the case of EM=TRUE, such as maxiter and tol.

Author

Aimee Cody

Details

This function fits a Hidden Markov Model to a sequence of observations using either:

  • An EM-based approximation via a semi-Markov model when EM = TRUE

  • Direct numerical maximization of the likelihood using nlm when EM = FALSE

The EM-based approach initializes dwell-time probabilities from the diagonal of Pi and estimates the HMM via the corresponding semi-Markov model. After fitting, transition probabilities are adjusted to incorporate self-transition probabilities. 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. Parameter requirements vary by distribution. When verbose = TRUE and EM = TRUE, progress messages from the EM algorithm are displayed.

See Also

generateHMM for simulating HMM data. findmleHSMM for EM-based semi-Markov estimation.

Examples

Run this code
J <- 3
Pi <- matrix(c(0.8, 0.15, 0.05,
               0.1, 0.7, 0.2,
               0.2, 0.3, 0.5), nrow = 3, byrow = TRUE)

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

x <- generateHMM(n = 200, J = J, obsdist = "norm",
                       obspar = obspar, Pi = Pi)$x

fit <- findmleHMM(J = J, x = x, obsdist = "norm",
                   obspar = obspar, Pi = Pi, EM = FALSE)

fit$estimate
fit$loglik
fit$AIC

fit_em <- findmleHMM(J = J, x = x, obsdist = "norm",
                      obspar = obspar, Pi = Pi, EM = TRUE, verbose = FALSE)

Run the code above in your browser using DataLab