I have a mer object that has fixed and random effects. How do I extract the variance estimates for the random effects? Here is a simplified version of my question.
study <- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)
study
This gives a long output - not too long in this case. Anyway, how do I explicitly select the
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 1378.18 37.124
Residual 960.46 30.991
part of the output? I want the values themselves.
I have taken long looks at
str(study)
and there's nothing there! Also checked any extractor functions in the lme4 package to no avail. Please help!
Try
As an example:
The end.
Some of the other answers are workable, but I claim that the best answer is to use the accessor method that is designed for this --
VarCorr
(this is the same as inlme4
's predecessor, thenlme
package).UPDATE in recent versions of
lme4
(version 1.1-7, but everything below is probably applicable to versions >= 1.0),VarCorr
is more flexible than before, and should do everything you want without ever resorting to fishing around inside the fitted model object.By default
VarCorr()
prints standard deviations, but you can get variances instead if you prefer:(
comp=c("Variance","Std.Dev.")
will print both).For more flexibility, you can use the
as.data.frame
method to convert theVarCorr
object, which gives the grouping variable, effect variable(s), and the variance/covariance or standard deviation/correlations:Finally, the raw form of the
VarCorr
object (which you probably shouldn't mess with you if you don't have to) is a list of variance-covariance matrices with additional (redundant) information encoding the standard deviations and correlations, as well as attributes ("sc"
) giving the residual standard deviation and specifying whether the model has an estimated scale parameter ("useSc"
).lmer
returns an S4 object, so this should work:Which prints:
...In general, you can look at the source of the
print
andsummary
methods for "mer" objects: