michaelhallquist / MplusAutomation

The MplusAutomation package leverages the flexibility of the R language to automate latent variable model estimation and interpretation using Mplus, a powerful latent variable modeling program developed by Muthen and Muthen (www.statmodel.com). Specifically, MplusAutomation provides routines for creating related groups of models, running batches of models, and extracting and tabulating model parameters and fit statistics.
84 stars 46 forks source link

Passing new usevariables when updating MplusObject #83

Closed franciscowilhelm closed 4 years ago

franciscowilhelm commented 5 years ago

How can I update the usevariables part of an R object using update function for mplusObject?

I tried it the following way:

m_reg_auto <- map(scalelist, function(y) {

  # create strings
  definevar <-str_c("'", "CENTER", "x", "(GROUPMEAN);", "'", sep = " ")
  modelvar <- str_c("'",y, "ON", "x;","'", sep  = " ")

  # update template
  body <- update(m_reg_template,
  DEFINE = as.formula(paste("~ ",definevar)),
  MODEL = as.formula(paste("~ . + ", modelvar)),
  usevariables = c("id", y, "csm_se"))

  mplusModeler(body, str_c("m_reg_", y, ".dat"), run = TRUE)
 })

However, the usevariables part of the update is not incorporated. Instead, R throws this suggestion:

R variables selected automatically as any variable name that occurs in the MODEL, VARIABLE, or DEFINE section. If any issues, suggest explicitly specifying USEVARIABLES. A starting point may be: USEVARIABLES = id crq_xpl_env csm_se;

franciscowilhelm commented 5 years ago

I can update manually outside the update function through body[["usevariables"]] <- (varnames), but is it possible to update inside the update function?

JWiley commented 5 years ago

@franciscowilhelm

This is a bug caused by the default behavior of trying to guess names running amuck. For now, a work around is:


m1 <- mplusObject(
  MODEL = "mpg ON cyl;",
  rdata = mtcars,
  usevariables = c("mpg", "cyl"),
  autov = FALSE)

m2 <- update(m1,
  MODEL = ~ . + "mpg ON am;",
  usevariables = c("mpg", "cyl", "am", "vs"))

As a quick note, if you are running a variety of models on the same dataset, there is computational efficiency in specifying all the variables you will use to usevariables so that R only has to write the dataset once. That looks like this:


m1 <- mplusObject(
  VARIABLE = "USEVARIABLES = mpg cyl;",
  MODEL = "mpg ON cyl;",
  rdata = mtcars,
  usevariables = c("mpg", "cyl", "am", "vs"),
  autov = FALSE)

m2 <- update(m1,
  VARIABLE = ~ "USEVARIABLES = mpg cyl am;", 
  MODEL = ~ . + "mpg ON am;")

I'm a bit swamped at the moment, but in a week or so I should get a proper fix out (i.e., update will not automatically guess variables regardless of autov flag if usevariables are explicitly passed.

franciscowilhelm commented 5 years ago

Thanks for the heads up! Had not realized that you can use the distinction between Mplus model command USEVARIABLES and usevariables this way. Looking forward to the update.