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

mplusModeler misses ; in syntax and ignores check=TRUE #189

Open andrew0302 opened 1 year ago

andrew0302 commented 1 year ago

I have a simple CFA syntax that uses 44 variables. My syntax will run if I use a small number of variables.

`mplus_model <- mplusObject( TITLE = "model;", MISSING = ".;", VARIABLE = " USEVARIABLES = z1-z44; IDVARIABLE = row_number;",

MODEL = "

m1 BY z1-z4; m2 BY z5-z44;

m1@1; m2@1;

m1 WITH m2;

",

OUTPUT = "standardized cinterval(hpd);",

SAVEDATA = "FILE IS machine_fscores.dat; Save = fscores;",

rdata = df )`

It looks like somehow, the variables are named accross multiple lines, but semicolons are not being added (see attached).

When I call mplusModeler I get an error that there are lines missing semicolon characters, even though 'check = TRUE' is in the function call:

fit <- mplusModeler(mplus_model, modelout = "VI_data_collection/_data/mplus_output/machine_model.inp", hashfilename = FALSE, check = TRUE, run=TRUE, writeData = 'always')

Is there a workaround? The only thing I can think of at the moment is to generate the .inp file, import into R as a text object, run parseMplus on it, save it as an .inp file again, and then use runModels on that adjusted file.

machine_model.txt

cjvanlissa commented 1 year ago

Maybe you have a semicolon too much in your code? I think TITLE does not take a semicolon?

andrew0302 commented 1 year ago

Thanks for the response! You're right it doesn't, but after a bit more investigation, I think it's the way the call in mplusModeller is being translated to text.

The .inp file shows:

NAMES = z1 z2 z3 z4 z5 z6 z7 z8 z9 z10 z11 z12 z13 z14 z15 z16 z17 z18 z19 z20 z21 z22 z23 z24 z25 z26 z27 z28 z29 z30 z31 z32 z33 z34 z35 z36 z37 z38 z39 z40 z41 z42 z43 z44 row_number;

It looks like it's missing semicolons at the end of each line. When I open it manually within the mplus editor, it runs if I adjust it to:

NAMES = z1-z44 row_number;

I was under the impression setting check=TRUE would add the semicolons, but it's not catching the issue here. Maybe I just have a large number of variables.

cjvanlissa commented 1 year ago

@michaelhallquist a well-placed strwrap() before writing the input text to file would do the trick. Mplus stops parsing after a number of characters, I think 80.