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.