# principles and practice of sem, 5th ed.
# rex b. kline, guilford press, 2023
# chapter 18, table 18.1, analysis 1
# single-factor model for ordinal data,
# option WLSMV (DWLS estimator with mean-
# and variance-corrected test statistic
# and robust standard errors)
# single-factor model for ordinal data,
# wlsmv estimator with robust standard errors
# 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)
citation("lavaan", auto = TRUE)
# read in external comma separated values (.csv) file,
# radloff.csv
# ** IMPORTANT **
# set the R working directory to the path for the location
# of the data file on your own computer
# the specification listed next is for my computer
# and will not work on your computer
setwd("C:/Users/rbkline/Desktop/")
#display working directory
getwd()
# copy data from csv file to a data frame
radloff.data = read.csv("radloff.csv")
# 4-point likert scale response format
# 0 = "< 1 day", 1 = "1-2 days",
# 3 = "3-4 days", 4 = "5-7 days"
# display for each item frequencies,
# cumulative proportions, normal deviates
# for cumulative proportions, and thresholds
# item statistics
# item x1
x1_frequencies <- table(radloff.data$x1)
x1_cum_proportions <- cumsum(table(radloff.data$x1)/2004)
x1_thresholds <- qnorm(cumsum(table(radloff.data$x1)/2004))
x1_frequencies
x1_cum_proportions
x1_thresholds
# item x2
x2_frequencies <- table(radloff.data$x2)
x2_cum_proportions <- cumsum(table(radloff.data$x2)/2004)
x2_thresholds <- qnorm(cumsum(table(radloff.data$x2)/2004))
x2_frequencies
x2_cum_proportions
x2_thresholds
# item x3
x3_frequencies <- table(radloff.data$x3)
x3_cum_proportions <- cumsum(table(radloff.data$x3)/2004)
x3_thresholds <- qnorm(cumsum(table(radloff.data$x3)/2004))
x3_frequencies
x3_cum_proportions
x3_thresholds
# item x4
x4_frequencies <- table(radloff.data$x4)
x4_cum_proportions <- cumsum(table(radloff.data$x4)/2004)
x4_thresholds <- qnorm(cumsum(table(radloff.data$x4)/2004))
x4_frequencies
x4_cum_proportions
x4_thresholds
# item x5
x5_frequencies <- table(radloff.data$x5)
x5_cum_proportions <- cumsum(table(radloff.data$x5)/2004)
x5_thresholds <- qnorm(cumsum(table(radloff.data$x5)/2004))
x5_frequencies
x5_cum_proportions
x5_thresholds
# specify 1-factor model
radloff.model <- '
Depression =~ x1 + x2 + x3 + x4 + x5 '
# analysis options specify that all
# observed variables are ordinal
# estimator is mean- and variance-adjusted weighted least squares
# with computation of robust standard errors
# parameterization is delta
# threshold parameter estimates are just the sample values
# a special symbol in the output, “~*~,”
# represents scaling factors for the variances
# of latent response variables, such as 1.0 in
# delta scaling, or their implied variances after
# fixing the variances of their error terms to 1.0
# in theta scaling
radloff <- lavaan::cfa(radloff.model, data = radloff.data,
parameterization = "delta", estimator = "wlsmv",
ordered = TRUE)
# parameter estimates
# note that loadings and error variances refer
# to latent response variables, but thresholds
# refer to the observed likert-scale items
lavaan::summary(radloff, fit = TRUE, standardized = TRUE,
rsquare = TRUE)
# standardized estimates with standard errors
lavaan::standardizedSolution(radloff)
# predicted correlations and residuals
# IMPORTANT
# note that labels x1-x5 in matrices for predicted
# correlations or for residuals refer to the latent
# response variables, not the observed variables
# model-implied correlation matrix,
# means, and thresholds
# all predicted means equal zero
# thresholds designated as, e.g.,
# x1|t1 for the 1st of 3 thresholds for item x1
lavaan::fitted(radloff)
# correlation residuals, z tests, and confidence
# intervals, exact-fit and close-fit tests
# based on the srmr as described by shi et al. (2020)
# all mean residuals and threshold residuals equal zero
lavaan::lavResiduals(radloff, type = "cor.bentler")