Building a lmer model with random effects
In the video, you learned about the county-level birth rate data. Counties exist within states and perhaps states contribute to variability. During these exercises, you'll build a series of mixed-effects models using this data.
In this exercise, you'll build a hierarchical model with a global intercept (fixed-effect) and random-effect for state. You will then look at the summary()
of the model and the plot()
of the residuals. Like other types of regression analysis, examining residuals can help you see if anything is wrong with the model.
With lmer()
, there are two methods for doing this: y ~ 1 + (1 | random_effect)
or the shortcut, y ~ (1 | random_effect)
. Use the shortcut in this exercise so that your answer passes the DataCamp test.
When building mixed-effect models, starting with simple models such as the global intercept model can check to see if problems exist with either the data or code. A global intercept assumes a single intercept can describe all of the variability in the data. One way to view a global intercept is that you cannot do any better modeling that data than to only model the mean without including any other predictor variables.
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.
# Build a lmer with State as a random effect
birth_rate_state_model <- lmer(___,
data =___)