Polynomial Regression and Step Function
Polynomial Regression and Step Function
Polynomial Regression
library(ISLR)
attach(Wage)
fit=lm(wage~poly(age,4),data=Wage)
summary(fit)
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
Call:
lm(formula = wage ~ poly(age, 4), data = Wage)
Residuals:
Min
1Q
-98.707 -24.626
Median
-4.993
3Q
Max
15.217 203.693
Coefficients:
fit2=lm(wage~poly(age,4,raw =T),data=Wage)
summary(fit2)
##
##
##
##
##
##
##
##
Call:
lm(formula = wage ~ poly(age, 4, raw = T), data = Wage)
Residuals:
Min
1Q
-98.707 -24.626
Median
-4.993
3Q
Max
15.217 203.693
##
##
##
##
##
##
##
##
##
##
##
##
##
Coefficients:
Pr(>|t|)
0.002180
0.000312
0.006261
0.026398
0.051039
**
***
**
*
.
agelims<-range(age)
age.grid=seq(min(age),max(age))
preds=predict(fit,newdata =list(age=age.grid),se=TRUE) # want standard error
se.bands=cbind(preds$fit+2*preds$se.fit, preds$fit-2*preds$se.fit)
plot(age,wage,xlim=agelims,cex =.5,col="darkgrey",main="Degree-4 Polynomial")
lines(age.grid,preds$fit,lwd=2,col ="red")
matlines(age.grid,se.bands,lwd=2,col ="blue",lty=3)
200
100
50
wage
300
Degree4 Polynomial
20
30
40
50
age
60
70
80
***
**
.
'.' 0.1 ' ' 1
0.20
| | || | | || || || || | | | |
0.10
0.15
0.05
0.00
||| ||| ||| ||| |||| |||| |||||| |||| |||| |||||| |||
||| |||||| |||||| |||| |||| |||| |||| |||| |||| |||||| |||| |||| |||| |||||| |||| |||||| |||| |||| |||| |||||| |||||| |||||| |||||| |||| |||| |||| |||| |||| |||| |||| |||| |||| |||| |||||| ||| |||||| ||| ||| ||| || || ||| ||| ||| || || || | || |
20
30
40
50
60
70
age
Step fucnction
fit=lm(wage~cut(age,4),data=Wage)
preds<-predict(fit,newdata=list(age=age.grid),se=T)
se.bands=cbind(preds$fit+2*preds$se.fit,preds$fit-2*preds$se.fit)
plot(age,wage,xlim=agelims,cex =.5,col="darkgrey",main="Degree-4 Polynomial")
lines(age.grid,preds$fit,lwd=2,col="red")
matlines(age.grid,se.bands,lwd=2,col="blue",lty=3)
||
80
200
50
100
wage
300
Degree4 Polynomial
20
30
40
50
60
70
age
80
0.20
|| | | || | | || || | | || |
0.10
0.15
| || | || | | || ||| || || | ||| || || || || | || | ||
0.05
0.00
||| ||| ||| ||| |||| |||| |||||| |||||| |||| |||||| |||||| |||||| |||| |||| |||
||| |||||| |||||| |||| |||||| |||||| |||| |||| |||| |||| |||| |||| |||||| |||| |||| |||||| |||||| |||| |||| |||| |||| |||| |||| |||| |||| |||| |||| |||| |||| |||| |||| ||||| ||| |||| |||| ||| || || ||| ||| || ||| || | || |
20
30
40
50
60
70
||
80
age
Reference:
James, Gareth, et al. An introduction to statistical learning. New
York: springer, 2013.