gam和mgcv之间的R包冲突?

gam和mgcv之间的R包冲突?,r,conflict,gam,mgcv,R,Conflict,Gam,Mgcv,在R中分离软件包不是一种好的做法(请参见?detach),但由于某些原因,我必须在软件包gam和mgcv之间切换。一旦附加和分离了mgcv(并卸载了名称空间中的所有依赖项!),gam的函数会产生一些奇怪的错误(请原谅术语)。看起来,即使之前卸载了一个步骤-mgcv和friends又回到了名称空间,函数分派也出现了错误。以前有人有过同样的问题吗 # fresh session t.s1 <- search() t.lN1 <- loadedNamespaces() # some du

R
中分离软件包不是一种好的做法(请参见
?detach
),但由于某些原因,我必须在软件包
gam
mgcv
之间切换。一旦附加和分离了
mgcv
(并卸载了名称空间中的所有依赖项!),
gam
的函数会产生一些奇怪的错误(请原谅术语)。看起来,即使之前卸载了一个步骤-
mgcv
和friends又回到了名称空间,函数分派也出现了错误。以前有人有过同样的问题吗

# 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()

我使用的是最新版本的gam(1.09)和mgcv(1.7-28)。任何提示都将不胜感激

正如您所预料的,问题在于gam和mgcv软件包都为“gam”对象安装了S3方法。 但是,如文件中所述:

将不会删除命名空间中已注册的S3方法

因此,在您的情况下,很容易看出这是您问题的原因:

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
库(gam)
#安装gam::print.summary.gam
相同(getS3method('print','summary.gam'),gam:::print.summary.gam)
[1] 真的
图书馆(mgcv)
#安装mgcv::print.summary.gam
相同(getS3method('print','summary.gam'),mgcv::print.summary.gam)
[1] 真的
#在卸载名称空间之前保存指针
mgcv_psgam
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