getMethod(“ summary”,签名=“ FitDiff”)中的错误 [英] Error in getMethod("summary", signature = "FitDiff")
问题描述
我正在使用 semTools :: compareFit
比较lavaan对象。它抛出了一个非常奇怪的错误消息。
I am comparing lavaan objects using semTools::compareFit
. It is throwing a very strange error message.
我也尝试了以下可重现的示例:
I tried also the following reproducible example:
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
speed =~ x7 + x8 + x9'
fit.A<- cfa(HS.modelA, data = HolzingerSwineford1939)
fit.B<- cfa(HS.modelB, data = HolzingerSwineford1939)
semTools::compareFit(fit.A,fit.B)
它返回:
getMethod( summary,signature = FitDiff)中的错误:未找到函数'summary'和签名FitDiff的方法
Error in getMethod("summary", signature = "FitDiff") : no method found for function 'summary' and signature FitDiff
也,因为代码位于函数内部,但是我想查看屏幕上显示的输出,所以我还包括:
Also, as the code is inside a function, but I would like to see the output printed in the screen, I also included:
result<-semTools::compareFit(fit.A,fit.B)
semTools::saveFile(result, file="",what="summary", tableFormat=FALSE)
这将返回
Length Class Mode
Length Class Mode
1 FitDiff S4
我在第一条错误消息中看到提及与概述
和方法有关的内容...我有一些S3 概述
方法,尝试将其形式化以供个人使用...不确定是否相关...我是否可能弄乱了某些东西?
RStudio安装中的多个项目中都在发生这种情况...给人的印象是它以前工作过...
I see the mention in the first error message something related to summary
and methods... I have some S3 summary
methods, trying to formalize in a package for personal use... not sure if it is related... is it possible I have messed up something?
It is happening in more than one project in a RStudio installation... I have the impression it worked before...
我非常感谢您的帮助。
推荐答案
我在这里报告我如何规避此问题。
I am reporting here how I circumvented the problem.
正在发生的事情的结论在这里:
这确实是S3方法弄乱了S4方法分派的问题。
This is really an issue with S3 methods messing up the S4 method dispatch.
如果我加载 showMethods(summary)
在加载semTools软件包之前,我得到:
If I load showMethods(summary)
before loading the semTools package, I get:
Function "summary":
<not an S4 generic function>)
但是如果我加载 showMethods(summary)
加载后,我得到:
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"
因此,解决方案:
考虑 FitDiff
对象结构,我创建了 summary.FitDiff
(s3方法):
Considering the FitDiff
object structure, I created a summary.FitDiff
(s3 method):
summary.FitDiff<-function(object){
print(object@nested)
return(object@fit)
}
,并且此摘要
方法与 FitDiff
对象:
a<-semTools::compareFit(fit.A,fit.B)
summary(a)
这不是一个完美的解决方案,理想的解决方案应该涉及如何指定s3方法而不弄乱所有内容可能是s4方法,但是我对s4方法没有足够的了解...它现在解决了我的问题...
This is not a perfect solution, the ideal solution should involve something with how to specify s3 methods without messing up all possible s4method, but I have not sufficient knowledge about s4 methods... It solved my problem for now...
这篇关于getMethod(“ summary”,签名=“ FitDiff”)中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!