Skip to content
lf-araujoPublic

About

Bridge to fastsem GPU backend for structural equation modelling (SSM and genome wide SEM/PRS)

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

fastsemR

📚 Escolha o idioma · 选择语言 · Choose language
🇧🇷 Português · 🇨🇳 中文 · 🇬🇧 English

Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public. R-CMD-check pkgdown Codecov test coverage License: MIT

fastsemR is an R interface to fastsem, a compiled Nim SEM engine that supports:

  • ML (full-data maximum likelihood) with analytical gradient and optional GPU acceleration
  • FIML (full-information ML for missing data and definition variables)
  • DWLS / WLSMV (diagonally weighted least squares for ordinal indicators)
  • Multi-group models with configural or constrained parameters
  • Equality constraints, parameter bounds, and derived parameters (delta-method SEs)
  • Cluster-robust standard errors
  • Standardised estimates (StdAll) with SEs via the delta method

The package translates umx / OpenMx RAM model objects into fastsem lavaan syntax, fits the model, and injects estimates back into the model object so that summary(), omxGetParameters(), and umxCompare() continue to work.


Installation

# Install from GitHub (requires devtools)
devtools::install_github("lf-araujo/fastsemR")

The compiled shared library is downloaded automatically from GitHub Releases the first time the package is loaded. To force a re-download (e.g. after a fastsem update):

library(fastsemR)
fastsem_update()

If you have built the library yourself from the fastsem source tree (nimble buildRnim), point the package to your local build:

fastsem_load("~/fastsem/libfastsem_r.so")

Example: mediation

Decompose the effect of engine displacement (disp) on fuel economy (mpg) through vehicle weight (wt). Specify the model in umx (or OpenMx) as usual, then let fastsem fit it: run_fastsem() returns the same MxModel, so every umx/OpenMx tool keeps working — including algebras such as the indirect effect ab, with delta-method SEs.

library(fastsemR)
library(umx)

df <- as.data.frame(scale(mtcars[, c("mpg", "disp", "wt")]))

m <- umxRAM("Mediation",
  umxPath("disp", to = "wt",  labels = "a"),
  umxPath("wt",   to = "mpg", labels = "b"),
  umxPath("disp", to = "mpg", labels = "c_prime"),
  umxPath(var   = c("disp", "wt", "mpg")),
  umxPath(means = c("disp", "wt", "mpg")),
  mxAlgebra(a * b, name = "ab"),
  data = df, autoRun = FALSE
)

m_fit <- run_fastsem(m)
umxSummary(m_fit)        # Algebra 'ab' = -0.483 CI95[-0.808, -0.158]
mxSE(ab, m_fit)          # 0.166, identical to mxRun()

The same works from lavaan: build the model with do.fit = FALSE and run_fastsem() returns a fitted lavaan object.

library(lavaan)
fit <- run_fastsem(sem("wt ~ a*disp\nmpg ~ b*wt + c*disp\nab := a*b",
                       data = df, do.fit = FALSE))
summary(fit)

run_fastsem() checks its own work: OpenMx / lavaan re-evaluate the model at the fastsem estimates, and a −2lnL that does not match the engine's is reported.

Contributing

Contributions are welcome. Please open an issue at https://github.com/lf-araujo/fastsemR/issues for bug reports, feature requests, or questions about the R interface. For issues specific to the underlying Nim engine (numerical results, GPU kernels, performance), file at https://github.com/lf-araujo/fastsem/issues.

Pull requests targeting main should pass R CMD check with no errors or warnings; the GitHub Actions workflow runs the same checks on Linux, macOS, and Windows.


License

fastsemR is released under the MIT License. See LICENSE for details.

About

Bridge to fastsem GPU backend for structural equation modelling (SSM and genome wide SEM/PRS)

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages