Error when using the define command

381 views
Skip to first unread message

Tim Welch

unread,
Apr 8, 2021, 12:48:23 PM4/8/21
to MplusAutomation
Hi,

I have a (hopefully) basic question. I want to define variables to use for an analysis but am struggling with the proper syntax. Here is a simple example using the HolzingerSwineford1939 data from the lavaan package to create three composite variables (a,b,c).  

library(MplusAutomation)
library(lavaan)
 script1 = mplusObject(
  TITLE = "Example #1 - Factor Model;",
  MODEL = "f1 by a b c;",
  usevariables = c("a", "b", "c"),
  DEFINE = "a=sum(x1-x3); b=sum(x4-x6); c=sum(x7-x9);",
  rdata = HolzingerSwineford1939)

fit <- mplusModeler(script1, modelout = "deleteme.inp", run=1)

Script1 runs successfully, but when I attempt to run the "fit" script I receive the following error message:

Error in `[.data.frame`(df, , keepCols, drop = FALSE) : 
  undefined columns selected

I'm guessing this must be a basic syntax error, but I couldn't find any examples online of using the define command. 

Any suggestions on where my code goes off? 

Thank you so much!    


Joshua Wiley

unread,
Apr 10, 2021, 12:20:00 AM4/10/21
to Tim Welch, MplusAutomation
Hi Tim,

Here is a working example:

library(MplusAutomation)
library(lavaan)
 script1 = mplusObject(
  TITLE = "Example #1 - Factor Model;",
  MODEL = "f1 by a b c;",
  VARIABLE = "USEVARIABLES = a b c;",
  usevariables = paste0("x", 1:9),
  DEFINE = "a=sum(x1-x3); b=sum(x4-x6); c=sum(x7-x9);",
  rdata = HolzingerSwineford1939)
fit <- mplusModeler(script1, modelout = "script1.inp", run = TRUE)

The usevariables argument to mplusObject is to tell R which variables from the dataset to use. Then you must tell Mplus in the VARIABLE section which variables to use in the model, which are only those you defined.

Cheers,

Josh


--
You received this message because you are subscribed to the Google Groups "MplusAutomation" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mplusautomati...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mplusautomation/6295e9d4-019c-4992-ade5-607931c5eeb1n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages