Mediation

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.4.3
Warning: package 'ggplot2' was built under R version 4.4.3
Warning: package 'tibble' was built under R version 4.4.3
Warning: package 'tidyr' was built under R version 4.4.3
Warning: package 'readr' was built under R version 4.4.3
Warning: package 'purrr' was built under R version 4.4.3
Warning: package 'dplyr' was built under R version 4.4.3
Warning: package 'stringr' was built under R version 4.4.3
Warning: package 'forcats' was built under R version 4.4.3
Warning: package 'lubridate' was built under R version 4.4.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(mediation)
Warning: package 'mediation' was built under R version 4.4.3
Loading required package: MASS
Warning: package 'MASS' was built under R version 4.4.3

Attaching package: 'MASS'

The following object is masked from 'package:dplyr':

    select

Loading required package: Matrix

Attaching package: 'Matrix'

The following objects are masked from 'package:tidyr':

    expand, pack, unpack

Loading required package: mvtnorm
Warning: package 'mvtnorm' was built under R version 4.4.3
Loading required package: sandwich
Warning: package 'sandwich' was built under R version 4.4.3
mediation: Causal Mediation Analysis
Version: 4.5.1
set.seed(2014)
data("framing", package = "mediation")
?framing
starting httpd help server ... done

Mediation is understanding how one variable causes the effecto of x on y.

Outcome model

med.fit <- lm(emo ~ treat + age + educ + gender + income, data = framing)

Mediator model

out.fit <- glm(cong_mesg ~ emo + treat + age + educ + gender + income, data = framing, family = binomial("probit"))
med.out <- mediate(med.fit, out.fit, treat = "treat", mediator = "emo", robustSE = TRUE, sims = 100)

summary(med.out)

Causal Mediation Analysis 

Quasi-Bayesian Confidence Intervals

                          Estimate 95% CI Lower 95% CI Upper p-value    
ACME (control)            0.079320     0.037253     0.128100  <2e-16 ***
ACME (treated)            0.079874     0.036602     0.127369  <2e-16 ***
ADE (control)             0.013880    -0.093249     0.134340    0.80    
ADE (treated)             0.014434    -0.101311     0.141463    0.80    
Total Effect              0.093754    -0.034939     0.215043    0.18    
Prop. Mediated (control)  0.803819    -2.632878    10.365799    0.18    
Prop. Mediated (treated)  0.816671    -2.384619     9.353927    0.18    
ACME (average)            0.079597     0.036949     0.127699  <2e-16 ***
ADE (average)             0.014157    -0.097280     0.137901    0.80    
Prop. Mediated (average)  0.810245    -2.508748     9.849949    0.18    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sample Size Used: 265 


Simulations: 100 

Non-parametric bootstrap

med.out <- mediate(med.fit, out.fit, boot = TRUE, treat = "treat",
mediator = "emo", sims = 100)
Running nonparametric bootstrap
summary(med.out)

Causal Mediation Analysis 

Nonparametric Bootstrap Confidence Intervals with the Percentile Method

                           Estimate 95% CI Lower 95% CI Upper p-value    
ACME (control)            0.0858350    0.0399480    0.1349581  <2e-16 ***
ACME (treated)            0.0869663    0.0412316    0.1331463  <2e-16 ***
ADE (control)             0.0117629   -0.0706944    0.1260725    0.58    
ADE (treated)             0.0128941   -0.0755873    0.1369177    0.58    
Total Effect              0.0987292   -0.0089077    0.2255384    0.06 .  
Prop. Mediated (control)  0.8693988   -0.5788256    3.6491192    0.06 .  
Prop. Mediated (treated)  0.8808573   -0.4397805    3.4876667    0.06 .  
ACME (average)            0.0864007    0.0405898    0.1343244  <2e-16 ***
ADE (average)             0.0123285   -0.0731408    0.1314951    0.58    
Prop. Mediated (average)  0.8751280   -0.5093030    3.5683930    0.06 .  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sample Size Used: 265 


Simulations: 100