# NOT RUN {
## set some example data missing at random
dat1 <- HolzingerSwineford1939
dat1$x5 <- ifelse(dat1$x1 <= quantile(dat1$x1, .3), NA, dat1$x5)
dat1$age <- dat1$ageyr + dat1$agemo/12
dat1$x9 <- ifelse(dat1$age <= quantile(dat1$age, .3), NA, dat1$x9)
## fit CFA model from lavaan's ?cfa help page
model <- '
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
'
## use ageyr and agemo as auxiliary variables
out <- cfa.2stage(model = model, data = dat1, aux = c("ageyr","agemo"))
## two versions of a corrected chi-squared test results are shown
out
## see Savalei & Bentler (2009) and Savalei & Falk (2014) for details
## the summary additionally provides the parameter estimates with corrected
## standard errors, test statistics, and confidence intervals, along with
## any other options that can be passed to parameterEstimates()
summary(out, standardized = TRUE)
## use parameter labels to fit a more constrained model
modc <- '
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + a*x8 + a*x9
'
outc <- cfa.2stage(model = modc, data = dat1, aux = c("ageyr","agemo"))
## use the anova() method to test this constraint
anova(out, outc)
## like for a single model, two corrected statistics are provided
# }
Run the code above in your browser using DataLab