Rescaling slopes
The last plot showed changes in crime rate varied by county. This shows you that you should include Year as both a random- and fixed-effect in your model. Including Year this way will estimate a global slope across all counties as well as slope for each county. The fixed-effect slope estimates the change in major crimes across all Maryland counties. The random-effect slope estimates model for that counties have different changes in crime.
But, fitting this model produces a warning message! To address this warning, change Year from starting at 2006 to starting at 0. We provide you with this new variable, Year2 (e.g., 2006 in Year is 0 in Year2). Sometimes when fitting regression, you need to scale or center the intercept to start at 0. This improves numerical stability of the model.
This exercise is part of the course
Hierarchical and Mixed Effects Models in R
Exercise instructions
- Build a
lmer()to predictCrimewithYearas both a fixed-effect and random-effect slope andCountyas the random-effect intercept. - Build a second
lmer()to predictCrimewithYear2as both a fixed-effect and random-effect slope andCountyas the random-effect intercept.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Fit the model with Year as both a fixed and random-effect
lmer(___ ~ Year + (1 + Year | ___) , data = md_crime)
# Fit the model with Year2 rather than Year
lmer(___ ~ Year2 + (1 + Year2 | ___) , data = md_crime)