mediation
, multilevel
, rockchalk
c("mediation", "multilevel", "rockchalk")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
set.seed(123)
100
N <- rnorm(N, 175, 7)
X1 <- abs(rnorm(N, 60, 30))
X2 <- rnorm(N, 30, 8)
M <- 0.5*X1 - 0.3*M - 0.4*X2 + 10 + rnorm(N, 0, 3) Y <-
c(scale(X1, center=TRUE, scale=FALSE))
X1c <- c(scale(M, center=TRUE, scale=FALSE))
Mc <- lm(Y ~ X1c + Mc + X1c:Mc)
fitMod <-coef(summary(fitMod))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 65.4617132 1.19492420 54.783151 3.154328e-74
X1c 0.6086687 0.19071197 3.191560 1.912979e-03
Mc -0.3591561 0.15821174 -2.270097 2.543797e-02
X1c:Mc -0.0377977 0.02323204 -1.626964 1.070227e-01
library(rockchalk)
plotSlopes(fitMod, plotx="X1c", modx="Mc", modxVals="std.dev") ps <-
testSlopes(ps)) (ts <-
Error in eval(expr, envir, enclos): Objekt 'fitMod' nicht gefunden
plot(ts)
100
N <- rnorm(N, 175, 7)
X <- 0.7*X + rnorm(N, 0, 5)
M <- 0.4*M + rnorm(N, 0, 5)
Y <- data.frame(X, M, Y) dfMed <-
lm(Y ~ X + M, data=dfMed)
fit <-summary(fit)
Call:
lm(formula = Y ~ X + M, data = dfMed)
Residuals:
Min 1Q Median 3Q Max
-12.4088 -3.5870 0.0208 3.5566 12.6895
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.69901 13.52495 -0.126 0.90029
X 0.05606 0.12139 0.462 0.64525
M 0.32752 0.11343 2.887 0.00479 **
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 5.183 on 97 degrees of freedom
Multiple R-squared: 0.2223, Adjusted R-squared: 0.2063
F-statistic: 13.86 on 2 and 97 DF, p-value: 5.066e-06
library(multilevel)
sobel(dfMed$X, dfMed$M, dfMed$Y)
$`Mod1: Y~X`
Estimate Std. Error t value Pr(>|t|)
(Intercept) -9.8283102 13.7149511 -0.7166129 4.753174e-01
pred 0.3311861 0.0779808 4.2470206 4.933825e-05
$`Mod2: Y~X+M`
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.69900547 13.5249478 -0.1256201 0.900292638
pred 0.05606161 0.1213932 0.4618182 0.645245742
med 0.32751615 0.1134279 2.8874394 0.004788962
$`Mod3: M~X`
Estimate Std. Error t value Pr(>|t|)
(Intercept) -24.8210806 11.78103163 -2.106868 3.768266e-02
pred 0.8400333 0.06698488 12.540641 4.315325e-22
$Indirect.Effect
[1] 0.2751245
$SE
[1] 0.09777623
$z.value
[1] 2.813817
$N
[1] 100
mediation
Also useful for much more complicated situations.
lm(M ~ X, data=dfMed)
fitM <- lm(Y ~ X + M, data=dfMed)
fitY <-
library(mediation)
mediate(fitM, fitY, sims=999, treat="X", mediator="M")
fitMed <-summary(fitMed)
Causal Mediation Analysis
Quasi-Bayesian Confidence Intervals
Estimate 95% CI Lower 95% CI Upper p-value
ACME 0.2731 0.0685 0.4673 0.01
ADE 0.0577 -0.1790 0.2946 0.67
Total Effect 0.3307 0.1782 0.4917 0.00
Prop. Mediated 0.8303 0.1934 1.8214 0.01
Sample Size Used: 100
Simulations: 999
plot(fitMed)
mediate(fitM, fitY, boot=TRUE, sims=999, treat="X", mediator="M")
fitMedBoot <-summary(fitMedBoot)
Causal Mediation Analysis
Nonparametric Bootstrap Confidence Intervals with the Percentile Method
Estimate 95% CI Lower 95% CI Upper p-value
ACME 0.2751 0.0685 0.4681 0.02
ADE 0.0561 -0.1809 0.3059 0.67
Total Effect 0.3312 0.1374 0.5008 0.00
Prop. Mediated 0.8307 0.1890 2.0355 0.02
Sample Size Used: 100
Simulations: 999
More complex structural equation models are supported by packages sem
, OpenMx
, and lavaan
. More packages can be found in CRAN task view Psychometric Models.
try(detach(package:rockchalk))
try(detach(package:mediation))
try(detach(package:Matrix))
try(detach(package:lpSolve))
try(detach(package:multilevel))
try(detach(package:MASS))
try(detach(package:nlme))
try(detach(package:sandwich))
try(detach(package:mvtnorm))
R markdown - markdown - R code - all posts