cjvanlissa / tidySEM

54 stars 7 forks source link

X Mplus is not available. #31

Closed cbratt closed 2 years ago

cbratt commented 2 years ago

I am about to try tidySEM, but tidySEM seems not to find Mplus:

> library(tidySEM)
> library(lavaan)
> library(MplusAutomation)
> df <- HolzingerSwineford1939
> names(df)[grepl("^x", names(df))] <- c("vis_1", "vis_2", "vis_3", "tex_1", 
                         "tex_2", "tex_3", "spe_1", "spe_2", "spe_3")
> model <- tidy_sem(df)
> model <- model |> measurement()
> model |> estimate_lavaan()
lavaan 0.6-9 ended normally after 35 iterations

Estimator                                         ML
Optimization method                           NLMINB
Number of model parameters                        21

Number of observations                           301

Model Test User Model:

    Test statistic                                85.306
Degrees of freedom                                24
P-value (Chi-square)                           0.000
> model |> estimate_mplus()
X  Mplus is not available.
NULL
> 

I'm on a Mac. I have no problem running Mplus from R with MplusAutomation. Mplus is installed under the following path: /Applications/Mplus/Mplus Editor.app

I have tried restarting R after installing tidySEM.

cjvanlissa commented 2 years ago

Dear @cbratt, this is an interesting case, because tidySEM relies completely on MplusAutomation here. What happens when you run mplusAvailable(TRUE) == 0 ?

cbratt commented 2 years ago

Thank you so much for your speedy reply!

> mplusAvailable(TRUE) == 0
[1] FALSE

> mplus_model <- mplusObject(
    +   VARIABLE = "usevariables = vis_1 vis_2;",
    +   MODEL = "vis_1 WITH vis_2;",
    +   rdata = df
    + )
> mplus_test <- mplusModeler(mplus_model, modelout = "model.inp", run = 1L)
> summary(mplus_test)
Estimated using ML 
Number of obs: 301, number of (free) parameters: 5 

Model: Chi2(df = 0) = 0, p = 0 
Baseline model: Chi2(df = 1) = 27.863, p = 0 

Fit Indices: 

CFI = 1, TLI = 1, SRMR = 0 
RMSEA = 0, 90% CI [0, 0], p < .05 = 0 
AIC = 1880.066, BIC = 1898.602 
> 
cjvanlissa commented 2 years ago

Interesting :) Looks like the bug is in MplusAutomation::mplusAvailable(). It might be worth opening an issue on their github.

One final question: What value does this return? print(MplusAutomation::mplusAvailable())

cbratt commented 2 years ago

Thank you for looking into this!

> print(MplusAutomation::mplusAvailable())
[1] 1
> 

I have done as you suggested: https://github.com/michaelhallquist/MplusAutomation/issues/148

cbratt commented 2 years ago

Just to let you know: I downgraded MplusAutomation to 0.7-3. The error persists, which makes me wonder... If this bug is not new, it should have been detected by others long ago. Could it be that my setup causes the error?

cjvanlissa commented 2 years ago

Yes; I'm quite sure that it is "caused" by your setup, but it's still a bug ;) That function should do what it needs to do on any system. Is there anything special about your setup that you think might cause it? Linux? Server session? No admin rights? Etc

cbratt commented 2 years ago

All is local, on my own machine (macOS).

> version
               _                           
platform       x86_64-apple-darwin17.0     
arch           x86_64                      
os             darwin17.0                  
system         x86_64, darwin17.0          
status                                     
major          4                           
minor          1.0                         
year           2021                        
month          05                          
day            18                          
svn rev        80317                        ##
language       R                           
version.string R version 4.1.0 (2021-05-18)
nickname       Camp Pontanezen             

RStudio Version 1.4.1717, but that shouldn't be relevant, I guess.

Very thankful for the work you do for the community, and of course now, in trying to solve this specific case.

================================================<-

Added a few hours later: I just tried on a different computer (MacBook), with a similar setup. Same problem there.

cbratt commented 2 years ago

Thanks to @JWiley, tidySEM can now find Mplus. But @cjvanlissa, I don't think tidySEM does more than find Mplus?

> library(tidySEM)
> library(lavaan)
> library(MplusAutomation)
> df <- HolzingerSwineford1939
> names(df)[grepl("^x", names(df))] <- c("vis_1", "vis_2", "vis_3", "tex_1", 
+                                                                                "tex_2", "tex_3", "spe_1", "spe_2", "spe_3")
> model <- tidy_sem(df)
> model <- model |> measurement()
> estimate_lavaan(model)
lavaan 0.6-9 ended normally after 35 iterations

  Estimator                                         ML
  Optimization method                           NLMINB
  Number of model parameters                        21

  Number of observations                           301

Model Test User Model:

  Test statistic                                85.306
  Degrees of freedom                                24
  P-value (Chi-square)                           0.000
> estimate_mplus(model)
v  Mplus is available.
> 

(I checked the working directory. No output there, it seems. And evidently, no output in the R console apart from celebrating that Mplus can be detected.)

@cjvanlissa, I fully understand your attitude towards continued support for Mplus (expressed in response to another post of mine), Mplus is neither open source nor free. I would definitely prefer lavaan, Mplus is expensive in the long run and thus exclusive, and also cumbersome to use (although MplusAutomation helps a lot). But Mplus is still what many of us need to be able to develop advanced latent variable modelling, and Mplus is robust and remarkably fast. If you with tidySEM can make OpenMX more accessible, then that would of course be an interesting alternative. I still think you might reach more people with support for Mplus. That could be a dilemma.

Unfortunately, I'm still stuck with tidySEM even after the new development version of Mplus.

cjvanlissa commented 2 years ago

Have you tried assigning the result, e.g., estimate_mplus(model) -> res; summary(res)?

I wouldn't say that people "need" Mplus to develop advanced lv modeling, but people definitely use it. I would like to contribute to changing that.

Regarding "robust", I'm not sure how you define this. Regarding fast, it definitely isn't. Have you tried Hamiltonian Monte Carlo in STAN? I'm uncomfortable with the fact that Mplus includes many proprietary solutions that are not sufficiently documented or tested. What if there is a mistake? E.g., I already noticed that Mplus often does not arrive at the global optimum for mixture modeling. That's not great, considering mixture modeling is one of the reasons I hear most often for using Mplus.

cjvanlissa commented 2 years ago

I see that tidySEM still has summary and print methods for MplusAutomation objects; this is a remnant from when these were not yet incorporated in MplusAutomation. Removing these will show summary(res) by default when the object is printed.

cbratt commented 2 years ago

@cjvanlissa You are so right!

Following up the code above (factor model for HolzingerSwineford1939):

  > res <- estimate_mplus(model)
  v  Mplus is available.
  > summary(res)
  Estimated using ML 
  Number of obs: 301, number of (free) parameters: 30 

  Model: Chi2(df = 24) = 85.306, p = 0 
  Baseline model: Chi2(df = 36) = 918.852, p = 0 

  Fit Indices: 

  CFI = 0.931, TLI = 0.896, SRMR = 0.06 
  RMSEA = 0.092, 90% CI [0.071, 0.114], p < .05 = 0.001 
  AIC = 7535.49, BIC = 7646.703 

Thanks a lot for your assistance.