##13. 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 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.
(a) Produce some numerical and graphical summaries of the Weekly data. Do there appear to be any patterns?
library(ISLR2)
attach(Weekly)
str(Weekly)
## 'data.frame': 1089 obs. of 9 variables:
## $ Year : num 1990 1990 1990 1990 1990 1990 1990 1990 1990 1990 ...
## $ Lag1 : num 0.816 -0.27 -2.576 3.514 0.712 ...
## $ Lag2 : num 1.572 0.816 -0.27 -2.576 3.514 ...
## $ Lag3 : num -3.936 1.572 0.816 -0.27 -2.576 ...
## $ Lag4 : num -0.229 -3.936 1.572 0.816 -0.27 ...
## $ Lag5 : num -3.484 -0.229 -3.936 1.572 0.816 ...
## $ Volume : num 0.155 0.149 0.16 0.162 0.154 ...
## $ Today : num -0.27 -2.576 3.514 0.712 1.178 ...
## $ Direction: Factor w/ 2 levels "Down","Up": 1 1 2 2 2 1 2 2 2 1 ...
pairs(Weekly)
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
There seems to be little to no corrolation between the
Lag variblies 1-5 to Today. There
is a correlation between year and Volume.
plot(Year,Volume)
(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)
##
## 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
Lag 2 is the only siginificant predictor. As the p-value is at the >0.5 level. (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.probs = predict(glm.fit, Weekly, type="response")
glm.probs [1:10]
## 1 2 3 4 5 6 7 8
## 0.6086249 0.6010314 0.5875699 0.4816416 0.6169013 0.5684190 0.5786097 0.5151972
## 9 10
## 0.5715200 0.5554287
glm.pred = rep("Down", 1089)
glm.pred[glm.probs > .5]=" Up"
table(glm.pred ,Direction)
## Direction
## glm.pred Down Up
## Up 430 557
## Down 54 48
mean(glm.pred==Direction)
## [1] 0.04958678
Running a Logistic Regession model, we see correct predictions is only .05%. This means we have more ups than down in the week. (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=(Year<2009)
Weekly.2009 = Weekly[!train,]
Direction.2009= Direction[!train]
glm.fit=glm(Direction~Lag2,data = Weekly, family= binomial, subset = train)
summary(glm.fit)$coef
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.20325743 0.06428036 3.162046 0.00156665
## Lag2 0.05809527 0.02870446 2.023911 0.04297934
glm.probs = predict(glm.fit, Weekly.2009, type="response")
glm.pred = rep("Down", length(glm.probs))
glm.pred[glm.probs > .5]=" Up"
table(glm.pred ,Direction.2009)
## Direction.2009
## glm.pred Down Up
## Up 34 56
## Down 9 5
mean(glm.pred==Direction.2009)
## [1] 0.08653846
(e) Repeat (d) using LDA.
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from 'package:ISLR2':
##
## Boston
lda.fit = lda(Direction ~ Lag2, data=Weekly, subset = train)
lda.fit
## Call:
## lda(Direction ~ Lag2, data = Weekly, subset = train)
##
## Prior probabilities of groups:
## Down Up
## 0.4477157 0.5522843
##
## Group means:
## Lag2
## Down -0.03568254
## Up 0.26036581
##
## Coefficients of linear discriminants:
## LD1
## Lag2 0.4414162
plot(lda.fit)
lda.pred =predict(lda.fit, Weekly.2009)
lda.class = lda.pred$class
table(lda.class, Direction.2009)
## Direction.2009
## lda.class Down Up
## Down 9 5
## Up 34 56
Here using Linear Discriminat Analysis we get 62% correct predictions on the test data, 92% of the time the model is correct. (f) Repeat (d) using QDA.
qda.fit = lda(Direction ~ Lag2, data=Weekly, subset = train)
qda.fit
## Call:
## lda(Direction ~ Lag2, data = Weekly, subset = train)
##
## Prior probabilities of groups:
## Down Up
## 0.4477157 0.5522843
##
## Group means:
## Lag2
## Down -0.03568254
## Up 0.26036581
##
## Coefficients of linear discriminants:
## LD1
## Lag2 0.4414162
qda.pred =predict(lda.fit, Weekly.2009)
qda.class = qda.pred$class
table(qda.class, Direction.2009)
## Direction.2009
## qda.class Down Up
## Down 9 5
## Up 34 56
Here using Quadratic Discrinant Analysis the model predicts the
correct Direction 53.8% of the time. (g) Repeat (d)
using KNN with K = 1.
library(class)
train.X=as.matrix(Lag2[train])
test.X=as.matrix(Lag2[!train])
train.Direction= Direction[train]
set.seed(1)
knn.pred = knn(train.X,test.X, train.Direction,k=1)
table(knn.pred, Direction.2009)
## Direction.2009
## knn.pred Down Up
## Down 21 30
## Up 22 31
(i) Which of these methods appears to provide the best results on this data? The knn had the best results with the data set, at 53% accuracy rate. (j) Experiment with different combinations of predictors, including possible transformations and interactions, for each of the methods. Report the variables, method, and associated confusion matrix that appears to provide the best results on the held out data. Note that you should also experiment with values for K in the KNN classifier.
detach(Weekly)
##14. 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)
attach(Auto)
(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.
mpg01= rep(0,length(Auto$mpg))
mpg01[Auto$mpg > median(Auto$mpg)]=1
Auto<-data.frame(Auto,mpg01)
summary(Auto)
## mpg cylinders displacement horsepower weight
## Min. : 9.00 Min. :3.000 Min. : 68.0 Min. : 46.0 Min. :1613
## 1st Qu.:17.00 1st Qu.:4.000 1st Qu.:105.0 1st Qu.: 75.0 1st Qu.:2225
## Median :22.75 Median :4.000 Median :151.0 Median : 93.5 Median :2804
## Mean :23.45 Mean :5.472 Mean :194.4 Mean :104.5 Mean :2978
## 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:275.8 3rd Qu.:126.0 3rd Qu.:3615
## Max. :46.60 Max. :8.000 Max. :455.0 Max. :230.0 Max. :5140
##
## acceleration year origin name
## Min. : 8.00 Min. :70.00 Min. :1.000 amc matador : 5
## 1st Qu.:13.78 1st Qu.:73.00 1st Qu.:1.000 ford pinto : 5
## Median :15.50 Median :76.00 Median :1.000 toyota corolla : 5
## Mean :15.54 Mean :75.98 Mean :1.577 amc gremlin : 4
## 3rd Qu.:17.02 3rd Qu.:79.00 3rd Qu.:2.000 amc hornet : 4
## Max. :24.80 Max. :82.00 Max. :3.000 chevrolet chevette: 4
## (Other) :365
## mpg01
## Min. :0.0
## 1st Qu.:0.0
## Median :0.5
## Mean :0.5
## 3rd Qu.:1.0
## Max. :1.0
##
(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.
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])
(c) Split the data into a training set and a test
set.
set.seed(123)
train= sample(1: dim(Auto)[1], dim(Auto)[1]*.7, rep=FALSE)
test=-train
training_data = Auto[train, ]
testing_data = Auto[test, ]
mpg01.test = mpg01[test]
(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? Test error rate 11.86%
library(MASS)
lda_model=lda(mpg01~cylinders+ horsepower+weight+acceleration,data=training_data)
lda_model
## Call:
## lda(mpg01 ~ cylinders + horsepower + weight + acceleration, data = training_data)
##
## Prior probabilities of groups:
## 0 1
## 0.4963504 0.5036496
##
## Group means:
## cylinders horsepower weight acceleration
## 0 6.786765 130.96324 3641.022 14.55588
## 1 4.188406 78.00725 2314.000 16.55072
##
## Coefficients of linear discriminants:
## LD1
## cylinders -0.489432400
## horsepower 0.003088978
## weight -0.001066834
## acceleration 0.001059284
lda_pred=predict(lda_model, testing_data)
names(lda_pred)
## [1] "class" "posterior" "x"
pred.lda=predict(lda_model,testing_data)
table(pred.lda$class, mpg01.test)
## mpg01.test
## 0 1
## 0 51 4
## 1 9 54
mean(pred.lda$class!=mpg01.test)
## [1] 0.1101695
(e) Perform QDA on the training data in order to predict mpg01 using the variables that seemed most associated with mpg01 in
qda_model=qda(mpg01~cylinders+ horsepower+weight+acceleration,data=training_data)
qda_model
## Call:
## qda(mpg01 ~ cylinders + horsepower + weight + acceleration, data = training_data)
##
## Prior probabilities of groups:
## 0 1
## 0.4963504 0.5036496
##
## Group means:
## cylinders horsepower weight acceleration
## 0 6.786765 130.96324 3641.022 14.55588
## 1 4.188406 78.00725 2314.000 16.55072
qda.class=predict(qda_model, testing_data)$class
table(qda.class, testing_data$mpg01)
##
## qda.class 0 1
## 0 53 4
## 1 7 54
(b). What is the test error of the model obtained? Test error rate 9.32% (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_model=glm(mpg01~cylinders+ horsepower+weight+acceleration,data=training_data, family=binomial)
summary(glm_model)
##
## Call:
## glm(formula = mpg01 ~ cylinders + horsepower + weight + acceleration,
## family = binomial, data = training_data)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.43752 -0.15874 0.09867 0.32966 2.78247
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 13.497082 3.374783 3.999 6.35e-05 ***
## cylinders -0.331439 0.270751 -1.224 0.22090
## horsepower -0.040224 0.024494 -1.642 0.10054
## weight -0.002824 0.000922 -3.063 0.00219 **
## acceleration 0.002515 0.155619 0.016 0.98711
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 379.83 on 273 degrees of freedom
## Residual deviance: 141.33 on 269 degrees of freedom
## AIC: 151.33
##
## Number of Fisher Scoring iterations: 7
probs= predict(glm_model, testing_data, type= "response")
pred.glm = rep(0,length(probs))
pred.glm[probs>0.5]=1
table(pred.glm, mpg01.test)
## mpg01.test
## pred.glm 0 1
## 0 54 4
## 1 6 54
(h) 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?
str(Auto)
## 'data.frame': 392 obs. of 10 variables:
## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : int 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : int 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : int 3504 3693 3436 3433 3449 4341 4354 4312 4425 3850 ...
## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : int 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : int 1 1 1 1 1 1 1 1 1 1 ...
## $ name : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
## $ mpg01 : num 0 0 0 0 0 0 0 0 0 0 ...
data=scale(Auto[,-9,10])
set.seed(1234)
train=sample(1:dim(Auto)[1],392*.7,rep=FALSE)
test=-train
training_data = as.matrix(train,(cylinders+horsepower+weight+acceleration))
testing_data = as.matrix(train,(cylinders+horsepower+weight+acceleration))
train.mpg01 = Auto$mpg01[train]
test.mpg01 = Auto$mpg01[test]
library(class)
set.seed(1234)
knn_pred_y= knn(training_data, testing_data, train.mpg01,k=1)
table(knn_pred_y,train.mpg01)
## train.mpg01
## knn_pred_y 0 1
## 0 132 0
## 1 0 142
##16. 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.
library(MASS)
attach(Boston)
crim01=rep(0,length(crim))
crim01[crim>median(crim)]<-1
Boston=data.frame(Boston,crim01)
train=1:(length(crim)/2)
test= (length(crim)/2+1):length(crim)
Boston.train=Boston[train,]
Boston.test=Boston[test,]
crim01.test=crim01[test]
#logistic regression
glm.fit <- glm(crim01 ~ . - crim01 - crim, data = Boston, family = binomial, subset = train)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(glm.fit)
##
## Call:
## glm(formula = crim01 ~ . - crim01 - crim, family = binomial,
## data = Boston, subset = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.83229 -0.06593 0.00000 0.06181 2.61513
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -91.319906 19.490273 -4.685 2.79e-06 ***
## zn -0.815573 0.193373 -4.218 2.47e-05 ***
## indus 0.354172 0.173862 2.037 0.04164 *
## chas 0.167396 0.991922 0.169 0.86599
## nox 93.706326 21.202008 4.420 9.88e-06 ***
## rm -4.719108 1.788765 -2.638 0.00833 **
## age 0.048634 0.024199 2.010 0.04446 *
## dis 4.301493 0.979996 4.389 1.14e-05 ***
## rad 3.039983 0.719592 4.225 2.39e-05 ***
## tax -0.006546 0.007855 -0.833 0.40461
## ptratio 1.430877 0.359572 3.979 6.91e-05 ***
## black -0.017552 0.006734 -2.606 0.00915 **
## lstat 0.190439 0.086722 2.196 0.02809 *
## medv 0.598533 0.185514 3.226 0.00125 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 329.367 on 252 degrees of freedom
## Residual deviance: 69.568 on 239 degrees of freedom
## AIC: 97.568
##
## Number of Fisher Scoring iterations: 10
glm.probs <- predict(glm.fit, Boston.test, type = "response")
glm.pred <- rep(0, length(glm.probs))
glm.pred[glm.probs > 0.5] <- 1
table(glm.pred, crim01.test)
## crim01.test
## glm.pred 0 1
## 0 68 24
## 1 22 139
glm.fit <- glm(crim01 ~ . - crim01 - crim -chas -nox -tax, data = Boston, family = binomial, subset = train)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(glm.fit)
##
## Call:
## glm(formula = crim01 ~ . - crim01 - crim - chas - nox - tax,
## family = binomial, data = Boston, subset = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -3.04443 -0.24461 -0.00114 0.38919 2.72999
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -17.291707 6.019497 -2.873 0.004071 **
## zn -0.478891 0.104276 -4.593 4.38e-06 ***
## indus 0.362719 0.082969 4.372 1.23e-05 ***
## rm -2.364642 0.967625 -2.444 0.014535 *
## age 0.063371 0.015457 4.100 4.14e-05 ***
## dis 1.494535 0.397249 3.762 0.000168 ***
## rad 1.756498 0.357330 4.916 8.85e-07 ***
## ptratio 0.575045 0.161917 3.551 0.000383 ***
## black -0.018916 0.006754 -2.801 0.005102 **
## lstat 0.057632 0.053051 1.086 0.277326
## medv 0.237282 0.081326 2.918 0.003527 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 329.37 on 252 degrees of freedom
## Residual deviance: 139.59 on 242 degrees of freedom
## AIC: 161.59
##
## Number of Fisher Scoring iterations: 9
mean(glm.pred != crim01.test)
## [1] 0.1818182
#LDA
lda.fit <- lda(crim01 ~ . - crim01 - crim, data = Boston, subset = train)
lda.pred <- predict(lda.fit, Boston.test)
table(lda.pred$class, crim01.test)
## crim01.test
## 0 1
## 0 80 24
## 1 10 139
mean(lda.pred$class != crim01.test)
## [1] 0.1343874
lda.fit <- lda(crim01 ~ . - crim01 - crim - chas - nox - tax, data = Boston, subset = train)
lda.pred <- predict(lda.fit, Boston.test)
table(lda.pred$class, crim01.test)
## crim01.test
## 0 1
## 0 83 28
## 1 7 135
mean(lda.pred$class != crim01.test)
## [1] 0.1383399
train.X <- cbind(zn, indus, chas, nox, rm, age, dis, rad, tax, ptratio, black, lstat, medv)[train, ]
test.X <- cbind(zn, indus, chas, nox, rm, age, dis, rad, tax, ptratio, black, lstat, medv)[test, ]
train.crim01 <- crim01[train]
set.seed(1)
knn.pred <- knn(train.X, test.X, train.crim01, k = 1)
table(knn.pred, crim01.test)
## crim01.test
## knn.pred 0 1
## 0 85 111
## 1 5 52
knn.pred <- knn(train.X, test.X, train.crim01, k = 10)
table(knn.pred, crim01.test)
## crim01.test
## knn.pred 0 1
## 0 83 23
## 1 7 140
knn.pred <- knn(train.X, test.X, train.crim01, k = 100)
table(knn.pred, crim01.test)
## crim01.test
## knn.pred 0 1
## 0 86 120
## 1 4 43