Learn R Programming

HMMHSMM (version 0.1.0)

generateHMM: Generate Data from a Hidden Markov Model

Description

Simulates observations and hidden states from a Hidden Markov Model (HMM) with specified observation distributions.

Usage

generateHMM(n, J, obsdist, obspar, Pi, delta = NULL, seed = NULL)

Value

A list containing:

x

Numeric vector of the simulated observations.

state

Numeric vector of the simulated hidden state sequence.

Arguments

n

Integer. The number of observations to generate.

J

Integer. The number of hidden states in the model.

obsdist

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

obspar

List. 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.

delta

Numeric vector of length J. The initial state distribution. If NULL, the stationary distribution is computed from Pi.

seed

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

Author

[Aimee Cody]

Details

This function simulates data from a Hidden Markov Model where:

  • Hidden states follow a discrete-time Markov chain with transition matrix Pi

  • At each time step, observations are generated from state-dependent distributions

The function supports multiple observation distributions including 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. The initial state is sampled according to the initial distribution delta. If delta is not provided, it is computed as the stationary distribution of the Markov chain defined by Pi.

See Also

generateHSMM for Hidden Semi-Markov Models.

Examples

Run this code
# Example with 3 states, normal observations
J <- 3
# HMM transition matrix
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)
# Observation parameters (normal distribution)
obspar <- list(
  mean = c(-2, 0, 3),
  sd = c(0.5, 1, 1.5)
)
# Generate 200 observations
sim_data <- generateHMM(n = 200, J = J, obsdist = "norm",
                       obspar = obspar, Pi = Pi)
# View the results
head(sim_data$x)      # observations
head(sim_data$state)  # hidden states
length(sim_data$x)    # number of observations

Run the code above in your browser using DataLab