In this blog post, you will learn how to conduct mediation analysis in R. This post focuses on the practical aspects of conducting mediation analysis using R. First, we will look at how to do it using the mediation package. Then, we will walk through an example of using the lavaan package to perform mediation analysis. Both mediation examples will include R code chunks with explanations. This blog post focuses not on the theoretical explanations of mediation analysis but on how to run it using R.
Table of Contents
- How to Install the Required R Packages
- How to Do Mediation Analysis in R Using the mediation Package
- How to Run Mediation Analysis in R with the lavaan Package
- Prerequisites
- Testing Different Cognitive Functions Relation to Speech Recognition in Noise Using Mediation Analysis
- Comparing mediation and lavaan for Mediation in R
- Conclusion
- Resources
How to Install the Required R Packages
To use the methods demonstrated, you need to install the mediation
and lavaan
packages. Run the following commands to install R packages:
install.packages("mediation")
install.packages("lavaan")
Code language: JavaScript (javascript)
If you’re using an older version of R, make sure to update R first to avoid compatibility issues.
Now that you have the required packages installed let us explore how to perform mediation analysis using them.
How to Do Mediation Analysis in R Using the mediation Package
Here is how to run mediation analysis in R:
# Load required packages
library(mediation)
# Fit models for mediation analysis
fitM <- lm(M ~ X, data = data) # Model for mediator
fitY <- lm(Y ~ M + X, data = data) # Model for outcome
# Mediation analysis
med <- mediate(fitM, fitY, treat = "X", mediator = "M", boot = TRUE, sims = 1000)
# Summarize results
summary(med)
Code language: PHP (php)
In the code chunk above, we first generated random data for our independent variable (X
), mediator (M
), and dependent variable (Y
). Then, we fit two linear models—one for M
as a function of X
and another for Y
as a function of both M
and X
. Finally, we used the mediate()
function to estimate mediation effects and summarized the results.
The summary will display key statistics, including the average causal mediation effect (ACME) and the average direct effect (ADE). Both help you interpret how much of the effect of X
on Y
is mediated through your mediator, M
.
How to Run Mediation Analysis in R with the lavaan
Package
The lavaan
package allows for structural equation modeling and is another goodchoice for mediation in R. Below is an example of running a mediation analysis using R:
# Specify mediation model
model <- '
M ~ a*X
Y ~ b*M + c*X
indirect := a*b
total := c + (a*b)
'
# Fit model
fit <- sem(model, data = data)
# Summarize results
summary(fit, standardized = TRUE, se = "boot", bootstrap = 1000)
Code language: PHP (php)
In the code chunk above, we defined a mediation model with explicit paths. The mediator (M
) depends on the independent variable (X
), with the path coefficient named a
. The dependent variable (Y
) depends on both the mediator (M
, path b
) and the independent variable (X
, path c
). Additionally, we specified indirect and total effects (indirect := a*b
and total := c + (a*b)
). The sem()
function, with bootstrap standard errors, estimates the model parameters. Finally, we summarized the results with summary()
to see both the parameter estimates and standardized coefficients.
One advantage of the lavaan
package is its flexibility in modeling latent variables in mediation analysis (i.e., within a structural equation model). This makes it very useful for more complex datasets.
Prerequisites
Before performing mediation analysis, you should, of course, understand its theoretical framework and assumptions. Mediation assumes a causal relationship where an independent variable (X) influences a dependent variable (Y) both directly and indirectly through a mediator variable (M).
Testing Different Cognitive Functions Relation to Speech Recognition in Noise Using Mediation Analysis
For example, in a recent study, my colleagues and I examined the relationship between working memory capacity, fluid intelligence, and speech recognition in noise among older adults with normal hearing and those using hearing aids. Using data from the n200 study, we tested whether fluid intelligence mediated the relationship between working memory capacity and performance on the Hagerman speech recognition in noise test. We carried out the analysis in R using the lavaan
package.
Comparing mediation
and lavaan
for Mediation in R
The mediation and lavaan
packages are good tools for mediation analysis in R, but they serve different needs. The mediation package is ideal for straightforward analyses involving linear models. It provides clear outputs, including the average causal mediation effect and average direct effect.
On the other hand, lavaan
offers more flexibility by supporting structural equation modeling (SEM). This allows users to specify and test complex models that include multiple mediators, latent variables, or more intricate causal pathways. For example, you can use lavaan
to combine mediation analysis with factor analysis or to analyze hierarchical structures, making it especially useful for advanced research designs.
In summary, use the mediation
package if your goal is simplicity and speed. If you need to model complex relationships or include latent variables in your analysis, lavaan
is your go-to package for mediation in R.
Conclusion
In this blog post, we have learned how to perform mediation analysis using the mediation
and lavaan
packages. First, we explored a step-by-step guide to analyze mediation effects, including fitting regression models and using mediate()
. Then, we demonstrated the flexibility of lavaan
for structural equation modeling, highlighting its ability to handle latent variables and bootstrap estimates. I would love to hear your thoughts—share this post with others and leave your comments or questions below to continue the discussion!
Resources
Explore more data analysis tutorials on my blog, covering R programming, statistics, and modeling techniques:
- Probit Regression in R: Interpretation & Examples
- Random Intercept Model in R: Interpretation and Visualization
- Correlation in R: Coefficients, Visualizations, & Matrix Analysis
- Mann Whitney U Test in R: A Comprehensive Guide