Fit a umx / OpenMx RAM model with fastsem and return the updated model.
Source:R/fastsem.R
run_fastsem.RdThe primary high-level function for users who work with umxRAM() models.
It:
Translates the model to lavaan syntax via
umx_to_lavaan().Extracts raw data from
model@data@observed.Fits the model with
fastsem_fit().Writes estimates back into the model matrices and populates
model@outputso thatsummary(),omxGetParameters(), andumxCompare()work as ifmxRun()had been called.
Arguments
- model
An
MxModelas returned byumxRAM()(or a baremxModelcontainer holding submodels for multi-group use). The model must have raw (row-level) data inmodel@data@observed.- control
Optional named list passed to the optimizer. Recognized fields:
maxIter(int),tol(gradient-norm tolerance),ftol(objective-change tolerance),tryHard(extra retry rounds on non-convergence; each round multipliesmaxIterby 3 and dividestolby 10). Any field absent or0/0.0uses the core default.- engine
One of
"auto","ram","lavaan". For multi-group models,"auto"selects the RAM engine. For single-group models, the lavaan engine is always used (RAM single-group is not yet wired in the R wrapper).
Value
The same MxModel object with:
- Matrix values updated
A, S, and M matrices hold the fastsem point estimates.
@outputpopulated$estimate,$standardErrors,$fit(-2lnL),$Chi,$ChiDoF, plus$.fastsem_*slots for the raw fastsem result list.@.wasRunset toTRUEMakes
summary()andumxCompare()work.
Multi-group models
When model@submodels is non-empty, run_fastsem() fits the joint
multi-group model and injects per-group estimates back into each submodel.
Two engines are available (selected via engine):
"ram"(default for multi-group) sends the OpenMx RAM matrices (A, S, optionally M) directly to fastsem's Nim core viafitSemFromMatricesMultigroupR. This bypasses the lavaan-syntax round-trip and honors cross-group fixed covariances exactly (e.g. cross-twin A=1.0 in MZ vs 0.5 in DZ)."lavaan"stacks data with afastsem_grpindicator, builds a combined lavaan syntax withc()per-group annotations, and fits viafastsem_fit(). Use this when the RAM engine errors on a model that uses features it does not yet support (definition variables, ordinal thresholds, configural cross-group different-label free parameters). Parameters that carry the same OpenMx label in all submodels are equated; others are group-specific.
See also
umx_to_lavaan() for syntax inspection;
umx_to_fastsem() for the raw result list without inject-back;
fastsem_fit() for fitting from raw lavaan syntax.
Examples
if (FALSE) { # \dontrun{
library(umx)
# ── CFA ──────────────────────────────────────────────────────────────────
df <- as.data.frame(scale(iris[, c(1, 3, 4)]))
names(df) <- c("sl", "pl", "pw")
m <- umxRAM("CFA",
umxPath(from = "g", to = c("sl", "pl", "pw")),
umxPath(v1m0 = "g"),
umxPath(var = c("sl", "pl", "pw")),
data = df, autoRun = FALSE
)
m_fit <- run_fastsem(m)
summary(m_fit)
omxGetParameters(m_fit)
# ── Multi-group CFA ───────────────────────────────────────────────────────
data(HolzingerSwineford1939, package = "lavaan")
hs <- HolzingerSwineford1939
mg_p <- umxRAM("mg_p",
umxPath(from = "g", to = c("x1","x2","x3")),
umxPath(v1m0 = "g"), umxPath(var = c("x1","x2","x3")),
umxPath(means = c("x1","x2","x3")),
data = hs[hs$school == "Pasteur", c("x1","x2","x3")]
)
mg_gw <- umxRAM("mg_gw",
umxPath(from = "g", to = c("x1","x2","x3")),
umxPath(v1m0 = "g"), umxPath(var = c("x1","x2","x3")),
umxPath(means = c("x1","x2","x3")),
data = hs[hs$school == "Grant-White", c("x1","x2","x3")]
)
r <- run_fastsem(mxModel("HS_mg", mg_p, mg_gw))
omxGetParameters(r@submodels$mg_p)
} # }