# principles and practice of sem, 5th ed.
# rex b. kline, guilford press, 2023
# chapter 9, table 9.1, analysis 1
# global estimation of the covariance structure in the
# roth et al. (1989) parametric path model of illness
# 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 information
citation("lavaan", auto = TRUE)
# input the correlations in lower diagnonal form
rothLower.cor <- '
1.00
-.03 1.00
.39 .07 1.00
-.05 -.23 -.13 1.00
-.08 -.16 -.29 .34 1.00 '
# name the variables and convert to full correlation matrix
roth.cor <- lavaan::getCov(rothLower.cor, names = c("exercise", "hardy",
"fitness", "stress", "illness"))
# display the correlations
roth.cor
# add the standard deviations and convert to covariances
roth.cov <- lavaan::cor2cov(roth.cor, sds = c(66.50,38.00,18.40,33.50,62.48))
# display the covariances
roth.cov
# specify path model
roth.model <- '
# regressions
fitness ~ a*exercise
stress ~ c*hardy
illness ~ b*fitness + d*stress
# unanalyzed association between exercise and hardy
# automatically specified
# specify indirect effects on illness
ab := a*b
cd := c*d '
# fit model to sample covariance matrix, N = 373
# variances and covariance of measured exogenous
# variables are free parameters
# variances calculated with N - 1 in the denominator instead of N
# default information matrix is expected
roth <- lavaan::sem(roth.model, sample.cov = roth.cov, sample.nobs = 373,
fixed.x = FALSE, sample.cov.rescale = FALSE)
# check for negative error variances
# output of "TRUE" means no negative variances
lavaan::lavInspect(roth, add.labels = TRUE, "post.check")
# request output including 2 standardized solutions (all, lv)
lavaan::summary(roth, fit.measures = TRUE, standardized = TRUE,
rsquare = TRUE)
# request standardized solution nox,
# which replaces Std.all in the output
lavaan::summary(roth, header = FALSE, std.nox = TRUE)
# generate predicted covariance matrix
lavaan::fitted(roth)
# generate predicted correlation matrix
lavaan::lavInspect(roth, add.labels = TRUE, "cor.ov")
# print residuals
# covariance residuals
lavaan::residuals(roth, type = "raw")
# standardized residuals calculated as in mplus
lavaan::residuals(roth, type = "standardized.mplus")
# normalized residuals
lavaan::residuals(roth, type = "normalized")
# method "bollen" for correlation residuals means that
# the observed and predicted covariance matrices were
# separately transformed to correlation matrices before
# residuals were calculated
lavaan::residuals(roth, type = "cor.bollen")
# print modification indices
lavaan::modindices(roth)