Mediation Analysis in R: How to Run it with two Packages

In this post, you will learn how to perform mediation analysis in R using two different packages. First, we will use the mediation package to run a mediation analysis. Next, we will look at how to do the same thing using the lavaan package.

For both methods, we will go through the R code step by step and explain what we are doing. The focus of this tutorial is on how to run a mediation analysis in R rather than the theory behind mediation analysis.

mediation in four steps

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 above, we first generated some random data for our independent variable (X), mediator (M), and dependent variable (Y). Next, we fitted two linear models. In the first model, we used X to predict M. In the second model, we used both X and M to predict Y.

We then used the mediate() function to estimate the mediation effects and summary() to examine the results.

In the output, we can see the average causal mediation effect (ACME) and the average direct effect (ADE). The ACME estimates the indirect effect of X on Y through M, whereas the ADE estimates the direct effect of X on Y that does not go through M. We can use these estimates to examine how much of the effect of X on Y is mediated through M.

How to Run Mediation Analysis in R with the lavaan Package

The lavaan package allows for structural equation modeling and is another good choice 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 post, you learned how to perform mediation analysis in R using the mediation and lavaan packages.

First, we used the mediation package to fit regression models and estimate the indirect and direct effects using the mediate() function. We also looked at how to examine the results, including the average causal mediation effect (ACME) and the average direct effect (ADE).

Next, we used lavaan to perform mediation analysis using structural equation modeling (SEM). We looked at how to specify a mediation model and estimate the indirect effect using bootstrapping.

As you have seen, both packages can be used for mediation analysis in R. Which package you choose depends on the type of model you want to estimate and the variables you are working with.

I hope you found this tutorial helpful. If you have any questions or suggestions, please leave a comment below. Also, if you found this post useful, feel free to share it with others who might be interested in mediation analysis using R.

Resources

Explore more data analysis tutorials on my blog, covering R programming, statistics, and modeling techniques:

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top