# principles and practice of sem, 5th ed.
# rex b. kline, guilford press, 2023
# chapter 20, table 20.1, analysis 1
# coefficients and effect sizes in basic
# mediation model fitted to data from
# jessor & jessor (1991)
# to avoid the problem that some R packages share the same name
# for different functions, all functions are specified next as
# package::function
# which prevents masking, or the default hiding of
# a function with a redundant name from a package used next
date()
v <- R.Version()
print(paste0(v$language, " version ", v$major, ".",
v$minor, " (", v$year, "-", v$month, "-", v$day, ")"))
library("MBESS")
library("lavaan")
# wider output to avoid wrapping matrices
options("width"=150)
# get citations
citation(package = "MBESS", auto = TRUE)
citation(package = "lavaan", auto = TRUE)
# input covariance matrix
jessorLower.cov <- '
2.268
.662 2.276
-.087 -.226 .092 '
jessor.cov <- lavaan::getCov(jessorLower.cov, names = c("X", "M", "Y"))
# display full covariance matrix
jessor.cov
# specify position of X, M, and Y in the covariance matrix
# all means are zero
# specify model in MBESS
MBESS::mediation(S = jessor.cov, N = 432,
x.location.S = 1, mediator.location.S = 2, dv.location.S = 3,
mean.x = 0, mean.m = 0, mean.dv = 0, conf.level=.95,
complete.set = TRUE)
# specify model in lavaan
# to get standardized solution and
# standard errors for indirect & total effects
jessor.model <- '
M ~ a*X
Y ~ b*M + cprime*X
# define indirect and total effects
ab := a*b
total := cprime + a*b '
jessor <- lavaan::sem(jessor.model, sample.cov = jessor.cov,
sample.cov.rescale = TRUE, sample.nobs = 432)
# print estimates at the same level of accuracy
# as MBESS
print(lavaan::parameterEstimates(jessor), nd = 8)
print(lavaan::standardizedSolution(jessor), nd = 8)