Skip to contents

Calculate a mediation analysis for an SEM based on a blavaan model.

Usage

# S3 method for class 'blavaan'
rmedsem(
  mod,
  indep,
  med,
  dep,
  approach = c("bk", "zlc"),
  p.threshold = 0.05,
  effect.size = c("RIT", "RID")
)

Arguments

mod

A fitted SEM model (blavaan). Note that the model has to be fit using save.lvs=T if the mediation model contains latent variables.

indep

A string indicating the name of the independent variable in the model.

med

A string indicating the name of the mediator variable in the model.

dep

A string indicating the name of the dependent variable in the model.

effect.size

calculate different effect-sizes; one or more of "RIT", "RID"

Value

A rmedsem structure containing the results from the analysis

Examples


model02 <- "
  # measurement model
    ind60 =~ x1 + x2 + x3
    dem60 =~ y1 + y2 + y3 + y4
    dem65 =~ y5 + y6 + y7 + y8
  # regressions
    dem60 ~ ind60
    dem65 ~ ind60 + dem60
"
library(blavaan)
#> Loading required package: Rcpp
#> This is blavaan 0.5-5
#> On multicore systems, we suggest use of future::plan("multicore") or
#>   future::plan("multisession") for faster post-MCMC computations.
mod <- bsem(model02, data=lavaan::PoliticalDemocracy, std.lv=TRUE,
            meanstructure=TRUE, n.chains=1,
            save.lvs=TRUE, burnin=500, sample=500)
#> 
#> SAMPLING FOR MODEL 'stanmarg' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0.00038 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 3.8 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:   1 / 1000 [  0%]  (Warmup)
#> Chain 1: Iteration: 100 / 1000 [ 10%]  (Warmup)
#> Chain 1: Iteration: 200 / 1000 [ 20%]  (Warmup)
#> Chain 1: Iteration: 300 / 1000 [ 30%]  (Warmup)
#> Chain 1: Iteration: 400 / 1000 [ 40%]  (Warmup)
#> Chain 1: Iteration: 500 / 1000 [ 50%]  (Warmup)
#> Chain 1: Iteration: 501 / 1000 [ 50%]  (Sampling)
#> Chain 1: Iteration: 600 / 1000 [ 60%]  (Sampling)
#> Chain 1: Iteration: 700 / 1000 [ 70%]  (Sampling)
#> Chain 1: Iteration: 800 / 1000 [ 80%]  (Sampling)
#> Chain 1: Iteration: 900 / 1000 [ 90%]  (Sampling)
#> Chain 1: Iteration: 1000 / 1000 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 7.783 seconds (Warm-up)
#> Chain 1:                6.11 seconds (Sampling)
#> Chain 1:                13.893 seconds (Total)
#> Chain 1: 
#> Warning: The largest R-hat is 1.06, indicating chains have not mixed.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#r-hat
#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#bulk-ess
#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
#> Running the chains for more iterations may help. See
#> https://mc-stan.org/misc/warnings.html#tail-ess
#> Computing post-estimation metrics (including lvs if requested)...
out <- rmedsem(mod,  indep="ind60", med="dem60", dep="dem65")
#> Warning: Some Rhat>1.05, check convergence!
print(out)
#> 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.3844
#> Std. Err.         0.0899
#> z-value           4.2750
#> P(z>0)            0.0000
#> P(z<0)            1.0000
#> ER+                    0
#> ER-                    ∞
#> HDI             [0, 0.2]
#> 
#> Effect sizes
#>    RIT = (Indirect effect / Total effect)
#>          (0.384/0.542) = 0.709
#>          Meaning that about  71% of the effect of 'ind60'
#>          on 'dem65' is mediated by 'dem60'
#>    RID = (Indirect effect / Direct effect)
#>          (0.384/0.158) = 2.437
#>          That is, the mediated effect is about 2.4 times as
#>          large as the direct effect of 'ind60' on 'dem65'