Mixed Effects Modeling with lmer() and Plotting Growth Curves: A Comprehensive Guide

Mixed Effects Modeling with lmer() and Plotting Growth Curves

As a data analyst or statistician, you often encounter situations where you need to model the relationship between a dependent variable and one or more independent variables. In this article, we’ll explore how to use R’s lmer() function for mixed effects modeling and plot growth curves with confidence intervals.

What is Mixed Effects Modeling?

Mixed effects modeling is an extension of traditional linear regression that allows you to model the relationship between a dependent variable and one or more independent variables while accounting for the variation within groups. In our case, we’re using lmer() to fit a mixed effects model that includes both fixed effects (e.g., age.c) and random effects (e.g., childid).

The basic syntax of an lmer() model is as follows:

AgeSES.model <- lmer(ReadingMeasure ~ Age.c*SESDLD1 + (1|childid), data = reshapedomit, REML = FALSE)

Here’s a breakdown of the components:

  • ReadingMeasure: The dependent variable.
  • Age.c and SESDLD1: The independent variables. Note that we’re using the “c” suffix to indicate that Age.c is centered.
  • (1|childid): This specifies the random effect structure, which in our case includes a single level of childid.

Interpreting the Model

Our model includes three key components:

  • The fixed effects: Age.c and SESDLD1. These coefficients represent the change in reading scores for each unit increase in age.c and SESDLD1, respectively.
  • The random effect: childid. This accounts for the variation within groups.

With this model, we can expect to see four positive slopes of ReadingMeasure growth with different intercepts and probably differing slopes. In other words, our model is designed to capture the unique relationship between age and reading scores for each group defined by SESDLD1.

Plotting Growth Curves

To plot growth curves with confidence intervals, we’ll use a combination of R’s built-in plot() function and the ggplot2 package. Before we dive into the code, let’s take a brief look at how we can modify our model to obtain the desired output.

Modifying the Model for Plotting

We want to extract the predicted values for each group from our mixed effects model and plot them with confidence intervals. To do this, we’ll use the predict() function in combination with the confint() function from ggplot2.

Here’s how you can modify your code:

# Load necessary libraries
library(ggplot2)
library(broom)

# Fit the mixed effects model
AgeSES.model <- lmer(ReadingMeasure ~ Age.c*SESDLD1 + (1|childid), data = reshapedomit, REML = FALSE)

# Extract predicted values and confidence intervals for each group
df <- tidy(AgeSES.model)
df$group <- cut(df$SESDLD1, breaks = unique(df$SESDLD1), labels = 0:3)
df$fit <- predict(AgeSES.model, type = "response", newdata = df)
df$confint <- confint(AgeSES.model)

# Plot the growth curves with confidence intervals
ggplot(df, aes(x = Age.c, y = fit)) + 
  geom_line(aes(color = group), alpha = 0.5) + 
  geom_poisson(aes(y = confint, color = group), position = "dodge", size = 2) + 
  labs(title = "Growth Curves with Confidence Intervals", x = "Age (months)", y = "Reading Measure") + 
  scale_color_discrete(name = "Group") + 
  facet_wrap(~ group)

Here’s what’s happening in the code:

  • We fit our mixed effects model using lmer().
  • We extract predicted values and confidence intervals for each group using predict() and confint(), respectively.
  • We use tidy() to convert the output of our model into a tidy dataframe that we can manipulate with ggplot2.
  • We plot the growth curves using geom_line() for the main line, geom_poisson() for the confidence intervals, and facet_wrap() to display each group separately.

Visualizing Mixed Effects Models

Mixed effects models offer several benefits over traditional linear regression, including:

  • Accounting for variation within groups: By incorporating random effects into our model, we can better capture the unique relationship between age and reading scores for each group defined by SESDLD1.
  • Capturing non-linear relationships: Our model includes a centered age variable (Age.c), which allows us to estimate both linear and quadratic relationships between age and reading scores.

To further explore mixed effects modeling in R, we recommend checking out the following resources:

  • The lmerTest package provides an interface for conducting hypothesis tests on mixed effects models.
  • The brms package offers a more user-friendly interface for building Bayesian linear regression models.
  • The lme4 package provides a comprehensive set of tools for fitting and analyzing linear mixed effects models.

Conclusion

In this article, we explored how to use R’s lmer() function for mixed effects modeling and plot growth curves with confidence intervals. By using a combination of built-in functions and the ggplot2 package, we were able to visualize our model’s output in a clear and informative manner. Whether you’re working with small or large datasets, mixed effects models offer powerful tools for capturing complex relationships between variables.


Last modified on 2023-10-27