# \donttest{
set.seed(20220722)
#To specify the model
demo ="
y ~ cp*x + start(0)*x + b*m + start(0.39)* m
m ~ a*x + start(0.39)*x
x ~~ start(1)*x
m ~~ start(1)*m
y ~~ start(1)*y
"
#To specify the indirect effects
mediation = "
ab := a*b
abc:= a*b + cp
"
#Power for mediation based on MC method when bootstrap method is used to test the effects:
# change nrep and nboot to at least 1,000 in real analysis
mediation.boot = wp.mc.sem.boot(model=demo, indirect=mediation, nobs=100,
nrep=10, nboot=10,
skewness=c(0, 0, 1.3), kurtosis=c(0,0,10), ovnames=c("x","m","y"))
#To print the power analysis results
summary(mediation.boot)
#Example: Power for Simple Mediation Analysis
ex1model <- "
math ~ c*ME + start(0)*ME + b*HE + start(0.39)*HE
HE ~ a*ME + start(0.39)*ME
"
indirect <- "ab:=a*b"
# change nrep and nboot to at least 1,000 in real analysis
boot.normal <- wp.mc.sem.boot(ex1model,indirect, 50, nrep=10, nboot=10)
summary(boot.normal)
boot.non.normal <- wp.mc.sem.boot(ex1model,indirect, 100, nrep=10, nboot=10,
skewness=c(-0.3, -0.7, 1.3),
kurtosis=c(1.5, 0, 5), ovnames=c('ME','HE','math'))
summary(boot.non.normal)
#Example: Multiple Group Mediation Analysis (Moderated Mediation)
ex3model <- "
y ~ start(c(0.283, 0.283))*x + c(c1,c2)*x + start(c(0.36, 0.14))*m + c(b1,b2)*m
m ~ start(c(0.721, 0.721))*x + c(a1,a2)*x
m =~ c(1,1)*m1 + start(c(0.8, 0.8))*m2 + start(c(0.8, 0.8))*m3
x ~~ start(c(0.25, 0.25))*x
y ~~ start(c(0.81, 0.95))*y
m ~~ start(c(0.87, 0.87))*m
m1 ~~ start(c(0.36, 0.36))*m1
m2 ~~ start(c(0.36, 0.36))*m2
m3 ~~ start(c(0.36, 0.36))*m3
"
# med1 and med2 are the mediation effect for group1 and group2, respectively.
indirect <- "
med1 := a1*b1
med2 := a2*b2
diffmed := a1*b1 - a2*b2
"
# change nrep and nboot to at least 1,000 in real analysis
bootstrap <- wp.mc.sem.boot(ex3model, indirect, nobs=c(400,200),
nrep=10, nboot=10)
summary(bootstrap)
#Example: A Longitudinal Mediation Model
ex4model <- "
x2 ~ start(.9)*x1 + x*x1
x3 ~ start(.9)*x2 + x*x2
m2 ~ start(.3)*x1 + a*x1 + start(.3)*m1 + m*m1
m3 ~ start(.3)*x2 + a*x2 + start(.3)*m2 + m*m2
y2 ~ start(.3)*m1 + b*m1 + start(.7)*y1 + y*y1
y3 ~ start(.3)*m2 + b*m2 + start(.7)*y2 + y*y2 + start(0)*x1 + c*x1
x1 ~~ start(.37)*m1
x1 ~~ start(.27)*y1
y1 ~~ start(.2278)*m1
x2 ~~ start(.19)*x2
x3 ~~ start(.19)*x3
m2 ~~ start(.7534)*m2
m3 ~~ start(.7534)*m3
y2 ~~ start(.3243)*y2
y3 ~~ start(.3243)*y3
"
indirect <- "ab := a*b"
# change nrep and nboot to at least 1,000 in real analysis
set.seed(10)
bootstrap <- wp.mc.sem.boot(ex4model, indirect, nobs=500, nrep=10, nboot=10)
summary(bootstrap)
# }
Run the code above in your browser using DataLab