IADR 2026 Workshop- Causal Mediation Analysis

Author

Sharon Tan

Published

March 20, 2026

Having trouble viewing? Click here to open or download the PDF directly.

Loading required package: MASS
Loading required package: Matrix
Loading required package: mvtnorm
Loading required package: sandwich
mediation: Causal Mediation Analysis
Version: 4.5.1
# Load it for use in the current R session

set.seed(2014)

# Continuous outcome (dmft)
med.fit <- lm(sugar_consumption ~ income_high + age_adult + gender + district_type, data = df)

out.fit <- lm(dmft ~ sugar_consumption + income_high + age_adult + gender + district_type, data = df)

med.out <- mediate(med.fit, out.fit, treat = "income_high", mediator = "sugar_consumption", robustSE = TRUE, sims = 1000)
summary(med.out)

Causal Mediation Analysis 

Quasi-Bayesian Confidence Intervals

               Estimate 95% CI Lower 95% CI Upper   p-value    
ACME           -3.37749     -3.61150     -3.11365 < 2.2e-16 ***
ADE            -1.84406     -2.16181     -1.55755 < 2.2e-16 ***
Total Effect   -5.22155     -5.44421     -4.99514 < 2.2e-16 ***
Prop. Mediated  0.64696      0.59504      0.69614 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sample Size Used: 5000 


Simulations: 1000 
# Sensitivity analysis for sequential ignorability
sens.out <- medsens(med.out, rho.by = 0.1, effect.type = "indirect", sims = 100)
summary(sens.out)

Mediation Sensitivity Analysis for Average Causal Mediation Effect

Sensitivity Region

     Rho   ACME 95% CI Lower 95% CI Upper R^2_M*R^2_Y* R^2_M~R^2_Y~
[1,] 0.4 0.0941      -0.1249       0.3132         0.16       0.0077

Rho at which ACME = 0: 0.4
R^2_M*R^2_Y* at which ACME = 0: 0.16
R^2_M~R^2_Y~ at which ACME = 0: 0.0077 
# Treatment and mediator interaction, with continuous outcome (dmft)
out.fit.interaction <- lm(dmft ~ sugar_consumption*income_high + age_adult + gender + district_type, data = df)

med.out.interaction <- mediate(med.fit, out.fit.interaction, treat = "income_high", mediator = "sugar_consumption", robustSE = TRUE, sims = 1000)
summary(med.out.interaction)

Causal Mediation Analysis 

Quasi-Bayesian Confidence Intervals

                         Estimate 95% CI Lower 95% CI Upper   p-value    
ACME (control)           -3.65938     -3.93553     -3.39060 < 2.2e-16 ***
ACME (treated)           -2.58540     -2.92441     -2.24118 < 2.2e-16 ***
ADE (control)            -2.78822     -3.22955     -2.37516 < 2.2e-16 ***
ADE (treated)            -1.71424     -2.01500     -1.41256 < 2.2e-16 ***
Total Effect             -5.37362     -5.60727     -5.14572 < 2.2e-16 ***
Prop. Mediated (control)  0.68127      0.63064      0.73315 < 2.2e-16 ***
Prop. Mediated (treated)  0.48179      0.41170      0.54804 < 2.2e-16 ***
ACME (average)           -3.12239     -3.37910     -2.87805 < 2.2e-16 ***
ADE (average)            -2.25123     -2.55946     -1.93450 < 2.2e-16 ***
Prop. Mediated (average)  0.58153      0.53052      0.63130 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sample Size Used: 5000 


Simulations: 1000 
test.TMint(med.out.interaction, conf.level= .95)

    Test of ACME(1) - ACME(0) = 0

data:  estimates from med.out.interaction
ACME(1) - ACME(0) = 1.074, p-value < 2.2e-16
alternative hypothesis: true ACME(1) - ACME(0) is not equal to 0
95 percent confidence interval:
 0.7121261 1.4302863
# Binary outcome (caries yes/no)
out.fit.logit <- glm(caries ~ sugar_consumption + income_high + age_adult + gender + district_type, data = df, family = binomial("logit"))

med.out.logit <- mediate(med.fit, out.fit.logit, treat = "income_high", mediator = "sugar_consumption", robustSE = TRUE, sims = 1000)
summary(med.out.logit)

Causal Mediation Analysis 

Quasi-Bayesian Confidence Intervals

                          Estimate 95% CI Lower 95% CI Upper   p-value    
ACME (control)           -0.029403    -0.049191    -0.014718 < 2.2e-16 ***
ACME (treated)           -0.083998    -0.095483    -0.073124 < 2.2e-16 ***
ADE (control)            -0.023193    -0.033070    -0.014092 < 2.2e-16 ***
ADE (treated)            -0.077788    -0.097568    -0.051506 < 2.2e-16 ***
Total Effect             -0.107191    -0.118599    -0.096681 < 2.2e-16 ***
Prop. Mediated (control)  0.263742     0.134004     0.485462 < 2.2e-16 ***
Prop. Mediated (treated)  0.785568     0.698468     0.865219 < 2.2e-16 ***
ACME (average)           -0.056701    -0.070875    -0.045511 < 2.2e-16 ***
ADE (average)            -0.050490    -0.064940    -0.033053 < 2.2e-16 ***
Prop. Mediated (average)  0.524655     0.419851     0.672286 < 2.2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sample Size Used: 5000 


Simulations: 1000