问题
I'm going through the "Introduction to Statistical Learning with Applications in R" (ISLR), and I'm stuck on a part on page 295, the lab on Generalized Additive Models. When I run the following code I get an error Error in plot.gam(gam1, se = TRUE, col = "red") : could not find function "plot.gam"
.
library(ISLR)
gam1 = lm(wage ~ ns(year, 4) + ns(age, 5) + education, data=Wage)
par(mfrow=c(1,3))
plot.gam(gam1, se=TRUE, col="red")
The book says that plot.gam
should be part of the general plot
function, so why can't R find it? Am I supposed to be doing something differently? I tried unsuccessfully to re-download the plot library with install.packages('plot', repos='http://cran.us.r-project.org')
.
This confuses me because the book says this:
The generic plot() function recognizes that gam2 is an object of class gam, andinvokestheappropriateplot.gam()method.Conveniently,eventhough plot.gam() gam1 is not of class gam but rather of class lm, we can still use plot.gam() on it. Figure 7.11 was produced using the following expression:
plot.gam(gam1, se=TRUE, col="red")
回答1:
Use plot.Gam
not plot.gam
.
Software updates, but the book has not kept up. Checking the change log for the gam package, we can see that the case was changed in early 2018:
2018-02-06 Trevor Hastie version 1.15 * major change class "gam" to "Gam" to avoid conflict with mgcv (grr!)
来源:https://stackoverflow.com/questions/51158531/could-not-find-function-plot-gam