Here is the entire code I used. At the very bottom, I run lavTestScore(fit.scalar) and View(parTable(fit.scalar)) to determine parameters to release.
cfs_efa_model_001 <- '
factor2 =~ Q1_8 + Q2_2 + Q3_2 + Q4_2 + Q5_2
factor4 =~ Q2_4 + Q3_4 + Q4_4 + Q6_4
factor6 =~ Q1_6 + Q2_6 + Q3_6 + Q4_6
Q4_4 ~~ Q6_4
'
######
######
######
# INVARIANCE TESTING
######
######
######
# add grouping variable here
g1 <- "dissgroup"
# add factor model here
mft_fact <- cfs_efa_model_001
# add data variable here
mft_dat <- cfs_3middle
#Invariance testing
syntax.config <- measEq.syntax(configural.model = mft_fact, data=mft_dat, parameterization="theta",
ID.fac="
std.lv", ID.cat = "Wu.Estabrook.2016",
group = g1, group.label=c(1,2,3,4))
cat(as.character(syntax.config))
summary(syntax.config)
## threshold invariance
syntax.thresh <- measEq.syntax(configural.model = mft_fact, data = mft_dat,
parameterization = "theta",
ID.fac = "
std.lv", ID.cat = "Wu.Estabrook.2016",
group = g1, group.label=c(1,2,3,4), group.equal = "thresholds")
## notice that constraining 4 thresholds allows intercepts and residual
## variances to be freely estimated in all but the first group & occasion
cat(as.character(syntax.thresh))
## print a summary of model features
summary(syntax.thresh)
## Fit a model to the data either in a subsequent step:
mod.config <- as.character(syntax.config)
fit.config <- cfa(mod.config, data = mft_dat, group = g1, group.label=c(1,2,3,4),
parameterization = "theta")
lavInspect(fit.config, "
cov.lv")
## or in a single step:
fit.thresh <- measEq.syntax(configural.model = mft_fact, data = mft_dat,
parameterization = "theta",
ID.fac = "
std.lv", ID.cat = "Wu.Estabrook.2016",
group = g1, group.label=c(1,2,3,4), group.equal = "thresholds", return.fit = TRUE)
## compare their fit to test threshold invariance
anova(fit.config, fit.thresh)
######
# METRIC INVARIANCE TESTING
######
## metric invariance
syntax.metric <- measEq.syntax(configural.model = mft_fact, data = mft_dat,
parameterization = "theta",
ID.fac = "
std.lv", ID.cat = "Wu.Estabrook.2016",
group = g1, group.label=c(1,2,3,4),
group.equal = c("thresholds","loadings"))
summary(syntax.metric) # summarize model features
mod.metric <- as.character(syntax.metric) # save as text
cat(mod.metric) # print/view lavaan syntax
## fit model to data
fit.metric <- cfa(mod.metric, data = mft_dat, group = g1, group.label=c(1,2,3,4),
parameterization = "theta")
## test equivalence of loadings, given equivalence of thresholds
anova(fit.thresh, fit.metric)
######
# SCALAR INVARIANCE TESTING
######
## scalar invariance
syntax.scalar <- measEq.syntax(configural.model = mft_fact, data = mft_dat,
parameterization = "theta",
ID.fac = "
std.lv", ID.cat = "Wu.Estabrook.2016",
group = g1, group.label=c(1,2,3,4),
group.equal = c("thresholds","loadings",
"intercepts"),
group.partial = c("Q2_4 ~1","Q3_4 ~ 1", "Q3_2 ~ 1"))
summary(syntax.scalar) # summarize model features
mod.scalar <- as.character(syntax.scalar) # save as text
cat(mod.scalar) # print/view lavaan syntax
## fit model to data
fit.scalar <- cfa(mod.scalar, data = mft_dat, group = g1, group.label=c(1,2,3,4),
parameterization = "theta")
## test equivalence of intercepts, given equal thresholds & loadings
anova(fit.metric, fit.scalar)
######
# INVARIANCE TESTING TABLE
######
## For a single table with all results, you can pass the models to
## summarize to the compareFit() function
compareFit(fit.config, fit.thresh, fit.metric, fit.scalar)
lavInspect(fit.metric, "
cov.lv")
#TEST FOR which parameters to release
lavTestScore(fit.scalar)
View(parTable(fit.scalar))