4
votes

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.

1
This would be much easier if you provide a reproducible example with sample data and the code for the model that you ran (or a similar one). There are many different types of model you can run. Would as.data.frame(VarCorr(model)) work for you? - MrFlick

1 Answers

7
votes

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.