R gam和mgcv之间的程序包冲突? [英] R Package conflict between gam and mgcv?
问题描述
在R
中分离软件包不是一个好习惯(请参阅?detach
),但是由于某些原因,我必须在软件包gam
和mgcv
之间切换.一旦mgcv
被附加和分离(并且卸载了命名空间中的所有依赖项!),gam
的函数就会产生一些奇怪的错误(请原谅术语).似乎-尽管先卸载了-mgcv
和朋友回到了命名空间中,但函数分派还是出错了.以前有人遇到过同样的问题吗?
Detaching packages in R
isnt good practice (see ?detach
), but for some reasons I have to switch between the packages gam
and mgcv
. Once mgcv
was attached and detached (and all the dependencies in the namespace unloaded!), functions of gam
produce some strange errors (please forgive the terminology). It seems that - even though unloaded one step before - mgcv
and friends are back in the namespace and function dispatching goes wrong. Does anyone had the same problem before?
# fresh session
t.s1 <- search()
t.lN1 <- loadedNamespaces()
# some dummy data
data <-data.frame(is.exc=sample(x=c(0,1),size=100,replace=T),
year=1:100,doy=rep(1:5,times=20))
t.dof <- 2
# everything works fine
library(gam)
t.gam1 <- gam::gam(is.exc~s(year,df=t.dof)+s(doy,df=t.dof),data=data,family=poisson)
t.pred1 <- gam::predict.gam(t.gam1,newdata=data,type='terms')
detach('package:gam',unload=T,character.only=T)
detach('package:splines',unload=T,character.only=T)
# compare attached packages and namespaces with fresh session
t.s2 <- search()
t.lN2 <- loadedNamespaces()
identical(t.s1,t.s2)
identical(t.lN1,t.lN2)
# attach and detach mgcv
library(mgcv)
detach('package:mgcv',unload=T,character.only=T)
unloadNamespace('nlme')
unloadNamespace('Matrix')
unloadNamespace('lattice')
unloadNamespace('grid')
# compare again attached packages and namespaces with fresh session
t.s2 <- search()
t.lN2 <- loadedNamespaces()
identical(t.s1,t.s2)
identical(t.lN1,t.lN2)
# use package gam again and produce errors
library(gam)
t.gam2 <- gam::gam(is.exc~s(year,df=t.dof)+s(doy,df=t.dof),data=data,family=poisson)
gam::summary.gam(t.gam2)
t.pred2 <- gam::predict.gam(t.gam2,newdata=data,type='terms')
# why do we have mgcv and friends in the namespace?
loadedNamespaces()
我的会话信息为(新会话):
My session infos are (fresh session):
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.0.2
,我使用gam(1.09)和mgcv(1.7-28)的最新版本.任何提示表示赞赏!
and I use the latest versions of gam (1.09) and mgcv (1.7-28). Any hints are appreciated!
推荐答案
正如您所想的那样,问题是gam和mgcv软件包都为"gam"对象安装了S3方法. 但是,如?detach文档中所述:
The problem, as you ANTICIPATED, is that both gam and mgcv packages install S3 methods for "gam" objects. But, as stated in the ?detach documentation:
从名称空间注册的S3方法将不会被删除.
registered S3 methods from the namespace will not be removed.
因此,在您的情况下,很容易看到这是造成问题的原因:
So in your case it is easy to see that is the cause of your problems:
library(gam)
# installs gam::print.summary.gam
identical(getS3method('print', 'summary.gam'), gam:::print.summary.gam)
[1] TRUE
library(mgcv)
# installs mgcv::print.summary.gam
identical(getS3method('print', 'summary.gam'), mgcv:::print.summary.gam)
[1] TRUE
# save a pointer before unloading namespaces
mgcv_psgam <- mgcv:::print.summary.gam
detach('package:mgcv',unload = TRUE, character.only = TRUE)
# after the detach, the method from mgcv is still installed !!!
identical(getS3method('print', 'summary.gam'), mgcv_psgam)
[1] TRUE
结论:确保您从不加载mgcv
Conclusion: make sure you never load mgcv
这篇关于R gam和mgcv之间的程序包冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!