Examples using the supported estimation methods
Source:vignettes/articles/estimation_methods.Rmd
estimation_methods.Rmd
The currently supported estimation methods are:
- covariance-based SEM estimated with the lavaan package
- PLS-SEM estimated using cSEM
- Bayesian SEM estimated using blavaan
Lavaan
Example 1 (lavaan)
library(lavaan)
library(rmedsem)
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science",
standardized=T, mcreps=5000,
approach = c("bk","zlc"))
print(out)
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'lavaan'
#> Mediation effect: 'math' -> 'read' -> 'science'
#>
#> Sobel Delta Monte-Carlo
#> Indirect effect 0.2506 0.2506 0.2506
#> Std. Err. 0.0456 0.0456 0.0449
#> z-value 5.5006 5.5006 5.5701
#> p-value 3.79e-08 3.79e-08 2.55e-08
#> CI [0.161, 0.34] [0.161, 0.34] [0.164, 0.34]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'math:read' (X -> M) with B=0.662 and p=0.000
#> STEP 2 - 'read:science' (M -> Y) with B=0.378 and p=0.000
#> STEP 3 - 'math:science' (X -> Y) with B=0.380 and p=0.000
#> As STEP 1, STEP 2 and STEP 3 as well as the Sobel's test above
#> are significant the mediation is partial.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Monte-Carlo
#> STEP 1 - 'math:science' (X -> Y) with B=0.380 and p=0.000
#> As the Monte-Carlo test above is significant, STEP 1 is
#> significant and their coefficients point in same direction,
#> there is complementary mediation (partial mediation).
#>
#> Effect sizes
#> RIT = (Indirect effect / Total effect)
#> (0.251/0.631) = 0.397
#> Meaning that about 40% of the effect of 'math'
#> on 'science' is mediated by 'read'
#> RID = (Indirect effect / Direct effect)
#> (0.251/0.380) = 0.659
#> That is, the mediated effect is about 0.7 times as
#> large as the direct effect of 'math' on 'science'
Example 2 (lavaan)
model02 <- "
# measurement model
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
"
mod <- sem(model02, data=lavaan::PoliticalDemocracy)
out <- rmedsem(mod, indep="ind60", med="dem60", dep="dem65",
standardized=T, mcreps=5000,
approach = c("bk","zlc"))
print(out)
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'lavaan'
#> Mediation effect: 'ind60' -> 'dem60' -> 'dem65'
#>
#> Sobel Delta Monte-Carlo
#> Indirect effect 0.4091 0.409 0.4091
#> Std. Err. 0.0956 0.116 0.0959
#> z-value 4.2817 3.524 4.2465
#> p-value 1.85e-05 0.000425 2.17e-05
#> CI [0.222, 0.596] [0.182, 0.637] [0.226, 0.6]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'ind60:dem60' (X -> M) with B=0.448 and p=0.000
#> STEP 2 - 'dem60:dem65' (M -> Y) with B=0.913 and p=0.000
#> STEP 3 - 'ind60:dem65' (X -> Y) with B=0.146 and p=0.038
#> As STEP 1, STEP 2 and STEP 3 as well as the Sobel's test above
#> are significant the mediation is partial.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Monte-Carlo
#> STEP 1 - 'ind60:dem65' (X -> Y) with B=0.146 and p=0.038
#> As the Monte-Carlo test above is significant, STEP 1 is
#> significant and their coefficients point in same direction,
#> there is complementary mediation (partial mediation).
#>
#> Effect sizes
#> RIT = (Indirect effect / Total effect)
#> (0.409/0.555) = 0.738
#> Meaning that about 74% of the effect of 'ind60'
#> on 'dem65' is mediated by 'dem60'
#> RID = (Indirect effect / Direct effect)
#> (0.409/0.146) = 2.811
#> That is, the mediated effect is about 2.8 times as
#> large as the direct effect of 'ind60' on 'dem65'
Example 3 (lavaan)
model03 <- "
Attractive =~ face + sexy
Appearance =~ body + appear + attract
Muscle =~ muscle + strength + endur
Weight =~ lweight + calories + cweight
Appearance ~ Attractive + age
Muscle ~ Appearance + Attractive + age
Weight ~ Appearance + Attractive + age
"
mod <- sem(model03, data=rmedsem::workout)
rmedsem(mod, indep="Attractive", med="Appearance", dep="Muscle",
standardized=T, mcreps=5000,
approach = c("bk","zlc"))
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'lavaan'
#> Mediation effect: 'Attractive' -> 'Appearance' -> 'Muscle'
#>
#> Sobel Delta Monte-Carlo
#> Indirect effect 0.0654 0.0654 0.0654
#> Std. Err. 0.0331 0.0338 0.0335
#> z-value 1.9748 1.9359 1.9641
#> p-value 0.0483 0.0529 0.0495
#> CI [0.000491, 0.13] [-0.000814, 0.132] [0.00453, 0.136]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'Attractive:Appearance' (X -> M) with B=0.158 and p=0.033
#> STEP 2 - 'Appearance:Muscle' (M -> Y) with B=0.414 and p=0.000
#> STEP 3 - 'Attractive:Muscle' (X -> Y) with B=-0.014 and p=0.850
#> As STEP 1, STEP 2 and the Sobel's test above are significant
#> and STEP 3 is not significant the mediation is complete.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Monte-Carlo
#> STEP 1 - 'Attractive:Muscle' (X -> Y) with B=-0.014 and p=0.850
#> As the Monte-Carlo test above is significant and STEP 1 is not
#> significant there indirect-only mediation (full mediation).
#>
#> Effect sizes
#> WARNING: Total effect is smaller than indirect effect!
#> Effect sizes should not be interpreted.
#> RIT = (Indirect effect / Total effect)
#> Total effect 0.052 is too small to calculate RIT
#> RID = (Indirect effect / Direct effect)
#> (0.065/0.014) = 4.714
#> That is, the mediated effect is about 4.7 times as
#> large as the direct effect of 'Attractive' on 'Muscle'
rmedsem(mod, indep="Attractive", med="Appearance", dep="Weight",
standardized=T, mcreps=5000,
approach = c("bk","zlc"))
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'lavaan'
#> Mediation effect: 'Attractive' -> 'Appearance' -> 'Weight'
#>
#> Sobel Delta Monte-Carlo
#> Indirect effect 0.0979 0.0979 0.0979
#> Std. Err. 0.0470 0.0484 0.0472
#> z-value 2.0810 2.0228 2.0716
#> p-value 0.0374 0.0431 0.0383
#> CI [0.00569, 0.19] [0.00304, 0.193] [0.00662, 0.192]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'Attractive:Appearance' (X -> M) with B=0.158 and p=0.033
#> STEP 2 - 'Appearance:Weight' (M -> Y) with B=0.619 and p=0.000
#> STEP 3 - 'Attractive:Weight' (X -> Y) with B=-0.125 and p=0.073
#> As STEP 1, STEP 2 and the Sobel's test above are significant
#> and STEP 3 is not significant the mediation is complete.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Monte-Carlo
#> STEP 1 - 'Attractive:Weight' (X -> Y) with B=-0.125 and p=0.073
#> As the Monte-Carlo test above is significant and STEP 1 is not
#> significant there indirect-only mediation (full mediation).
#>
#> Effect sizes
#> WARNING: Total effect is smaller than indirect effect!
#> Effect sizes should not be interpreted.
#> RIT = (Indirect effect / Total effect)
#> Total effect 0.027 is too small to calculate RIT
#> RID = (Indirect effect / Direct effect)
#> (0.098/0.125) = 0.784
#> That is, the mediated effect is about 0.8 times as
#> large as the direct effect of 'Attractive' on 'Weight'
rmedsem(mod, indep="age", med="Appearance", dep="Muscle",
standardized=T, mcreps=5000,
approach = c("bk","zlc"))
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'lavaan'
#> Mediation effect: 'age' -> 'Appearance' -> 'Muscle'
#>
#> Sobel Delta Monte-Carlo
#> Indirect effect -0.1602 -0.1602 -0.1602
#> Std. Err. 0.0397 0.0395 0.0395
#> z-value -4.0391 -4.0579 -4.0487
#> p-value 5.37e-05 4.95e-05 5.15e-05
#> CI [-0.238, -0.0825] [-0.238, -0.0828] [-0.243, -0.0886]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'age:Appearance' (X -> M) with B=-0.387 and p=0.000
#> STEP 2 - 'Appearance:Muscle' (M -> Y) with B=0.414 and p=0.000
#> STEP 3 - 'age:Muscle' (X -> Y) with B=-0.147 and p=0.065
#> As STEP 1, STEP 2 and the Sobel's test above are significant
#> and STEP 3 is not significant the mediation is complete.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Monte-Carlo
#> STEP 1 - 'age:Muscle' (X -> Y) with B=-0.147 and p=0.065
#> As the Monte-Carlo test above is significant and STEP 1 is not
#> significant there indirect-only mediation (full mediation).
#>
#> Effect sizes
#> RIT = (Indirect effect / Total effect)
#> (0.160/0.307) = 0.521
#> Meaning that about 52% of the effect of 'age'
#> on 'Muscle' is mediated by 'Appearance'
#> RID = (Indirect effect / Direct effect)
#> (0.160/0.147) = 1.089
#> That is, the mediated effect is about 1.1 times as
#> large as the direct effect of 'age' on 'Muscle'
rmedsem(mod, indep="age", med="Appearance", dep="Weight",
standardized=T, mcreps=5000,
approach = c("bk","zlc"))
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'lavaan'
#> Mediation effect: 'age' -> 'Appearance' -> 'Weight'
#>
#> Sobel Delta Monte-Carlo
#> Indirect effect -0.2397 -0.2397 -0.2397
#> Std. Err. 0.0453 0.0449 0.0452
#> z-value -5.2867 -5.3398 -5.3107
#> p-value 1.25e-07 9.31e-08 1.09e-07
#> CI [-0.329, -0.151] [-0.328, -0.152] [-0.334, -0.156]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'age:Appearance' (X -> M) with B=-0.387 and p=0.000
#> STEP 2 - 'Appearance:Weight' (M -> Y) with B=0.619 and p=0.000
#> STEP 3 - 'age:Weight' (X -> Y) with B=0.341 and p=0.000
#> As STEP 1, STEP 2 and STEP 3 as well as the Sobel's test above
#> are significant the mediation is partial.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Monte-Carlo
#> STEP 1 - 'age:Weight' (X -> Y) with B=0.341 and p=0.000
#> As the Monte-Carlo test above is significant, STEP 1 is
#> significant and their coefficients point in opposite
#> direction, there is competitive mediation (partial mediation).
#>
#> Effect sizes
#> WARNING: Total effect is smaller than indirect effect!
#> Effect sizes should not be interpreted.
#> RIT = (Indirect effect / Total effect)
#> Total effect 0.101 is too small to calculate RIT
#> RID = (Indirect effect / Direct effect)
#> (0.240/0.341) = 0.704
#> That is, the mediated effect is about 0.7 times as
#> large as the direct effect of 'age' on 'Weight'
cSEM
Example 1 (cSEM)
library(cSEM)
library(rmedsem)
mod.txt <- "
# need to use single-item measurement models for PLS-SEM
Read =~ read
Math =~ math
Science =~ science
# the actual path model
Read ~ Math
Science ~ Read + Math
"
mod <- cSEM::csem(.model=mod.txt, .data=rmedsem::hsbdemo,
.resample_method = "bootstrap", .R = 200)
rmedsem(mod, indep="Math", med="Read", dep="Science",
approach = c("bk", "zlc"))
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'cSEM'
#> Mediation effect: 'Math' -> 'Read' -> 'Science'
#>
#> Sobel Delta Bootstrap
#> Indirect effect 0.2506 0.2506 0.2506
#> Std. Err. 0.0528 0.0544 0.0515
#> z-value 4.7464 4.6082 4.8690
#> p-value 2.07e-06 4.06e-06 1.12e-06
#> CI [0.147, 0.354] [0.144, 0.357] [0.152, 0.348]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'Math:Read' (X -> M) with B=0.662 and p=0.000
#> STEP 2 - 'Read:Science' (M -> Y) with B=0.378 and p=0.000
#> STEP 3 - 'Math:Science' (X -> Y) with B=0.380 and p=0.000
#> As STEP 1, STEP 2 and STEP 3 as well as the Sobel's test above
#> are significant the mediation is partial.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Bootstrap
#> STEP 1 - 'Math:Science' (X -> Y) with B=0.380 and p=0.000
#> As the Bootstrap test above is significant, STEP 1 is
#> significant and their coefficients point in same direction,
#> there is complementary mediation (partial mediation).
#>
#> Effect sizes
#> RIT = (Indirect effect / Total effect)
#> (0.251/0.631) = 0.397
#> Meaning that about 40% of the effect of 'Math'
#> on 'Science' is mediated by 'Read'
#> RID = (Indirect effect / Direct effect)
#> (0.251/0.380) = 0.659
#> That is, the mediated effect is about 0.7 times as
#> large as the direct effect of 'Math' on 'Science'
Example 2 (cSEM)
model02 <- "
# measurement model
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
"
mod <- cSEM::csem(.model=model02, .data=lavaan::PoliticalDemocracy,
.resample_method = "bootstrap", .R = 200)
rmedsem(mod, indep="ind60", med="dem60", dep="dem65",
approach = c("bk","zlc"))
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'cSEM'
#> Mediation effect: 'ind60' -> 'dem60' -> 'dem65'
#>
#> Sobel Delta Bootstrap
#> Indirect effect 0.3988 0.3988 0.3988
#> Std. Err. 0.0879 0.0938 0.0785
#> z-value 4.5384 4.2498 5.0797
#> p-value 5.67e-06 2.14e-05 3.78e-07
#> CI [0.227, 0.571] [0.215, 0.583] [0.258, 0.54]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'ind60:dem60' (X -> M) with B=0.439 and p=0.000
#> STEP 2 - 'dem60:dem65' (M -> Y) with B=0.909 and p=0.000
#> STEP 3 - 'ind60:dem65' (X -> Y) with B=0.159 and p=0.021
#> As STEP 1, STEP 2 and STEP 3 as well as the Sobel's test above
#> are significant the mediation is partial.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Bootstrap
#> STEP 1 - 'ind60:dem65' (X -> Y) with B=0.159 and p=0.021
#> As the Bootstrap test above is significant, STEP 1 is
#> significant and their coefficients point in same direction,
#> there is complementary mediation (partial mediation).
#>
#> Effect sizes
#> RIT = (Indirect effect / Total effect)
#> (0.399/0.557) = 0.715
#> Meaning that about 72% of the effect of 'ind60'
#> on 'dem65' is mediated by 'dem60'
#> RID = (Indirect effect / Direct effect)
#> (0.399/0.159) = 2.514
#> That is, the mediated effect is about 2.5 times as
#> large as the direct effect of 'ind60' on 'dem65'
Example 3 (cSEM)
model03 <- "
Attractive =~ face + sexy
Appearance =~ body + appear + attract
Muscle =~ muscle + strength + endur
Weight =~ lweight + calories + cweight
Age =~ age ## need single-indicator LV for cSEM
Appearance ~ Attractive + Age
Muscle ~ Appearance + Attractive + Age
Weight ~ Appearance + Attractive + Age
"
mod <- cSEM::csem(.model=model03, .data=na.omit(rmedsem::workout),
.resample_method = "bootstrap", .R = 200)
rmedsem(mod, indep="Attractive", med="Appearance", dep="Muscle",
approach = c("bk","zlc"))
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'cSEM'
#> Mediation effect: 'Attractive' -> 'Appearance' -> 'Muscle'
#>
#> Sobel Delta Bootstrap
#> Indirect effect 0.1124 0.1124 0.1124
#> Std. Err. 0.0387 0.0404 0.0388
#> z-value 2.9041 2.7801 2.8984
#> p-value 0.00368 0.00543 0.00375
#> CI [0.0365, 0.188] [0.0331, 0.192] [0.0512, -0.282]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'Attractive:Appearance' (X -> M) with B=0.236 and p=0.001
#> STEP 2 - 'Appearance:Muscle' (M -> Y) with B=0.475 and p=0.000
#> STEP 3 - 'Attractive:Muscle' (X -> Y) with B=-0.010 and p=0.902
#> As STEP 1, STEP 2 and the Sobel's test above are significant
#> and STEP 3 is not significant the mediation is complete.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Bootstrap
#> STEP 1 - 'Attractive:Muscle' (X -> Y) with B=-0.010 and p=0.902
#> As the Bootstrap test above is significant and STEP 1 is not
#> significant there indirect-only mediation (full mediation).
#>
#> Effect sizes
#> WARNING: Total effect is smaller than indirect effect!
#> Effect sizes should not be interpreted.
#> RIT = (Indirect effect / Total effect)
#> Total effect 0.102 is too small to calculate RIT
#> RID = (Indirect effect / Direct effect)
#> (0.112/0.010) = 10.768
#> That is, the mediated effect is about 10.8 times as
#> large as the direct effect of 'Attractive' on 'Muscle'
blavaan
Example 1 (blavaan)
library(blavaan)
library(rmedsem)
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- bsem(mod.txt, data=rmedsem::hsbdemo,
n.chains=3, burnin=500, sample=500,
bcontrol = list(cores = 3))
#> Computing post-estimation metrics (including lvs if requested)...
rmedsem(mod, indep="math", med="read", dep="science",
approach = c("bk","zlc"))
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'blavaan'
#> Mediation effect: 'math' -> 'read' -> 'science'
#>
#> Prior (regression coefs): normal(0,10)
#> Bayes
#> Indirect effect 0.2477
#> Std. Err. 0.0455
#> z-value 5.4472
#> P(z>0) 0.0000
#> P(z<0) 1.0000
#> ER+ 0
#> ER- ∞
#> HDI [0, 0.163]
#>
#> Effect sizes
#> RIT = (Indirect effect / Total effect)
#> (0.248/0.628) = 0.394
#> Meaning that about 39% of the effect of 'math'
#> on 'science' is mediated by 'read'
#> RID = (Indirect effect / Direct effect)
#> (0.248/0.381) = 0.651
#> That is, the mediated effect is about 0.7 times as
#> large as the direct effect of 'math' on 'science'
print(out)
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'lavaan'
#> Mediation effect: 'ind60' -> 'dem60' -> 'dem65'
#>
#> Sobel Delta Monte-Carlo
#> Indirect effect 0.4091 0.409 0.4091
#> Std. Err. 0.0956 0.116 0.0959
#> z-value 4.2817 3.524 4.2465
#> p-value 1.85e-05 0.000425 2.17e-05
#> CI [0.222, 0.596] [0.182, 0.637] [0.226, 0.6]
#>
#> Baron and Kenny approach to testing mediation
#> STEP 1 - 'ind60:dem60' (X -> M) with B=0.448 and p=0.000
#> STEP 2 - 'dem60:dem65' (M -> Y) with B=0.913 and p=0.000
#> STEP 3 - 'ind60:dem65' (X -> Y) with B=0.146 and p=0.038
#> As STEP 1, STEP 2 and STEP 3 as well as the Sobel's test above
#> are significant the mediation is partial.
#>
#> Zhao, Lynch & Chen's approach to testing mediation
#> Based on p-value estimated using Monte-Carlo
#> STEP 1 - 'ind60:dem65' (X -> Y) with B=0.146 and p=0.038
#> As the Monte-Carlo test above is significant, STEP 1 is
#> significant and their coefficients point in same direction,
#> there is complementary mediation (partial mediation).
#>
#> Effect sizes
#> RIT = (Indirect effect / Total effect)
#> (0.409/0.555) = 0.738
#> Meaning that about 74% of the effect of 'ind60'
#> on 'dem65' is mediated by 'dem60'
#> RID = (Indirect effect / Direct effect)
#> (0.409/0.146) = 2.811
#> That is, the mediated effect is about 2.8 times as
#> large as the direct effect of 'ind60' on 'dem65'
Example 2 (blavaan)
model02 <- "
# measurement model
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
"
mod <- bsem(model02, data=lavaan::PoliticalDemocracy, std.lv=T,
meanstructure=T, n.chains=3,
save.lvs=T, burnin=1000, sample=1000, bcontrol = list(cores = 3))
#> Computing post-estimation metrics (including lvs if requested)...
rmedsem(mod, indep="ind60", med="dem60", dep="dem65")
#> Significance testing of indirect effect (standardized)
#> Model estimated with package 'blavaan'
#> Mediation effect: 'ind60' -> 'dem60' -> 'dem65'
#>
#> Prior (regression coefs): normal(0,10)
#> Bayes
#> Indirect effect 0.386893
#> Std. Err. 0.096384
#> z-value 4.014075
#> P(z>0) 0.000333
#> P(z<0) 0.999667
#> ER+ 3.33e-04
#> ER- 3.00e+03
#> HDI [0.000333, 0.192]
#>
#> Effect sizes
#> RIT = (Indirect effect / Total effect)
#> (0.387/0.537) = 0.720
#> Meaning that about 72% of the effect of 'ind60'
#> on 'dem65' is mediated by 'dem60'
#> RID = (Indirect effect / Direct effect)
#> (0.387/0.151) = 2.571
#> That is, the mediated effect is about 2.6 times as
#> large as the direct effect of 'ind60' on 'dem65'