factor_scores: Extract Latent Factor Scores

View source: R/factor_scores.R

factor_scoresR Documentation

Extract Latent Factor Scores

Description

Returns a data frame of estimated latent factor scores, one column per factor and one row per observation. Works with any esem_fit, plain lavaan object, or esem_invariance result.

Usage

factor_scores(
  x,
  method = c("regression", "bartlett"),
  level = "auto",
  align = NULL,
  dCFI_cutoff = -0.01,
  ...
)

Arguments

x

An esem_fit, lavaan S4, or esem_invariance object.

method

Character. Estimation method: "regression" (default, BLUP – minimises MSE, scores are correlated when factors are correlated) or "bartlett" (unbiased, correct factor variance). For WLSMV B-ESEM models with orthogonal rotation both methods return identical scores (\Phi = I).

level

Character. Only used when x is an esem_invariance object. "auto" (default) selects the most constrained fitted invariance level whose own transition \DeltaCFI \ge -0.010, including "varcov" and "means" when esem_invariance() was run with through. Override with "configural", "weak", "strong", "strict", "varcov" or "means". "auto" skips a level whose reported solution is inadmissible (a negative variance or a non-positive-definite matrix, recorded in inv$notes) even when its \DeltaCFI passes. Silently ignored for plain fit objects.

align

Optional alignment of the score columns. NULL (default) returns scores in the model's native rotation orientation – which can sign-flip or column-permute across reruns or solvers (lavaan vs Mplus) for B-ESEM target rotation. Supply one of:

  • "canonical" – deterministic rule (columns sorted by \Sigma\lambda^2 descending, sign-flipped so the largest |\lambda| per column is positive). No external reference; reproducible across reruns.

  • "group1" – multi-group only; rotate each group's scores to match group 1's loading orientation.

  • a numeric matrix (single-group) or list of matrices (multi-group) of reference standardized loadings (rownames = items, colnames = factors). Useful for aligning to an external reference such as Mplus output via extract_mplus_loadings().

  • another fit object (esem_fit, esem_invariance) – aligns to that fit's loadings.

Internally calls align_loadings() to compute the per-group rotation Q, then rotates the score matrix by Q. Note: under "canonical" columns may be permuted, in which case the column names retain their original ordering – treat them as positional after alignment. Not supported for besem_fit_ordered fits produced by besem_ordered() with custom WLSMV polychoric scoring.

dCFI_cutoff

Numeric. The \DeltaCFI threshold used by level = "auto" to accept an invariance level (a level qualifies when its transition \DeltaCFI \ge dCFI_cutoff). Defaults to -0.010 (Cheung & Rensvold, 2002). Only used when x is an esem_invariance object and level = "auto".

...

Reserved for future use.

Details

For multi-group ordered B-ESEM scoring, a single pooled polychoric correlation matrix is applied to every group. A warning is issued because scores for non-reference groups may be biased when their correlation structures differ. If the polychoric correlation matrix or the score-information matrix is singular, factor_scores() raises an informative error identifying the singular matrix rather than exposing a bare solve() failure.

Value

A data.frame with one column per latent factor (named by the model's factor labels) and one row per observation. A group column is prepended automatically for multi-group models. Rows for missing observations contain NA in all factor columns.

Examples

data("HolzingerSwineford1939", package = "lavaan")
d <- HolzingerSwineford1939[, paste0("x", 1:9)]


# Single fit
fit    <- esem(d, nfactors = 3)
scores <- factor_scores(fit)
head(scores)

# Merge back to original data
d_with_scores <- cbind(d, factor_scores(fit))

# Bartlett method
scores_b <- factor_scores(fit, method = "bartlett")



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