问题
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:
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
.
来源:https://stackoverflow.com/questions/25084924/extracting-standard-deviation-of-random-effects-components-using-glmer