2

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)

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

Below is the output:

> 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

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

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.

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
TimW
  • 65
  • 5
  • Try loading `gam` package before `mgcv`. See if that makes a difference. – Shree Jun 02 '19 at 18:22
  • I cannot reproduce this issue. Can you: (1) try this code in a fresh R session, and if you still receive the error message, (2) edit your question to include the output from `sessionInfo()`? – duckmayr Jun 02 '19 at 18:28
  • @duckmayr thank you for comments. See update. Also as Shree guessed the order of loading makes a difference. – TimW Jun 02 '19 at 18:56
  • @TimW Thanks for the update. "part of the deal of loading mgcv is that you can no longer use certain functions in gam?" The answer to that question is no. When a function is masked by another, you can still access it using the namespace operator (`::`). If two packages have a function of the same name, the function from the **last** loaded package is used if no namespace is specified. That's why you get a warning about masking -- it helps you keep track of which package's function will be called if you don't specify via `gam::` or `mgcv::` – duckmayr Jun 02 '19 at 18:59
  • @duckmayr I am still confused. When I load gam before mgcv, I get an error message when I run the line "gam.m3 <- gam::gam(wage~s(year,4)+s(age,5)+education,data=Wage) " but the error goes away if I detach mgcv. So I am not sure why that isn't working? – TimW Jun 02 '19 at 19:03
  • That does not happen for me. I may have misread your update, because I thought you meant that you no longer got your error running the code in a fresh R session. If you still get the error in a fresh R session, can you edit your question to include the output from `sessionInfo()`? – duckmayr Jun 02 '19 at 19:07
  • Sorry for the confusion but to try to clarify - (1) You are correct that I no longer get the error message when I run the same code in a fresh R session, (2) However, I *do* get the same error message when I replace the order of loading gam and mgcv, and (3) I'm not sure why I get that error message, since when I run that "gam.m3" line I am using "gam::gam". (also to answer why it's important even though the original code runs - I still have the more general problem of not being able to use both "gam" functions even when I use the (::) operator. – TimW Jun 02 '19 at 19:17
  • @TimW Thanks for the clarification, that helped me figure it out. Answer below. – duckmayr Jun 02 '19 at 19:26
  • @TimW I would accept Roland's answer below -- it is a much better solution to your problem. I will therefore be deleting my answer later. – duckmayr Jun 07 '19 at 15:34

1 Answers1

3

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

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
Roland
  • 127,288
  • 10
  • 191
  • 288