| Title: | Compare Nested and Non-Nested Structural Equation Models |
|---|---|
| Description: | A comprehensive package for comparing multiple Structural Equation Models (SEM). Supports both nested and non-nested model comparisons, chi-square difference tests, and extraction of multiple fit indices including AIC, BIC, CFI, TLI, RMSEA, and SRMR. Built on top of the lavaan package for seamless SEM model comparison workflows. |
| Authors: | Your Name [aut, cre] |
| Maintainer: | Your Name <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.2.1 |
| Built: | 2026-05-25 09:08:09 UTC |
| Source: | https://github.com/ssjerf-stack/modelscompete4 |
Package startup message
.onAttach(libname, pkgname).onAttach(libname, pkgname)
libname |
library location |
pkgname |
package name |
Package load
.onLoad(libname, pkgname).onLoad(libname, pkgname)
libname |
library location |
pkgname |
package name |
Perform bootstrap-based comparison of lavaan models
bootstrap_lavaan_comparison( model1, model2, R = 1000, parallel = "no", ncpus = 1 )bootstrap_lavaan_comparison( model1, model2, R = 1000, parallel = "no", ncpus = 1 )
model1 |
First lavaan model |
model2 |
Second lavaan model |
R |
Number of bootstrap replications (default: 1000) |
parallel |
Type of parallel processing (if any) |
ncpus |
Number of CPUs to use for parallel processing |
A list containing bootstrap results
## Not run: library(lavaan) model1 <- 'F1 =~ x1 + x2 + x3' model2 <- 'F1 =~ x1 + x2 + x3 + x4' fit1 <- cfa(model1, data = HolzingerSwineford1939) fit2 <- cfa(model2, data = HolzingerSwineford1939) boot_result <- bootstrap_lavaan_comparison(fit1, fit2, R = 100) print(boot_result) ## End(Not run)## Not run: library(lavaan) model1 <- 'F1 =~ x1 + x2 + x3' model2 <- 'F1 =~ x1 + x2 + x3 + x4' fit1 <- cfa(model1, data = HolzingerSwineford1939) fit2 <- cfa(model2, data = HolzingerSwineford1939) boot_result <- bootstrap_lavaan_comparison(fit1, fit2, R = 100) print(boot_result) ## End(Not run)
Compare Latent Variable Models
compare_latent_models( ..., nested = FALSE, fit_measures = c("chisq", "df", "pvalue", "cfi", "tli", "rmsea", "srmr"), method = "default" )compare_latent_models( ..., nested = FALSE, fit_measures = c("chisq", "df", "pvalue", "cfi", "tli", "rmsea", "srmr"), method = "default" )
... |
lavaan model objects |
nested |
logical, whether models are nested |
fit_measures |
character vector of fit measures to extract |
method |
comparison method |
A latent_comparison object
This is the core function of the modelscompete4 package. It automatically fits a list of SEM models, determines their nesting relationship, and performs the appropriate statistical comparison (chi-square difference test for nested models, Vuong test for non-nested models).
compare_models( model_list, data, estimator = "ML", se = "standard", bootstrap = 1000, parallel = "no", ... )compare_models( model_list, data, estimator = "ML", se = "standard", bootstrap = 1000, parallel = "no", ... )
model_list |
A named list. Each element is a character string specifying the model syntax in lavaan format. |
data |
A data.frame containing the observed variables used in the models. |
estimator |
The estimator to be used (e.g., "ML"). Passed to
|
se |
Type of standard errors. Default is "standard". Use "bootstrap" for bootstrapped standard errors and confidence intervals. |
bootstrap |
Number of bootstrap draws if
|
parallel |
Method for parallel processing for bootstrapping ("multicore", "snow", or "no"). Recommended for large samples. |
... |
Additional arguments passed to
|
An object of class modelscompete4. This is a list
containing:
fit_list: The list of fitted lavaan objects.
fit_table: A data.frame of key fit indices for all
models.
comparison_matrix: A matrix showing pairwise nesting
relationships and test results.
test_results: Detailed results of the statistical
tests performed.
bootstrap_summary: Summary of bootstrapped results if
requested.
Advanced Model Comparison with Latent Variable Support
compare_models_advanced_lv( models, model_names = NULL, model_types = NULL, criteria = c("AIC", "BIC", "CFI", "TLI", "RMSEA", "SRMR"), latent_indicators = NULL, bootstrap = FALSE, n_bootstrap = 1000 )compare_models_advanced_lv( models, model_names = NULL, model_types = NULL, criteria = c("AIC", "BIC", "CFI", "TLI", "RMSEA", "SRMR"), latent_indicators = NULL, bootstrap = FALSE, n_bootstrap = 1000 )
models |
List of model objects (lm, lavaan) |
model_names |
Character vector of model names |
model_types |
Character vector of model types ("lm", "lavaan") |
criteria |
Criteria to calculate |
latent_indicators |
List of latent variable indicators (for lavaan) |
bootstrap |
Logical, whether to perform bootstrapping |
n_bootstrap |
Number of bootstrap replications |
Comparison results with latent variable support
Extract Latent Variable Fit Indices
extract_latent_fit(model)extract_latent_fit(model)
model |
lavaan model object |
Comprehensive fit indices for latent variable model
This function extracts parameters (loadings, variances, etc.) from a lavaan model. P-values are formatted appropriately (e.g., <0.001 for very small values).
extract_latent_parameters( model, type = "loadings", standardized = FALSE, digits = 3, ... )extract_latent_parameters( model, type = "loadings", standardized = FALSE, digits = 3, ... )
model |
A fitted lavaan model. |
type |
Type of parameters to extract: "loadings", "variances", or "all". Default is "loadings". |
standardized |
Logical; if TRUE, returns standardized estimates. Default is FALSE. |
digits |
Number of decimal places for p-value formatting (default=3) |
... |
Additional arguments passed to |
A data frame containing the extracted parameters with formatted p-values.
## Not run: library(lavaan) model <- 'F1 =~ x1 + x2 + x3' fit <- cfa(model, data = HolzingerSwineford1939) extract_latent_parameters(fit, type = "loadings") ## End(Not run)## Not run: library(lavaan) model <- 'F1 =~ x1 + x2 + x3' fit <- cfa(model, data = HolzingerSwineford1939) extract_latent_parameters(fit, type = "loadings") ## End(Not run)
Creates visualization of model comparison results
plot_latent_comparison(x, type = "fit", ...)plot_latent_comparison(x, type = "fit", ...)
x |
An object of class 'modelscompete4' or 'latent_comparison' |
type |
Type of plot: 'fit' for fit indices, 'diff' for differences |
... |
Additional arguments passed to plotting functions |
A ggplot object (if ggplot2 and tidyr are available), otherwise NULL
## Not run: library(lavaan) model1 <- 'F1 =~ x1 + x2 + x3' fit1 <- cfa(model1, data = HolzingerSwineford1939) result <- compare_latent_models(fit1) plot_latent_comparison(result) ## End(Not run)## Not run: library(lavaan) model1 <- 'F1 =~ x1 + x2 + x3' fit1 <- cfa(model1, data = HolzingerSwineford1939) result <- compare_latent_models(fit1) plot_latent_comparison(result) ## End(Not run)
Prints a summary of latent model comparison results.
## S3 method for class 'latent_comparison' print(x, digits = 3, ...) ## S3 method for class 'latent_comparison' print(x, digits = 3, ...)## S3 method for class 'latent_comparison' print(x, digits = 3, ...) ## S3 method for class 'latent_comparison' print(x, digits = 3, ...)
x |
An object of class 'latent_comparison' |
digits |
Number of digits to display (default: 3) |
... |
Additional arguments passed to print method |
Invisibly returns the input object
Print Method for modelscompete4_advanced Objects
## S3 method for class 'modelscompete4_advanced' print(x, ...)## S3 method for class 'modelscompete4_advanced' print(x, ...)
x |
A modelscompete4_advanced object |
... |
Additional arguments passed to print |