  Hidden Markov models (HMMs) are widely applied in studies where a
discrete-valued process of interest is observed indirectly. They have for
example been used to model behaviour from human and animal tracking data,
disease status from medical data, and financial market volatility from stock
prices. The model has two main sets of parameters: transition probabilities,
which drive the latent state process, and observation parameters, which
characterise the state-dependent distributions of observed variables. One
particularly useful extension of HMMs is the inclusion of covariates on those
parameters, to investigate the drivers of state transitions or to implement
Markov-switching regression models. We present the new R package hmmTMB for HMM
analyses, with flexible covariate models in both the hidden state and
observation parameters. In particular, non-linear effects are implemented using
penalised splines, including multiple univariate and multivariate splines, with
automatic smoothness selection. The package allows for various random effect
formulations (including random intercepts and slopes), to capture between-group
heterogeneity. hmmTMB can be applied to multivariate observations, and it
accommodates various types of response data, including continuous (bounded or
not), discrete, and binary variables. Parameter constraints can be used to
implement non-standard dependence structures, such as semi-Markov, higher-order
Markov, and autoregressive models. Here, we summarise the relevant statistical
methodology, we describe the structure of the package, and we present an
example analysis of animal tracking data to showcase the workflow of the
package.
