# principles and practice of sem, 5th ed.
# rex b. kline, guilford press, 2023
# chapter 15, table 15.1, analysis 2
# step 2 (evaluate original sr with 5 paths among factors)
# in 2-step modeling for a full SR model of achievement and
# classroom adjustment
# 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)
library(semTools)
# get citation information
citation("lavaan", auto = TRUE)
citation("semTools", auto = TRUE)
# input the correlations in lower diagnonal form
worlandLower.cor <- '
1.00
.70 1.00
.65 .60 1.00
.55 .50 .45 1.00
.50 .45 .40 .70 1.00
.35 .35 .30 .55 .50 1.00
.30 .30 .30 .50 .45 .44 1.00
.25 .20 .22 .41 .28 .34 .40 1.00
.35 .32 .32 .48 .45 .42 .60 .45 1.00
-.25 -.24 -.22 -.21 -.18 -.15 -.15 -.12 -.17 1.00
-.22 -.26 -.30 -.25 -.22 -.18 -.17 -.14 -.20 .42 1.00 '
# name the variables and convert to full correlation matrix
worland.cor <- lavaan::getCov(worlandLower.cor, names = c("verbal","visual",
"memory","read","math","spell","motive","harmony","stable","parent","ses"))
# add the standard deviations and convert to covariances
worland.cov <- lavaan::cor2cov(worland.cor,
sds = c(13.75,14.80,12.60,14.90,15.25,13.85,9.50,11.10,8.70,
12.00,8.50))
options(width = 130)
# display correlations
worland.cor
# display covariances
worland.cov
# step 2a
# 4-factor SR model with 5 paths among factors
# by default, lavaan frees the disturbance covariance
# between a pair of outcomes in a structural model
# when there is no direct effect between them
# thus, this parameter is explicitly fixed to zero
# in this analysis
worlandSRa.model <- '
# measurement part
Cognitive =~ verbal + visual + memory
Achieve =~ read + math + spell
Adjust =~ motive + harmony + stable
Risk =~ parent + ses
# structural part (5 paths)
Achieve ~ Cognitive + Risk
Adjust ~ Cognitive + Risk
# constrain disturbance covariance to zero
Adjust ~~ 0*Achieve '
worlandSRa <- lavaan::sem(worlandSRa.model, sample.cov = worland.cov,
sample.nobs = 158)
summary(worlandSRa, fit.measures = TRUE, standardized = TRUE,
rsquare = TRUE)
# predicted covariance matrix
lavaan::fitted(worlandSRa)
# predicted correlation matrix for indicators
lavaan::lavInspect(worlandSRa, "cor.ov")
# predicted correlation matrix for factors
lavaan::lavInspect(worlandSRa, "cor.lv")
# residuals
lavaan::residuals(worlandSRa, type = "raw")
lavaan::residuals(worlandSRa, type = "standardized.mplus")
lavaan::residuals(worlandSRa, type = "cor.bollen")
# step 2b
# 4-factor SR model with 6 paths among factors
# this model is equivalent to the basic 4-factor
# CFA measurement model analyzed in step 1
worlandSRb.model <- '
# measurement part
Cognitive =~ verbal + visual + memory
Achieve =~ read + math + spell
Adjust =~ motive + harmony + stable
Risk =~ parent + ses
# structural part (6 paths)
Achieve ~ Cognitive + Risk
Adjust ~ Cognitive + Risk
Adjust ~~ Achieve '
worlandSRb <- lavaan::sem(worlandSRb.model, sample.cov = worland.cov,
sample.nobs = 158)
lavaan::summary(worlandSRb, fit.measures = TRUE, rsquare = TRUE)
# standardized estimates with standard errors
lavaan::standardizedSolution(worlandSRb)
# predicted covariance matrix
lavaan::fitted(worlandSRb)
# predicted correlation matrix for indicators
lavaan::lavInspect(worlandSRb, "cor.ov")
# predicted correlation matrix for factors
lavaan::lavInspect(worlandSRb, "cor.lv")
# residuals
lavaan::residuals(worlandSRb, type = "raw")
lavaan::residuals(worlandSRb, type = "standardized.mplus")
lavaan::residuals(worlandSRb, type = "cor.bollen")