Test- discussion

Author

Jingyi Yang

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.4.3
Warning: package 'ggplot2' 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.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── 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

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
Loading required package: sandwich
Warning: package 'sandwich' was built under R version 4.4.3
mediation: Causal Mediation Analysis
Version: 4.5.0
library(haven)
library(stargazer)

Please cite as: 

 Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
 R package version 5.2.3. https://CRAN.R-project.org/package=stargazer 
library(skimr)
library(jtools)
Warning: package 'jtools' was built under R version 4.4.3

Attaching package: 'jtools'

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

    standardize
data<- mediation::framing
complete_data <- data %>%
  filter(if_all(c(emo, treat), ~ !is.na(.))) %>%
  mutate(english= as.numeric(english))

skim(complete_data)
Data summary
Name complete_data
Number of rows 265
Number of columns 15
_______________________
Column type frequency:
factor 4
numeric 11
________________________
Group variables None

Variable type: factor

skim_variable n_missing complete_rate ordered n_unique top_counts
cond 0 1 FALSE 4 1: 68, 2: 67, 3: 67, 4: 63
anx 0 1 FALSE 4 som: 86, a l: 74, ver: 60, not: 45
educ 0 1 FALSE 4 hig: 92, bac: 83, som: 70, les: 20
gender 0 1 FALSE 2 fem: 139, mal: 126, not: 0, ref: 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
age 0 1 47.77 15.97 18 35 47 60 85 ▆▇▇▆▂
income 0 1 10.80 3.92 1 8 11 13 19 ▂▅▆▇▂
emo 0 1 6.97 2.77 3 5 7 9 12 ▇▇▇▅▅
p_harm 0 1 5.89 1.76 2 4 6 8 8 ▂▃▂▅▇
tone 0 1 0.51 0.50 0 0 1 1 1 ▇▁▁▁▇
eth 0 1 0.51 0.50 0 0 1 1 1 ▇▁▁▁▇
treat 0 1 0.26 0.44 0 0 0 1 1 ▇▁▁▁▃
english 0 1 3.42 0.77 1 3 4 4 4 ▁▁▁▅▇
immigr 0 1 3.03 0.97 1 2 3 4 4 ▂▅▁▆▇
anti_info 0 1 0.11 0.31 0 0 0 0 1 ▇▁▁▁▁
cong_mesg 0 1 0.33 0.47 0 0 0 1 1 ▇▁▁▁▃
#Model 0: Predict your DV with IV and Other Controls
model_0 <- lm(immigr~treat, complete_data)

#Model 1: Predict your Mediator with IV and Other Controls
model_m <- lm(emo ~treat, complete_data)
#Model 2: Predict your DV with IV, Mediator and Other Controls
model_y <- lm(immigr~treat+emo, complete_data)
export_summs(model_0, model_m, model_y, digits=3, model.names = c("immigration w/o \ntreat", 
                                                                  "Mediator Model", 
                                                                  "immigration w/ \ntreat"))
immigration w/o
treat
Mediator Modelimmigration w/
treat
(Intercept)2.914 ***6.594 ***1.674 ***
(0.068)   (0.193)   (0.134)   
treat0.439 ** 1.480 ***0.161    
(0.134)   (0.380)   (0.116)   
emo                0.188 ***
                (0.018)   
N265        265        265        
R20.040    0.054    0.315    
*** p < 0.001; ** p < 0.01; * p < 0.05.
#Run the mediation analysis with bootstrapping for CIs
#Assign specific variables to be Mediator and Treatment (Treatment has to be numeric or binary)
results <- mediate(model_m, model_y, treat='treat', mediator= 'emo',
                   boot=TRUE, sims=100)
Running nonparametric bootstrap
#Has to have exact same number of cases in each model
complete_data_1 <- data %>%
  filter(if_all(c(treat, emo), 
                ~ !is.na(.)))

#Model 1: Predict your Mediator with IV and Other Controls
model_m_m <- lm(emo ~ treat, complete_data_1)
#Model 2: Predict your DV with IV, Mediator and Other Controls
model_y_y <- lm(immigr~treat+emo, complete_data_1)
export_summs(model_0, model_m_m, model_y_y, digits=3, model.names = c("Immigration w/o \nTreat", 
                                                                  "Mediator Model", 
                                                                  "Immigration w/ \nTreat"))
Immigration w/o
Treat
Mediator ModelImmigration w/
Treat
(Intercept)2.914 ***6.594 ***1.674 ***
(0.068)   (0.193)   (0.134)   
treat0.439 ** 1.480 ***0.161    
(0.134)   (0.380)   (0.116)   
emo                0.188 ***
                (0.018)   
N265        265        265        
R20.040    0.054    0.315    
*** p < 0.001; ** p < 0.01; * p < 0.05.
#Run the mediation analysis with bootstrapping for CIs
results_1 <- mediate(model_m_m, model_y_y, treat='treat', mediator='emo',
                   boot=TRUE, sims=100)
Running nonparametric bootstrap
summary(results_1)

Causal Mediation Analysis 

Nonparametric Bootstrap Confidence Intervals with the Percentile Method

               Estimate 95% CI Lower 95% CI Upper p-value    
ACME             0.2781       0.1605         0.43  <2e-16 ***
ADE              0.1611      -0.0572         0.35    0.16    
Total Effect     0.4392       0.2052         0.64  <2e-16 ***
Prop. Mediated   0.6332       0.3968         1.22  <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sample Size Used: 265 


Simulations: 100 
plot(results_1)

#Model 0: Predict your DV with IV and Other Controls
model_0_1 <- lm(english ~ treat, complete_data)
#Model 1: Predict your Mediator with IV and Other Controls
model_m_1 <- lm(emo ~ treat, complete_data)
#Model 2: Predict your DV with IV, Mediator and Other Controls
model_y_1 <- lm(english ~ treat+emo, complete_data)
export_summs(model_0_1, model_m_1, model_y_1, digits=3, model.names = c("english w/o \ntreat", 
                                                                  "Mediator Model", 
                                                                  "english w/ \ntreat"))
english w/o
treat
Mediator Modelenglish w/
treat
(Intercept)3.396 ***6.594 ***2.836 ***
(0.055)   (0.193)   (0.123)   
treat0.104    1.480 ***-0.022    
(0.108)   (0.380)   (0.107)   
emo                0.085 ***
                (0.017)   
N265        265        265        
R20.003    0.054    0.092    
*** p < 0.001; ** p < 0.01; * p < 0.05.
#Run the mediation analysis with bootstrapping for CIs
#Assign specific variables to be Mediator and Treatment (Treatment has to be numeric or binary)
results <- mediate(model_m_1, model_y_1, treat='treat', mediator= 'emo',
                   boot=TRUE, sims=100)
Running nonparametric bootstrap
#Has to have exact same number of cases in each model
complete_data_2 <- data %>%
  filter(if_all(c(treat, emo), 
                ~ !is.na(.)))%>%
   mutate(english= as.numeric(english))

#Model 1: Predict your Mediator with IV and Other Controls
model_m_m_1 <- lm(emo ~ treat, complete_data_2)
#Model 2: Predict your DV with IV, Mediator and Other Controls
model_y_y_1 <- lm(english ~ treat+emo, complete_data_2)
export_summs(model_0_1, model_m_m_1, model_y_y_1, digits=3, model.names = c("Immigration w/o \nTreat", 
                                                                  "Mediator Model", 
                                                                  "Immigration w/ \nTreat"))
Immigration w/o
Treat
Mediator ModelImmigration w/
Treat
(Intercept)3.396 ***6.594 ***2.836 ***
(0.055)   (0.193)   (0.123)   
treat0.104    1.480 ***-0.022    
(0.108)   (0.380)   (0.107)   
emo                0.085 ***
                (0.017)   
N265        265        265        
R20.003    0.054    0.092    
*** p < 0.001; ** p < 0.01; * p < 0.05.
#Run the mediation analysis with bootstrapping for CIs
results_2 <- mediate(model_m_m_1, model_y_y_1, treat='treat', mediator='emo',
                   boot=TRUE, sims=100)
Running nonparametric bootstrap
summary(results_2)

Causal Mediation Analysis 

Nonparametric Bootstrap Confidence Intervals with the Percentile Method

               Estimate 95% CI Lower 95% CI Upper p-value    
ACME             0.1256       0.0530         0.22  <2e-16 ***
ADE             -0.0215      -0.1934         0.18    0.64    
Total Effect     0.1041      -0.0922         0.28    0.22    
Prop. Mediated   1.2071      -5.9869        34.23    0.22    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sample Size Used: 265 


Simulations: 100 
plot(results_2)