if (FALSE) {
# Load data set "HolzingerSwineford1939" in the lavaan package
data("HolzingerSwineford1939", package = "lavaan")
#---------------------------
# Measurement model with one factor
# Specification using the argument 'x'
item.cfa(HolzingerSwineford1939[, c("x1", "x2", "x3")])
# Alternative specification using the argument 'model'
item.cfa(HolzingerSwineford1939, model = c("x1", "x2", "x3"))
# Alternative specification using the argument 'model'
item.cfa(HolzingerSwineford1939, model = list(visual = c("x1", "x2", "x3")))
#---------------------------
# Measurement model with three factors
# Specification using the argument 'model'
item.cfa(HolzingerSwineford1939,
model = list(visual = c("x1", "x2", "x3"),
textual = c("x4", "x5", "x6"),
speed = c("x7", "x8", "x9")))
#---------------------------
# Residual covariances
# One residual covariance
item.cfa(HolzingerSwineford1939,
model = list(visual = c("x1", "x2", "x3"),
textual = c("x4", "x5", "x6"),
speed = c("x7", "x8", "x9")),
rescov = c("x1", "x2"))
# Two residual covariances
item.cfa(HolzingerSwineford1939,
model = list(visual = c("x1", "x2", "x3"),
textual = c("x4", "x5", "x6"),
speed = c("x7", "x8", "x9")),
rescov = list(c("x1", "x2"), c("x4", "x5")))
#---------------------------
# Second-order factor model based on three first-order factors
item.cfa(HolzingerSwineford1939,
model = list(visual = c("x1", "x2", "x3"),
textual = c("x4", "x5", "x6"),
speed = c("x7", "x8", "x9")),
hierarch = TRUE)
#---------------------------
# Measurement model with ordered-categorical indicators
item.cfa(round(HolzingerSwineford1939[, c("x4", "x5", "x6")]), ordered = TRUE)
#---------------------------
# Cluster-robust standard errors
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
# Specification using a variable in 'x'
item.cfa(Demo.twolevel[, c("y4", "y5", "y6", "cluster")], cluster = "cluster")
# Specification of the cluster variable in 'cluster'
item.cfa(Demo.twolevel[, c("y4", "y5", "y6")], cluster = Demo.twolevel$cluster)
# Specification using a variable in 'x'
item.cfa(Demo.twolevel, model = c("y4", "y5", "y6"), cluster = "cluster")
# Specification of the cluster variable in 'cluster'
item.cfa(Demo.twolevel, model = c("y4", "y5", "y6"), cluster = Demo.twolevel$cluster)
#---------------------------
# Print argument
# Request all results
item.cfa(HolzingerSwineford1939[, c("x1", "x2", "x3")], print = "all")
# Request modification indices with value equal or higher than 5
item.cfa(HolzingerSwineford1939[, c("x1", "x2", "x3", "x4")],
print = "modind", min.value = 5)
#---------------------------
# lavaan summary of the estimated model
mod <- item.cfa(HolzingerSwineford1939[, c("x1", "x2", "x3")], output = FALSE)
lavaan::summary(mod$mod.fit, standardized = TRUE, fit.measures = TRUE)
#---------------------------
# Write Results into a Excel file
item.cfa(HolzingerSwineford1939[, c("x1", "x2", "x3")], write = "CFA.xlsx")
result <- item.cfa(HolzingerSwineford1939[, c("x1", "x2", "x3")], output = FALSE)
write.result(result, "CFA.xlsx")
}
Run the code above in your browser using DataLab