Mediation, moderation, and mediated moderation analysis in R

infoart.ca
5 min readMay 4, 2023

--

Mediation, moderation and mediated moderation analysis are important tools in psychology/behavioural science that allow us to explore the complex relationships among various psychological factors.

Mediation analysis is used to examine whether the relationship between an independent variable and a dependent variable is mediated by one/more intervening variables, also called mediators. For example, we may want to understand the mediating role of self-esteem on the relationship between physical exercise and depression. Here our dependent variable is depression, independent variable is physical exercise and mediating variable is self-esteem.

There are 3 paths in the simplest mediation model:

c = path denoting the regression coefficient of the direct effect of the predictor on the outcome

a = path denoting the regression coefficient of the direct effect of the predictor on the mediator

b = path denoting the regression coefficient of the direct effect of the mediator on the outcome

The product of a and b is called the indirect effect of the predictor on the outcome, because the effect of the predictor variable on the outcome variable is not direct, but rather it is mediated (if at all e.g. p<.05) by the mediator variable.

Moderation analysis examines whether the relationship between an independent and a dependent variable depends on the level of another variable, called a moderator. Thus, moderation analysis helps to understand when or for whom the independent variable has a stronger or weaker or any effect at all on a dependent variable. For example, we may want to understand the moderating role of sex on the relationship between physical exercise and depression i.e. whether the association is true/stronger/weaker for all types of sex. Here our dependent variable is depression, independent variable is physical exercise and moderating variable is sex.

Mediated moderation analysis is a mixture of the two which is used to examine whether the indirect effect of an independent variable on a dependent variable through a mediator is influenced by a moderator variable. In other words, moderated mediation analysis helps to understand when and for whom the relationship between an independent variable and a dependent variable is mediated by the mediator.

Enough talking, now let’s see the analyses in action.

We are going to use the moderndivepackage for moderation, and the lavaan package for mediation analysis.

Moderation analysis

In this example analysis using mtcars data our outcome variable is “mpg” and the moderator is “cyl”, while “wt” is the predictor variable.


library(moderndive)

#the regression model
model <- lm(mpg ~ wt * cyl, data = mtcars)

# the moderation model
model <- lm(mpg ~ wt * cyl, data = mtcars)

# Print the model summary
> summary(model)

Call:
lm(formula = mpg ~ wt * cyl, data = mtcars)

Residuals:
Min 1Q Median 3Q Max
-4.2288 -1.3495 -0.5042 1.4647 5.2344

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 54.3068 6.1275 8.863 1.29e-09 ***
wt -8.6556 2.3201 -3.731 0.000861 ***
cyl -3.8032 1.0050 -3.784 0.000747 ***
wt:cyl 0.8084 0.3273 2.470 0.019882 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.368 on 28 degrees of freedom
Multiple R-squared: 0.8606, Adjusted R-squared: 0.8457
F-statistic: 57.62 on 3 and 28 DF, p-value: 4.231e-12

The coefficients table shows that “wt” has a significant negative effect on “mpg” (Estimate = -8.6556, p-value = 0.000861). “cyl” also has a significant negative effect on “mpg” (Estimate = -3.8032, p-value = 0.000747).

Moreover, the interaction term “wt:cyl” has a significant positive effect on “mpg” (Estimate = 0.8084, p-value = 0.019882). This means that the relationship between “wt” and “mpg” depends on the value of “cyl”. In other words, the effect of “wt” on “mpg” is stronger or weaker depending on the value of “cyl”.

The adjusted R-squared of the model is 0.8457, which means that the model explains 84.57% of the variance in “mpg”. The F-statistic is 57.62, with a p-value of 4.231e-12, indicating that the overall model is significant.

Mediation analysis

# Load the required package
library(lavaan)

# Fit the mediation model
model <- 'disp ~ a * wt
mpg ~ b * wt + c * disp
indirect := a * b
total := c + indirect'

fit <- sem(model, data = mtcars)

# Get the results
summary(fit)

Results

lavaan 0.6-12 ended normally after 1 iterations

Estimator ML
Optimization method NLMINB
Number of model parameters 5

Number of observations 32

Model Test User Model:

Test statistic 0.000
Degrees of freedom 0

Parameter Estimates:

Standard errors Standard
Information Expected
Information saturated (h1) model Structured

Regressions:
Estimate Std.Err z-value P(>|z|)
disp ~
wt (a) 112.478 10.298 10.923 0.000
mpg ~
wt (b) -3.351 1.108 -3.024 0.002
disp (c) -0.018 0.009 -2.026 0.043

Variances:
Estimate Std.Err z-value P(>|z|)
.disp 3147.160 786.790 4.000 0.000
.mpg 7.709 1.927 4.000 0.000

Defined Parameters:
Estimate Std.Err z-value P(>|z|)
indirect -376.895 129.338 -2.914 0.004
total -376.912 129.330 -2.914 0.004
  • The regression results show the estimated coefficients for each path. The coefficient for the path between wt and disp (labeled “a” in parentheses) is 112.478, with a standard error of 10.298. This indicates that for each one-unit increase in wt, disp increases by 112.478 units on average. The coefficient for the path between wt and mpg (labeled “b” in parentheses) is -3.351, with a standard error of 1.108. This indicates that for each one-unit increase in wt, mpg decreases by 3.351 units on average. The coefficient for the path between disp and mpg (labeled “c” in parentheses) is -0.018, with a standard error of 0.009. This indicates that for each one-unit increase in disp, mpg decreases by 0.018 units on average, after accounting for the effect of wt.
  • The indirect effect is estimated to be -376.895, with a standard error of 129.338. This represents the effect of wt on mpg that is mediated by disp. It indicates that for each one-unit increase in wt, mpg decreases by 376.895 units on average, through its effect on disp.
  • The total effect is estimated to be -376.912, with a standard error of 129.330. This represents the total effect of wt on mpg, including both the direct effect (not mediated by disp) and the indirect effect (mediated by disp).

Finally, let’s also calculate the percent mediation:

percent_mediation <- (indirect_effect/total_effect) * 100
percent_mediation

So, substituting these values in the formula, we get:

% mediation = (-376.895 / -376.912) * 100% % mediation = 99.995%

Therefore, the percent mediation is 99.995%, which means almost all the effect of wt (weight of the car) on mpg (miles per gallon) is mediated by disp.

Happy meditating!!!

--

--

infoart.ca
infoart.ca

Written by infoart.ca

Center for Social Capital & Environmental Research | Posts by Bishwajit Ghose, BI consultant and lecturer at the University of Ottawa

Responses (2)