Skip to contents

The workhorse function of fastsemR. Passes lavaan model syntax and a data matrix to the fastsem compiled engine and returns a named list of fit statistics and parameter estimates.

Usage

fastsem_fit(syntax, data, col_names = NULL, control = list())

Arguments

syntax

Character string of lavaan model syntax.

data

Data frame or numeric matrix. Non-numeric columns are coerced to NA. Row order is preserved.

col_names

Character vector of column names. Defaults to colnames(data). Must include every observed variable named in syntax.

Value

A named list with the following elements:

estimator

Character. "ML", "FIML", or "DWLS".

seType

Character. SE method, e.g. "OIM" or "cluster-robust".

nobs

Integer. Number of observations (or effective N for weighted models).

nFreeParams

Integer. Number of free parameters.

df

Integer. Model degrees of freedom.

chi2

Numeric. Chi-square test statistic.

pvalue

Numeric. Chi-square p-value.

neg2logL

Numeric. \(-2 \ln L\) value.

aic

Numeric. Akaike information criterion.

bic

Numeric. Bayesian information criterion.

paramNames

Character vector. Structural names of all free parameters (e.g. "f->y1", "Var(y1)", "y1~1").

estimates

Numeric vector. Parameter estimates in the same order as paramNames. Variance parameters are on the original (not log) scale.

se

Numeric vector. Standard errors.

z

Numeric vector. z-statistics (estimates / se).

stdEst

Numeric vector. StdAll standardised estimates.

stdSE

Numeric vector. Delta-method SEs for stdEst.

groupEstimates

List of numeric vectors (multi-group models). groupEstimates[[g]] contains the per-group estimates for group g in the shared parameter space.

groupSEs

List of numeric vectors (multi-group models).

Estimators

The estimator is chosen automatically based on the data and model:

  • ML — complete data, no definition variables, no ordinal indicators.

  • FIML — missing data (NA) present, or definition variables used.

  • DWLS / WLSMV — ordinal indicators declared with ordered: or threshold syntax (y | t1 + t2).

Extended lavaan syntax

Beyond standard lavaan, fastsem supports:

  • Parameter labelsb1*var: assign the label b1 to the path.

  • Equality constraints — two params with the same label are equated, or b1 == b2 on a standalone line.

  • Boundsb1 > 0.5 / b1 < 2.0.

  • Starting valuesstart(0.8)*var.

  • Derived parametersindirect := a * b (delta-method SE).

  • Definition variablesdata.colname * var on the RHS of =~.

  • Multi-groupgroup: colname directive plus c(v1, v2)*var per-group annotations.

  • Weighted observationsweight: colname.

  • Cluster-robust SEscluster: colname.

See also

print_fastsem() to display the result; run_fastsem() for the umx/OpenMx bridge.

Examples

if (FALSE) { # \dontrun{
# Single-factor CFA on simulated data
set.seed(1)
n  <- 200
f  <- rnorm(n)
df <- data.frame(y1 = 0.8*f + rnorm(n, sd=0.6),
                 y2 = 0.9*f + rnorm(n, sd=0.5),
                 y3 = 0.7*f + rnorm(n, sd=0.7))

syntax <- "
  f =~ 1*y1 + y2 + y3
  f ~~ f
  y1 ~~ y1;  y2 ~~ y2;  y3 ~~ y3
"
res <- fastsem_fit(syntax, df)
print_fastsem(res)

# Mediation with derived indirect effect
df2 <- as.data.frame(scale(mtcars[, c("mpg","hp","wt")]))
syntax2 <- "
  wt  ~ a*hp
  mpg ~ b*wt + c_prime*hp
  hp ~~ hp;  wt ~~ wt;  mpg ~~ mpg
  hp ~ 1;  wt ~ 1;  mpg ~ 1
  ab := a * b
"
res2 <- fastsem_fit(syntax2, df2)
cat("Indirect hp->wt->mpg:", res2$estimates[res2$paramNames == "ab"],
    "+/-", res2$se[res2$paramNames == "ab"], "\n")
} # }