This question should be answered using the Weekly data set, which is part of the ISLR2 package. This data is similar in nature to the market 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.
library(ISLR2)
## Warning: package 'ISLR2' was built under R version 4.6.1
attach(Weekly)
library(MASS)
## Warning: package 'MASS' was built under R version 4.6.1
##
## Attaching package: 'MASS'
## The following object is masked from 'package:ISLR2':
##
## Boston
library(class)
library(e1071)
## Warning: package 'e1071' was built under R version 4.6.1
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
##
##
##
##
pairs(Weekly)
B) Use the full data set to perform a logistic regression with Direction
as the response and the five lag variables plus Volume as predictors.
Use the summary function to print the results. Do any of the predictors
appear to be statistically significant? If so, which ones?
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)
##
## 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.pred <- ifelse(glm.probs > 0.5, "Up", "Down")
table(Predicted = glm.pred, Actual = Weekly$Direction)
## Actual
## Predicted Down Up
## Down 54 48
## Up 430 557
mean(glm.pred == Weekly$Direction)
## [1] 0.5610652
train <- Weekly$Year <= 2008
test <- Weekly[!train, ]
glm.fit2 <- glm(Direction ~ Lag2,data = Weekly,subset = train,family = binomial)
glm.probs2 <- predict(glm.fit2, newdata = test, type = "response")
glm.pred2 <- ifelse(glm.probs2 > 0.5, "Up", "Down")
table(Predicted = glm.pred2, Actual = test$Direction)
## Actual
## Predicted Down Up
## Down 9 5
## Up 34 56
mean(glm.pred2 == test$Direction)
## [1] 0.625
lda.fit <- lda(Direction ~ Lag2, data = Weekly, subset = train)
lda.pred <- predict(lda.fit, newdata = test)
table(Predicted = lda.pred$class, Actual = test$Direction)
## Actual
## Predicted Down Up
## Down 9 5
## Up 34 56
mean(lda.pred$class == test$Direction)
## [1] 0.625
qda.fit <- qda(Direction ~ Lag2, data = Weekly, subset = train)
qda.pred <- predict(qda.fit, newdata = test)
table(Predicted = qda.pred$class, Actual = test$Direction)
## Actual
## Predicted Down Up
## Down 0 0
## Up 43 61
mean(qda.pred$class == test$Direction)
## [1] 0.5865385
train.X <- matrix(Weekly$Lag2[ train], ncol = 1)
test.X <- matrix(Weekly$Lag2[!train], ncol = 1)
train.Y <- Weekly$Direction[train]
set.seed(1)
knn.pred1 <- knn(train.X, test.X, train.Y, k = 1)
table(Predicted = knn.pred1, Actual = test$Direction)
## Actual
## Predicted Down Up
## Down 21 30
## Up 22 31
mean(knn.pred1 == test$Direction)
## [1] 0.5
nb.fit <- naiveBayes(Direction ~ Lag2, data = Weekly, subset = train)
nb.pred <- predict(nb.fit, newdata = test)
table(Predicted = nb.pred, Actual = test$Direction)
## Actual
## Predicted Down Up
## Down 0 0
## Up 43 61
mean(nb.pred == test$Direction)
## [1] 0.5865385
Logistic Regression and LDA appear to be the best at .625 successful prediction rate.
Direction.2009 <- Weekly$Direction[!train]
qda.fit <- qda(Direction ~ Lag2, data = Weekly, subset = train)
qda.pred <- predict(qda.fit, test)
table(qda.pred$class, Direction.2009)
## Direction.2009
## Down Up
## Down 0 0
## Up 43 61
mean(qda.pred$class == Direction.2009)
## [1] 0.5865385
In this problem, you will develop a model to predict whether a given car gets high or low gas mileage based on the Auto data set.
library(ISLR2)
library(MASS)
library(class)
library(e1071)
data(Auto)
mpg01 <- ifelse(Auto$mpg > median(Auto$mpg), 1, 0)
Auto <- data.frame(Auto, mpg01)
mpg_median <- median(Auto$mpg)
cor(Auto[, -9])
## 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
## origin 0.5652088 -0.5689316 -0.6145351 -0.4551715 -0.5850054
## mpg01 0.8369392 -0.7591939 -0.7534766 -0.6670526 -0.7577566
## acceleration year origin mpg01
## mpg 0.4233285 0.5805410 0.5652088 0.8369392
## cylinders -0.5046834 -0.3456474 -0.5689316 -0.7591939
## displacement -0.5438005 -0.3698552 -0.6145351 -0.7534766
## horsepower -0.6891955 -0.4163615 -0.4551715 -0.6670526
## weight -0.4168392 -0.3091199 -0.5850054 -0.7577566
## acceleration 1.0000000 0.2903161 0.2127458 0.3468215
## year 0.2903161 1.0000000 0.1815277 0.4299042
## origin 0.2127458 0.1815277 1.0000000 0.5136984
## mpg01 0.3468215 0.4299042 0.5136984 1.0000000
pairs(Auto[, -9])
boxplot(cylinders ~ mpg01, data = Auto, main = "Cylinders")
boxplot(displacement ~ mpg01, data = Auto, main = "Displacement")
boxplot(horsepower~ mpg01, data = Auto, main = "Horsepower")
boxplot(weight~ mpg01, data = Auto, main = "Weight")
set.seed(1)
n <- nrow(Auto)
train <- sample(n, n * 0.8)
Auto.train <- Auto[ train,]
Auto.test <- Auto[-train,]
mpg01.test <- Auto$mpg01[-train]
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 35 0
## 1 7 37
mean(lda.pred$class != mpg01.test)
## [1] 0.08860759
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 37 2
## 1 5 35
mean(qda.pred$class != mpg01.test)
## [1] 0.08860759
glm.fit <- glm(mpg01 ~ cylinders + displacement + horsepower + weight, data = Auto, subset = train, family = binomial)
glm.prob <- predict(glm.fit, Auto.test, type = "response")
glm.pred <- ifelse(glm.prob > 0.5, 1, 0)
table(glm.pred, mpg01.test)
## mpg01.test
## glm.pred 0 1
## 0 38 1
## 1 4 36
mean(glm.pred != mpg01.test)
## [1] 0.06329114
nb.fit <- naiveBayes(mpg01 ~ cylinders + displacement + horsepower + weight, data = Auto, subset = train)
nb.pred <- predict(nb.fit, Auto.test)
table(nb.pred, mpg01.test)
## mpg01.test
## nb.pred 0 1
## 0 37 1
## 1 5 36
mean(nb.pred != mpg01.test)
## [1] 0.07594937
predictors <- c("cylinders", "displacement", "horsepower", "weight")
train.X <- as.matrix(Auto.train[, predictors])
test.X <- as.matrix(Auto.test[, predictors])
train.Y <- Auto.train$mpg01
test.Y <- Auto.test$mpg01
set.seed(1)
knn.pred <- knn(train.X, test.X, train.Y, k = 3)
table(knn.pred, test.Y)
## test.Y
## knn.pred 0 1
## 0 38 3
## 1 4 34
mean(knn.pred != test.Y)
## [1] 0.08860759
k-fold = 3 returned the lowest error rate of .0886 after testing 1-22.
Using the Boston data set, fit classification models in order to predict whether a given census tract has a crime rate above or below the median. Explore logistic regression, LDA, naive Bayes, and KNN models using various subsets of the predictors. Describe your findings. Hint: You will have to create the response variable yourself, using the variables that are contained in the Boston data set.
data(Boston)
?Boston
## starting httpd help server ... done
crim01 <- ifelse(Boston$crim > median(Boston$crim), 1, 0)
Boston <- data.frame(Boston, crim01)
cor(Boston[, -1])[, "crim01"]
## zn indus chas nox rm age
## -0.43615103 0.60326017 0.07009677 0.72323480 -0.15637178 0.61393992
## dis rad tax ptratio black lstat
## -0.61634164 0.61978625 0.60874128 0.25356836 -0.35121093 0.45326273
## medv crim01
## -0.26301673 1.00000000
boxplot(nox ~ crim01, data = Boston, main = "NOX")
boxplot(rad ~ crim01, data = Boston, main = "RAD")
boxplot(tax ~ crim01, data = Boston, main = "TAX")
boxplot(age ~ crim01, data = Boston, main = "AGE")
boxplot(dis ~ crim01, data = Boston, main = "DIS")
boxplot(indus ~ crim01, data = Boston, main = "INDUS")
set.seed(1)
n <- nrow(Boston)
train <- sample(n, n * 0.7)
Boston.train <- Boston[ train,]
Boston.test <- Boston[-train,]
crim01.test <- Boston$crim01[-train]
glm.fit <- glm(crim01 ~ nox + rad + tax + age + dis + indus, data = Boston, subset = train, family = binomial)
glm.prob <- predict(glm.fit, Boston.test, type = "response")
glm.pred <- ifelse(glm.prob > 0.5, 1, 0)
table(glm.pred, crim01.test)
## crim01.test
## glm.pred 0 1
## 0 58 6
## 1 15 73
mean(glm.pred != crim01.test)
## [1] 0.1381579
lda.fit <- lda(crim01 ~ nox + rad + tax + age + dis + indus,data = Boston, subset = train)
lda.pred <- predict(lda.fit, Boston.test)
table(lda.pred$class, crim01.test)
## crim01.test
## 0 1
## 0 71 20
## 1 2 59
mean(lda.pred$class != crim01.test)
## [1] 0.1447368
nb.fit <- naiveBayes(crim01 ~ nox + rad + tax + age + dis + indus,data = Boston, subset = train)
nb.pred <- predict(nb.fit, Boston.test)
table(nb.pred, crim01.test)
## crim01.test
## nb.pred 0 1
## 0 65 19
## 1 8 60
mean(nb.pred != crim01.test)
## [1] 0.1776316
predictors <- c("nox", "rad", "tax", "age", "dis", "indus")
train.X <- as.matrix(Boston.train[, predictors])
test.X <- as.matrix(Boston.test[, predictors])
train.Y <- Boston.train$crim01
set.seed(1)
knn.pred3 <- knn(train.X, test.X, train.Y, k = 3)
mean(knn.pred3 != crim01.test)
## [1] 0.08552632