Multiple polynomial regression versus GAM How does using GAM differ from using multiple polynomial regression? They seem to produce the same result. Below I run a polynomial regression using lm() with
$wage=\beta_0 + \beta_{age}\cdot age+\beta_{age^2}\cdot age^2 + \beta_{year}\cdot year+\beta_{year^2}\cdot year^2 $
Then I do the same with gam() and compare the 2 models using anova(). The RSS for both is identical. Same using splines of $year$ and $age$.
If I am trying to explore the degree of nonlinearity between response and predictors, how would I do it differently using gam()? Is gam() meant to make it easier to compare a quadratic model to a cubic one? Thank you.
library(ISLR) # for Wage dataset
library(gam)

lm.wage.poly <- lm(wage ~ poly(age, 2, raw = TRUE) + poly(year, 2, raw = TRUE), data = Wage)
lm.wage.gam <- gam(wage ~ poly(age, 2, raw = TRUE) + poly(year, 2, raw = TRUE), data = Wage)
anova(lm.wage.poly, lm.wage.gam)
## Model 1: wage ~ poly(age, 2, raw = TRUE) + poly(year, 2, raw = TRUE)
## Model 2: wage ~ poly(age, 2, raw = TRUE) + poly(year, 2, raw = TRUE)

##   Res.Df     RSS Df Sum of Sq F Pr(>F)

## 1   2995 4771710                  
## 2   2995 4771710  0         0 

library(splines)
# Using cubic splines for age and year
lm.wage.poly.spline <- lm(wage ~ bs(age) + bs(year), data = Wage)
lm.wage.gam.spline <- gam(wage ~ bs(age) + bs(year), data = Wage)
anova(lm.wage.poly.spline, lm.wage.gam.spline)

## Model 1: wage ~ bs(age) + bs(year)

## Model 2: wage ~ bs(age) + bs(year)

##   Res.Df     RSS Df Sum of Sq F Pr(>F)
## 1   2993 4754108    
## 2   2993 4754108  0         0  

 A: If we use lo and s basis expansion in gam, we will see the difference. Here is the demo:
Using no basis expansion and polynomial, gam and lm are the same.
> library(gam)
> anova(lm(mpg~wt,data=mtcars),gam(mpg~wt,data=mtcars))
Analysis of Variance Table

Model 1: mpg ~ wt
Model 2: mpg ~ wt
  Res.Df    RSS Df  Sum of Sq F Pr(>F)
1     30 278.32                       
2     30 278.32  0 5.6843e-14         
> anova(lm(mpg~poly(wt,2,raw = T),data=mtcars),gam(mpg~poly(wt,2,raw = T),data=mtcars))
Analysis of Variance Table

Model 1: mpg ~ poly(wt, 2, raw = T)
Model 2: mpg ~ poly(wt, 2, raw = T)
  Res.Df    RSS Df Sum of Sq F Pr(>F)
1     29 203.75                      
2     29 203.75  0         0         

Using lo and s expansion, gam and lm are different.
> anova(lm(mpg~lo(wt),data=mtcars), gam(mpg~lo(wt),data=mtcars))
Analysis of Variance Table

Model 1: mpg ~ lo(wt)
Model 2: mpg ~ lo(wt)
  Res.Df    RSS  Df Sum of Sq      F   Pr(>F)   
1   30.0 278.32                                 
2   26.6 177.47 3.4    100.86 4.4462 0.009422 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> anova(lm(mpg~s(wt),data=mtcars), gam(mpg~s(wt),data=mtcars))
Analysis of Variance Table

Model 1: mpg ~ s(wt)
Model 2: mpg ~ s(wt)
  Res.Df    RSS Df Sum of Sq      F  Pr(>F)  
1     30 278.32                              
2     27 190.00  3    88.321 4.1837 0.01483 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

