# principles and practice of sem, 5th ed.
# rex b. kline, guilford press, 2023
# chapter 20, table 20.1, analysis 2
# interventional in(direct) effects in a randomized study
# of a family-based program on bmi
# 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("lavaan")
# get citation
citation(package = "lavaan", auto = TRUE)
# input correlation matrix and standard deviations
vanryzinLower.cor <- '
1.00
.04 1.00
-.03 .30 1.00
.00 .20 .03 1.00 '
vanryzin.cor <- lavaan::getCov(vanryzinLower.cor, names = c("intervention",
"eatAtt", "depression", "bmi"))
vanryzin.cov <- lavaan::cor2cov(vanryzin.cor, sds = c(.50,.25,4.63,6.31))
# display covariance matrix
vanryzin.cov
# specify the analytical model,
# which is parallel mediator model
vanryzin.model <- '
# marginal model
eatAtt ~ a1*intervention
depression ~ a2*intervention
#outcome model
bmi ~ b1*eatAtt + b2*depression +cprime*intervention
# define interventional direct effect as cprime
ide := cprime
# calculate interventional indirect effects
# for each of eating attitude and depression
iie1 := a1*b1
iie2 := a2*b2
# calculate joint interventional indirect effect
iiejoint := iie1 + iie2
# calculate total interventional (overall) effect
tie := iiejoint + ide '
# fit model to data
# results about fit statistics and residuals
# are not of interest here because the model
# is an analytical model for deriving interventional
# (in)direct effects, not a causal model on its own
options(width = 130)
vanryzin <- lavaan::sem(vanryzin.model, sample.cov = vanryzin.cov,
sample.nobs = 792)
lavaan::parameterEstimates(vanryzin)
lavaan::standardizedSolution(vanryzin)