This function computes the log-likelihood value of a (hierarchical) hidden Markov model for given observations and parameter values.
Arguments
- parUncon
An object of class
parUncon
, which is anumeric
vector
with identified and unconstrained model parameters in the following order:non-diagonal transition probabilities
gammasUncon
expectations
muUncon
standard deviations
sigmaUncon
(if any)degrees of freedom
dfUncon
(if any)fine-scale parameters for each coarse-scale state, in the same order (if any)
- observations
A
numeric
vector
of time-series data.In the hierarchical case (
hierarchy = TRUE
), amatrix
with coarse-scale data in the first column and corresponding fine-scale data in the rows.- controls
Either a
list
or an object of classfHMM_controls
.The
list
can contain the following elements, which are described in more detail below:hierarchy
, defines an hierarchical HMM,states
, defines the number of states,sdds
, defines the state-dependent distributions,horizon
, defines the time horizon,period
, defines a flexible, periodic fine-scale time horizon,data
, alist
of controls that define the data,fit
, alist
of controls that define the model fitting
Either none, all, or selected elements can be specified.
Unspecified parameters are set to their default values.
Important: Specifications in
controls
always override individual specifications.- hierarchy
A
logical
, set toTRUE
for an hierarchical HMM.If
hierarchy = TRUE
, some of the other controls must be specified for the coarse-scale and the fine-scale layer.By default,
hierarchy = FALSE
.- states
An
integer
, the number of states of the underlying Markov chain.If
hierarchy = TRUE
,states
must be avector
of length 2. The first entry corresponds to the coarse-scale layer, while the second entry corresponds to the fine-scale layer.By default,
states = 2
ifhierarchy = FALSE
andstates = c(2, 2)
ifhierarchy = TRUE
.- sdds
A
character
, specifying the state-dependent distribution. One of"normal"
(the normal distribution),"lognormal"
(the log-normal distribution),"t"
(the t-distribution),"gamma"
(the gamma distribution),"poisson"
(the Poisson distribution).
The distribution parameters, i.e. the
mean
mu
,standard deviation
sigma
(not for the Poisson distribution),degrees of freedom
df
(only for the t-distribution),
can be fixed via, e.g.,
"t(df = 1)"
or"gamma(mu = 0, sigma = 1)"
. To fix different values of a parameter for different states, separate by "|", e.g."poisson(mu = 1|2|3)"
.If
hierarchy = TRUE
,sdds
must be avector
of length 2. The first entry corresponds to the coarse-scale layer, while the second entry corresponds to the fine-scale layer.By default,
sdds = "normal"
ifhierarchy = FALSE
andsdds = c("normal", "normal")
ifhierarchy = TRUE
.- negative
Either
TRUE
to return the negative log-likelihood value (useful for optimization) orFALSE
(default), else.- check_controls
Either
TRUE
to check the defined controls orFALSE
to not check them (which saves computation time), else.
Examples
### HMM log-likelihood
controls <- set_controls(states = 2, sdds = "normal")
parameters <- fHMM_parameters(controls)
parUncon <- par2parUncon(parameters, controls)
observations <- 1:10
ll_hmm(parUncon, observations, controls)
#> [1] -268.9179
### HHMM log-likelihood
controls <- set_controls(
hierarchy = TRUE, states = c(2, 2), sdds = c("normal", "normal")
)
parameters <- fHMM_parameters(controls)
parUncon <- par2parUncon(parameters, controls)
observations <- matrix(dnorm(110), ncol = 11, nrow = 10)
ll_hmm(parUncon, observations, controls)
#> [1] -70.26436