eglhmm {eglhmm} | R Documentation |
Fit (extended) generalised linear hidden Markov models.
Description
Fits an (extended) generalised linear model to a data set where the response in each “cell” of the model consists of a time series whose serial dependence is modelled by a hidden Markov model.
Usage
eglhmm(formula = NULL, response = NULL, data,
distr = c("Gaussian", "Poisson", "Binomial", "Dbd", "Multinom", "discnp"),
inclTau=TRUE,preSpecSigma=NULL, indep = NULL, size = NULL, nbot = NULL, ntop = NULL,
cells = NULL, cf = "singlecell", K = NULL, par0 = NULL, randStart = NULL,
method = c("lm", "em", "bf"), optimiser = c("optim", "nlm"),
optimMethod = "BFGS", nlmWarn = FALSE, lmc = 10, tolerance = NULL,
digits = NULL, verbose = FALSE, itmax = 200,
contrast = c("treatment", "sum", "helmert"),
crit = c("CLL", "L2", "Linf", "ABSGRD"), breaks = NULL, hessian = FALSE,
useAnalGrad = FALSE, ca = FALSE, checkDecrLL=TRUE)
Arguments
formula |
A model formula specifying the linear predictor for the model. The
formula should not include |
response |
A character scalar or a length-2 vector of such scalars,
specifying the name or names of the response(s).
If |
data |
A data frame with columns providing the response(s) and the predictor variables in the model. |
distr |
Character string specifying the distribution of the response(s)
(“emissions”) variable(s). Currently (13/02/2024) the only
distributions accommodated are Gaussian, Poisson, Binomial, Dbd,
and Multinom. Note that |
inclTau |
Logical scalar. Should the transition probability matrix parameters
“ |
preSpecSigma |
Numeric vector of length |
indep |
Logical scalar; should the components of a bivariate
model be considered to be independent? Ignored unless the
model is bivariate (i.e. unless |
size |
Scalar integer specifying the number of trials in the experiment generating
binomial data (see the |
nbot |
Scalar integer specifying the lower end (0 or 1) of the range
of values of the discretised Beta distribution. Ignored unless
|
ntop |
Scalar integer specifying the upper end of the range of values
of the discretised Beta distribution. Ignored unless |
cells |
A character vector giving the names of the factors
(columns of the |
cf |
A factor (“cell factor”) specifying the cells
of the model. If |
K |
Scalar integer specifying the number of states of the hidden Markov
model in question. If |
par0 |
A list comprising starting values for the parameter estimates,
to be used by the various methods. (See |
randStart |
Either a logical scalar or a list of three logical scalars
named |
method |
Character string specifying the method used to fit the model. This
may be |
optimiser |
Character string specifying which of |
optimMethod |
Character string specifying the optimisation method to be used
by |
nlmWarn |
The |
lmc |
Positive numeric scalar. The initial “Levenberg-Marquardt
constant”. Ignored unless |
tolerance |
Positive numeric scalar. The convergence tolerance to be used.
What this value actually means depends upon |
digits |
Integer scalar. The number of digits to which “progress
reports” are printed when |
verbose |
Logical scalar; if |
itmax |
Integer scalar. The maximum number of iterative steps to
take. Has a somewhat different meaning when |
contrast |
Text string specifying the contrast (in respect of
unordered factors) (see |
crit |
Text string specifying the stopping criterion to be used. Possible values are “CLL” (scaled change in log likelihood), “L2” (scaled square root of the sum of squares of the changes in the parameter estimates), “Linf” (scaled maximum of the absolute value of the changes in the parameter estimates), and “ABSGRD” (scaled maximum of the absolute values of the entries of the gradient vector). The latter only makes sense for the Levenberg-Marquardt algorithm. This argument is ignored if |
breaks |
A vector of |
hessian |
Logical scalar; should a Hessian matrix obtained by numerical
differentiation be returned? Ignored unless |
useAnalGrad |
Logical scalar; should “analytical” calculation of the
gradient be conducted? This argument is ignored unless the method
is |
ca |
Logical scalar; “check analyticals”. Used only when
the method is |
checkDecrLL |
Logical scalar; “check for a decrease in the log likelihood”.
Ignored unless the |
Value
An object of class "eglhmm"
, consisting of a list with
components:
call |
The call by which this object was created. Present
so that |
tpm |
The estimated transition probability matrix. |
ispd |
The estimated initial state probability distribution. |
phi |
Except for the |
theta |
The vector of parameter estimates that the estimation
procedure actually works with. It consists of the catenation of
the non-redundant parameterization of the transition probability
matrix and the vector |
Rho |
A matrix, or a list of two matrices or a three dimensional
array specifying the emissions probabilities for a multinomial
distribution. Present only if |
log.like |
The value of the log likelihood of the model evaluated at the parameter estimates, i.e. the (approximately) maximal value of the log likelihood. |
gradient |
(Not present for the |
numHess |
(Present only if |
Hessian |
(Present only if |
mu |
A data frame with |
sigma |
Numeric vector of length |
preSpecSigma |
Numeric vector equal to the |
stopCritVal |
Numeric scalar equal to the value, assumed by the
stopping criterion specified by the argument |
anomaly |
Logical scalar. Did an “anomaly” occur
in an application of the EM algorithm? (See Remarks.) '
Present only if |
converged |
A logical scalar. For the For the |
nstep |
The number of steps (iterations) actually used by
the algorithm. For the |
mean |
A vector of the fitted mean values underlying each
combination of observed predictors and state (i.e. corresponding to each
entry of |
sd |
A vector of the fitted values of the standard
deviations underlying each combination of observed predictors and state,
i.e. corresponding to each entry of |
lambda |
A vector of estimated values of the Poisson
parameter associated with each combination of observed predictors and state,
i.e. corresponding to each entry of |
p |
A vector of estimated values of the “success”
probabilities associated with each combination of observed
predictors and state, i.e. corresponding to each entry of
|
alpha |
A numeric vector of the fitted “alpha”
parameters, of the discretised Beta distribution, corresponding to
each observation. Present only if |
beta |
A numeric vector of the fitted “beta”
parameters, of the discretised Beta distribution, corresponding to
each observation. Present only if |
fy |
The values of the “emission probability (density)”
function, calculated at each observed value, for each state
(i.e. at each entry of |
message |
A (long) text string that is produced if the EM
algorithm encounters the anomaly of a decrease in the log
likelihood after an EM step. It warns the user that this has
occurred and suggests consulting the help file for an explanation.
Present only if |
par0 |
The starting values used in the estimation procedure.
Either those provided by the argument |
cells |
A character vector indicating the names of the
factors specifying the “cells” of the model. (Equal to
the |
formula |
The formula for the model that was fitted; equal
to the |
distr |
Text string specifying the distribution of the
response variable. Equal to the |
nbot |
Integer scalar. The lower endpoint of the range of
values of the discretised beta distribution. Equal to the value
of the |
ntop |
Integer scalar. The upper endpoint of the range of
values of the discretised beta distribution. Equal to the value
of the |
size |
Scalar integer equal to the number of trials in the
“experiments” generating the data. Equal to the |
tolerance |
The convergence tolerance used to fit the model.
Equal to the |
crit |
Character scalar specifying the stopping criterion
that was used. Equal to the |
contrast |
Text string specifying the contrast for unordered
factors that was used in fitting the model. Equal to the
|
method |
The method ( |
stationary |
Logical scalar. Was a stationary Markov
chain fitted? Currently (13/02/2024) |
data |
The data frame to which the model was fitted.
It is a rearrangement of the |
bicm |
Numerical scalar. The number by which |
AIC |
Numerical scalar. The Akaike Information criterion,
calculated as |
BIC |
Numerical scalar. The Bayesian Information criterion,
calculated as |
missFrac |
The fraction or proportion of missing values in the observations. |
Remarks
- Available models:
Although this documentation refers to (extended) “generalised linear models”, the only such models currently (13/02/2024) available are the Gaussian model with the identity link, the Poisson model, with the log link, and the Binomial model with the logit link. When
distr
is"Dbd"
or"Multinom"
the model fitted is is a generalised linear model only in a rather extended sense. Even the Gaussian model is not strictly speaking a generalised linear model, since the (state dependent) standard deviations are estimated by a method separate from the generalised linear model paradigm. Other models may be added at a future date.- Decrease in the log likelihood:
-
If
method
is equal to"EM"
there may be a decrease (!!!) in the log likelihood at some EM step. This is “theoretically impossible” but can occur in practice due to an intricacy in the way that the EM algorithm treatsispd
whenstationary
isTRUE
. It turns out to be effectively impossible to maximise the expected log likelihood unless the term in that quantity corresponding toispd
is ignored (whence it is ignored). Ignoring this term is “asymptotically negligible” but can have the unfortunate effect of occasionally leading to a decrease in the log likelihood. Ifmethod
is equal to"em"
, then the object returned byeglhmm()
has a componentanomaly
which isTRUE
if such a decrease in the log likelihood was detected, andFALSE
otherwise.If such a decrease/anomaly is detected, then (provided that
checkDecrLL
isTRUE
) the algorithm terminates and theconverged
component of the returned value is set equal toNA
. The algorithm issues a message to the effect that the decrease occurred. The message suggests that another method be used and that perhaps the results from the penultimate EM step (which are returned by this function) be used as starting values. This of course is not possible if the response is bivariate, in which case only the EM algorithm is applicable.Note that if
checkDecrLL
isFALSE
, then the algorithm proceeds “normally”. That is, it treats the decrease in the log likelihood to mean that the “increase” in the log likeihood is less thantolerance
and deems convergence to be achieved.The value of
checkDecrLL
is set toFALSE
in the functionbcov()
so as to speed up the rate at which the iterations proceed. In other circumstances it is probably judicious to leave it at its default value ofTRUE
.
Author(s)
Rolf Turner rolfturner@posteo.net
References
T. Rolf Turner, Murray A. Cameron, and Peter J. Thomson (1998). Hidden Markov chains in generalized linear models. Canadian Journal of Statististics 26, pp. 107 – 125, DOI: https://doi.org/10.2307/3315677.
Rolf Turner (2008). Direct maximization of the likelihood of a hidden Markov model. Computational Statistics and Data Analysis 52, pp. 4147 – 4160, DOI: https://doi.org/10.1016/j.csda.2008.01.029
See Also
fitted.eglhmm()
reglhmm.default()
reglhmm.eglhmm()
bcov()
Examples
loc4 <- c("LngRf","BondiE","BondiOff","MlbrOff")
SCC4 <- SydColCount[SydColCount$locn %in% loc4,]
SCC4$locn <- factor(SCC4$locn) # Get rid of unused levels.
rownames(SCC4) <- 1:nrow(SCC4)
fitP.em <- eglhmm(y~locn+depth,data=SCC4,distr="P",cells=c("locn","depth"),
K=2,method="em",verb=TRUE)
## Not run:
fitP.lm <- eglhmm(y~locn+depth,data=SCC4,distr="P",cells=c("locn","depth"),
K=2,verb=TRUE)
fitD.lm <- eglhmm(formula=y~ma.com+nh.com+bo.com,data=SCC4,nbot=0,ntop=11,
cells=c("locn","depth"),distr="Dbd",K=2,method="lm",verb=TRUE,
tolerance=NULL)
SCD4 <- SydColDisc[SydColDisc$locn %in% loc4,]
SCD4$locn <- factor(SCD4$locn) # Get rid of unused levels.
fitM.lm <- eglhmm(formula=y~ma.com+nh.com+bo.com,data=SCD4,
cells=c("locn","depth"),distr="Multinom",K=2,
verb=TRUE)
xxx <- split(SCD4,f=SCD4$locn)
X <- with(xxx,data.frame(y.LngRf=LngRf$y,y.BondiE=BondiE$y,depth=LngRf$depth))
fitBiv <- eglhmm(response=c("y.LngRf","y.BondiE"),data=X,K=2,cells="depth",
indep=FALSE,verb=TRUE)
## End(Not run)
# See the help for ionChannelData for more examples involving the
# ion channel data.