I'm trying to use gam in MGCV to automatically select my level of smoothness using cross validation or some equivalent method for my generalized additive model. reading around online it looks like REML will get the job done (although any advice on interpretation would be appreciated) and i'm just playing with data from the ISLR library, specifically "Wage". here are my calls:

myWageGam= gam(wage~s(year ,4)+s(age ,5)+education ,data=Wage)
myWageGam1= mgcv::gam(wage~s(year ,4)+s(age ,5)+education ,data=Wage, method = "REML")

this is what i'm doing, but although the first function call (i think from the gam library) works the second doesn't and I get the following error: Error in as.matrix(x) : object 'year' not found

any idea what going on and if REML is the best way to get an estimated test error? Thanks!


closed as off-topic by Michael Chernick, mdewey, kjetil b halvorsen, jbowman, Peter Flom - Reinstate Monica Jan 16 '18 at 12:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Michael Chernick, mdewey, kjetil b halvorsen, jbowman, Peter Flom - Reinstate Monica
If this question can be reworded to fit the rules in the help center, please edit the question.


Don't load the gam and mgcv packages at the same time; they mask so many functions of each other that stuff breaks, as you observed.

Your second model works OK if you only load mgcv, and if you name the second argument: mgcv::gam() expects: s(year, k = 4) because the first argument to s() is ...:

myWageGam1 <- mgcv::gam(wage ~ s(year, k = 4) + s(age, k = 5) + education, 
                        data = Wage, method = "REML")

Not the answer you're looking for? Browse other questions tagged or ask your own question.