run_comparison: Run the Full CFA / ESEM / B-ESEM Comparison Pipeline

View source: R/pipeline.R

run_comparisonR Documentation

Run the Full CFA / ESEM / B-ESEM Comparison Pipeline

Description

Takes a model specification from specify_model and automatically fits CFA, ESEM, and B-ESEM in R, optionally runs all three in Mplus, and returns a comparison table plus all fitted objects.

Usage

run_comparison(
  spec,
  mplus_folder = NULL,
  mplus_command = "Mplus",
  run_alignment = TRUE,
  group_equal = NULL,
  n_starts = 30L
)

Arguments

spec

An esem_spec object from specify_model.

mplus_folder

Character. Path to a folder for Mplus files. If NULL (default), Mplus models are skipped.

mplus_command

Character. Path or command used to invoke Mplus. Default "Mplus" (assumes it is on PATH). Requires MplusAutomation.

run_alignment

Logical. Run alignment check? Default TRUE.

group_equal

Character vector of lavaan equality constraints (e.g. "loadings" for metric, c("loadings","intercepts") for scalar invariance).

n_starts

Integer. Random rotation starts for the B-ESEM rotation search. Default 30L (matches Mplus).

Value

An object of class "esem_comparison_pipeline" containing:

spec

The original model specification.

fit_cfa

lavaan CFA fit object.

fit_esem

esem_fit object.

fit_besem

besem_fit object.

alignment

alignment_check result (if run).

comparison_table

Data frame of fit indices.

mplus_results

List of Mplus readModels results (if run).

Examples

data("HolzingerSwineford1939", package = "lavaan")

spec <- specify_model(
  Visual  = c("x1", "x2", "x3"),
  Textual = c("x4", "x5", "x6"),
  Speed   = c("x7", "x8", "x9"),
  data    = HolzingerSwineford1939,
  label   = "Holzinger-Swineford"
)


# Fit CFA, ESEM, and B-ESEM in R and build the comparison table
results <- run_comparison(spec, n_starts = 5L)
print(results)

# Access individual fits
summary(results$fit_esem, fit.measures = TRUE, standardized = TRUE)
std_loadings(results$fit_besem, suppress = 0.10)
factor_correlations(results$fit_esem)


## Not run: 
# Also run all three models in Mplus and compare side by side.
# Requires a licensed Mplus installation reachable via `mplus_command`.
results <- run_comparison(spec, mplus_folder = tempfile("mplus_"))
print(results)

## End(Not run)


bifactory documentation built on Oct. 28, 2026, 1:06 a.m.