I am currently looking into mixed effect models and I am trying to understand the difference between adding a random effect and adding a covariate to a linear model. Consider this example:
library(lme4) df <- read.csv("https://assets.datacamp.com/production/repositories/1803/datasets/eb95cb6973afa56c38ba53cfd8058c72f768322f/countyBirthsDataUse.csv") df <- na.omit(df) # only look at some states, otherwise to messy df <- df %>% filter(State %in% c("SC","CT","CA")) model1 <- lmer(BirthRate ~ AverageAgeofMother + (1|State), df) ggplot(df, aes(x = AverageAgeofMother, y = BirthRate, colour = State)) + geom_point(size=3) + geom_line(aes(y = predict(model1)),size=1)
As you can see, I am trying to predict the
BirthRate using the
I see that there are differences depending on the state (different intercepts).
If I now use
model1 to predict new data, does the model account for these differences in state?
If so, how is the done on a mathematical level (if possible, please explain it rather simple).
Using a very simple linear model gives a different intercept and slope, so I guess something more complex than just averaging the intercepts/slope is going on.
Lastly, can somebody point out to me what the difference is between using a mixed model like I did and using multiple regression (
lm(BirthRate ~ AverageAgeofMother + State). If I am not mistaken, in this case I am also considering the effect the
State has on the
Any insights are appreciated!