-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_7.R
More file actions
165 lines (163 loc) · 5.77 KB
/
Chapter_7.R
File metadata and controls
165 lines (163 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
library(ISLR)
names(Wage)
# Polynomial Regression
lm.fit = lm(wage~poly(age,4),data = Wage)
coef(summary(lm.fit))
#Orthogonal polynomials from 1 to degree are generated by default
fit2 <- lm(wage~poly(age,4,raw=TRUE),data = Wage)
coef(summary(fit2))
age_grid = seq(25:45)
fit2a = lm(wage~age + I(age^2)+I(age^3)+I(age^4),data = Wage)
fit2b = lm(wage~cbind(age,age^2,age^3,age^4),data = Wage)
agelims = range(Wage$age)
length(agelims)
agelims[1]
agelims[2]
age.grid = seq(from = 20,to = 80,length = 7)
age.grid
preds2 = predict(fit2,newdata = list(age = age.grid),se = TRUE)
preds= predict(fit,newdata = list(age = age.grid),se = TRUE)
max(abs(preds2$fit-preds$fit))
preds$fit
preds$se.fit
out = cbind(preds$fit-2*preds$se.fit,preds$fit,preds$fit+2*preds$se)
length(Wage$wage)
age.grid = seq(from=18, to=80,length = 3000)
par(mfrow = c(1,1), mar = c(1,1,1,1))
plot(x = Wage$age,y = Wage$wage,xlim = agelims,col = "darkgrey")
lines(age.grid,preds$fit,lwd = 2,col = "blue")
sebands = cbind((preds$fit-2*preds$se.fit),(preds$fit+2*preds$se.fit))
matlines(age.grid,sebands,col= 'red',lty = 3)
fit = lm(wage~age, data = Wage)
fit2 = lm(wage~poly(age,2),data = Wage)
fit3 = lm(wage~poly(age,3),data = Wage)
fit4 = lm(wage~poly(age,4),data = Wage)
fit5 = lm(wage~poly(age,5),data = Wage)
anova(fit,fit2,fit3,fit4,fit5)
class.fit = glm(I(Wage$wage>250)~poly(age,4),data = Wage,family= "binomial")
class.fit
preds = predict(class.fit,newdata = list(age=age.grid),se = TRUE)
names(preds)
pred_probs= exp(preds$fit)/(1+exp(preds$fit))
pred_probs = predict(class.fit, newdata = list(age=age.grid),type= 'response')
se.bands.logit =cbind(pred_probs-2*preds$se.fit,pred_probs+2*preds$se.fit)
se.bands = exp(se.bands.logit)/1+exp(se.bands.logit)
se.bands
range(Wage$wage)
plot(jitter(Wage$age) ,I((Wage$wage >250)/5),xlim = agelims,ylim = c(0,0.2),col = "darkgrey")
lines(age.grid,lwd = 2, col= 'blue')
matlines(age.grid,se.bands,lwd=1,col='red')
length(age.grid)
dim(se.bands)
# To fit a step function we use 'cut'
#'cut' fucntion cuts the range of variable evenly. To give our specific
# break points, we can use the 'breaks' option in the cut function.
range(Wage$age)
cut(Wage$age,4)
unique(cut(Wage$age,4))
(80.1-17.9)/4
17.9+15.55+15.55+15.55
piecewise.regfit = lm(wage~cut(age,4),data = Wage)
summary(piecewise.regfit)
breaks <- seq(from =agelims[1],to= agelims[2],length = 7)
agelims[1]
agelims[2]
?cut
lm.piecewis = lm(wage~cut(age,c(30,40,50,60,70)),data = Wage)
summary(lm.piecewis)
test_age = c(45,55,65,75)
predict(lm.piecewis,newdata = list(age=test_age))
# For regression splines, we use the splines library
# The bs function generates the basis function. By default it generates a
#cubic spline. For basis function, use 'bs' with function 'lm'.
library(splines)
spline.fit = lm(wage~bs(age, knots = c(25,40,60)),data = Wage)
names(summary(spline.fit))
predict(spline.fit, newdata = list(age = c(20,30,50)),se= TRUE)
summary(spline.fit)
age.grid
preds = predict(spline.fit,newdata = list(age = age.grid), se = TRUE)
plot(age.grid,preds$fit,pch = 16)
stderr = cbind(preds$fit-2*preds$se.fit,preds$fit+2*preds$se.fit)
matlines(age.grid,stderr,col = "blue")
#if we do not want to pre-specify knots, we can use df=6 to create knots
#at all 4 quartiles
dim(bs(Wage$age,df = 6))
attr(bs(Wage$age,df=6), "knots")
(bs(Wage$age,df=6))
fit2 = lm(wage~ns(age,df = 4),data = Wage)
summary((fit2))
coef(summary(fit2))
pred2 = predict(fit2,newdata = list(age = age.grid),se = TRUE)
pred2$fit
pred2$se.fit
serr = cbind(pred2$fit-2*pred2$se.fit,pred2$fit+2*pred2$se.fit)
plot(Wage$age,Wage$wage,pch=16,col="darkgrey")
lines(age.grid,pred2$fit,col='blue',lwd = 2)
matlines(age.grid,serr,col='red',led=2)
predict(fit2,newdata = list(age=age.grid),interval = "confidence")
## For a smoothing spline we use the function smooth.spline
fit = smooth.spline(Wage$age,Wage$wage,df= 16)
plot(Wage$age,Wage$wage,xlim = agelims,col = 'darkgrey')
preds = predict(fit,newdata = data.frame(age= age.grid))
fit2 = smooth.spline(Wage$age,Wage$wage,cv = TRUE)
fit2$df
lines(fit,co= "red",lwd =2)
lines(fit2,col= "blue",lwd =2)
legend("topright",legend= c('Df 16','Df 6.8'),c('red','blue'),lwd= 2,cex=0.8)
plot(Wage$age,Wage$wage,xlim= agelims,cex = 0.5, col= "darkgrey")
title('Local Regression')
local.regr = loess(Wage$age,Wage$wage,span=.2)
?loess
local.regr = loess(wage~age , data = Wage , span = 0.2)
summary(local.regr)
names(Wage)
# gam function is a part of the gam library and used to find gam models
install.packages("gam")
library(gam)
gam1= lm(wage~ns(age,5)+ns(year,4)+education,data = Wage[train,],)
gam2 = gam(wage~s(age,4)+year+education, data = Wage[train,])
gam3 = gam(wage~s(age,4)+education,data = Wage[train,])
summary(gam1)
dim(Wage)
train= sample(3000,1500)
test = -train
pred1 = predict(gam1,newdata = Wage[test,])
pred1
actual_y = Wage$wage[test]
length(actual_y)
mse1 = mean((pred1-actual_y)^2)
pred2 = predict(gam2,newdata = Wage[test,])
pred3 = predict(gam3,newdata = Wage[test,])
mse2 = mean((pred2-actual_y)^2)
mse3 = mean((pred3-actual_y)^2)
mse1
mse2
mse3
plot(Wage$age,Wage$wage,col ="darkgrey")
par(mfrow=c(1,3))
plot.gam(gam1,se = TRUE,col = "blue")
plot(gam2,se = TRUE,col = 'red')
plot(gam2,se= TRUE,col= 'blue')
lines(Wage$age[test],gam1$fitted.values,col = 'blue')
length(gam1$fitted.values)
length(train)
length(-train)
test = -train
length(test)
anova(gam1,gam2,gam3)
summary(gam2)
## akima is the package that can be used to plot 2-d figures that use
#smooth splines with local regression
install.packages('akima')
library(akima)
fit.complex = gam(wage~s(age,4)+lo(year,span=0.5),data = Wage)
warnings()
plot(fit.complex)
# To use logistic regression with gam, specify family = 'binomial'
gam.logistic = gam(I(wage>250)~s(age,4)+lo(year,span=0.5+education),data = Wage)
warnings()
plot(gam.logistic)
par(mfrow= c(1,3))
plot(gam.logistic,se = TRUE, col = "green")
table(Wage$education,I(Wage$wage>250))