Creating Smooth Lines and Confidence Intervals in LMER Models with ggplot2 Using ggeffects: A Step-by-Step Guide

Introduction to Smooth Lines and Confidence Intervals in LMER Models with ggplot2

In this article, we will explore how to create smooth lines and confidence intervals for the predicted values of a linear model using the ggeffects package in R. We will also cover some common issues and solutions related to creating smooth lines and confidence intervals in ggplot2.

What are LMER Models?

A Linear Mixed Effects (LMER) model is a type of regression model that accounts for variation in the data that cannot be explained by the fixed effects variables. In the context of this article, we will use an LMER model to fit a linear relationship between the dependent variable r and the independent variable(s).

What are Confidence Intervals?

A confidence interval is a range of values within which we expect the true population parameter to lie with a certain level of confidence. In the context of regression models, confidence intervals can be used to estimate the uncertainty associated with the predicted values.

The Problem

The original poster was trying to create smooth lines and confidence intervals for the predicted values of an LMER model using ggplot2. However, they were facing issues with jagged lines and incorrect confidence intervals. We will explore some common solutions to these problems in this article.

A Solution Using Ggeffects

One possible solution to creating smooth lines and confidence intervals is to use the ggeffects package. This package provides a convenient interface for visualizing the effects of fixed and random effects in linear mixed models.

To create a smooth line plot with confidence intervals using ggeffects, we can use the following code:

library(ggeffects)
model <- lm(r ~ Myc * N + TRTYEAR + Asp, data = tempEf)
pred <- ggpredict(model, c("TRTYEAR", "Myc"))
plot(pred)

This code will create a smooth line plot with confidence intervals for the predicted values of r against TRTYEAR and Myc.

Adjusting the Plot

We can adjust the plot to include additional features such as data labels, grid lines, or a title.

library(ggplot2)

# Create a new dataframe containing the predicted values
pred_df <- data.frame(TRTYEAR = pred$TRTYEAR,
                     Myc = pred$Myc,
                     Fit = pred$Fit)

# Add data labels to the plot
ggplot(pred_df, aes(x = TRTYEAR, y = Fit)) +
  geom_line() +
  geom_point(data = tempEf, aes(x = TRTYEAR, y = r), size = 0.8) +
  labs(title = "Smooth Line Plot with Confidence Intervals",
       subtitle = "Predicted values of r vs. TRTYEAR and Myc")

This code will add data labels to the plot, which show the actual values of r for each value of TRTYEAR.

Conclusion

In this article, we explored how to create smooth lines and confidence intervals for the predicted values of an LMER model using the ggeffects package in R. We covered some common solutions to issues such as jagged lines and incorrect confidence intervals, and provided examples of how to adjust the plot to include additional features.

By following these steps, you should be able to create smooth line plots with confidence intervals for your own LMER models using ggplot2.

References

  • “ggeffects: A package for plotting effects in linear mixed effects models” by Hadley Wickham
  • “ggplot2: Elegant Statistical Graphics for Data Analysis and Visualization” by Hadley Wickham and Garrett Grolemund

Last modified on 2025-03-27