lavaan and Confirmatory Factor Analysis Knowledge Patch
Use this skill when writing, reviewing, or updating R code for confirmatory factor analysis and related structural equation models in lavaan. It is especially relevant for categorical outcomes, robust estimators, multigroup constraints, multilevel models, missing data, sampling weights, effects, and diagnostics.
Before changing a fit:
- Inspect the installed lavaan version and the variables' continuous, ordinal, binary, grouping, cluster, and weight roles.
- Separate estimation choices from test-statistic and standard-error corrections; similarly named robust estimators do not behave identically.
- Check whether a parameter is fixed, group-constrained, or freely estimated before selecting a modification-index or score-test workflow.
- Preserve explicit parser, optimizer, and weighting choices when migrating compatibility-sensitive code.
- Open the topic reference that matches the task and retain exact option, operator, and parameter-table names.
Reference index
| Reference | Topics | | --- | --- | | model-syntax-and-estimation.md | API and parser migration, categorical and robust estimation, instruments, composites, correlations, tuning lists, ESEM targets, and optimization | | groups-multilevel-and-weights.md | Multigroup modifiers and equality constraints, sampling weights, two-level least squares, random slopes, missing-data optimization, and level-specific fit | | inference-effects-and-diagnostics.md | Missing-data auxiliaries, residuals, effects, structured-after-measurement inference, corrected tests, fit indices, prediction, modification diagnostics, and parameter inspection |
Quick reference: compatibility-sensitive changes
Argument and parser migration
- Prefer
snake_casefor user-facing formal arguments. Dotted argument names remain accepted but are deprecated. - Option names accept snake case or capital letters.
- The
opensyntax parser is now the default. Code that depended on the previous parser should choose or test parser behavior explicitly. - The minimum R version is 3.4, and installing lavaan does not require compilation.
fit <- cfa(model, data = dat, sampling_weights = "weight")
Replaced tuning interfaces
estimator,rotation, andbootstrapmay each be a list; use these in place of their separate*.argsarguments.optim_method = "gn"now means Levenberg-Marquardt damped Fisher scoring, and its controls belong ingn_args.- Composite models using
<~have an analytic gradient. Remove the obsoleteoptim_gradient = "numerical"workaround. - For ESEM, assign a named list of target matrices to
rotation_args$targetto provide one target per EFA block.
Fit-index naming
fitMeasures()reports the Maydeu-Olivares et al. GFI under the usual GFI name.- Request
gfi_lisreloragfi_lisrelwhen downstream code intends the previously named GFI or AGFI statistics.
Quick reference: categorical and robust estimation
Categorical endogenous variables
- lavaan supports binary and ordinal endogenous variables, not nominal ones.
- Pass selected variables with
ordered = c(...), or useordered = TRUEfor all endogenous variables. - This automatically selects WLSMV: parameter estimation uses DWLS, while robust standard errors and the mean-and-variance-adjusted test use the full weight matrix.
- ULSMV and PML are alternatives. FIML is not supported for this categorical workflow.
fit <- cfa(model, data = dat, ordered = c("item1", "item2"))
Robust continuous-data variants
MLMuses a Satorra-Bentler test,MLMVSa Satterthwaite mean-and-variance adjustment, andMLMVa scale-shifted adjustment. These three require complete data.MLFcombines first-derivative standard errors with a conventional test.MLRcombines Huber-White standard errors with an asymptotic Yuan-Bentler test. BothMLFandMLRsupport complete or incomplete data.- Less-common choices include distributionally weighted least squares
(
DLS) and pairwise maximum likelihood (PML). - Robust DWLS and ULS variants estimate parameters with the diagonal weight matrix but use the full matrix for standard-error and test corrections.
fit <- cfa(model, data = dat, estimator = "MLR", missing = "ML")
Quick reference: syntax and structural choices
External instruments
Use |~ to declare external instruments and estimator = "IV" to select
MIIV-2SLS.
model <- '
y ~ x
y |~ z1 + z2
'
fit <- sem(model, data = dat, estimator = "IV")
The IV implementation handles multiple groups, categorical data, simple equality constraints, two-stage missing data, user-specified instruments, and Sargan/Hansen overidentification tests.
Model composition and covariance structure
- Pass a character vector directly as
modelto combine reusable or conditional syntax fragments. - A character vector supplied to
correlationrequests a partial-correlation structure and can be combined withfixed_x = TRUE. auto_cov_x = TRUEfreely estimates covariances between latent and observed exogenous variables; its default isFALSE.- Composites support a robust mean structure, higher-order factors,
multilevel models, and the
composites_covoption. - Reduced-bias M-estimation is available as an initial framework for reducing finite-sample bias.
Quick reference: groups, levels, and weights
Multigroup constraints
- In multigroup syntax,
c(...)supplies one value or label per group. - Repeat a label to impose equality. Use
NAat a group position to leave that group's parameter free while another group's value is fixed. group_equalapplies supported classes of equality constraints in bulk;group_partialnames exceptions in model syntax.modindices()only considers parameters fixed to zero. UselavTestScore()to assess release of equality constraints.
fit <- cfa(
model, data = dat, group = "school",
group_equal = c("loadings", "intercepts"),
group_partial = c("visual=~x2", "x7~1")
)
Two-level models
- Two-level models can use DWLS and WLS, including WLSMV for categorical
data; handle exogenous covariates with
fixed_xandconditional_x. rv()adds random slopes to continuous two-level models. Supported paths include ML throughnlminbor EM, MLR standard errors, empirical-Bayes predictions, and complete data ormissing = "ml".- With ML missing-data handling, EM is the default two-level optimizer. It supports SQUAREM or quasi-Newton acceleration and analytic Louis observed information.
- Summaries can show level-specific fit based on partially saturated models.
Request a level through
fitMeasures(fit, level = ...)and control the behavior withfit_by_level.
Sampling weights
- Use
sampling_weights_typeto distinguish design weights from frequency weights. - Weights are normalized within group by default.
- Least-squares estimators use a properly weighted Gamma/NACOV matrix.
Quick reference: missing data and inference
Auxiliary and conditional variables
- Supply missing-data auxiliary variables through
aux. - For continuous single-level models, FIML, two-stage, and robust-two-stage
missing-data handling support
conditional_x = TRUE, including standard errors and the corresponding Gamma/NACOV matrix.
Effects and test statistics
lavEffects()computes total, indirect, and direct effects with delta-method, Monte Carlo, or bootstrap standard errors.- For parameters defined with
:=, usese_def = "mc"for Monte Carlo standard errors and confidence intervals;se_delta_second_orderenables the second-order delta option. - Foldnes-Moss-Gronneberg statistics are available. Select the Hayakawa
variants with
test = "mean.var.adjusted.corrected"ortest = "scaled.shifted.corrected". - With a robust estimator,
lavTestScore()andlavTestWald()report scaled, adjusted, and robust statistics.
Residuals, prediction, and inspection
lavResiduals(output = "text")can display largest-residual tables and supplies elementwise residual standard errors and z-tests. It also supportsconditional_x, multilevel models, and a supplied saturated model viah1.lavPredict()can return standard errors for categorical factor scores and acceptsnewdatafor two-level models.- The
mdistcasewise Mahalanobis-distance diagnostics inlavPredict()andlavInspect()support categorical and ordered data. - With no
what,lavInspect()returns model matrices whose nonzero integers identify free parameters. Usewhat = "start"for starting-value matrices andwhat = "list"for the full parameter table returned byparTable(fit).
Use the references for complete option sets, supported combinations, and diagnostic distinctions before editing production analyses.