Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting standard deviation of random effects components using glmer

Tags:

r

lme4

I am using glmer and I wish to extract the standard deviation of the variance components of the random effects (intercept and slope).

I have tried using:

VarCorr(model)

which returns the two standard deviation values (plus the correlation), but I just wish to extract the Intercept and Slope SD values.

I tried using:

VarrCorr(model)[1]

to extract the random intercept SD, which lets me know that:

attr(,"stddev")
(Intercept)        year 
      0.075       0.011 

but I don't know how to extract these as individual elements.

like image 369
user3628889 Avatar asked Mar 18 '23 21:03

user3628889


1 Answers

There are 2 ways to do this.

## make up a model
library(lme4)
(gm <- glmer(incidence ~ period + (size | herd),
              family = poisson, data = cbpp))

Approach 1

The current version of lme4 allows you to coerce a VarCorr object to a data frame:

as.data.frame(VarCorr(gm))

Then you can select rows 1:2 and column 5 to extract standard deviations of random intercept and slope.

Approach 2

If you want to extract the values in a old-fashioned way, you can use attributes:

attributes(VarCorr(gm)$herd)$stddev
(Intercept)        size 
 1.18970662  0.08826278 

If you want to get rid of the names (i.e., (intercept), size), then you can use as.numeric or unname.

like image 176
Masato Nakazawa Avatar answered Apr 30 '23 19:04

Masato Nakazawa