if (FALSE) {
# Load data set "HolzingerSwineford1939" in the lavaan package
data("HolzingerSwineford1939", package = "lavaan")
#----------------------------------------------------------------------------
# Between-Group Measurement Invariance: Continuous Indicators
#..................
# Measurement model with one factor
# Example 1a: Model specification using the argument '...'
item.invar(HolzingerSwineford1939, x1, x2, x3, x4, group = "sex")
# Example 1b: Alternative model specification without using the argument '...'
item.invar(HolzingerSwineford1939[, c("x1", "x2", "x3", "x4")],
group = HolzingerSwineford1939$sex)
# Example 1c: Alternative model specification without using the argument '...'
item.invar(HolzingerSwineford1939[, c("x1", "x2", "x3", "x4", "sex")], group = "sex")
# Example 1d: Alternative model specification using the argument 'model'
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"), group = "sex")
#..................
# Measurement model with two factors
# Example 2: Model specification using the argument 'model'
item.invar(HolzingerSwineford1939,
model = list(c("x1", "x2", "x3", "x4"), c("x5", "x6", "x7", "x8")),
group = "sex")
#..................
# Configural, metric, scalar, and strict measurement invariance
# Example 3: Evaluate configural, metric, scalar, and strict measurement invariance
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", invar = "strict")
#..................
# Between-group partial measurement invariance
# Example 4a: Two Groups
# Free factor loadings for 'x2' and 'x3'
# Free intercept for 'x1'
# Free residual variance for 'x4'
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", invar = "strict",
partial = list(load = c("x2", "x3"),
inter = "x1",
resid = "x4"))
# Example 4b: More than Two Groups
# Free factor loading for 'x2' in group 2
# Free factor loading for 'x4' in group 1 and 4
# Free intercept for 'x1' in group 3
# Free residual variance for 'x3' in group 1 and 3
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "ageyr", invar = "strict",
partial = list(load = list(x2 = "g2", x4 = c("g1", "g4")),
inter = list(x1 = "g3"),
resid = list(x3 = c("g1", "g3"))))
#..................
# Residual covariances
# Example 5a: One residual covariance
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
rescov = c("x3", "x4"), group = "sex")
# Example 5b: Two residual covariances
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
rescov = list(c("x1", "x4"), c("x3", "x4")), group = "sex")
#..................
# Scaled test statistic
# Example 6a: Specify cluster variable using a variable name in 'data'
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", cluster = "agemo")
# Example 6b: Specify cluster variable as vector
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", cluster = HolzingerSwineford1939$agemo)
#..................
# Default Null model
# Example 7: Specify default null model for computing incremental fit indices
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", null.model = FALSE)
#..................
# Print argument
# Example 8a: Request all results
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", print = "all")
# Example 8b: Request fit indices with ad hoc non-normality correction
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", print.fit = "scaled")
# Example 8c: Request modification indices with value equal or higher than 2
# and highlight residual correlations equal or higher than 0.3
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", print = c("modind", "resid"),
mod.minval = 2, resid.minval = 0.3)
#..................
# Model syntax and lavaan summary of the estimated model
# Example 9a: Model specification using the argument '...'
mod1 <- item.invar(HolzingerSwineford1939, x1, x2, x3, x4, group = "sex",
output = FALSE)
# lavaan summary of the scalar invariance model
lavaan::summary(mod1$model.fit$scalar, standardized = TRUE, fit.measures = TRUE)
# Example 9b: Do not estimate any models
mod2 <- item.invar(HolzingerSwineford1939, x1, x2, x3, x4, group = "sex",
lavaan.run = FALSE)
# lavaan model syntax metric invariance model
cat(mod2$model$metric)
# lavaan model syntax scalar invariance model
cat(mod2$model$scalar)
#----------------------------------------------------------------------------
# Longitudinal Measurement Invariance: Continuous Indicators
# Example 10: Two time points with three indicators at each time point
item.invar(HolzingerSwineford1939,
model = list(c("x1", "x2", "x3"), c("x5", "x6", "x7")), long = TRUE)
#..................
# Longitudinal partial measurement invariance
# Example 11: Two Time Points with three indicators at each time point
# Free factor loading for 'x2'
# Free intercepts for 'x1' and x2
item.invar(HolzingerSwineford1939,
model = list(c("x1", "x2", "x3"), c("x5", "x6", "x7")), long = TRUE,
partial = list(load = "x2",
inter = c("x1", "x2")))
#----------------------------------------------------------------------------
# Between-Group Measurement Invariance: Ordered Categorical Indicators
#
# Note that the example analysis for ordered categorical indicators cannot be
# conduct since the data set 'data' is not available.
# Example 12a: Delta parameterization (default)
item.invar(data, item1, item2, item3, item4, group = "two.group", ordered = TRUE)
# Example 12a: Theta parameterization
item.invar(data, item1, item2, item3, item4, group = "two.group", ordered = TRUE,
parameterization = "theta")
#----------------------------------------------------------------------------
# Between-Group Partial Measurement Invariance: Ordered Categorical Indicators
# Example 13a: Two Groups
# Free 2nd and 4th threshold of 'item1'
# Free 1st threshold of 'item3'
# Free factor loadings for 'item2' and 'item4'
# Free intercept for 'item1'
# Free residual variance for 'item3'
item.invar(data, item1, item2, item3, item4, group = "two.group", ordered = TRUE,
partial = list(thres = list(item1 = c("t2", "t4"),
item3 = "t1"),
load = c("item2", "item4"),
inter = "item1",
resid = "item3"))
# Example 13b: More than Two Groups
# Free 1st threshold of 'item1' in group 1 and 2
# Free 3rd threshold of 'item3' in group 3
# Free factor loadings for 'item2' in group 1
# Free intercept for 'item2' in group 1
# Free intercept for 'item3' in group 2 and 4
# Free residual variance for 'item1' in group 1 and 3
item.invar(data, item1, item2, item3, item4, group = "four.group", ordered = TRUE,
partial = list(thres = list(item1 = list(t1 = c("g1", "g2")),
item3 = list(t3 = "g3")),
load = list(item2 = "g1"),
inter = list(item2 = "g1", item3 = c("g2", "g4")),
resid = list(item1 = c("g1", "g3"))))
#----------------------------------------------------------------------------
# Longitudinal Measurement Invariance: Ordered Categorical Indicators
# Example 14: Two Time Points
item.invar(data, model = list(c("aitem1", "aitem2", "aitem3"),
c("bitem1", "bitem2", "bitem3")),
long = TRUE, ordered = TRUE)
#..................
# Longitudinal partial measurement invariance: Ordered Categorical Indicators
# Example 15: Two Time Points
# Free 2nd and 4th threshold of 'aitem1'
# Free 1st threshold of 'aitem4'
# Free factor loading for 'aitem2
# Free intercepts for 'aitem1' and 'bitem2'
# Free residual variance for 'aitem3'
item.invar(data, model = list(c("aitem1", "aitem2", "aitem3"),
c("bitem1", "bitem2", "bitem3")),
long = TRUE, ordered = TRUE, invar = "strict",
partial = list(thres = list(aitem1 = c("t2", "t4"), aitem3 = "t1"),
load = "aitem2",
inter = c("aitem1", "bitem2"),
resid = "aitem3"))
#------------------------------------------------
# Write Results
# Example 16a: Write Results into a text file
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", print = "all", write = "Invariance.txt", output = FALSE)
# Example 16b: Write Results into a Excel file
item.invar(HolzingerSwineford1939, model = c("x1", "x2", "x3", "x4"),
group = "sex", print = "all", write = "Invariance.xlsx", output = FALSE)
}
Run the code above in your browser using DataLab