[R] depmixs4 standardError() issue

Heather Lucas h|uc@@2 @end|ng |rom |@u@edu
Fri Jul 7 18:23:57 CEST 2023

Hi Rolf,

Thank you so much for this very helpful response (and I apologize for the slow response to the response)!  I did have the opportunity to speak to Maarten Speekenbrink recently and he provided similar information, so I believe you are exactly right and your response helped provide some additional context.  Good to know that it is not just me but somewhat of an inherent difficulty with HMMs.

Thank you for letting me know about your eglhmm package!  I will keep in it in mind for any future projects in which there is only one predictor.

For now, I have switched from depmixS4 to a new package called hmmTBM.  Not so much because of the issue with the NaN CIs but because it allows random effects. This is extremely helpful for me since I am working with eye tracking data, which has fixations nested in trials and trials nested in participants.  While I am still learning, this package seems to be working well for me so far.

Thanks again for taking the time to respond to my question!


Heather D. Lucas, PhD
(she/her pronouns)
Assistant Professor, Cognitive and Brain Sciences
Committee on Diversity and Outreach in Psychology, Member
Louisiana State University
210 Audubon Hall
Baton Rouge, LA 70803

From: Rolf Turner <rolfturner using posteo.net>
Date: Saturday, June 24, 2023 at 8:36 PM
To: Heather Lucas <hlucas2 using lsu.edu>
Cc: r-help using r-project.org <r-help using r-project.org>
Subject: Re: [R] depmixs4 standardError() issue
[You don't often get email from rolfturner using posteo.net. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

On Tue, 30 May 2023 17:43:31 +0000
Heather Lucas <hlucas2 using lsu.edu> wrote:

> Hello,
> I've been enjoying using the "Mixture and Hidden Markov Models in R"
> by Visser & Speekenbrink to learn how to apply these analyses to my
> own data using depmixS4.
> I currently have a fitted 4-state mixture model with three emissions
> variables and one binomial covariate (HS). I am trying to compute
> confidence intervals using the following code, where fmms4s is the
> model:
> fmms4Svov <- vcov(fmms4S)$vcov  #this line runs fine
> fmms4Sse <- standardError(fmms4S)  #this is where I get the error
> fmms4SCI <- confint(fmms4S)
> This worked fine before I added the covariate, but with the covariate
> I receive a warning message: In sqrt(diag(vc$vcov)) : NaNs produced.
> As a result, several of my parameters have NaNs as CIs.  In general,
> I get this error more frequently for more complex models (even when
> these models converge and show a better fit than simpler models) but
> I cannot find any information as to why this happens.  Getting rid of
> one of the emissions variables but leaving the covariate also seems
> to �fix� the issue but it crops up quite often for me in general.
> Many thanks,

Greetings.  I hope, given the very belated nature of my response, that
you are still interested in this issue.  I have seen no answers to your
enquiry, so I am chipping in, despite my woeful lack of expertise.

Basically my guess would be that the NaNs result from  the covariance
matrix vc$vcov having negative diagonal entries --- negative variance
estimates.  I believe that vc$vcov would have been produced, at some
level, by inverting the negative of the Hessian matrix of the parameter
estimates.  In my experience, the Hessian matrices that arise in the
hidden Markov model context can be, shall we say, a bit delicate.
(Downright flaky, not to put too fine a point on it.) It may be the
case that the Hessian is not negative definite.

Theoretically the Hessian matrix will be negative definite at the
maximum (or at a local maximum) of the log likelihood of the model.
Convergence issues may be such that, even though the algorithm is judged
to have converged, the parameter estimates are sufficiently far from
the maximum point so that the Hessian will have some positive
eigenvalues (equivalently its negative will have some negative
eigenvalues) leading to the negative variance estimates.  If you could
look at it, you would probably find that the gradient vector (which
should theoretically be zero at the maximum) has some unacceptably
large entries.

The numerical analysis considerations that arise in maximising the log
likelihood of a hidden Markov model are fraught with difficulties and
subtleties.  The more complex the model, the more likely it is that
these difficulties will rear their ugly little heads.  When they do,
it seems to me, there is not much that an ordinary mortal can do about

A couple of other comments.

(a) In the past I have communicated a bit with the maintainer of the
depmixS4 package (Ingmar Visser <i.visser using uva.nl>) and found him to be
reasonably helpful and responsive.  You might try contacting him
directly and see if he has any useful suggestions.  You would of course
have to make your data available to him, in order for him to get

(b) You might like to try experimenting with my package "eglhmm"
("extended generalised linear hidden Markov models").  This package has
considerable overlap with depmixS4, but has a very different syntax and
a different set of bells an whistles.  However I note that in your
enquiry you say that you have "three emission variables" so it appears
that you are dealing with a multivariate problem.  My package does not
do multivariate stuff, except in one restricted (and rather récherché)
context so my package probably won't be of any real use to you.

Also my package does not do mixtures as such.

If you *should* be interested in trying my package, you can get it from
my web page (https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.stat.auckland.ac.nz%2F~rolf&data=05%7C01%7Chlucas2%40lsu.edu%7C992a7466238a4739cfef08db751ca58a%7C2d4dad3f50ae47d983a09ae2b1f466f8%7C0%7C0%7C638232538139907909%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=zkIxJHl75Xuw4p1wPBiDIrX94IX%2F1dASRxrU3l%2FiNOQ%3D&reserved=0; scroll to the
bottom and click on "Eglhmm").  The source package and a Windoze binary
are available.  I have not yet submitted my package to CRAN, although I
hope to do so in the fairly (???) near future.

My package may well have a fair few bugs lurking in its innards.  (But
then, don't they all?)  If you do experiment with eglhmm, please let me
know about any bugs that you encounter.

I hope this of some help and interest.


Rolf Turner

Honorary Research Fellow
Department of Statistics
University of Auckland
Stats. Dep't. (secretaries) phone:
         +64-9-373-7599 ext. 89622
Home phone: +64-9-480-4619

	[[alternative HTML version deleted]]

More information about the R-help mailing list