Mediation analysis

Reference

Four steps:

df=iris
set.seed(12334)
df$random1=runif(nrow(df),min=min(df$Sepal.Length),max=max(df$Sepal.Length))
df$mediator=df$Sepal.Length*0.35+df$random1*0.65

df$random2=runif(nrow(df),min=min(df$mediator),max=max(df$mediator))
df$dv=df$mediator*0.35+df$random2*0.65

y~x

fit.totaleffect=lm(dv~Sepal.Length,df)
summary(fit.totaleffect)
## 
## Call:
## lm(formula = dv ~ Sepal.Length, data = df)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.15930 -0.45815 -0.01242  0.44662  1.20905 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   5.29106    0.32791  16.136   <2e-16 ***
## Sepal.Length  0.12984    0.05557   2.337   0.0208 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5616 on 148 degrees of freedom
## Multiple R-squared:  0.03558,    Adjusted R-squared:  0.02907 
## F-statistic:  5.46 on 1 and 148 DF,  p-value: 0.02079

m~x

fit.mediator=lm(mediator~Sepal.Length,df)
summary(fit.mediator)
## 
## Call:
## lm(formula = mediator ~ Sepal.Length, data = df)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.2494 -0.5082  0.0123  0.5483  1.0799 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   4.32300    0.37901  11.406  < 2e-16 ***
## Sepal.Length  0.30429    0.06422   4.738 5.02e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6492 on 148 degrees of freedom
## Multiple R-squared:  0.1317, Adjusted R-squared:  0.1258 
## F-statistic: 22.45 on 1 and 148 DF,  p-value: 5.019e-06

y~x+m

fit.dv=lm(dv~Sepal.Length+mediator,df)
summary(fit.dv)
## 
## Call:
## lm(formula = dv ~ Sepal.Length + mediator, data = df)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.90734 -0.46316  0.00764  0.39751  0.87026 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   3.68314    0.40721   9.045 8.17e-16 ***
## Sepal.Length  0.01667    0.05402   0.309    0.758    
## mediator      0.37194    0.06443   5.773 4.46e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5088 on 147 degrees of freedom
## Multiple R-squared:  0.2138, Adjusted R-squared:  0.2031 
## F-statistic: 19.99 on 2 and 147 DF,  p-value: 2.092e-08

In such cases, the total effect of the IV on the DV is explained by the mediator.

# installed the mediation package yet.
library(mediation)
## Loading required package: MASS
## Loading required package: Matrix
## Loading required package: mvtnorm
## Loading required package: sandwich
## mediation: Causal Mediation Analysis
## Version: 4.5.0

m~x

y~x+m

results = mediate(fit.mediator, fit.dv, treat='Sepal.Length', mediator='mediator', boot=T)
## Running nonparametric bootstrap

The mediation package asks for the models we just estimated and then requires us to specify the variable that is the treatment, i.e., our independent variable, and the variable that is the mediator.

summary(results)
## 
## Causal Mediation Analysis 
## 
## Nonparametric Bootstrap Confidence Intervals with the Percentile Method
## 
##                Estimate 95% CI Lower 95% CI Upper p-value    
## ACME             0.1132       0.0575         0.17  <2e-16 ***
## ADE              0.0167      -0.0904         0.12   0.752    
## Total Effect     0.1298       0.0152         0.24   0.018 *  
## Prop. Mediated   0.8716       0.3796         3.68   0.018 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Sample Size Used: 150 
## 
## 
## Simulations: 1000

`ACME stands for average causal mediation (indirect) effects. This effect to be .0905 — this is exactly .39 (the effect of the IV on the mediator from step #2) times .23 (the mediator’s effect on the DV from step #3).

ADE stands for average direct effects. We have calculated this effect in step #3: the direct effect of the IV on the DV when controlling for the mediator. 0.03.

Total Effect stands for the total effect (direct + indirect) of the IV onto the DV. We calculated this in step #1. We can also get it by simply adding the ACME (.0905) and the ADE (.03) to receive the total effect of .1197.

Prop. Mediated describes the proportion of the effect of the IV on the DV that goes through the mediator. It’s calculated by dividing the ACME (.0905) by the total effect (.1197) to receive .75. `

The indirect effect was (.23)*(.39) = .0905. The bootstrapped unstandardized indirect effect was .0905, and the 95% confidence interval ranged from 0.0280 to 0.17. Thus, the indirect effect was statistically significant (p<.001).