R plot.gam Error "Error in 1: object $ nsdf: argument of length 0"

I am trying to build a gam object in R which I made with the gam package. I get the same error that was reported in Error at 1: object $ nsdf: argument of length 0 when using plot.gam . However, the solution found, updating to the latest versions (I think), does not work for me. I run R 3.3.1, gam 1.12, and mgcv 1.8.12 (mgcv is a plot.gam function).

Unfortunately, I cannot share the data I work with. However, the following code is pulled straight from p.294 from Intro. for statistical training using R - reproduces the error for me:

library(gam) library(ISLR) # contains the Wage dataset used here gam.mod <- gam(wage ~ s(year, 4) + s(age, 5) + education, data = Wage) plot(gam.mod) 

Does anyone know what is going on here or how to fix it?

Thanks.

+5
source share
2 answers

If you still receive this message, you need to update mgcv and gam package to the latest version. A big change was made for the gam package in February 2018: Could not find the plot.gam function . This means that the GAM installed by the gam package now has the "Gam" class, and even if the mgcv package mgcv loaded, plot will not select mgcv::plot.gam to build it.

However, it is still unsafe to have both packages in an R session. Therefore, the next proposal made in 2016 is still highly recommended.


Sentence

It would be nice to have this toy feature to check if the R session is ok to start the GAM analysis.

 GAM_status <- function () { if (all(c("gam", "mgcv") %in% .packages())) print("Not OK") else print("OK") } 

nsdf is a strict freedom number , a term used exclusively in mgcv . As you mentioned: mgcv is the plot.gam function.

The problem is that you have gam and mgcv , two incompatible packages in your R session at the same time. You fit your gam.mod with gam::gam , but then build the model with mgcv::plot.gam .

Note that what is usually true when using :: will lose effect here. Usually, when two packages have some functions between masks, the :: means is a means of protection. But, for mgcv and gam , this is completely impossible. Therefore, I suggest that if you use gam , never touch mgcv in your R session, and vice versa.

So, I am starting a new R session and doing the following, everything is in order!

 library(gam) library(ISLR) # contains the Wage dataset used here gam.mod <- gam(wage ~ s(year, 4) + s(age, 5) + education, data = Wage) par(mfrow = c(2,2)); plot(gam.mod) 

enter image description here


Thanks for your reply. I never downloaded mgcv , I just assumed this was a gam . I started a new R session and the code you provided worked. I found that this car library is actually causing the same problem.

mgcv and gam are independent of each other, but since mgcv is more popular than gam , many packages are dependent on mgcv , for example, car :

 car: Companion to Applied Regression Functions and Datasets to Accompany J. Fox and S. Weisberg, An R Companion to Applied Regression, Second Edition, Sage, 2011. Version: 2.1-3 Depends: R (≥ 3.2.0) Imports: MASS, mgcv, nnet, pbkrtest (≥ 0.4-4), quantreg, grDevices, utils, stats, graphics 

Note that in the Import field, the library(car) will load these packages at the same time.

+4
source

My mgcv version is 1.8-28, but I still have this question. Try converting all char variables to factors and gam() run gam() or bam() . It works for me.

0
source

All Articles