Load Library
library(readr)
library(stats)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(broom)
library(ggplot2)
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
stress_df<- read_csv("stress_withdraw.csv")
## Rows: 262 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (7): tenure, stress, depress, withdraw, sex, age, efficacy
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# X = stress
#y = withdraw
# z =depress
library("mediation")
## Loading required package: Matrix
## Loading required package: mvtnorm
## Loading required package: sandwich
## mediation: Causal Mediation Analysis
## Version: 4.5.0
# X = stress
#y = withdraw
# z =depress
med.simple <- lm(depress~stress, data = stress_df) # M on X
out.simple <- lm(withdraw~stress + depress, data = stress_df) # Y on X and M
summary(med.simple)
##
## Call:
## lm(formula = depress ~ stress, data = stress_df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.0095 -0.4195 -0.1609 0.2498 4.0278
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.79936 0.14331 5.578 6.11e-08 ***
## stress 0.17288 0.02965 5.831 1.63e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.6819 on 260 degrees of freedom
## Multiple R-squared: 0.1156, Adjusted R-squared: 0.1122
## F-statistic: 34 on 1 and 260 DF, p-value: 1.63e-08
summary(out.simple)
##
## Call:
## lm(formula = withdraw ~ stress + depress, data = stress_df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.1716 -0.9472 -0.2249 0.8490 2.9049
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.44706 0.25201 5.742 2.61e-08 ***
## stress -0.07685 0.05239 -1.467 0.144
## depress 0.76913 0.10306 7.463 1.29e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.133 on 259 degrees of freedom
## Multiple R-squared: 0.1804, Adjusted R-squared: 0.174
## F-statistic: 28.49 on 2 and 259 DF, p-value: 6.528e-12
med.out.simple <- mediate(med.simple, out.simple, treat = 'stress', mediator = 'depress', boot = T, sims = 1000, boot.ci.type = 'bca')
## Running nonparametric bootstrap
summary(med.out.simple)
##
## Causal Mediation Analysis
##
## Nonparametric Bootstrap Confidence Intervals with the BCa Method
##
## Estimate 95% CI Lower 95% CI Upper p-value
## ACME 0.1330 0.0678 0.20 <2e-16 ***
## ADE -0.0768 -0.1817 0.03 0.17
## Total Effect 0.0561 -0.0750 0.17 0.36
## Prop. Mediated 2.3694 1.4627 1749.52 0.36
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Sample Size Used: 262
##
##
## Simulations: 1000
#summary.mediate(med.out.simple)
plot(med.out.simple)
