if (FALSE) {
# Load data set "Demo.twolevel" in the lavaan package
data("Demo.twolevel", package = "lavaan")
#----------------------------------------------------------------------------
# Cluster variable specification
# Example 1a: Cluster variable 'cluster' in 'x'
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4", "cluster")], cluster = "cluster")
# Example 1b: Cluster variable 'cluster' not in 'x'
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")], cluster = Demo.twolevel$cluster)
# Example 1c: Alternative specification using the 'data' argument
multilevel.invar(y1:y4, data = Demo.twolevel, cluster = "cluster")
#----------------------------------------------------------------------------
# Model specification using 'x' for a one-factor model
#..........
# Level of measurement invariance
# Example 2a: Configural invariance
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, invar = "config")
# Example 2b: Metric invariance
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, invar = "metric")
# Example 2c: Scalar invariance
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, invar = "scalar")
#..........
# Residual covariance at the Within level and residual variance at the Between level
# Example 3a: Residual covariance between "y3" and "y4" at the Within level
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, rescov = c("y3", "y4"))
# Example 3b: Residual variances of 'y1' at the Between level fixed at 0
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, fix.resid = "y1")
#..........
# Example 4: Print all results
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, print = "all")
#..........
# Example 5: lavaan model and summary of the estimated model
mod <- multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, output = FALSE)
# lavaan syntax of the metric invariance model
mod$model$metric
# Fitted lavaan object of the metric invariance model
lavaan::summary(mod$model.fit$metric, standardized = TRUE, fit.measures = TRUE)
#----------------------------------------------------------------------------
# Model specification using 'model' for one or multiple factor model
# Example 6a: One-factor model
multilevel.invar(Demo.twolevel, cluster = "cluster", model = c("y1", "y2", "y3", "y4"))
# Example 6b: Two-factor model
multilevel.invar(Demo.twolevel, cluster = "cluster",
model = list(c("y1", "y2", "y3"), c("y4", "y5", "y6")))
#----------------------------------------------------------------------------
# Write results
# Example 7a: Write results into an Excel file
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, print = "all",
write = "Multilevel_Invariance.txt")
# Example 7b: Write results into an Excel file
multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, print = "all",
write = "Multilevel_Invariance.xlsx")
# Assign results into an object and write results into an Excel file
mod <- multilevel.invar(Demo.twolevel[,c("y1", "y2", "y3", "y4")],
cluster = Demo.twolevel$cluster, print = "all",
output = FALSE)
# Write results into an Excel file
write.result(mod, "Multilevel_Invariance.xlsx")
}
Run the code above in your browser using DataLab