simsem / semTools

Useful tools for structural equation modeling
74 stars 36 forks source link

showMethods(summary) does not produce the right object for "FitDiff" #116

Open AlexGareau opened 1 year ago

AlexGareau commented 1 year ago

Here is a stackoverflow post about a specific problem with compareFit() working with summary()

When installing semTools it works fine. Whenever I open Rstudio the next day and run the script the summary function does not produce the comparefit() output

I think the stackoverflow post summarise the problem better https://stackoverflow.com/questions/63065696/error-in-getmethodsummary-signature-fitdiff/74574698#74574698

TDJorgensen commented 1 year ago

I'm not sure whether the apparent similarity of your problem to the SO post is superficial or is the same underlying cause. Since that SO post (in 2020), semTools updated (https://github.com/simsem/semTools/commit/5e71ff29b1622996e43b7fc2a8147dc7ea6021fe) the summary() method to fix a different problem (I inherited the very messy compareFit() function and its methods from the original maintainer, and am trying not to tear it down and start from scratch). It might be the same cause, but I am just as eluded by S4 method dispatch issues as the SO poster was.

This is the same type of problem that frequently occurs with R not finding my S4 methods for lavaan.mi objects. I am preparing to deprecate runMI() and move all that functionality to a separate lavaan.mi package, where I do some handling and naming of S4 methods. If those seem to solve the problems that people have frequently encountered, then I will implement analogous changes to the remaining S4 methods in semTools.

AlexGareau commented 1 year ago

I understand your intention and plan of action for the development of semTools. Here is the showMethods(summary) output when the summary() is used on a compareFit() object which is not able to produce the output. Well it does when it is not inherited from: object = "ANY"

Function: summary (package base)
object="ANY"
object="DBIObject"
object="diagonalMatrix"
object="FitDiff"
    (inherited from: object="ANY")
object="lavaan"
object="lavaanList"
object="mle"
object="sparseMatrix"
object="stanfit"

I was able to correct the issue by loading and unloading package lavaan and semTools. If lavaan is loaded then semTools::comparefit() is used it brings this inheritance issue.

> detach(package:semTools, unload = T)
> detach(package:lavaan, unload = T)
> semTools::compareFit(meq.list) %>% summary()
 Length   Class    Mode 
      1 FitDiff      S4 
> library(lavaan)
This is lavaan 0.6-12
lavaan is FREE software! Please report any bugs.
> semTools::compareFit(meq.list) %>% summary()
 Length   Class    Mode 
      1 FitDiff      S4 
> showMethods(summary)
Function: summary (package base)
object="ANY"
object="DBIObject"
object="diagonalMatrix"
object="FitDiff"
    (inherited from: object="ANY")
object="lavaan"
object="lavaanList"
object="mle"
object="sparseMatrix"

If I were to just load the semTools, the errors never appear. I am not sure I can understand why it is happening, but I can tell you that if I fully load the lavaan and semTools, the issue does not appear. I hope this can help finding the issue.

lazaricl commented 6 months ago

This has been a while since this post was published. However, I am running into a similar S4 class problem: When using measEq.syntax and then printing the syntax, assigning it as character to be used as models syntax in lavaan::cfa() I am getting the following S4 related- error:

library(semTools) syntax.config <- measEq.syntax(configural.model = mod.cat, data = df_mi_sex , ordered = c("n_psomatic", "n_pworries", "n_punhappy", "n_pclingy", "n_pafraid", "n_ptantrum", "n_qobeys", "n_pfights", "n_plies", "n_psteals", "n_prestles", "n_pfidgety", "n_pdistrac", "n_qreflect", "n_qattends", "n_ploner", "n_qfriend", "n_qpopular", "n_pbullied", "n_poldbest", "n_pconsid", "n_pshares", "n_pcaring", "n_pkind", "n_phelpout"), group = "sex_incl_trans",group.label=c("1", "2"), estimator ="DWLS", test = "scaled.shifted", se = "robust", parameterization = "theta", ID.fac = "std.lv", ID.cat = "Wu.Estabrook.2016", group.equal = "thresholds", return.fit = TRUE) cat(as.character(syntax.config)) Error in as.character.default(syntax.config) : no method for coercing this S4 class to a vector

mod.config <- as.character(syntax.config) Error in as.character.default(syntax.config) : no method for coercing this S4 class to a vector

fit.config <- cfa(syntax.config, data = df_mi_sex, ordered = c("n_psomatic", "n_pworries", "n_punhappy", "n_pclingy", "n_pafraid", "n_ptantrum", "n_qobeys", "n_pfights", "n_plies", "n_psteals", "n_prestles", "n_pfidgety", "n_pdistrac", "n_qreflect", "n_qattends", "n_ploner", "n_qfriend", "n_qpopular", "n_pbullied", "n_poldbest", "n_pconsid", "n_pshares", "n_pcaring", "n_pkind", "n_phelpout"), group = "sex", group.label=c("1", "2"), estimator ="DWLS", test = "scaled.shifted", se = "robust", parameterization = "theta") Does not run ofc.

showMethods(summary) Function: summary (package base) object="ANY" object="BootMiss" object="diagonalMatrix" object="EFA" object="FitDiff" object="lavaan" object="lavaan.mi" object="lavaanList" object="measEq.syntax" object="mle" object="Net" object="permuteMeasEq" object="sparseMatrix" object="twostage"

I have tried the mentioned trouble shooting (here and at SO) including detaching/unloading both lavaan and semTools as well as uninstalling both packages and reinstalling them, loading semTools without prior loading of lavaan. Still, I am running into the same issue and cannot proceed. I am happy for any hints on how to fix it - the syntax worked at a prior date (jan 2024) but I had the S4 issue in November with a different data set but also in relation to S4 error and measEq.syntax and summary() .

Happy to give more information if needed :) I hope this cross-post is rightly placed. I am also happy to open a new issue if necessary.