# [R-sig-ME] extracting gradient and hessian matrix from lme4

Ben Bolker bbolker at gmail.com
Mon Mar 19 20:15:35 CET 2012

Joshua Wiley <jwiley.psych at ...> writes:

>
> Hi,
>
> I am trying to use a multivariate mixed effects linear model to
> examine mediation.  This works fine.  The final step is to compute the
> indirect effect and its standard error.  The indirect effect is easy
> (product of coefficients plus their covariance).  For the standard
> error, I need the gradient (D) and the hessian (H):
> the variance is then:
>
> D'\Sigma(\theta)D + \frac{1}{2}Tr{(\mathbf{H}\boldsymbol{\Sigma}(\theta))^{2}}
>
> This is all given in the Appendix of
> http://www.unc.edu/~dbauer/manuscripts/bauer-preacher-gil-PM-2006.pdf
>
> Is there a way to get this out of a mer class object?  Looking at
> class?mer, the appropriate bits of vcov give me $\Sigma(\theta)$.  @V
> seems like it would give me the gradient but is null for a basic lmer
> model.

If you're willing to try out the development version (i.e., lme4
from r-forge), I think you can do this as follows:

fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
fm1Fun <- update(fm1,devFunOnly=TRUE)
library(numDeriv)
fm1_thpar <- getME(fm1,"theta")
h <- hessian(fm1Fun,fm1_thpar)