After running a multilevel model in R using the lme4 package, you may wish to extract information to allow you to look at predicted values, check model assumptions, and understand the results. After running a regression model in R using glm or lm, the returned object is "glm" or "lm". There are numerous R functions (summary, anova, wfit, residuals, coeff, effects) that can be used with these objects. After running a regression model using lmer in the lme4 package, the returned object is "mer". This object is not compatible with the R functions designed for "lm" and "glm" objects and is generally less transparent than these other model objects.
This page will demonstrate several approaches to the "mer" object that allow interesting components to be accessed and examined.
Example 1: Checking Normality of Random Intercepts
Below, we fit a random intercept model. We may then wish to check the normality of the intercepts.
mlm1 <- lmer(mathach ~ female + (1 | id), data = hsb)
summary(mlm1)
Linear mixed model fit by REML
Formula: mathach ~ female + (1 | id)
Data: hsb
AIC BIC logLik deviance REMLdev
47064 47092 -23528 47053 47056
Random effects:
Groups Name Variance Std.Dev.
id (Intercept) 8.1686 2.8581
Residual 38.8501 6.2330
Number of obs: 7185, groups: id, 160
Fixed effects:
Estimate Std. Error t value
(Intercept) 13.3450 0.2546 52.41
female -1.3590 0.1714 -7.93
Correlation of Fixed Effects:
(Intr)
female -0.350
We can view a list of the random intercepts from the model by entering ranef(mlm1), but this is an object of class "ranef.mer". It is not considered a vector of values.
remlm1 <- ranef(mlm1) class(remlm1) [1] "ranef.mer" hist(remlm1) Error in hist.default(y) : 'x' must be numeric
We can coerce it to be a vector of numeric values that can then be examined.
remlm1 <- as.vector(ranef(mlm1)[[1]])[,1] length(remlm1) [1] 160 class(remlm1) [1] "numeric"
Example 2: Generating predicted values based on fixed effects
