jhorzek / lessSEM

lessSEM estimates sparse structural equation models.
https://jhorzek.github.io/lessSEM/
GNU General Public License v2.0
6 stars 1 forks source link

lessSEM with an unfitted lavaan model #356

Closed B260198 closed 11 months ago

B260198 commented 11 months ago

Dear Jannik,

I wanted to ask you if it is possible to specify an unfitted lavaan model in the argument "lavaanModel = " in the lasso function of lessSEM. If I do this like here in the example:

lassoFit <- lasso (lavaanModel = unfittedlavaan, data = data, 
                  regularized = c("deltaa5", "deltaa6", "deltaa7"), 
                  nLambdas = 100,
                  method = "glmnet",
                  control = controlGlmnet(),
                  modifyModel = modifyModel(transformations = transformations)
)

I always get the error message " unused argument (data = data)". Does the lasso function not know the "data = " argument or do I perhaps have to specify the data differently? Or is it simply not possible to enter an unfitted model? I thought maybe it works because lasso refits the model again anyway...

Thank you and best regards, Britta

jhorzek commented 11 months ago

Hi Britta, great question! lessSEM expects that the model was fitted with lavaan. However, what you can do is to not let lavaan optimize the model:

library(lessSEM)

dataset <- simulateExampleData()

lavaanSyntax <- "
f =~ l1*y1 + l2*y2 + l3*y3 + l4*y4 + l5*y5 + 
     l6*y6 + l7*y7 + l8*y8 + l9*y9 + l10*y10 + 
     l11*y11 + l12*y12 + l13*y13 + l14*y14 + l15*y15
f ~~ 1*f
"

lavaanModel <- lavaan::sem(lavaanSyntax,
                           data = dataset,
                           meanstructure = TRUE,
                           std.lv = TRUE,
                           do.fit = FALSE # don't optimize, just set up the model; see ?lavOptions 
                           # for more details
                           )

lsem <- lasso(
  lavaanModel = lavaanModel,
  regularized = paste0("l", 6:15),
  nLambdas = 50)

All arguments of the lasso - function are explained here: https://jhorzek.github.io/lessSEM/reference/lasso.html#arguments.

Best, Jannik