1

My mixed effects model is very simple, one outcome, one covariate, one random intercept. Something similar to this.

# download data directly from URL
url <- "https://raw.githubusercontent.com/hauselin/rtutorialsite/master/data/simpsonsParadox.csv"
df1 <- fread(url)

# LMER model
m_intercept <- lmer(grades ~ iq + (1 | class), data = df1)
summary(m_intercept)

My question is how do I plot this using ggplot function geom_smooth ?

Something similar to this

 ggplot(mpg, aes(displ, hwy)) +
 geom_point() +
 geom_smooth(method = lm, se = FALSE)

Thanks.

  • 1
    This answer requires a few steps, but might help: https://stackoverflow.com/questions/31075407/plot-mixed-effects-model-in-ggplot – Jon Spring Mar 12 '23 at 06:45
  • 1
    If you're trying to plot marginal effects, I'd suggest taking a look at [`sjPlot`](https://strengejacke.github.io/sjPlot/articles/plot_marginal_effects.html) which can be used for mixed models. It will provide a much simpler answer. – Oliver Mar 12 '23 at 08:00

1 Answers1

2

Simply create a data frame of predictions and plot:

newdata <- expand.grid(iq = 90:130, class = letters[1:4])
newdata$grades <- predict(m_intercept, newdata)

ggplot(df1, aes(iq, grades, color = class)) +
  geom_point() +
  geom_line(data = newdata)

enter image description here

A nice demonstration of Simpson's paradox indeed. If your goal is to actually demonstrate Simpson's paradox, you might want to add some bells and whistles:

newdata <- data.frame(iq = c(90, 105, 95, 115, 105, 125, 110, 130),
                      class = rep(letters[1:4], each = 2))
newdata$grades <- predict(m_intercept, newdata)

ggplot(df1, aes(iq, grades, color = class)) +
  geom_point(size = 3, alpha = 0.5) +
  geom_line(data = newdata) +
  geom_smooth(se = FALSE, aes(linetype = "Naive linear model"),
              color = "gray50", method = "lm") +
  scale_linetype_manual(NULL, values = 3) +
  theme_minimal(base_size = 16) +
  scale_color_brewer("Mixed effect model\nwith intercept\nper class", 
                     palette = "Set1") +
  ggtitle("Illustration of Simpson's Paradox")

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • @AllenCameron, when i use the `geom_line(data = newdata)` option I get an error saying " `Error in geom_line()` `Problem while computing asthetics` `Error occurred in the 2nd layer.` `Caused by error in FUN()`: `! object iq not found` – Ahir Bhairav Orai Mar 16 '23 at 04:28
  • 1
    @AhirBhairavOrai are you sure that you have created `newdata` correctly? This error is telling you that `newdata` doesn't have a column called `iq`, but if you copy-paste the code that creates `newdata` it definitely _does_ have a column called `iq` – Allan Cameron Mar 16 '23 at 07:33
  • i am infact doing prediction y_hat for the outcome on the same dataset, – Ahir Bhairav Orai Mar 16 '23 at 07:39