如何在具有相同名称的2个不同库中使用2个不同函数 [英] How to use 2 different functions in 2 different libraries that have the same name

查看:131
本文介绍了如何在具有相同名称的2个不同库中使用2个不同函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试探索mgcv软件包与gam软件包中"gam"功能的工作方式之间的差异.但是,我无法在一个R会话中同时运行这两个gam函数.我以为如果我以mgcv :: gam或gam :: gam开头,它将能够运行正确的功能,但是看起来我必须分离mgcv才能在gam软件包中运行gam函数.

I'm trying to explore the difference in how the "gam" function works in the mgcv package versus the gam package. But, I'm not able to run both gam functions in one R session. I thought if I preface with mgcv::gam or gam::gam it would be able to run the right function, but it looks like I have to detach mgcv in order to run the gam function in the gam package.

library(ISLR)
library(mgcv)
library(gam)

# I get an error message when it runs this
gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)

# No error message when I detach mgcv
detach(package:mgcv)
gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)

有一种方法可以在一个会话中同时运行两个gam函数吗?

Is there a way I can run both gam functions in one session?

下面是输出:

> library(ISLR)
> library(mgcv)
> library(gam)
> #I get an error message when it runs this
> gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
Error in terms.formula(reformulate(term[i])) : 
  invalid model formula in ExtractVars
> #No error message when I detach mgcv
> detach(package:mgcv)
> gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
Warning message:
In model.matrix.default(mt, mf, contrasts) :
  non-list contrasts argument ignored

更新:我通过一个干净的R会话重新运行了这个故事,但情况有所不同.之前,我清除了工作空间,但是没有清除的R会话.现在,如果我以干净的会话运行,则gam.m3模型似乎可以正常工作.但是-如果我在mgcv之前更改库加载和加载gam的顺序,则会遇到相同的错误.在加载gam之后加载mgcv时,我确实收到了以下消息:

Update: I re-ran this with a clean R session and the story is different. Before, I cleared the workspace but did not have a clear R session. Now, if I run with a clean session the gam.m3 model seems to work. BUT - if I change the order of the library load and load gam before mgcv, I get the same error. When mgcv is loaded after gam is loaded, I do get this message:

The following objects are masked from ‘package:gam’:

gam, gam.control, gam.fit, s

所以我想加载mgcv只是部分原因,就是您不能再在gam中使用某些功能了?真烦人.仅供参考,在加载mgcv之后加载gam时,我收到类似的警告消息-某些对象将从'package:mgcv'中屏蔽.

So I guess just part of the deal of loading mgcv is that you can no longer use certain functions in gam? That is annoying. FYI I get the analogous warning message when loading gam after mgcv is loaded - that certain objects will be masked from 'package:mgcv'.

推荐答案

如我对您的其他问题的回答所示,则不能使用gam::s.

As shown in my answer to your other question, you can't use gam::s.

但是,您可以告诉R在gam程序包名称空间中评估调用:

However, you can tell R to evaluate the call in the gam package namespace:

library(ISLR)
library(gam)

fit1 <- gam(wage~s(year,4)+s(age,5)+education,data=Wage)

library(mgcv)

gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage)
#errors

fit2 <- eval(quote(gam(wage~s(year,4)+s(age,5)+education,data=Wage)), 
               envir = asNamespace("gam"))
#works

all.equal(coef(fit1), coef(fit2))
#[1] TRUE

这篇关于如何在具有相同名称的2个不同库中使用2个不同函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆