This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.
plot(cars)
rm(list = ls())
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Cmd+Option+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).
Instructions – Part 3 Create a dataset based on the observed coefficient estimates from the results on page 13 for the Passion → Locomotion → Grit portion of the model. Set your number of observations equal to 204, which is the same as that reported in the paper, and use set.seed(080203) for your simulation.
Then conduct a sensitivity analysis using the mediation package using this data, determining the estimated value for rho where the indirect effect would be zero. Based on this analysis, what do you conclude about the threat of omitted variable bias in the paper?
Mean S.D. 1 2 3
1.Passion 32.12 10.77 1 .41 .26 2.Locomotion 5.01 .57 .41 1 .43 3.Grit 3.77 3.77 .26 .43 1
N = 204 Seed 080203
Creating the correlation matrix
cov.matrix <- matrix(c(1,.41,.26,.41,1,.43,.26,.43,1),
nrow = 3, ncol = 3,
dimnames = list(c("Passion","Locomotion","Grit")))
cov.matrix
[,1] [,2] [,3]
Passion 1.00 0.41 0.26
Locomotion 0.41 1.00 0.43
Grit 0.26 0.43 1.00
The above is the covariance matrix intended
library(MASS)
set.seed(080203)
my.ds = mvrnorm(n=204,
mu = c(32.12, 5.01,3.77),
Sigma = cov.matrix,
tol = .1,
empirical = TRUE)
my.df <- as.data.frame(my.ds)
myCov.matrix <- cov(my.df, method = c("pearson"))
round(myCov.matrix,2)
Passion Locomotion Grit
Passion 1.00 0.41 0.26
Locomotion 0.41 1.00 0.43
Grit 0.26 0.43 1.00
The intended dataset has been obtained.
Performing the mediation analysis:
library(lavaan)
mediation.model <- 'Locomotion ~ a*Passion
Grit ~ b*Locomotion
ab: = a*b'
mediation.model.fit<- sem(mediation.model, data= my.df)
parameterestimates(mediation.model.fit)
lhs op rhs label est se z pvalue ci.lower ci.upper
1 Locomotion ~ Passion a 0.410 0.064 6.420 0 0.285 0.535
2 Grit ~ Locomotion b 0.430 0.063 6.803 0 0.306 0.554
3 Locomotion ~~ Locomotion 0.828 0.082 10.100 0 0.667 0.988
4 Grit ~~ Grit 0.811 0.080 10.100 0 0.654 0.969
5 Passion ~~ Passion 0.995 0.000 NA NA 0.995 0.995
6 ab := a*b ab 0.176 0.038 4.669 0 0.102 0.250
fitmeasures(mediation.model.fit, c("chisq", "df", "pvalue"))
chisq df pvalue
2.119 1.000 0.146
The chisquare shows that the model estimated is not free from alternative explainations.
library(mediation)
# We first set up our two regression equations
mediator.model <- lm(Locomotion ~ Passion, data = my.df)
outcome.model <- lm(Grit ~ Passion + Locomotion, data = my.df)
# Now we specify the full model using the 'mediate' function
mediation.si.fit <- mediate(mediator.model, outcome.model, treat = "Passion",
mediator = "Locomotion", robustSE = TRUE, sims = 1000)
summary(mediation.si.fit)
Causal Mediation Analysis
Quasi-Bayesian Confidence Intervals
Estimate 95% CI Lower 95% CI Upper p-value
ACME 0.1593 0.0969 0.2348 0.00
ADE 0.0969 -0.0396 0.2306 0.15
Total Effect 0.2561 0.1166 0.3877 0.00
Prop. Mediated 0.6250 0.3392 1.3087 0.00
Sample Size Used: 204
Simulations: 1000
Calculation of Rho:
sensitivity.fit <- medsens(mediation.si.fit, rho.by = 0.1,
effect.type = "indirect", sims = 1000)
summary(sensitivity.fit)
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.3 0.0324 -0.0165 0.0813 0.09 0.0604
[2,] 0.4 -0.0168 -0.0651 0.0315 0.16 0.1074
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.1074
The above table shows the value of ρ at which the confidence invervals contains zero for the Average Causal Mediaiton Effect. In the above analysis, we find that the confidence intervals for the ACME contains zero when ρ is between 0.3 and 0.4.
plot(sensitivity.fit, sens.par = "rho", main = "Mediator")