hmmTMB1.1.2 package

Fit Hidden Markov Models using Template Model Builder

32 available

as_character_formula

Read formula with as.character without splitting

as_sparse

Transforms matrix to dgTMatrix

bdiag_check

Create block diagonal matrix (safe version)

check_contiguous

Check values in vector are contiguous

cov_grid

Grid of covariates

Dist

R6 class for probability distribution

dvm

Density function of von Mises distribution

dwrpcauchy

Density function of wrapped Cauchy distribution

find_re

Find s(, bs = "re") terms in formula

gdeterminant

Generalized matrix determinant

HMM

R6 class for hidden Markov model

hmmTMB_cols

hmmTMB colour palette

hmmTMB-package

hmmTMB: Fit Hidden Markov Models using Template Model Builder

invmlogit

Multivarite inverse logit function

is_whole_number

Check if number of whole number

logLik.HMM

logLik function for SDE objects

logsumexp

Log of sum of exponentials

make_cov

Make covariance matrix from standard deviations and correlations

make_formulas

Process formulas and store in nested list

make_matrices

Create model matrices

MarkovChain

R6 class for HMM hidden process model

mlogit

Multivariate logit function

mvnorm_invlink

Multivariate Normal inverse link function

mvnorm_link

Multivariate Normal link function

na_fill

Fill in NAs

Observation

R6 class for HMM observation model

prec_to_cov

Get covariance matrix from precision matrix

quad_pos_solve

Solve for positive root of quadratic ax^2 + bx + c = 0 when it exists

rvm

Sample from von Mises distribution

rwrpcauchy

Sample from wrapped Cauchy distribution

strip_comments

Strip comments marked with a hash from a character vector

update.HMM

Update a model to a new model by changing one formula

hmmTMB

This R package implements flexible hidden Markov models, based on Template Model Builder (TMB): flexible state-dependent distributions, transition probability structures, random effects, and smoothing splines.

Paper

The statistical background, as well as details about the implementation of the package, and several example analyses, are presented in the following paper.

Michelot, T. (2025). hmmTMB: hidden Markov models with flexible covariate effects in R. Journal of Statistical Software, 114(5), 1-45.

Package installation

The package is available on CRAN, and the stable version can therefore be installed using

install.packages("hmmTMB")

The development version of the package can be installed from Github using devtools,

devtools::install_github("TheoMichelot/hmmTMB")

Package documentation

To find help files for the methods implemented in the package, search for help using the name of the corresponding class, e.g.,

?MarkovChain
?Observation
?HMM

We describe functionalities of the package in several vignettes, which are listed below.

Main vignette

Other features

  • 'Bayesian inference in hmmTMB': Description of workflow for Bayesian analysis in hmmTMB, including specifying priors, and extracting posterior samples.

  • 'Advanced features of hmmTMB': Description of some other useful functionalities, including (semi-)supervised learning, parameter constraints, selection of initial parameter values, etc.

  • 'General dependence structures in hmmTMB': Implementation details for hidden Markov models (HMMs) with non-standard dependence structures, including hidden semi-Markov models, higher-order HMMs, autoregressive HMMs, and coupled HMMs.

Case studies

  • 'Flexible animal movement modelling using hmmTMB': Description of wild haggis movement analysis, illustrating how non-parametric covariate effects can be included. This includes two different types of movement models: (1) correlated random walks based on step lengths and turning angles, and (2) correlated random walks based on locations directly.

  • 'Occupancy modelling using hmmTMB': Analysis of occupancy data set of crossbill from Kéry et al. (2013), "Analysing and mapping species range dynamics using occupancy models, Journal of Biogeography, 40(8), 1463-1474.

  • 'The categorical distribution in hmmTMB': The categorical distribution is slightly different from others, in that the number of parameters depends on the data (specifically, on the number of categories in the data). This vignette shows an example analysis for a data set on patient-therapist interactions from the mHMMbayes package.

  • 'The multivariate normal distribution in hmmTMB': We illustrate the state-dependent multivariate normal distribution for modelling correlated variables, using time series of log-returns. The vignette describes the formulation in terms of standard deviations and correlation parameters, and compares this approach to using multiple univariate normal distributions.

Reference vignettes

  • 'List of distributions in hmmTMB': List of observation distributions currently available in hmmTMB.

  • 'Adding new distributions in hmmTMB': Technical vignette about adding new observation distributions to hmmTMB, with detailed instructions to add the required R and C++ code.

  • 'Syntax of model formulas in hmmTMB': Flexible covariate dependence can be specified using the syntax of mgcv formulas, and this vignette describes a few special cases, including random effects (intercepts or slopes), smooth non-linear relationships, factor-smooth interactions, multidimensional smooths, varying-coefficient models, cyclical effects, etc.

  • 'HMM package comparison': Comparison of syntax between several R packages (hmmTMB, momentuHMM, depmixS4, LMest) and a Python package (hmmlearn) for a simple 2-state Gaussian HMM.

Fitting hidden Markov models using automatic differentiation and Laplace approximation, allowing for fast inference and flexible covariate effects (including random effects and smoothing splines) on model parameters. The package is described by Michelot (2025) <doi:10.18637/jss.v114.i05>.