El alumno deberá realizar los ejercicios 5.3.1, 5.3.2 y 5.3.3 del libro de texto (inicia en página 190). Además deberá realizar el ejercicio 4.6.5 sobre KNN ubicado en la página 163 del libro de texto.
mean((mpg-predict(lm.fit3,Auto))[-train]^2)
[1] 19.2574
5.3.2 LOOCV
cv.error=rep(0,5)
for(i in 1:5){
glm.fit=glm(mpg~poly(horsepower, i), data=Auto)
cv.error[i] = cv.glm(Auto, glm.fit)$delta[1]
}
cv.error
[1] 24.23151 19.24821 19.33498 19.42443 19.03321
5.3.3 k-fold Cross-Validation
set.seed(17)
cv.error.10=rep(0,10)
for(i in 1:10){
glm.fit=glm(mpg~poly(horsepower, i),data=Auto)
cv.error.10[i]=cv.glm(Auto, glm.fit, K=10)$delta
}
number of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement lengthnumber of items to replace is not a multiple of replacement length
cv.error.10
[1] 24.20520 19.18924 19.30662 19.33799 18.87911 19.02103 18.89609 19.71201
[9] 18.95140 19.50196
5.3.4 The Bootstrap
boot(Portfolio, alpha.fn, R=1000)
ORDINARY NONPARAMETRIC BOOTSTRAP
Call:
boot(data = Portfolio, statistic = alpha.fn, R = 1000)
Bootstrap Statistics :
original bias std. error
t1* 0.6596797 0.01211578 0.3073555
summary(lm(mpg~horsepower+I(horsepower^2),data=Auto))$coef
Estimate Std. Error t value Pr(>|t|)
(Intercept) 56.900099702 1.8004268063 31.60367 1.740911e-109
horsepower -0.466189630 0.0311246171 -14.97816 2.289429e-40
I(horsepower^2) 0.001230536 0.0001220759 10.08009 2.196340e-21
4.6.5 KNN
library(class)
library(MASS)
library(ISLR)
names(Smarket)
[1] "Year" "Lag1" "Lag2" "Lag3" "Lag4" "Lag5"
[7] 1250 9
summary(Smarket)
Year Lag1 Lag2 Lag3
Min. :2001 Min. :-4.922000 Min. :-4.922000 Min. :-4.922000
1st Qu.:2002 1st Qu.:-0.639500 1st Qu.:-0.639500 1st Qu.:-0.640000
Median :2003 Median : 0.039000 Median : 0.039000 Median : 0.038500
Mean :2003 Mean : 0.003834 Mean : 0.003919 Mean : 0.001716
3rd Qu.:2004 3rd Qu.: 0.596750 3rd Qu.: 0.596750 3rd Qu.: 0.596750
Max. :2005 Max. : 5.733000 Max. : 5.733000 Max. : 5.733000
Lag4 Lag5 Volume
Min. :-4.922000 Min. :-4.92200 Min. :0.3561
1st Qu.:-0.640000 1st Qu.:-0.64000 1st Qu.:1.2574
Median : 0.038500 Median : 0.03850 Median :1.4229
Mean : 0.001636 Mean : 0.00561 Mean :1.4783
3rd Qu.: 0.596750 3rd Qu.: 0.59700 3rd Qu.:1.6417
Max. : 5.733000 Max. : 5.73300 Max. :3.1525
Today Direction
Min. :-4.922000 Down:602
1st Qu.:-0.639500 Up :648
Median : 0.038500
Mean : 0.003138
3rd Qu.: 0.596750
Max. : 5.733000
attach(Smarket)
The following objects are masked from Smarket (pos = 3):
Direction, Lag1, Lag2, Lag3, Lag4, Lag5, Today, Volume, Year
The following objects are masked from Smarket (pos = 4):
Direction, Lag1, Lag2, Lag3, Lag4, Lag5, Today, Volume, Year
The following objects are masked from Smarket (pos = 5):
Direction, Lag1, Lag2, Lag3, Lag4, Lag5, Today, Volume, Year
train = (Year<2005)
Direction.2005 = Direction[-train]
train.X=cbind(Lag1, Lag2)[train,]
test.X=cbind(Lag1, Lag2)[!train,]
train.Direction = Direction[train]
set.seed(1)
knn.pred=knn(train.X, test.X, train.Direction, k=1)
table(knn.pred, Direction.2005)
Error in table(knn.pred, Direction.2005) :
all arguments must have the same length
table(knn.pred, Direction.2005)
Error in table(knn.pred, Direction.2005) :
object 'Direction.2005' not found