Random-effect slopes
In the previous exercise, you saw how to code random-effect intercepts. You will now see how to code random-effect slopes. With lme4
syntax, lmer()
uses (countinuous_predictor | random_effect_group)
for a random-effect slope. When lme4
estimates a random-effect slope, it also estimates a random-effect intercept.
scale()
rescaled the predictor variable mathkind
to make the model more numerically stable. Without this change, lmer()
cannot fit the model.
In the previous exercise, you estimated a random-effect intercept for each classroom and one slope
for all data. Here, you will estimate a random-effect intercept for each class and a random-effect slope for each classroom. Like a random-effect intercept, a random-effect slope comes from a shared distribution of all random-effect slopes.
This exercise is part of the course
Hierarchical and Mixed Effects Models in R
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Rescale mathkind to make the model more stable
student_data <-
student_data %>%
mutate(mathkind_scaled = scale(mathkind))
# Build lmer models
lmer_intercept <- lmer(___ ~ ___ + (1 | ___),
data = ___)
lmer_slope <- lmer(___ ~ (___ | ___),
data = ___)