0

this question is related to my previous question.

I figured out that question has something with S3 summary methods I am programming to a class I am defining. I understand summary is already a S3 method. And I was able to specify it without setting:

summary<-function(data,...){
   UseMethod("summary",data)
}

And for what I know, it is working, for my summary methods.

But, when I try to use semTools::compareFit command, I realized that possibly I messed up something.

library(lavaan)
library(semTools)
data("HolzingerSwineford1939",package="lavaan")
HS.modelA <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9'

HS.modelB<- ' visual  =~ x1 + x2
              textual =~ x4 + x5 + x6 + x7 + x8 + x9'

fit.A<- cfa(HS.modelA, data = HolzingerSwineford1939)           
fit.B<- cfa(HS.modelB, data = HolzingerSwineford1939)
a<-semTools::compareFit(fit.A,fit.B)
show(a)

returns:

Error in getMethod("summary", signature = "FitDiff") : 
  no method found for function 'summary' and signature FitDiff
 

That's weird. And I am unable to understand it clearly, as I am new in S3 S4 stuffs... But, taking a look on the semTools::CompareFit function definition (here), I see it has something to some summary method (if I understood correctly, a S4 method)

and I believe that's also why

 result<-semTools::compareFit(fit.A,fit.B)
semTools::saveFile(result, file="",what="summary", tableFormat=FALSE)

returns

Length Class Mode

  1 FitDiff      S4

from which I suppose the issue has something with the S3method messing up the S4 method dispatch...

The problem appears just when I load the package in which I am joining the functions (which includes the summary method mentioned above).

I am aware of the way to set the default methods like...

summary.default<-function(data, ...){
   base::summary(data)
}

Which I understood is not required as the summary function is already a generic function. And in fact, is not the solution as the problem is with CompareFit, not a summary method...

I am aware of the following related questions here and here.

Also, we do not see any summary function when we try ?semTools::summary... From the semTools::CompareFit documentation, the summary method is internal (or whatever... not sure)... or an alias for something else (I am very confused about..)

How can we make such arrangement for an internal method of an already available package?... Or how to avoid this kind of problem? or how to specify S3 method adequately to avoid or circumvent this kind of problem?

I appreciate any help. I am really very confused with this S3 and S4 method stuffs...


Update

This is really an issue with S3 methods messing up the S4 method dispatch.

If I load showMethods(summary) before loading the semTools package, I get:

Function "summary":
 <not an S4 generic function>)

But if I load showMethods(summary) after loading it, I get:

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

I am circumventing the problem for now assessing the output object structure and formalizing a specific summary.FitDiff object to extract the informations... which solves in my current problem, but not for the potential of messing up any other s4 method...

The ideal solution should involve something with how to specify s3 methods without messing up all possible s4method.

hamagust
  • 728
  • 2
  • 10
  • 28
  • First, this must be the summary called by semTools: `semTools:::summary.lavaan.mi`. You need to use `:::` instead of `::` to see it because it's not an exported function – Edo Jul 25 '20 at 01:38
  • Second, can you create a reproducible example where I can get your same error? Because your code works fine to me. – Edo Jul 25 '20 at 01:41
  • Hi, thank you very much for your effort here... Unfortunately I am not able to know what exactly is causing that to make it reproducible... – hamagust Jul 25 '20 at 01:44
  • Let's dig into it.. clean your environment and restart R with Ctrl+Shift+F10. Then run your code till that error and launch `sessionInfo()`. Add the result to your question, please – Edo Jul 25 '20 at 01:48
  • I just imagine it's something with the s3 method... Now as you taught me about detaching the libraries in the other question, I will try to see how to reproduce it... thank you very much. – hamagust Jul 25 '20 at 01:51
  • I was aware of semTools::summary.lavaan.mi, but I thought it was not related, as the object class appears to be FitDiff, not lavaan.mi... but I really do not understand nothing about S4 methods... So, can it be solved specifying a summary.FitDiff method directing to summary.lavaan.mi? – hamagust Jul 25 '20 at 01:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218555/discussion-between-edo-and-hamagust). – Edo Jul 25 '20 at 01:59

0 Answers0