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:

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)

enter image description here

As you can see, I am trying to predict the BirthRate using the AverageAgeofMother. 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 BirthRate

Any insights are appreciated!



Yes, the model will account for differences in states. As you have noted, each state has it's own intercept and this is exactly what happens on the mathematical level - there is a global intercept and each state has a it's own intercepts which is an offset from the global intercept. In most software the random effects are assumed to be normally distributed, so when you have few groups it is often better to model them as fixed effects, otherwise the software is trying to estimate a variance for a variable from very few observations.

As for the difference the model you fitted and a model with just fixed effects, there isn't much difference when you have a small number of groups, but when the number of groups becomes large it is inconvenent to fit fixed effects, and is also more parsimoneous to use random effects.

  • $\begingroup$ Thank you for your explanation. If I understood it correctly, depending on the State the woman I am looking at, I am simply adding the intercept which is predicted for the corresponding state from my "global" regression equation. Is that in essence correct? In this case State would simply be a matrix of 0 and 1. $\endgroup$
    – nickhir
    Jun 10 '21 at 12:12
  • $\begingroup$ The predicted value would be the global intercept +/- the intercept for the state, plus the fixed effects. $\endgroup$ Jun 10 '21 at 12:50
  • $\begingroup$ When you have only 3 groups you don't have much information for estimating the variance of the random effects. So I'm not sure random effects works in this case. $\endgroup$ Aug 21 '21 at 12:30
  • 1
    $\begingroup$ @FrankHarrell indeed, that's why I recommended fixed effects instead. $\endgroup$ Aug 21 '21 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.