Compare Nested and Non‑Nested Structural Equation
Models
Built on lavaan – automatic nesting detection, chi‑square
difference tests, Vuong tests, and comprehensive fit indices.
# install.packages("remotes")
remotes::install_github("ssjerf-stack/modelscompete4")library(modelscompete4)
library(lavaan)
# Example data
data(HolzingerSwineford1939)
# Define models as named list of lavaan syntax strings
model_list <- list(
"OneFactor" = "g =~ x1 + x2 + x3 + x4 + x5 + x6",
"TwoFactor" = "
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
"
)
# Compare them – the function fits the models automatically
result <- compare_models(model_list, data = HolzingerSwineford1939)
# Print summary
print(result)
# Access detailed results
result$fit_table # fit indices for all models
result$comparison_matrix # pairwise test resultslavTestLRT)nonnest2::vuongtest)print(),
summary()# Three non‑nested models with different factor structures
models <- list(
"M1" = "F1 =~ x1 + x2 + x3",
"M2" = "F1 =~ x1 + x2 + x3 + x4",
"M3" = "
F1 =~ x1 + x2 + x3
F2 =~ x4 + x5 + x6
"
)
result <- compare_models(models, data = HolzingerSwineford1939)
print(result)The returned object of class modelscompete4
contains:
| Component | Description |
|---|---|
$fit_list |
List of fitted lavaan objects |
$fit_table |
Data frame with fit indices for all models |
$comparison_matrix |
Matrix with pairwise test results |
$test_results |
Detailed statistical tests (LRT / Vuong) |
$bootstrap_summary |
Bootstrapped estimates (if requested) |
See https://www.gnu.org/licenses/gpl-3.0 for the full license.