#R Mediation & Moderation
library(mediation) #Mediation package
## Loading required package: MASS
## Loading required package: Matrix
## Loading required package: mvtnorm
## Loading required package: sandwich
## mediation: Causal Mediation Analysis
## Version: 4.5.0
library(rockchalk) #Graphing simple slopes; moderation
##
## Attaching package: 'rockchalk'
## The following object is masked from 'package:MASS':
##
## mvrnorm
library(multilevel) #Sobel Test
## Loading required package: nlme
library(bda) #Another Sobel Test option
## Loading required package: boot
## bda v15 (Bin Wang, 2020)
library(gvlma) #Testing Model Assumptions
library(stargazer) #Handy regression tables
##
## Please cite as:
## Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
#Useful Help
?lm
## starting httpd help server ...
## done
?mediation
library(QuantPsyc)
##
## Attaching package: 'QuantPsyc'
## The following object is masked from 'package:rockchalk':
##
## meanCenter
## The following object is masked from 'package:Matrix':
##
## norm
## The following object is masked from 'package:base':
##
## norm
library(pequod)
## Loading required package: ggplot2
## Loading required package: car
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:boot':
##
## logit
library (MASS)
library(Matrix)
library(mvtnorm)
library(sandwich)
myData <- read.csv('http://static.lib.virginia.edu/statlab/materials/data/mediationData.csv')
head(myData)
## X M Y
## 1 6 5 6
## 2 7 5 5
## 3 7 7 4
## 4 8 4 8
## 5 4 3 5
## 6 4 4 7
model.0 <- lm(Y ~ X, myData)
summary(model.0)
##
## Call:
## lm(formula = Y ~ X, data = myData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -5.0262 -1.2340 -0.3282 1.5583 5.1622
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.8572 0.6932 4.122 7.88e-05 ***
## X 0.3961 0.1112 3.564 0.000567 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.929 on 98 degrees of freedom
## Multiple R-squared: 0.1147, Adjusted R-squared: 0.1057
## F-statistic: 12.7 on 1 and 98 DF, p-value: 0.0005671
model.M <- lm(M ~ X, myData)
summary(model.M)
##
## Call:
## lm(formula = M ~ X, data = myData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.3046 -0.8656 0.1344 1.1344 4.6954
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.49952 0.58920 2.545 0.0125 *
## X 0.56102 0.09448 5.938 4.39e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.639 on 98 degrees of freedom
## Multiple R-squared: 0.2646, Adjusted R-squared: 0.2571
## F-statistic: 35.26 on 1 and 98 DF, p-value: 4.391e-08
model.Y <- lm(Y ~ X + M, myData)
summary(model.Y)
##
## Call:
## lm(formula = Y ~ X + M, data = myData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.7631 -1.2393 0.0308 1.0832 4.0055
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.9043 0.6055 3.145 0.0022 **
## X 0.0396 0.1096 0.361 0.7187
## M 0.6355 0.1005 6.321 7.92e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.631 on 97 degrees of freedom
## Multiple R-squared: 0.373, Adjusted R-squared: 0.3601
## F-statistic: 28.85 on 2 and 97 DF, p-value: 1.471e-10
library(mediation)
results <- mediate(model.M, model.Y, treat='X', mediator='M',
boot=TRUE, sims=500)
## Running nonparametric bootstrap
model.M <- lm(M ~ X, myData)
model.Y <- lm(Y ~ X + M, myData)
results <- mediate(model.M, model.Y, treat='X', mediator='M',
boot=TRUE, sims=100)
## Running nonparametric bootstrap
## Running nonparametric bootstrap
summary(results)
##
## Causal Mediation Analysis
##
## Nonparametric Bootstrap Confidence Intervals with the Percentile Method
##
## Estimate 95% CI Lower 95% CI Upper p-value
## ACME 0.3565 0.2333 0.52 <2e-16 ***
## ADE 0.0396 -0.1589 0.26 0.74
## Total Effect 0.3961 0.1853 0.63 <2e-16 ***
## Prop. Mediated 0.9000 0.5211 1.79 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Sample Size Used: 100
##
##
## Simulations: 100