(a) Produce some numerical and graphical summaries of the Weekly data. Do there appear to be any patterns?
library(ISLR)
pairs(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
##
##
##
##
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
Outside of Year and Volume, there doesn’t seem to be any obvious patterns. (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 <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, data = Weekly, family = "binomial")
summary(glm)
##
## 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
The predictor with the most significance is Lag2 (c) Compute the confusion matrix and overall fraction of correct predictions. Explain what the confusion matrix is telling you about the types of mistakes made by logistic regression.
glm.pred <- rep("Down", 1089)
glm.probs=predict(glm,type="response")
glm.pred[glm.probs>.5]="Up"
table(glm.pred, Weekly$Direction)
##
## glm.pred Down Up
## Down 54 48
## Up 430 557
The confusion matrix is telling us that the models has (54+557)/1089 = .56 or a 56% accuracy rate, which is not too great. (d) Now fit the logistic regression model using a training data period from 1990 to 2008, with 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 <- Weekly[Weekly$Year <= 2008, ]
test <- Weekly[Weekly$Year > 2008, ]
glm.lag2 <- glm(Direction ~ Lag2, data = train, family = "binomial")
glm.lag2.probs <- predict(glm.lag2, newdata = test, type="response")
glm.pred.lag2 <- rep("Down", 1089)
glm.pred.lag2[glm.lag2.probs>.5]="Up"
table(glm.pred.lag2, Weekly$Direction)
##
## glm.pred.lag2 Down Up
## Down 63 85
## Up 421 520
For this scenario, the fraction of correct predictions is \((63 + 520)/1089 = .53\) or 53% (e) Repeat (d) using LDA.
library(MASS)
train <- Weekly[Weekly$Year <= 2008, ]
test <- Weekly[Weekly$Year > 2008, ]
lda <- lda(Direction ~ Lag2, data = train)
lda.pred = predict(lda, newdata = test)
table(lda.pred$class, test$Direction)
##
## Down Up
## Down 9 5
## Up 34 56
(a) 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.
Auto$mpg01 <- factor(as.numeric(Auto$mpg > median(Auto$mpg)))
(b) Explore the data graphically in order to investigate the association between 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. (c) Split the data into a training set and a test set.
rand_obs <- sample.int(nrow(Auto), nrow(Auto)*.7)
train_set <- Auto[rand_obs,]
test_set <- Auto[-rand_obs,]
(d) Perform LDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained? I decided on weight, horsepower, acceleration.
lda.fit=lda(mpg01~weight+acceleration+horsepower,data=train_set)
lda.fit
## Call:
## lda(mpg01 ~ weight + acceleration + horsepower, data = train_set)
##
## Prior probabilities of groups:
## 0 1
## 0.4927007 0.5072993
##
## Group means:
## weight acceleration horsepower
## 0 3634.504 14.44370 132.17037
## 1 2372.619 16.45324 80.05036
##
## Coefficients of linear discriminants:
## LD1
## weight -0.001823084
## acceleration 0.057643547
## horsepower 0.003441345
(e) Perform QDA on the training data in order to predict 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~weight+acceleration+horsepower,data=train_set)
qda.fit
## Call:
## qda(mpg01 ~ weight + acceleration + horsepower, data = train_set)
##
## Prior probabilities of groups:
## 0 1
## 0.4927007 0.5072993
##
## Group means:
## weight acceleration horsepower
## 0 3634.504 14.44370 132.17037
## 1 2372.619 16.45324 80.05036
(f) Perform logistic regression on the training data in order to predict 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~weight+acceleration+horsepower,data=train_set, family = binomial())
summary(glm.fit)
##
## Call:
## glm(formula = mpg01 ~ weight + acceleration + horsepower, family = binomial(),
## data = train_set)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.2796 -0.2380 0.1136 0.3904 2.8851
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 12.0550136 3.0063344 4.010 6.08e-05 ***
## weight -0.0030545 0.0007067 -4.322 1.54e-05 ***
## acceleration 0.0284943 0.1358897 0.210 0.8339
## horsepower -0.0382763 0.0226309 -1.691 0.0908 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 379.79 on 273 degrees of freedom
## Residual deviance: 160.57 on 270 degrees of freedom
## AIC: 168.57
##
## Number of Fisher Scoring iterations: 7
(g) Perform KNN on the training data, with several values of K, in order to predict 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? ### 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. Change the Crime column into 1s and 0s based on if it’s above or below the median:
Boston$crim <- factor(ifelse(Boston$crim > median(Boston$crim), 1, 0))
Create a sets for training and test
rand_obs <- sample.int(nrow(Boston), nrow(Boston)*.7)
train_set <- Boston[rand_obs,]
test_set <- Boston[-rand_obs,]