This post finishes the discussion of two other ones (this and this) by providing an example how to carry out causal mediation analysis with a single mediator in R. The “mediation” package is utilised, for a full description of the package’s capabilities, you can refer to Tingley et al. (2014). For STATA users out there, there is a “paramed” package in STATA which should also produce the same results (within rounding error). A note of caution though: the “paramed” package can only be used with linear mediators and outcomes, and with binary logit models with rare outcomes (see: VanderWeele 2016). The dataset for the analysis can be downloaded from the bottom of the page.

The substantive question of the current analysis is, whether procedural justice mediates previous experiences with the police’s effect on normative alignment with the police. As discussed earlier, causal mediation analysis can handle the presence of interactions in the analysis. Thus, two models are fitted, one with, and another without the treatment-mediator interaction:

```
#Causal mediation analysis
na.medifit <- lm(pj ~ treatment + female + age + breath_test + marry + ed_attain + emp1 + emp2 + house1 + house2, data = scotcet_n)
na.outfit <- lm(na ~ pj + treatment + female + age + breath_test + marry + ed_attain + emp1 + emp2 + house1 + house2, data = scotcet_n)
na.medout <- mediate(na.medifit, na.outfit, treat = "treatment", mediator = "pj", sobustSE = TRUE, sims = 1000)
#Causal mediation analysis with interaction
naint.medifit <- lm(pj ~ treatment + female + age + breath_test + marry + ed_attain + emp1 + emp2 + house1 + house2, data = scotcet_n)
naint.outfit <- lm(na ~ pj + treatment + pj*treatment + female + age + breath_test + marry + ed_attain + emp1 + emp2 + house1 + house2, data = scotcet_n)
naint.medout <- mediate(naint.medifit, naint.outfit, treat = "treatment", mediator = "pj", sobustSE = TRUE, sims = 1000)
summary(na.medout)
Causal Mediation Analysis
Quasi-Bayesian Confidence Intervals
Estimate 95% CI Lower 95% CI Upper p-value
ACME -0.2034 -0.4012 -0.01 0.038 *
ADE -0.0189 -0.2527 0.23 0.868
Total Effect -0.2222 -0.5146 0.10 0.164
Prop. Mediated 0.8050 -3.0977 4.39 0.154
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Sample Size Used: 383
Simulations: 1000
summary(naint.medout)
Causal Mediation Analysis with Interaction
Quasi-Bayesian Confidence Intervals
Estimate 95% CI Lower 95% CI Upper p-value
ACME (control) -0.16661 -0.32785 -0.02 0.026 *
ACME (treated) -0.23959 -0.47070 -0.03 0.026 *
ADE (control) 0.03338 -0.21716 0.28 0.782
ADE (treated) -0.03960 -0.28879 0.21 0.774
Total Effect -0.20621 -0.50091 0.11 0.202
Prop. Mediated (control) 0.70492 -4.04414 4.99 0.200
Prop. Mediated (treated) 1.01760 -7.01330 7.03 0.200
ACME (average) -0.20310 -0.40261 -0.02 0.026 *
ADE (average) -0.00311 -0.25637 0.23 1.000
Prop. Mediated (average) 0.86126 -5.62282 5.84 0.200
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Sample Size Used: 383
Simulations: 1000
```

In the above tables, ACME refers to the average causally mediated effect and ADE to the average direct effect. As you can see from the results above, both the total and natural indirect effects are somewhat subdued after including the interaction. Moreover, by including the interaction, you can also gauge how much this moderated effect contributes to the overall effect by comparing the “control” and “treated” conditions (i.e., under the treated condition the interaction’s influence is attributed to that particular direct/indirect effect). In the current case, this moderated effect’s effect size seems to be approximately 0.072, or more than third of the total effect. Hence, considering the interaction seems essential in the current example.

In terms of the substantive findings, it appears that procedural justice fully mediates the impact of previous experiences of the police on normative alignment (the proportion mediated is either 80.5% or 86.1% depending on the chosen model). This implies that there is causal evidence for the main hypothesis of the procedural justice literature, namely that procedural justice is key in channelling the treatment’s effect towards at least one element of police legitimacy.

*References*

*Tingley, Dustin, Teppei Yamamoto, Kentaro Hirose, Luke Keele, and Kosuke Imai. 2014. “Mediation: R Package for Causal Mediation Analysis.” Journal of Statistical Software 59(5):1–38.*

* VanderWeele, Tyler J. 2016. “Mediation Analysis: A Practitioner’s Guide.” Annual Review of Public Health 37(1):17–32.*

**Download the dataset from here:**

scotcet_blog

## One thought on “ScotCET and causal mediation analysis with a single mediator 3. – A demonstration in R”