Exercise 10
This question should be answered using the Weekly data set, which is part of the ISLR package. This data is similar in nature to the Smarket data from this chapter’s lab, except that it contains 1,089 weekly returns for 21 years, from the beginning of 1990 to the end of 2010.
attach(Weekly)
summary(Weekly)
## Year Lag1 Lag2 Lag3
## Min. :1990 Min. :-18.1950 Min. :-18.1950 Min. :-18.1950
## 1st Qu.:1995 1st Qu.: -1.1540 1st Qu.: -1.1540 1st Qu.: -1.1580
## Median :2000 Median : 0.2410 Median : 0.2410 Median : 0.2410
## Mean :2000 Mean : 0.1506 Mean : 0.1511 Mean : 0.1472
## 3rd Qu.:2005 3rd Qu.: 1.4050 3rd Qu.: 1.4090 3rd Qu.: 1.4090
## Max. :2010 Max. : 12.0260 Max. : 12.0260 Max. : 12.0260
## Lag4 Lag5 Volume Today
## Min. :-18.1950 Min. :-18.1950 Min. :0.08747 Min. :-18.1950
## 1st Qu.: -1.1580 1st Qu.: -1.1660 1st Qu.:0.33202 1st Qu.: -1.1540
## Median : 0.2380 Median : 0.2340 Median :1.00268 Median : 0.2410
## Mean : 0.1458 Mean : 0.1399 Mean :1.57462 Mean : 0.1499
## 3rd Qu.: 1.4090 3rd Qu.: 1.4050 3rd Qu.:2.05373 3rd Qu.: 1.4050
## Max. : 12.0260 Max. : 12.0260 Max. :9.32821 Max. : 12.0260
## Direction
## Down:484
## Up :605
##
##
##
##
Weekly data.cor(Weekly[,-9])
## Year Lag1 Lag2 Lag3 Lag4
## Year 1.00000000 -0.032289274 -0.03339001 -0.03000649 -0.031127923
## Lag1 -0.03228927 1.000000000 -0.07485305 0.05863568 -0.071273876
## Lag2 -0.03339001 -0.074853051 1.00000000 -0.07572091 0.058381535
## Lag3 -0.03000649 0.058635682 -0.07572091 1.00000000 -0.075395865
## Lag4 -0.03112792 -0.071273876 0.05838153 -0.07539587 1.000000000
## Lag5 -0.03051910 -0.008183096 -0.07249948 0.06065717 -0.075675027
## Volume 0.84194162 -0.064951313 -0.08551314 -0.06928771 -0.061074617
## Today -0.03245989 -0.075031842 0.05916672 -0.07124364 -0.007825873
## Lag5 Volume Today
## Year -0.030519101 0.84194162 -0.032459894
## Lag1 -0.008183096 -0.06495131 -0.075031842
## Lag2 -0.072499482 -0.08551314 0.059166717
## Lag3 0.060657175 -0.06928771 -0.071243639
## Lag4 -0.075675027 -0.06107462 -0.007825873
## Lag5 1.000000000 -0.05851741 0.011012698
## Volume -0.058517414 1.00000000 -0.033077783
## Today 0.011012698 -0.03307778 1.000000000
pairs(Weekly)
Direction as the response and the five lag variables plus Volume as predictors.glm.fit = glm(Direction ~ Lag1+Lag2+Lag3+Lag4+Lag5+Volume, data=Weekly, family=binomial)
summary(glm.fit)
##
## Call:
## glm(formula = Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 +
## Volume, family = binomial, data = Weekly)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.6949 -1.2565 0.9913 1.0849 1.4579
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.26686 0.08593 3.106 0.0019 **
## Lag1 -0.04127 0.02641 -1.563 0.1181
## Lag2 0.05844 0.02686 2.175 0.0296 *
## Lag3 -0.01606 0.02666 -0.602 0.5469
## Lag4 -0.02779 0.02646 -1.050 0.2937
## Lag5 -0.01447 0.02638 -0.549 0.5833
## Volume -0.02274 0.03690 -0.616 0.5377
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1496.2 on 1088 degrees of freedom
## Residual deviance: 1486.4 on 1082 degrees of freedom
## AIC: 1500.4
##
## Number of Fisher Scoring iterations: 4
glm.probs = predict(glm.fit, type = "response")
glm.preds = rep("Down", 1089)
glm.preds[glm.probs > .5]="Up"
table(glm.preds, Direction)
## Direction
## glm.preds Down Up
## Down 54 48
## Up 430 557
(54+557)/1089
## [1] 0.5610652
54/(54+430)
## [1] 0.1115702
557/(48+557)
## [1] 0.9206612
Lag2 as the only predictor. Compute the confusion matrix and the overall fraction of correct predictions for the held out data (that is, the data from 2009 and 2010).train = Year < 2009
test = Weekly[!train, ]
glm.fit = glm(Direction ~ Lag2, data=Weekly, family=binomial, subset=train)
glm.probs = predict(glm.fit, test, type = "response")
glm.preds = rep("Down", nrow(test))
glm.preds[glm.probs > .5]="Up"
table(glm.preds, test$Direction)
##
## glm.preds Down Up
## Down 9 5
## Up 34 56
mean(glm.preds == test$Direction)
## [1] 0.625
lda.fit = lda(Direction ~ Lag2, data=Weekly, subset=train)
lda.preds = predict(lda.fit, test)$class
table(lda.preds, test$Direction)
##
## lda.preds Down Up
## Down 9 5
## Up 34 56
mean(lda.preds == test$Direction)
## [1] 0.625
qda.fit = qda(Direction ~ Lag2, data=Weekly, subset=train)
qda.preds = predict(qda.fit, test)$class
table(qda.preds, test$Direction)
##
## qda.preds Down Up
## Down 0 0
## Up 43 61
mean(qda.preds == test$Direction)
## [1] 0.5865385
train.X = as.matrix(Lag2[train])
test.X = as.matrix(Lag2[!train])
train.Direction = Direction[train]
set.seed(1)
knn.preds = knn(train.X, test.X, train.Direction, k=1)
table(knn.preds, test$Direction)
##
## knn.preds Down Up
## Down 21 30
## Up 22 31
mean(knn.preds == test$Direction)
## [1] 0.5
glm.fit = glm(Direction ~ Lag1+Lag2, data=Weekly, family=binomial, subset=train)
glm.probs = predict(glm.fit, test, type = "response")
glm.preds = rep("Down", nrow(test))
glm.preds[glm.probs > .5]="Up"
table(glm.preds, test$Direction)
##
## glm.preds Down Up
## Down 7 8
## Up 36 53
mean(glm.preds == test$Direction)
## [1] 0.5769231
lda.fit = lda(Direction ~ Lag1*Lag2, data=Weekly, subset=train)
lda.preds = predict(lda.fit, test)$class
table(lda.preds, test$Direction)
##
## lda.preds Down Up
## Down 7 8
## Up 36 53
mean(lda.preds == test$Direction)
## [1] 0.5769231
qda.fit = qda(Direction ~ I(Lag1^2) + I(Lag2^2), data=Weekly, subset=train)
qda.preds = predict(qda.fit, test)$class
table(qda.preds, test$Direction)
##
## qda.preds Down Up
## Down 7 7
## Up 36 54
mean(qda.preds == test$Direction)
## [1] 0.5865385
knn.preds = knn(train.X, test.X, train.Direction, k=10)
table(knn.preds, test$Direction)
##
## knn.preds Down Up
## Down 17 18
## Up 26 43
mean(knn.preds == test$Direction)
## [1] 0.5769231
Exercise 11
Auto data set. Create a binary variable, mpg01, that contains a 1 if mpg contains a value above its median, and a 0 if mpg contains a value below its median. You can compute the median using the median() function. Note you may find it helpful to use the data.frame() function to create a single data set containing both mpg01 and the other Auto variables.data(Auto)
attach(Auto)
## The following object is masked from package:ggplot2:
##
## mpg
mpg01 = ifelse(median(mpg) < mpg, 1, 0)
Auto = data.frame(Auto, mpg01)
mpg01 and the other features. Which of the other features seem most likely to be useful in predicting mpg01? Scatterplots and boxplots may be useful tools to answer this question. Describe your findings.pairs(Auto[1-9:10])
cor(Auto[1-9:10])
## mpg cylinders displacement horsepower weight
## mpg 1.0000000 -0.7776175 -0.8051269 -0.7784268 -0.8322442
## cylinders -0.7776175 1.0000000 0.9508233 0.8429834 0.8975273
## displacement -0.8051269 0.9508233 1.0000000 0.8972570 0.9329944
## horsepower -0.7784268 0.8429834 0.8972570 1.0000000 0.8645377
## weight -0.8322442 0.8975273 0.9329944 0.8645377 1.0000000
## acceleration 0.4233285 -0.5046834 -0.5438005 -0.6891955 -0.4168392
## year 0.5805410 -0.3456474 -0.3698552 -0.4163615 -0.3091199
## mpg01 0.8369392 -0.7591939 -0.7534766 -0.6670526 -0.7577566
## acceleration year mpg01
## mpg 0.4233285 0.5805410 0.8369392
## cylinders -0.5046834 -0.3456474 -0.7591939
## displacement -0.5438005 -0.3698552 -0.7534766
## horsepower -0.6891955 -0.4163615 -0.6670526
## weight -0.4168392 -0.3091199 -0.7577566
## acceleration 1.0000000 0.2903161 0.3468215
## year 0.2903161 1.0000000 0.4299042
## mpg01 0.3468215 0.4299042 1.0000000
set.seed(1)
train = sample(1:nrow(Auto), 0.75*nrow(Auto), replace = FALSE)
Auto.train = Auto[train,]
Auto.test = Auto[-train,]
mpg01.test = Auto.test$mpg01
mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?lda.fit = lda(mpg01 ~ cylinders + displacement + horsepower + weight, data = Auto, subset = train)
lda.pred = predict(lda.fit, Auto.test)
table(lda.pred$class, mpg01.test)
## mpg01.test
## 0 1
## 0 42 2
## 1 11 43
mean(lda.pred$class != mpg01.test)
## [1] 0.1326531
mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?qda.fit = qda(mpg01 ~ cylinders + displacement + horsepower + weight, data = Auto, subset = train)
qda.pred = predict(qda.fit, Auto.test)
table(qda.pred$class, mpg01.test)
## mpg01.test
## 0 1
## 0 45 4
## 1 8 41
mean(qda.pred$class != mpg01.test)
## [1] 0.122449
mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?glm.fit = glm(mpg01 ~ cylinders + displacement + horsepower + weight, data = Auto, family = binomial, subset = train)
glm.probs = predict(glm.fit, Auto.test, type = "response")
glm.pred = rep(0, length(glm.probs))
glm.pred[glm.probs > 0.5] = 1
table(glm.pred, mpg01.test)
## mpg01.test
## glm.pred 0 1
## 0 45 2
## 1 8 43
mean(glm.pred != mpg01.test)
## [1] 0.1020408
mpg01. Use only the variables that seemed most associated with mpg01 in (b). What test errors do you obtain? Which value of K seems to perform the best on this data set?train.X = cbind(Auto$cylinders, Auto$displacement, Auto$horsepower, Auto$weight)[train,]
test.X = cbind(Auto$cylinders, Auto$displacement, Auto$horsepower, Auto$weight)[-train,]
train.mpg01 = mpg01[train]
knn.pred = knn(train.X, test.X, train.mpg01, k = 1)
table(knn.pred, mpg01.test)
## mpg01.test
## knn.pred 0 1
## 0 43 4
## 1 10 41
mean(knn.pred != mpg01.test)
## [1] 0.1428571
knn.pred2 = knn(train.X, test.X, train.mpg01, k = 10)
table(knn.pred2, mpg01.test)
## mpg01.test
## knn.pred2 0 1
## 0 42 4
## 1 11 41
mean(knn.pred2 != mpg01.test)
## [1] 0.1530612
knn.pred3 = knn(train.X, test.X, train.mpg01, k = 100)
table(knn.pred3, mpg01.test)
## mpg01.test
## knn.pred3 0 1
## 0 42 3
## 1 11 42
mean(knn.pred3 != mpg01.test)
## [1] 0.1428571
Exercise 13 Using the Boston data set, fit classification models in order to predict whether a given suburb has a crime rate above or below the median. Explore logistic regression, LDA, and KNN models using various subsets of the predictors. Describe your findings.
View(Boston)
summary(Boston)
## crim zn indus chas
## Min. : 0.00632 Min. : 0.00 Min. : 0.46 Min. :0.00000
## 1st Qu.: 0.08205 1st Qu.: 0.00 1st Qu.: 5.19 1st Qu.:0.00000
## Median : 0.25651 Median : 0.00 Median : 9.69 Median :0.00000
## Mean : 3.61352 Mean : 11.36 Mean :11.14 Mean :0.06917
## 3rd Qu.: 3.67708 3rd Qu.: 12.50 3rd Qu.:18.10 3rd Qu.:0.00000
## Max. :88.97620 Max. :100.00 Max. :27.74 Max. :1.00000
## nox rm age dis
## Min. :0.3850 Min. :3.561 Min. : 2.90 Min. : 1.130
## 1st Qu.:0.4490 1st Qu.:5.886 1st Qu.: 45.02 1st Qu.: 2.100
## Median :0.5380 Median :6.208 Median : 77.50 Median : 3.207
## Mean :0.5547 Mean :6.285 Mean : 68.57 Mean : 3.795
## 3rd Qu.:0.6240 3rd Qu.:6.623 3rd Qu.: 94.08 3rd Qu.: 5.188
## Max. :0.8710 Max. :8.780 Max. :100.00 Max. :12.127
## rad tax ptratio black
## Min. : 1.000 Min. :187.0 Min. :12.60 Min. : 0.32
## 1st Qu.: 4.000 1st Qu.:279.0 1st Qu.:17.40 1st Qu.:375.38
## Median : 5.000 Median :330.0 Median :19.05 Median :391.44
## Mean : 9.549 Mean :408.2 Mean :18.46 Mean :356.67
## 3rd Qu.:24.000 3rd Qu.:666.0 3rd Qu.:20.20 3rd Qu.:396.23
## Max. :24.000 Max. :711.0 Max. :22.00 Max. :396.90
## lstat medv
## Min. : 1.73 Min. : 5.00
## 1st Qu.: 6.95 1st Qu.:17.02
## Median :11.36 Median :21.20
## Mean :12.65 Mean :22.53
## 3rd Qu.:16.95 3rd Qu.:25.00
## Max. :37.97 Max. :50.00
attach(Boston)
plot(Boston)
crim1 <- rep(0, length(crim))
crim1[crim > median(crim)] <- 1
Boston <- data.frame(Boston, crim1)
train = 1:(length(crim) / 2)
test = (length(crim) / 2 + 1):length(crim)
Boston.train = Boston[train, ]
Boston.test = Boston[-train, ]
crim1.test = crim1[-train]
newBoston <- glm(crim1 ~ crim+zn+indus+chas+nox+rm+age+dis+rad+tax+black+lstat+medv, data = Boston, family = binomial, subset = train)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
newBoston.probs <- predict(newBoston, Boston.test, type = "response")
newBoston.pred <- rep(0, length(newBoston.probs))
newBoston.pred[newBoston.probs > 0.5] <- 1
table(newBoston.pred, crim1.test)
## crim1.test
## newBoston.pred 0 1
## 0 83 8
## 1 7 155
mean(newBoston.pred == crim1.test)
## [1] 0.9407115
Bostonlda <- lda(crim1 ~ crim+zn+indus+chas+nox+rm+age+dis+rad+tax+black+lstat+medv, data = Boston)
Bostonlda.pred <- predict(Bostonlda, Boston.test)
table(Bostonlda.pred$class, Boston.test$crim1)
##
## 0 1
## 0 88 19
## 1 2 144
mean(Bostonlda.pred$class == Boston.test$crim1)
## [1] 0.916996
set.seed(1)
Bostonknn=as.matrix(crim1[train])
Bostonknn.test=as.matrix(crim1[!train])
Bostonknn1 =crim1[train]
Bostonknn.pred=knn(Bostonknn,Bostonknn.test,Bostonknn1, Bostonknn,k=1)
summary(Bostonknn)
## V1
## Min. :0.0000
## 1st Qu.:0.0000
## Median :0.0000
## Mean :0.3557
## 3rd Qu.:1.0000
## Max. :1.0000
mean(Bostonknn.pred != crim1.test)
## [1] NaN