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())Value
A named list with the following elements:
estimatorCharacter.
"ML","FIML", or"DWLS".seTypeCharacter. SE method, e.g.
"OIM"or"cluster-robust".nobsInteger. Number of observations (or effective N for weighted models).
nFreeParamsInteger. Number of free parameters.
dfInteger. Model degrees of freedom.
chi2Numeric. Chi-square test statistic.
pvalueNumeric. Chi-square p-value.
neg2logLNumeric. \(-2 \ln L\) value.
aicNumeric. Akaike information criterion.
bicNumeric. Bayesian information criterion.
paramNamesCharacter vector. Structural names of all free parameters (e.g.
"f->y1","Var(y1)","y1~1").estimatesNumeric vector. Parameter estimates in the same order as
paramNames. Variance parameters are on the original (not log) scale.seNumeric vector. Standard errors.
zNumeric vector. z-statistics (
estimates / se).stdEstNumeric vector. StdAll standardised estimates.
stdSENumeric vector. Delta-method SEs for
stdEst.groupEstimatesList of numeric vectors (multi-group models).
groupEstimates[[g]]contains the per-group estimates for groupgin the shared parameter space.groupSEsList 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 labels —
b1*var: assign the labelb1to the path.Equality constraints — two params with the same label are equated, or
b1 == b2on a standalone line.Bounds —
b1 > 0.5/b1 < 2.0.Starting values —
start(0.8)*var.Derived parameters —
indirect := a * b(delta-method SE).Definition variables —
data.colname * varon the RHS of=~.Multi-group —
group: colnamedirective plusc(v1, v2)*varper-group annotations.Weighted observations —
weight: colname.Cluster-robust SEs —
cluster: 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")
} # }