1

Random effect and variance-covariance matrix of random effect with lme4 package are extracted as following:

library(lme4)
fm1 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy)
fm1.rr <- ranef(fm1,condVar=TRUE)
fm1.pv <- attr(rr[[1]],"postVar")

I wonder how I can do this with mgcv? 'gam.vcomp' function does extract the estimated variance components, but not for each level of random effect.

library(mgcv)
fm2 <- gam(Reaction ~ Days + s (Subject, bs="re"), data = sleepstudy, method = "REML")
gam.vcomp(fm2)
olyashevska
  • 427
  • 4
  • 18
  • Does `mgcv::random.effects()` or `nlme::ranef()` do what you need? Must admit I'm less familiar with `mgcv`. If not can you post a small sample of your data, perhaps with `dput()`? See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for help – Phil Jan 09 '18 at 16:33
  • You should explain in more detail what you expect. You do get an estimate of the "standard deviation" around the smoothing spline for `Subject`. @Phil: The questioner loaded the data into his workspace because it is in hte lme4 package. You need to execute: `data(sleepstudy, package="lme4")` if you didn't run the first bit of code. You will also need to use `data=sleepstudy`, since `gam`'s second argument is not `data`. – IRTFM Jan 09 '18 at 17:31
  • Thank you. In lme4 example I get random effect and an estimate of standard deviation for each level of variable `Subject`, i.e. 18 random effects and 18 variances. In `mgcv` I get only 1 estimate around the spline. Basically I want to get same output with `mgcv` as it is produced by `lme4`. Am I missing something? The reason I want to use `mgcv` is because my real model contains a few other types of splines as well as random effects. – olyashevska Jan 09 '18 at 17:49

2 Answers2

3
library(lme4)
data(sleepstudy)

fm1 <- lmer(Reaction ~ Days + (1|Subject), sleepstudy)
fm1.rr <- ranef(fm1,condVar=TRUE)$Subject[,1]
fm1.pv <- sqrt(attr(ranef(fm1,condVar=TRUE) [['Subject']],"postVar")[1,1,])

library(mgcv)
fm2 <- gam(Reaction ~ Days + s (Subject, bs="re"), 
data = sleepstudy,   method = "REML")

To extract random effect for each Subject

idx <-grep("Subject", names(coef(fm2)))
fm2.rr<-coef(fm2)[idx]
attributes(fm2.rr)<-NULL

We can see that random effects in both models are identical as expected.

To extract variance-covariance matrix for random effect and calculate an error we use parameter Vp which is a Bayesian posterior covariance matrix:

fm2.pv <-sqrt(diag(fm2$Vp))[idx]

Or frequentist estimated covariance matrix Ve

fm2.pv <-sqrt(diag(fm2$Ve))[idx]

We can see that random effect errors estimated with mgcv slightly differ that those estimated with lme4 model. Errors based on a Bayesian posterior covariance matrix are larger, whereas based on a frequentist matrix are smaller.

olyashevska
  • 427
  • 4
  • 18
1

You can also use the package gamm4, which is based on the gamm package but using lme4 underneath. The model would be fitted as:

fm3 <- gamm4(Reaction ~ Days, random = ~ (1|Subject), data = sleepstudy)

Random effects and variance-covariance matrix of random effects can be obtained following the normal lme4 procedure.

fm3.rr <- ranef(fm3$mer,condVar=TRUE)
fm3.pv <- attr(fm3.rr[[1]],"postVar")[1,1,]

However gamm4 can be much slower than gam so read the help file to see when it best suits your need.

JWilliman
  • 3,558
  • 32
  • 36