tbates / umx

Making Structural Equation Modeling (SEM) in R quick & powerful
https://tbates.github.io/
45 stars 17 forks source link

lavaan-style syntax for OpenMx models #219

Closed jhorzek closed 1 month ago

jhorzek commented 1 year ago

Hi, I have recently spent some time on a side project, where I implemented a lavaan-like syntax for OpenMx in the R package mxsem. I've been made aware that there is already a somewhat similar syntax parser in umx (see here) and also one in metaSEM. As mxsem is a small side project, I am not sure if it should remain a separate package or if integrating it in a bigger project would be better. I initially thought it could be interesting for OpenMx itself, but based on the feedback umx may be the better fit. What would be your preference? Would you be interested in adding the mxsem functions to umx or would you prefer waiting for mxsem to be well tested as a separate package and then import it if necessary?

Demonstration

mxsem implements a custom syntax and does not rely on lavaan for the model parsing. I made this decision to easily extend the syntax in order to unlock some of the unique features of OpenMx while still keeping the syntax familiar to lavaan users. For instance, inspired by metaSEM, the current mxsem syntax allows for simple algebras and definition variables:

library(mxsem)
set.seed(9820)
dataset <- simulate_moderated_nonlinear_factor_analysis(N = 100)

model <- "
  # loadings
     xi =~ x1 + x2 + x3
     eta =~ y1 + y2 + y3
  # regression
     eta ~ {a0 + a1*data.k} * xi
"

mxsem(model = model,
      data = dataset) |>
  mxTryHard() |>
  summary()

Best, Jannik

lf-araujo commented 1 year ago

Definition variables can be added to the model with the below syntax, mixing with the lavaan syntax. Current implementation already accepts some algebra, if I am not mistaken.

m1 = umxRAM(data = mtcars, "#modelName
      mpg ~ wt + disp", umxPath(defn = "cyl"))
umxSummary(m1)

I think that it would be nice to expand the current code for parsing the lavaan syntax. Last word should be @tbates s, though.

jhorzek commented 1 year ago

Very interesting! Short update on mxsem: I've uploaded the package on CRAN because I wanted to share it easily with colleagues in a project I am currently working on. So, if you want to check out what mxsem can do, you can now install from CRAN.