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.

(a) Produce some numerical and graphical summaries of the Weekly data. Do there appear to be any patterns?

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  
##            
##            
##            
## 
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
data=Weekly

There appear to be no meaningful patterns for the Weekly data set.

(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_weekly <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, data=Weekly, family=binomial)
summary(glm_weekly)
## 
## 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

From the table above, we can conclude that Lag2 is the only statistically significant predictor.

(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.

pred = predict(glm_weekly,type="response")
pred_choice = ifelse(pred>0.5, "Up", "Down")

caret::confusionMatrix(as.factor(pred_choice),as.factor(Weekly$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down  Up
##       Down   54  48
##       Up    430 557
##                                          
##                Accuracy : 0.5611         
##                  95% CI : (0.531, 0.5908)
##     No Information Rate : 0.5556         
##     P-Value [Acc > NIR] : 0.369          
##                                          
##                   Kappa : 0.035          
##                                          
##  Mcnemar's Test P-Value : <2e-16         
##                                          
##             Sensitivity : 0.9207         
##             Specificity : 0.1116         
##          Pos Pred Value : 0.5643         
##          Neg Pred Value : 0.5294         
##              Prevalence : 0.5556         
##          Detection Rate : 0.5115         
##    Detection Prevalence : 0.9063         
##       Balanced Accuracy : 0.5161         
##                                          
##        'Positive' Class : Up             
## 

Our specificity is very low, therefore the model is predicting Direction as “Down” only 11% of the time when the true direction is actually “Down”. Basically this model is just predicting everything as “Up”, therefore our sensitivity is high but our specificity is very low. Note that the caret function calculates the overall fraction of correct predictions for us.

(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$Year<2009)
weekly_test = Weekly[!train,]
glm_weekly = glm(Direction~Lag2, data=Weekly, family=binomial, subset=train)

pred = predict(glm_weekly, weekly_test, type="response")
pred_choice = ifelse(pred>0.5, "Up", "Down")

caret::confusionMatrix(as.factor(pred_choice),as.factor(weekly_test$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down    9  5
##       Up     34 56
##                                          
##                Accuracy : 0.625          
##                  95% CI : (0.5247, 0.718)
##     No Information Rate : 0.5865         
##     P-Value [Acc > NIR] : 0.2439         
##                                          
##                   Kappa : 0.1414         
##                                          
##  Mcnemar's Test P-Value : 7.34e-06       
##                                          
##             Sensitivity : 0.9180         
##             Specificity : 0.2093         
##          Pos Pred Value : 0.6222         
##          Neg Pred Value : 0.6429         
##              Prevalence : 0.5865         
##          Detection Rate : 0.5385         
##    Detection Prevalence : 0.8654         
##       Balanced Accuracy : 0.5637         
##                                          
##        'Positive' Class : Up             
## 

The accuracy is better than the previous model. However, we are still dealing with low specificity issues. Note that the caret function calculates the overall fraction of correct predictions for us.

(e) Repeat (d) using LDA.

lda_weekly = lda(Direction~Lag2, data=Weekly, subset=train)
pred = (predict(lda_weekly, weekly_test,type="response"))

caret::confusionMatrix(as.factor(pred$class),as.factor(weekly_test$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down    9  5
##       Up     34 56
##                                          
##                Accuracy : 0.625          
##                  95% CI : (0.5247, 0.718)
##     No Information Rate : 0.5865         
##     P-Value [Acc > NIR] : 0.2439         
##                                          
##                   Kappa : 0.1414         
##                                          
##  Mcnemar's Test P-Value : 7.34e-06       
##                                          
##             Sensitivity : 0.9180         
##             Specificity : 0.2093         
##          Pos Pred Value : 0.6222         
##          Neg Pred Value : 0.6429         
##              Prevalence : 0.5865         
##          Detection Rate : 0.5385         
##    Detection Prevalence : 0.8654         
##       Balanced Accuracy : 0.5637         
##                                          
##        'Positive' Class : Up             
## 

We get the same accuracy/sensitivity/specificity as the previous model.

(f) Repeat (d) using QDA.

qda_weekly = qda(Direction~Lag2, data = Weekly, subset = train)

pred = predict(qda_weekly, weekly_test, type="response")

caret::confusionMatrix(as.factor(pred$class),as.factor(weekly_test$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down    0  0
##       Up     43 61
##                                           
##                Accuracy : 0.5865          
##                  95% CI : (0.4858, 0.6823)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.5419          
##                                           
##                   Kappa : 0               
##                                           
##  Mcnemar's Test P-Value : 1.504e-10       
##                                           
##             Sensitivity : 1.0000          
##             Specificity : 0.0000          
##          Pos Pred Value : 0.5865          
##          Neg Pred Value :    NaN          
##              Prevalence : 0.5865          
##          Detection Rate : 0.5865          
##    Detection Prevalence : 1.0000          
##       Balanced Accuracy : 0.5000          
##                                           
##        'Positive' Class : Up              
## 

Note that we get lower accuracy with QDA than with LDA and logistic regression. Also, this model only ever predicts “Up” and therefore has a specificity of 0.

(g) Repeat (d) using KNN with K = 1.

knn_train = matrix(Weekly$Lag2[train])
knn_test = matrix(Weekly$Lag2[!train])
knn_direction = matrix(Weekly$Direction[train])

set.seed(100)
knn_weekly = knn(knn_train, knn_test, knn_direction, k=1)
caret::confusionMatrix(knn_weekly,as.factor(weekly_test$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   21 29
##       Up     22 32
##                                          
##                Accuracy : 0.5096         
##                  95% CI : (0.4097, 0.609)
##     No Information Rate : 0.5865         
##     P-Value [Acc > NIR] : 0.9540         
##                                          
##                   Kappa : 0.0127         
##                                          
##  Mcnemar's Test P-Value : 0.4008         
##                                          
##             Sensitivity : 0.5246         
##             Specificity : 0.4884         
##          Pos Pred Value : 0.5926         
##          Neg Pred Value : 0.4200         
##              Prevalence : 0.5865         
##          Detection Rate : 0.3077         
##    Detection Prevalence : 0.5192         
##       Balanced Accuracy : 0.5065         
##                                          
##        'Positive' Class : Up             
## 

Using KNN classifier the accuracy is around 50% which is not much better than picking at random. Sensitivity and specificity are also both around 0.5.

(h) Which of these methods appears to provide the best results on this data?

The best accuracy scores were from the LDA and logistic regression models. However, we had low specificity in both of those cases. We may need to adjust our cutoff value in order to bring specificity up.

(i) 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.

glm_weekly <- glm(Direction ~ Lag2 + I(Lag2^2), data=Weekly, family=binomial)
summary(glm_weekly)
## 
## Call:
## glm(formula = Direction ~ Lag2 + I(Lag2^2), family = binomial, 
##     data = Weekly)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.7884  -1.2572   0.9953   1.0956   1.2091  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)   
## (Intercept) 0.191030   0.065505   2.916  0.00354 **
## Lag2        0.067576   0.027025   2.500  0.01240 * 
## I(Lag2^2)   0.004303   0.004262   1.010  0.31273   
## ---
## 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: 1489.4  on 1086  degrees of freedom
## AIC: 1495.4
## 
## Number of Fisher Scoring iterations: 4
pred = predict(glm_weekly,type="response")
pred_choice = ifelse(pred>0.5, "Up", "Down")

caret::confusionMatrix(as.factor(pred_choice),as.factor(Weekly$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down  Up
##       Down   28  24
##       Up    456 581
##                                          
##                Accuracy : 0.5592         
##                  95% CI : (0.5292, 0.589)
##     No Information Rate : 0.5556         
##     P-Value [Acc > NIR] : 0.4159         
##                                          
##                   Kappa : 0.02           
##                                          
##  Mcnemar's Test P-Value : <2e-16         
##                                          
##             Sensitivity : 0.96033        
##             Specificity : 0.05785        
##          Pos Pred Value : 0.56027        
##          Neg Pred Value : 0.53846        
##              Prevalence : 0.55556        
##          Detection Rate : 0.53352        
##    Detection Prevalence : 0.95225        
##       Balanced Accuracy : 0.50909        
##                                          
##        'Positive' Class : Up             
## 
glm_weekly <- glm(Direction ~ Lag2*Lag1 +Lag2*Lag3 +Lag2*Lag4 +Lag2*Lag5 + Lag2*Volume, data=Weekly, family=binomial)
summary(glm_weekly)
## 
## Call:
## glm(formula = Direction ~ Lag2 * Lag1 + Lag2 * Lag3 + Lag2 * 
##     Lag4 + Lag2 * Lag5 + Lag2 * Volume, family = binomial, data = Weekly)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.5574  -1.2632   0.9914   1.0799   1.5971  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)   
## (Intercept)  0.2746484  0.0863176   3.182  0.00146 **
## Lag2         0.0367346  0.0401493   0.915  0.36022   
## Lag1        -0.0378339  0.0284117  -1.332  0.18298   
## Lag3        -0.0210872  0.0273460  -0.771  0.44063   
## Lag4        -0.0338093  0.0272644  -1.240  0.21496   
## Lag5        -0.0180775  0.0269970  -0.670  0.50311   
## Volume      -0.0209647  0.0373072  -0.562  0.57415   
## Lag2:Lag1    0.0046169  0.0072577   0.636  0.52469   
## Lag2:Lag3    0.0068300  0.0077065   0.886  0.37547   
## Lag2:Lag4    0.0001626  0.0084469   0.019  0.98464   
## Lag2:Lag5    0.0015600  0.0074716   0.209  0.83461   
## Lag2:Volume  0.0112194  0.0123049   0.912  0.36188   
## ---
## 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: 1484.9  on 1077  degrees of freedom
## AIC: 1508.9
## 
## Number of Fisher Scoring iterations: 4
pred = predict(glm_weekly,type="response")
pred_choice = ifelse(pred>0.5, "Up", "Down")

caret::confusionMatrix(as.factor(pred_choice),as.factor(Weekly$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down  Up
##       Down   60  50
##       Up    424 555
##                                           
##                Accuracy : 0.5647          
##                  95% CI : (0.5347, 0.5944)
##     No Information Rate : 0.5556          
##     P-Value [Acc > NIR] : 0.2814          
##                                           
##                   Kappa : 0.0448          
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.9174          
##             Specificity : 0.1240          
##          Pos Pred Value : 0.5669          
##          Neg Pred Value : 0.5455          
##              Prevalence : 0.5556          
##          Detection Rate : 0.5096          
##    Detection Prevalence : 0.8990          
##       Balanced Accuracy : 0.5207          
##                                           
##        'Positive' Class : Up              
## 
lda_weekly = lda(Direction~ Lag2 + I(Lag2^2), data=Weekly, subset=train)
pred = (predict(lda_weekly, weekly_test,type="response"))

caret::confusionMatrix(as.factor(pred$class),as.factor(weekly_test$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down    7  4
##       Up     36 57
##                                           
##                Accuracy : 0.6154          
##                  95% CI : (0.5149, 0.7091)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.311           
##                                           
##                   Kappa : 0.1092          
##                                           
##  Mcnemar's Test P-Value : 9.509e-07       
##                                           
##             Sensitivity : 0.9344          
##             Specificity : 0.1628          
##          Pos Pred Value : 0.6129          
##          Neg Pred Value : 0.6364          
##              Prevalence : 0.5865          
##          Detection Rate : 0.5481          
##    Detection Prevalence : 0.8942          
##       Balanced Accuracy : 0.5486          
##                                           
##        'Positive' Class : Up              
## 
lda_weekly = lda(Direction~ Lag2*Lag1 +Lag2*Lag3 +Lag2*Lag4 +Lag2*Lag5 + Lag2*Volume, data=Weekly, subset=train)
pred = (predict(lda_weekly, weekly_test,type="response"))

caret::confusionMatrix(as.factor(pred$class),as.factor(weekly_test$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   30 41
##       Up     13 20
##                                           
##                Accuracy : 0.4808          
##                  95% CI : (0.3817, 0.5809)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.9885067       
##                                           
##                   Kappa : 0.0233          
##                                           
##  Mcnemar's Test P-Value : 0.0002386       
##                                           
##             Sensitivity : 0.3279          
##             Specificity : 0.6977          
##          Pos Pred Value : 0.6061          
##          Neg Pred Value : 0.4225          
##              Prevalence : 0.5865          
##          Detection Rate : 0.1923          
##    Detection Prevalence : 0.3173          
##       Balanced Accuracy : 0.5128          
##                                           
##        'Positive' Class : Up              
## 
qda_weekly = qda(Direction~Lag2 + I(Lag2^2), data = Weekly, subset = train)

pred = predict(qda_weekly, weekly_test, type="response")

caret::confusionMatrix(as.factor(pred$class),as.factor(weekly_test$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down    7  3
##       Up     36 58
##                                          
##                Accuracy : 0.625          
##                  95% CI : (0.5247, 0.718)
##     No Information Rate : 0.5865         
##     P-Value [Acc > NIR] : 0.2439         
##                                          
##                   Kappa : 0.1281         
##                                          
##  Mcnemar's Test P-Value : 2.99e-07       
##                                          
##             Sensitivity : 0.9508         
##             Specificity : 0.1628         
##          Pos Pred Value : 0.6170         
##          Neg Pred Value : 0.7000         
##              Prevalence : 0.5865         
##          Detection Rate : 0.5577         
##    Detection Prevalence : 0.9038         
##       Balanced Accuracy : 0.5568         
##                                          
##        'Positive' Class : Up             
## 
qda_weekly = qda(Direction~Lag2*Lag1 +Lag2*Lag3 +Lag2*Lag4 +Lag2*Lag5 + Lag2*Volume, data = Weekly, subset = train)

pred = predict(qda_weekly, weekly_test, type="response")

caret::confusionMatrix(as.factor(pred$class),as.factor(weekly_test$Direction), positive = 'Up')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   23 28
##       Up     20 33
##                                          
##                Accuracy : 0.5385         
##                  95% CI : (0.438, 0.6367)
##     No Information Rate : 0.5865         
##     P-Value [Acc > NIR] : 0.8631         
##                                          
##                   Kappa : 0.0738         
##                                          
##  Mcnemar's Test P-Value : 0.3123         
##                                          
##             Sensitivity : 0.5410         
##             Specificity : 0.5349         
##          Pos Pred Value : 0.6226         
##          Neg Pred Value : 0.4510         
##              Prevalence : 0.5865         
##          Detection Rate : 0.3173         
##    Detection Prevalence : 0.5096         
##       Balanced Accuracy : 0.5379         
##                                          
##        'Positive' Class : Up             
## 
knn_test_error = rep(0,10)


train.x = matrix(Weekly$Lag2[train])
test.x = matrix(Weekly$Lag2[!train])
train.direction = matrix(Weekly$Direction[train])

for (i in 1:10){
set.seed(100)
knn.weekly <- knn(train.x,test.x, train.direction, k=i)


knn_test_error[i] <- mean(knn.weekly!=weekly_test$Direction)
print(c("k equals:",i))
print(c("test error rate:",knn_test_error[i]))
print(caret::confusionMatrix(as.factor(knn.weekly),as.factor(weekly_test$Direction), positive = 'Up'))
}
## [1] "k equals:" "1"        
## [1] "test error rate:"  "0.490384615384615"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   21 29
##       Up     22 32
##                                          
##                Accuracy : 0.5096         
##                  95% CI : (0.4097, 0.609)
##     No Information Rate : 0.5865         
##     P-Value [Acc > NIR] : 0.9540         
##                                          
##                   Kappa : 0.0127         
##                                          
##  Mcnemar's Test P-Value : 0.4008         
##                                          
##             Sensitivity : 0.5246         
##             Specificity : 0.4884         
##          Pos Pred Value : 0.5926         
##          Neg Pred Value : 0.4200         
##              Prevalence : 0.5865         
##          Detection Rate : 0.3077         
##    Detection Prevalence : 0.5192         
##       Balanced Accuracy : 0.5065         
##                                          
##        'Positive' Class : Up             
##                                          
## [1] "k equals:" "2"        
## [1] "test error rate:"  "0.471153846153846"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   21 27
##       Up     22 34
##                                           
##                Accuracy : 0.5288          
##                  95% CI : (0.4285, 0.6275)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.9017          
##                                           
##                   Kappa : 0.045           
##                                           
##  Mcnemar's Test P-Value : 0.5677          
##                                           
##             Sensitivity : 0.5574          
##             Specificity : 0.4884          
##          Pos Pred Value : 0.6071          
##          Neg Pred Value : 0.4375          
##              Prevalence : 0.5865          
##          Detection Rate : 0.3269          
##    Detection Prevalence : 0.5385          
##       Balanced Accuracy : 0.5229          
##                                           
##        'Positive' Class : Up              
##                                           
## [1] "k equals:" "3"        
## [1] "test error rate:"  "0.442307692307692"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   16 19
##       Up     27 42
##                                         
##                Accuracy : 0.5577        
##                  95% CI : (0.457, 0.655)
##     No Information Rate : 0.5865        
##     P-Value [Acc > NIR] : 0.7579        
##                                         
##                   Kappa : 0.0623        
##                                         
##  Mcnemar's Test P-Value : 0.3020        
##                                         
##             Sensitivity : 0.6885        
##             Specificity : 0.3721        
##          Pos Pred Value : 0.6087        
##          Neg Pred Value : 0.4571        
##              Prevalence : 0.5865        
##          Detection Rate : 0.4038        
##    Detection Prevalence : 0.6635        
##       Balanced Accuracy : 0.5303        
##                                         
##        'Positive' Class : Up            
##                                         
## [1] "k equals:" "4"        
## [1] "test error rate:"  "0.403846153846154"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   21 20
##       Up     22 41
##                                           
##                Accuracy : 0.5962          
##                  95% CI : (0.4954, 0.6913)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.4626          
##                                           
##                   Kappa : 0.1616          
##                                           
##  Mcnemar's Test P-Value : 0.8774          
##                                           
##             Sensitivity : 0.6721          
##             Specificity : 0.4884          
##          Pos Pred Value : 0.6508          
##          Neg Pred Value : 0.5122          
##              Prevalence : 0.5865          
##          Detection Rate : 0.3942          
##    Detection Prevalence : 0.6058          
##       Balanced Accuracy : 0.5803          
##                                           
##        'Positive' Class : Up              
##                                           
## [1] "k equals:" "5"        
## [1] "test error rate:"  "0.451923076923077"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   16 20
##       Up     27 41
##                                           
##                Accuracy : 0.5481          
##                  95% CI : (0.4474, 0.6459)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.8152          
##                                           
##                   Kappa : 0.0453          
##                                           
##  Mcnemar's Test P-Value : 0.3815          
##                                           
##             Sensitivity : 0.6721          
##             Specificity : 0.3721          
##          Pos Pred Value : 0.6029          
##          Neg Pred Value : 0.4444          
##              Prevalence : 0.5865          
##          Detection Rate : 0.3942          
##    Detection Prevalence : 0.6538          
##       Balanced Accuracy : 0.5221          
##                                           
##        'Positive' Class : Up              
##                                           
## [1] "k equals:" "6"        
## [1] "test error rate:"  "0.451923076923077"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   18 22
##       Up     25 39
##                                           
##                Accuracy : 0.5481          
##                  95% CI : (0.4474, 0.6459)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.8152          
##                                           
##                   Kappa : 0.0586          
##                                           
##  Mcnemar's Test P-Value : 0.7705          
##                                           
##             Sensitivity : 0.6393          
##             Specificity : 0.4186          
##          Pos Pred Value : 0.6094          
##          Neg Pred Value : 0.4500          
##              Prevalence : 0.5865          
##          Detection Rate : 0.3750          
##    Detection Prevalence : 0.6154          
##       Balanced Accuracy : 0.5290          
##                                           
##        'Positive' Class : Up              
##                                           
## [1] "k equals:" "7"        
## [1] "test error rate:"  "0.451923076923077"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   15 19
##       Up     28 42
##                                           
##                Accuracy : 0.5481          
##                  95% CI : (0.4474, 0.6459)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.8152          
##                                           
##                   Kappa : 0.0386          
##                                           
##  Mcnemar's Test P-Value : 0.2432          
##                                           
##             Sensitivity : 0.6885          
##             Specificity : 0.3488          
##          Pos Pred Value : 0.6000          
##          Neg Pred Value : 0.4412          
##              Prevalence : 0.5865          
##          Detection Rate : 0.4038          
##    Detection Prevalence : 0.6731          
##       Balanced Accuracy : 0.5187          
##                                           
##        'Positive' Class : Up              
##                                           
## [1] "k equals:" "8"        
## [1] "test error rate:"  "0.451923076923077"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   15 19
##       Up     28 42
##                                           
##                Accuracy : 0.5481          
##                  95% CI : (0.4474, 0.6459)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.8152          
##                                           
##                   Kappa : 0.0386          
##                                           
##  Mcnemar's Test P-Value : 0.2432          
##                                           
##             Sensitivity : 0.6885          
##             Specificity : 0.3488          
##          Pos Pred Value : 0.6000          
##          Neg Pred Value : 0.4412          
##              Prevalence : 0.5865          
##          Detection Rate : 0.4038          
##    Detection Prevalence : 0.6731          
##       Balanced Accuracy : 0.5187          
##                                           
##        'Positive' Class : Up              
##                                           
## [1] "k equals:" "9"        
## [1] "test error rate:"  "0.442307692307692"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   17 20
##       Up     26 41
##                                         
##                Accuracy : 0.5577        
##                  95% CI : (0.457, 0.655)
##     No Information Rate : 0.5865        
##     P-Value [Acc > NIR] : 0.7579        
##                                         
##                   Kappa : 0.0689        
##                                         
##  Mcnemar's Test P-Value : 0.4610        
##                                         
##             Sensitivity : 0.6721        
##             Specificity : 0.3953        
##          Pos Pred Value : 0.6119        
##          Neg Pred Value : 0.4595        
##              Prevalence : 0.5865        
##          Detection Rate : 0.3942        
##    Detection Prevalence : 0.6442        
##       Balanced Accuracy : 0.5337        
##                                         
##        'Positive' Class : Up            
##                                         
## [1] "k equals:" "10"       
## [1] "test error rate:"  "0.432692307692308"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction Down Up
##       Down   17 19
##       Up     26 42
##                                           
##                Accuracy : 0.5673          
##                  95% CI : (0.4665, 0.6641)
##     No Information Rate : 0.5865          
##     P-Value [Acc > NIR] : 0.6921          
##                                           
##                   Kappa : 0.0859          
##                                           
##  Mcnemar's Test P-Value : 0.3711          
##                                           
##             Sensitivity : 0.6885          
##             Specificity : 0.3953          
##          Pos Pred Value : 0.6176          
##          Neg Pred Value : 0.4722          
##              Prevalence : 0.5865          
##          Detection Rate : 0.4038          
##    Detection Prevalence : 0.6538          
##       Balanced Accuracy : 0.5419          
##                                           
##        'Positive' Class : Up              
## 

After testing various models, the ones that performed the best were: the lda/qda model with the terms Lag2 and Lag2^2, as well as the knn model with k=4. The best model overall seems to be the knn classifier with k=4.

11. 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.

(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 = ifelse(Auto$mpg>median(Auto$mpg), 1, 0)
auto_df = data.frame(Auto,mpg01)
str(auto_df)
## 'data.frame':    392 obs. of  10 variables:
##  $ mpg         : num  18 15 18 16 17 15 14 14 14 15 ...
##  $ cylinders   : num  8 8 8 8 8 8 8 8 8 8 ...
##  $ displacement: num  307 350 318 304 302 429 454 440 455 390 ...
##  $ horsepower  : num  130 165 150 150 140 198 220 215 225 190 ...
##  $ weight      : num  3504 3693 3436 3433 3449 ...
##  $ acceleration: num  12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
##  $ year        : num  70 70 70 70 70 70 70 70 70 70 ...
##  $ origin      : num  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 ...

(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.

pairs(auto_df)

par(mfrow=c(2,2))
attach(auto_df)
## The following object is masked _by_ .GlobalEnv:
## 
##     mpg01
boxplot(cylinders~mpg01)
boxplot(displacement~mpg01)
boxplot(horsepower~mpg01)
boxplot(weight~mpg01)

par(mfrow=c(2,2))
attach(auto_df)
## The following object is masked _by_ .GlobalEnv:
## 
##     mpg01
## The following objects are masked from auto_df (pos = 3):
## 
##     acceleration, cylinders, displacement, horsepower, mpg, mpg01,
##     name, origin, weight, year
boxplot(acceleration~mpg01)
boxplot(year~mpg01)
boxplot(origin~mpg01)

From the pairs plots we can see that there are highly correlated variables, namely: displacement, horsepower, weight, and acceleration. We will need to keep this in mind when running our model because some methods may be sensitive to multicollinearity. From the boxplots we note that most predictors look like they will be significant besides acceleration. Obviously, we can also exlcude name from the model as it is irrelevant to mpg01.

(c) Split the data into a training set and a test set.

set.seed(100)
train = sample(1:nrow(auto_df), 0.8*nrow(auto_df))
auto_df_train = auto_df[train,]
auto_df_test = auto_df[-train,]

str(auto_df_train)
## 'data.frame':    313 obs. of  10 variables:
##  $ mpg         : num  29.5 24.2 19 20 16 37.2 36.4 16 14 25 ...
##  $ cylinders   : num  4 6 4 4 8 4 5 6 8 4 ...
##  $ displacement: num  97 146 122 130 304 86 121 250 454 140 ...
##  $ horsepower  : num  71 120 85 102 150 65 67 100 220 92 ...
##  $ weight      : num  1825 2930 2310 3150 3433 ...
##  $ acceleration: num  12.2 13.8 18.5 15.7 12 16.4 19.9 18 9 14.9 ...
##  $ year        : num  76 81 73 76 70 80 80 73 70 76 ...
##  $ origin      : num  2 3 1 2 1 3 2 1 1 1 ...
##  $ name        : Factor w/ 304 levels "amc ambassador brougham",..: 287 90 156 297 14 82 20 62 54 40 ...
##  $ mpg01       : num  1 1 0 0 0 1 1 0 0 1 ...
str(auto_df_test)
## 'data.frame':    79 obs. of  10 variables:
##  $ mpg         : num  14 14 15 15 18 24 26 10 9 27 ...
##  $ cylinders   : num  8 8 8 8 6 4 4 8 8 4 ...
##  $ displacement: num  440 455 390 400 199 107 121 307 304 97 ...
##  $ horsepower  : num  215 225 190 150 97 90 113 200 193 88 ...
##  $ weight      : num  4312 4425 3850 3761 2774 ...
##  $ acceleration: num  8.5 10 8.5 9.5 15.5 14.5 12.5 15 18.5 14.5 ...
##  $ year        : num  70 70 70 70 70 70 70 70 70 71 ...
##  $ origin      : num  1 1 1 1 1 2 2 1 1 3 ...
##  $ name        : Factor w/ 304 levels "amc ambassador brougham",..: 223 241 2 57 8 16 22 68 163 95 ...
##  $ mpg01       : num  0 0 0 0 0 1 1 0 0 1 ...

We have split the data randomly using an 80/20 train/test split.

(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?

lda_auto_df = lda(mpg01~cylinders+displacement+horsepower+weight+year+origin, data=auto_df_train)

pred = (predict(lda_auto_df, auto_df_test,type="response"))

caret::confusionMatrix(as.factor(pred$class),as.factor(auto_df_test$mpg01), positive = "1")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 44  1
##          1  2 32
##                                          
##                Accuracy : 0.962          
##                  95% CI : (0.893, 0.9921)
##     No Information Rate : 0.5823         
##     P-Value [Acc > NIR] : 8.599e-15      
##                                          
##                   Kappa : 0.9223         
##                                          
##  Mcnemar's Test P-Value : 1              
##                                          
##             Sensitivity : 0.9697         
##             Specificity : 0.9565         
##          Pos Pred Value : 0.9412         
##          Neg Pred Value : 0.9778         
##              Prevalence : 0.4177         
##          Detection Rate : 0.4051         
##    Detection Prevalence : 0.4304         
##       Balanced Accuracy : 0.9631         
##                                          
##        'Positive' Class : 1              
## 
mean(pred$class != auto_df_test$mpg01)
## [1] 0.03797468

Here we can see our test error of the lda model is 0.037 or 3.7%.

(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_auto_df = qda(mpg01~cylinders+displacement+horsepower+weight+year+origin, data=auto_df_train)

pred = (predict(qda_auto_df, auto_df_test,type="response"))

caret::confusionMatrix(as.factor(pred$class),as.factor(auto_df_test$mpg01), positive = "1")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 44  3
##          1  2 30
##                                           
##                Accuracy : 0.9367          
##                  95% CI : (0.8584, 0.9791)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 1.314e-12       
##                                           
##                   Kappa : 0.8693          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9091          
##             Specificity : 0.9565          
##          Pos Pred Value : 0.9375          
##          Neg Pred Value : 0.9362          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3797          
##    Detection Prevalence : 0.4051          
##       Balanced Accuracy : 0.9328          
##                                           
##        'Positive' Class : 1               
## 
mean(pred$class != auto_df_test$mpg01)
## [1] 0.06329114

Here we can see our test error of the qda model is 0.063 or 6.3%.

(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_auto_df = glm(mpg01~cylinders+displacement+horsepower+weight+year+origin, data=auto_df_train, family = binomial)

pred = predict(glm_auto_df, auto_df_test, type="response")
pred_choice = ifelse(pred>0.5, 1, 0)

caret::confusionMatrix(as.factor(pred_choice),as.factor(auto_df_test$mpg01), positive = '1')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 43  4
##          1  3 29
##                                           
##                Accuracy : 0.9114          
##                  95% CI : (0.8259, 0.9636)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 9.092e-11       
##                                           
##                   Kappa : 0.8171          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.8788          
##             Specificity : 0.9348          
##          Pos Pred Value : 0.9062          
##          Neg Pred Value : 0.9149          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3671          
##    Detection Prevalence : 0.4051          
##       Balanced Accuracy : 0.9068          
##                                           
##        'Positive' Class : 1               
## 
mean(pred_choice != auto_df_test$mpg01)
## [1] 0.08860759

Here we can see our test error of the logit model is 0.088 or 8.8%.

(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?

train.x = cbind(auto_df_train$cylinders,auto_df_train$displacement,auto_df_train$horsepower,auto_df_train$weight,auto_df_train$year,auto_df_train$origin)

test.x = cbind(auto_df_test$cylinders,auto_df_test$displacement,auto_df_test$horsepower,auto_df_test$weight,auto_df_test$year,auto_df_test$origin)

train.mpg01 = auto_df_train$mpg01

knn_test_error = rep(0,10)


for (i in 1:10){
set.seed(100)
knn_auto_df <- knn(train.x,test.x, train.mpg01, k=i)



knn_test_error[i] <- mean(knn_auto_df!=auto_df_test$mpg01)
print(c("k equals:",i))
print(c("test error rate:",knn_test_error[i]))
print(caret::confusionMatrix(as.factor(knn_auto_df),as.factor(auto_df_test$mpg01), positive = '1'))
}
## [1] "k equals:" "1"        
## [1] "test error rate:"  "0.113924050632911"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 41  4
##          1  5 29
##                                           
##                Accuracy : 0.8861          
##                  95% CI : (0.7947, 0.9466)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 3.489e-09       
##                                           
##                   Kappa : 0.7668          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.8788          
##             Specificity : 0.8913          
##          Pos Pred Value : 0.8529          
##          Neg Pred Value : 0.9111          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3671          
##    Detection Prevalence : 0.4304          
##       Balanced Accuracy : 0.8850          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "2"        
## [1] "test error rate:"  "0.113924050632911"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 41  4
##          1  5 29
##                                           
##                Accuracy : 0.8861          
##                  95% CI : (0.7947, 0.9466)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 3.489e-09       
##                                           
##                   Kappa : 0.7668          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.8788          
##             Specificity : 0.8913          
##          Pos Pred Value : 0.8529          
##          Neg Pred Value : 0.9111          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3671          
##    Detection Prevalence : 0.4304          
##       Balanced Accuracy : 0.8850          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "3"        
## [1] "test error rate:"   "0.0506329113924051"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 43  1
##          1  3 32
##                                          
##                Accuracy : 0.9494         
##                  95% CI : (0.8754, 0.986)
##     No Information Rate : 0.5823         
##     P-Value [Acc > NIR] : 1.196e-13      
##                                          
##                   Kappa : 0.8968         
##                                          
##  Mcnemar's Test P-Value : 0.6171         
##                                          
##             Sensitivity : 0.9697         
##             Specificity : 0.9348         
##          Pos Pred Value : 0.9143         
##          Neg Pred Value : 0.9773         
##              Prevalence : 0.4177         
##          Detection Rate : 0.4051         
##    Detection Prevalence : 0.4430         
##       Balanced Accuracy : 0.9522         
##                                          
##        'Positive' Class : 1              
##                                          
## [1] "k equals:" "4"        
## [1] "test error rate:"   "0.0759493670886076"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 42  2
##          1  4 31
##                                          
##                Accuracy : 0.9241         
##                  95% CI : (0.842, 0.9716)
##     No Information Rate : 0.5823         
##     P-Value [Acc > NIR] : 1.188e-11      
##                                          
##                   Kappa : 0.8452         
##                                          
##  Mcnemar's Test P-Value : 0.6831         
##                                          
##             Sensitivity : 0.9394         
##             Specificity : 0.9130         
##          Pos Pred Value : 0.8857         
##          Neg Pred Value : 0.9545         
##              Prevalence : 0.4177         
##          Detection Rate : 0.3924         
##    Detection Prevalence : 0.4430         
##       Balanced Accuracy : 0.9262         
##                                          
##        'Positive' Class : 1              
##                                          
## [1] "k equals:" "5"        
## [1] "test error rate:"   "0.0632911392405063"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 44  3
##          1  2 30
##                                           
##                Accuracy : 0.9367          
##                  95% CI : (0.8584, 0.9791)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 1.314e-12       
##                                           
##                   Kappa : 0.8693          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9091          
##             Specificity : 0.9565          
##          Pos Pred Value : 0.9375          
##          Neg Pred Value : 0.9362          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3797          
##    Detection Prevalence : 0.4051          
##       Balanced Accuracy : 0.9328          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "6"        
## [1] "test error rate:"   "0.0886075949367089"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 42  3
##          1  4 30
##                                           
##                Accuracy : 0.9114          
##                  95% CI : (0.8259, 0.9636)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 9.092e-11       
##                                           
##                   Kappa : 0.8186          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9091          
##             Specificity : 0.9130          
##          Pos Pred Value : 0.8824          
##          Neg Pred Value : 0.9333          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3797          
##    Detection Prevalence : 0.4304          
##       Balanced Accuracy : 0.9111          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "7"        
## [1] "test error rate:"   "0.0886075949367089"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 42  3
##          1  4 30
##                                           
##                Accuracy : 0.9114          
##                  95% CI : (0.8259, 0.9636)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 9.092e-11       
##                                           
##                   Kappa : 0.8186          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9091          
##             Specificity : 0.9130          
##          Pos Pred Value : 0.8824          
##          Neg Pred Value : 0.9333          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3797          
##    Detection Prevalence : 0.4304          
##       Balanced Accuracy : 0.9111          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "8"        
## [1] "test error rate:" "0.10126582278481"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 42  4
##          1  4 29
##                                           
##                Accuracy : 0.8987          
##                  95% CI : (0.8102, 0.9553)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 6.012e-10       
##                                           
##                   Kappa : 0.7918          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.8788          
##             Specificity : 0.9130          
##          Pos Pred Value : 0.8788          
##          Neg Pred Value : 0.9130          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3671          
##    Detection Prevalence : 0.4177          
##       Balanced Accuracy : 0.8959          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "9"        
## [1] "test error rate:"   "0.0886075949367089"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 42  3
##          1  4 30
##                                           
##                Accuracy : 0.9114          
##                  95% CI : (0.8259, 0.9636)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 9.092e-11       
##                                           
##                   Kappa : 0.8186          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9091          
##             Specificity : 0.9130          
##          Pos Pred Value : 0.8824          
##          Neg Pred Value : 0.9333          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3797          
##    Detection Prevalence : 0.4304          
##       Balanced Accuracy : 0.9111          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "10"       
## [1] "test error rate:"   "0.0886075949367089"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 42  3
##          1  4 30
##                                           
##                Accuracy : 0.9114          
##                  95% CI : (0.8259, 0.9636)
##     No Information Rate : 0.5823          
##     P-Value [Acc > NIR] : 9.092e-11       
##                                           
##                   Kappa : 0.8186          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9091          
##             Specificity : 0.9130          
##          Pos Pred Value : 0.8824          
##          Neg Pred Value : 0.9333          
##              Prevalence : 0.4177          
##          Detection Rate : 0.3797          
##    Detection Prevalence : 0.4304          
##       Balanced Accuracy : 0.9111          
##                                           
##        'Positive' Class : 1               
## 
knn_test_error
##  [1] 0.11392405 0.11392405 0.05063291 0.07594937 0.06329114 0.08860759
##  [7] 0.08860759 0.10126582 0.08860759 0.08860759

As we can see from the results above, knn classification with k=3 gives us the lowest test error rate of 0.051 or 5.1%.

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.

Here we will add crime01 (binary crime) to the Boston dataset and create a train/test split

crime01 = ifelse(Boston$crim>median(Boston$crim), 1, 0)
boston.df = data.frame(Boston,crime01)
str(boston.df)
## 'data.frame':    506 obs. of  15 variables:
##  $ crim   : num  0.00632 0.02731 0.02729 0.03237 0.06905 ...
##  $ zn     : num  18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
##  $ indus  : num  2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
##  $ chas   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ nox    : num  0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
##  $ rm     : num  6.58 6.42 7.18 7 7.15 ...
##  $ age    : num  65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
##  $ dis    : num  4.09 4.97 4.97 6.06 6.06 ...
##  $ rad    : int  1 2 2 3 3 3 5 5 5 5 ...
##  $ tax    : num  296 242 242 222 222 222 311 311 311 311 ...
##  $ ptratio: num  15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
##  $ black  : num  397 397 393 395 397 ...
##  $ lstat  : num  4.98 9.14 4.03 2.94 5.33 ...
##  $ medv   : num  24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
##  $ crime01: num  0 0 0 0 0 0 0 0 0 0 ...
set.seed(100)
train = sample(1:nrow(boston.df), 0.8*nrow(boston.df))
boston.train = boston.df[train,]
boston.test = boston.df[-train,]

str(boston.train)
## 'data.frame':    404 obs. of  15 variables:
##  $ crim   : num  0.0345 0.0453 3.8497 0.1008 0.2391 ...
##  $ zn     : num  82.5 0 0 0 0 0 0 0 0 0 ...
##  $ indus  : num  2.03 11.93 18.1 10.01 9.69 ...
##  $ chas   : int  0 0 1 0 0 0 0 0 0 0 ...
##  $ nox    : num  0.415 0.573 0.77 0.547 0.585 0.58 0.489 0.609 0.659 0.58 ...
##  $ rm     : num  6.16 6.12 6.39 6.71 6.02 ...
##  $ age    : num  38.4 76.7 91 81.6 65.3 75 22.3 98.8 100 56.7 ...
##  $ dis    : num  6.27 2.29 2.51 2.68 2.41 ...
##  $ rad    : int  2 1 24 6 6 24 4 4 24 24 ...
##  $ tax    : num  348 273 666 432 391 666 277 711 666 666 ...
##  $ ptratio: num  14.7 21 20.2 17.8 19.2 20.2 18.6 20.1 20.2 20.2 ...
##  $ black  : num  394 397 391 396 397 ...
##  $ lstat  : num  7.43 9.08 13.27 10.16 12.92 ...
##  $ medv   : num  24.1 20.6 21.7 22.8 21.2 23.2 22.6 13.6 11.9 20.1 ...
##  $ crime01: num  0 0 1 0 0 1 0 0 1 1 ...
str(boston.test)
## 'data.frame':    102 obs. of  15 variables:
##  $ crim   : num  0.069 0.1446 0.17 0.7842 0.9884 ...
##  $ zn     : num  0 12.5 12.5 0 0 0 0 0 0 0 ...
##  $ indus  : num  2.18 7.87 7.87 8.14 8.14 8.14 8.14 8.14 5.96 6.91 ...
##  $ chas   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ nox    : num  0.458 0.524 0.524 0.538 0.538 0.538 0.538 0.538 0.499 0.448 ...
##  $ rm     : num  7.15 6.17 6 5.99 5.81 ...
##  $ age    : num  54.2 96.1 85.9 81.7 100 94.4 87.3 96.9 68.2 95.3 ...
##  $ dis    : num  6.06 5.95 6.59 4.26 4.1 ...
##  $ rad    : int  3 5 5 4 4 4 4 4 5 3 ...
##  $ tax    : num  222 311 311 307 307 307 307 307 279 233 ...
##  $ ptratio: num  18.7 15.2 15.2 21 21 21 21 21 19.2 17.9 ...
##  $ black  : num  397 397 387 387 395 ...
##  $ lstat  : num  5.33 19.15 17.1 14.67 19.88 ...
##  $ medv   : num  36.2 27.1 18.9 17.5 14.5 18.4 21 13.5 18.9 14.4 ...
##  $ crime01: num  0 0 0 1 1 1 1 1 0 0 ...
pairs(boston.df)

cor(boston.df[,-15])[,"crim"]
##        crim          zn       indus        chas         nox          rm 
##  1.00000000 -0.20046922  0.40658341 -0.05589158  0.42097171 -0.21924670 
##         age         dis         rad         tax     ptratio       black 
##  0.35273425 -0.37967009  0.62550515  0.58276431  0.28994558 -0.38506394 
##       lstat        medv 
##  0.45562148 -0.38830461

chas seems to be the only variable that not very correlated at all with crime0 based on the table above. Also, from the pairs plot it looks as though some variables such as lstat, rm, medv may be correlated with each other. First, we will run a logistic regression with all predictors.

glm.boston = glm(crime01~.-crim, data=boston.train, family = binomial)

summary(glm.boston)
## 
## Call:
## glm(formula = crime01 ~ . - crim, family = binomial, data = boston.train)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.1490  -0.1839  -0.0020   0.0036   3.3823  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -35.563369   7.234946  -4.915 8.86e-07 ***
## zn           -0.061499   0.035264  -1.744  0.08116 .  
## indus        -0.031617   0.049685  -0.636  0.52455    
## chas          0.440435   0.806740   0.546  0.58510    
## nox          45.505591   8.154368   5.581 2.40e-08 ***
## rm           -0.614829   0.769615  -0.799  0.42436    
## age           0.034393   0.014270   2.410  0.01595 *  
## dis           0.780795   0.246066   3.173  0.00151 ** 
## rad           0.635762   0.175862   3.615  0.00030 ***
## tax          -0.006423   0.002977  -2.158  0.03096 *  
## ptratio       0.450300   0.138823   3.244  0.00118 ** 
## black        -0.011476   0.006177  -1.858  0.06320 .  
## lstat         0.010570   0.059725   0.177  0.85953    
## medv          0.206090   0.077958   2.644  0.00820 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 560.05  on 403  degrees of freedom
## Residual deviance: 173.37  on 390  degrees of freedom
## AIC: 201.37
## 
## Number of Fisher Scoring iterations: 9
pred = predict(glm.boston, boston.test, type="response")
pred_choice = ifelse(pred>0.5, 1, 0)

caret::confusionMatrix(as.factor(pred_choice),as.factor(boston.test$crime01), positive = '1')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 48  4
##          1  2 48
##                                           
##                Accuracy : 0.9412          
##                  95% CI : (0.8764, 0.9781)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.8824          
##                                           
##  Mcnemar's Test P-Value : 0.6831          
##                                           
##             Sensitivity : 0.9231          
##             Specificity : 0.9600          
##          Pos Pred Value : 0.9600          
##          Neg Pred Value : 0.9231          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4706          
##    Detection Prevalence : 0.4902          
##       Balanced Accuracy : 0.9415          
##                                           
##        'Positive' Class : 1               
## 
mean(pred_choice != boston.test$crime01)
## [1] 0.05882353

Note that for this logistic regression with all predictors, we have a test error rate of 0.058 or 5.8%. Let’s now run the model again without chas and also remove other statistically insignificant variables.

glm.boston = glm(crime01~.-crim-chas-lstat-indus, data=boston.train, family = binomial)

summary(glm.boston)
## 
## Call:
## glm(formula = crime01 ~ . - crim - chas - lstat - indus, family = binomial, 
##     data = boston.train)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.1228  -0.1815  -0.0016   0.0026   3.3997  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -34.440231   6.908405  -4.985 6.19e-07 ***
## zn           -0.065102   0.033453  -1.946  0.05164 .  
## nox          43.262400   7.338494   5.895 3.74e-09 ***
## rm           -0.642364   0.719755  -0.892  0.37214    
## age           0.036465   0.013057   2.793  0.00522 ** 
## dis           0.776828   0.245617   3.163  0.00156 ** 
## rad           0.698457   0.160120   4.362 1.29e-05 ***
## tax          -0.007245   0.002737  -2.647  0.00811 ** 
## ptratio       0.441324   0.137801   3.203  0.00136 ** 
## black        -0.011230   0.006027  -1.863  0.06244 .  
## medv          0.206377   0.078413   2.632  0.00849 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 560.05  on 403  degrees of freedom
## Residual deviance: 173.98  on 393  degrees of freedom
## AIC: 195.98
## 
## Number of Fisher Scoring iterations: 9
pred = predict(glm.boston, boston.test, type="response")
pred_choice = ifelse(pred>0.5, 1, 0)

caret::confusionMatrix(as.factor(pred_choice),as.factor(boston.test$crime01), positive = '1')
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 47  4
##          1  3 48
##                                          
##                Accuracy : 0.9314         
##                  95% CI : (0.8637, 0.972)
##     No Information Rate : 0.5098         
##     P-Value [Acc > NIR] : <2e-16         
##                                          
##                   Kappa : 0.8627         
##                                          
##  Mcnemar's Test P-Value : 1              
##                                          
##             Sensitivity : 0.9231         
##             Specificity : 0.9400         
##          Pos Pred Value : 0.9412         
##          Neg Pred Value : 0.9216         
##              Prevalence : 0.5098         
##          Detection Rate : 0.4706         
##    Detection Prevalence : 0.5000         
##       Balanced Accuracy : 0.9315         
##                                          
##        'Positive' Class : 1              
## 
mean(pred_choice != boston.test$crime01)
## [1] 0.06862745

I removed various variables one by one and the best one removed the terms: crim, chas, lstat and indus. However, our test error is worse than with all predictors 0.068 as opposed to 0.058. Next we will try an lda model with all of the predictors.

lda.boston = lda(crime01~.-crim, data=boston.train)

pred = (predict(lda.boston, boston.test,type="response"))

caret::confusionMatrix(as.factor(pred$class),as.factor(boston.test$crime01), positive = "1")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 47 14
##          1  3 38
##                                           
##                Accuracy : 0.8333          
##                  95% CI : (0.7466, 0.8998)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : 8.823e-12       
##                                           
##                   Kappa : 0.6679          
##                                           
##  Mcnemar's Test P-Value : 0.01529         
##                                           
##             Sensitivity : 0.7308          
##             Specificity : 0.9400          
##          Pos Pred Value : 0.9268          
##          Neg Pred Value : 0.7705          
##              Prevalence : 0.5098          
##          Detection Rate : 0.3725          
##    Detection Prevalence : 0.4020          
##       Balanced Accuracy : 0.8354          
##                                           
##        'Positive' Class : 1               
## 
mean(pred$class != boston.test$crime01)
## [1] 0.1666667
lda.boston = lda(crime01~.-crim-chas-lstat-indus, data=boston.train)

pred = (predict(lda.boston, boston.test,type="response"))

caret::confusionMatrix(as.factor(pred$class),as.factor(boston.test$crime01), positive = "1")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 49 14
##          1  1 38
##                                           
##                Accuracy : 0.8529          
##                  95% CI : (0.7691, 0.9153)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : 3.353e-13       
##                                           
##                   Kappa : 0.7072          
##                                           
##  Mcnemar's Test P-Value : 0.001946        
##                                           
##             Sensitivity : 0.7308          
##             Specificity : 0.9800          
##          Pos Pred Value : 0.9744          
##          Neg Pred Value : 0.7778          
##              Prevalence : 0.5098          
##          Detection Rate : 0.3725          
##    Detection Prevalence : 0.3824          
##       Balanced Accuracy : 0.8554          
##                                           
##        'Positive' Class : 1               
## 
mean(pred$class != boston.test$crime01)
## [1] 0.1470588

Note here that we get a test error of 0.16 for all predictors and 0.14 after removing insignificant variables. Note that these values are still worse than our first logistic regression model with all predictors. Next, we will try a qda model:

qda.boston = qda(crime01~.-crim, data=boston.train)

pred = (predict(qda.boston, boston.test,type="response"))

caret::confusionMatrix(as.factor(pred$class),as.factor(boston.test$crime01), positive = "1")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 49  7
##          1  1 45
##                                           
##                Accuracy : 0.9216          
##                  95% CI : (0.8513, 0.9655)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.8434          
##                                           
##  Mcnemar's Test P-Value : 0.0771          
##                                           
##             Sensitivity : 0.8654          
##             Specificity : 0.9800          
##          Pos Pred Value : 0.9783          
##          Neg Pred Value : 0.8750          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4412          
##    Detection Prevalence : 0.4510          
##       Balanced Accuracy : 0.9227          
##                                           
##        'Positive' Class : 1               
## 
mean(pred$class != boston.test$crime01)
## [1] 0.07843137
qda.boston = qda(crime01~.-crim-chas-lstat-indus, data=boston.train)

pred = (predict(qda.boston, boston.test,type="response"))

caret::confusionMatrix(as.factor(pred$class),as.factor(boston.test$crime01), positive = "1")
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 49 12
##          1  1 40
##                                           
##                Accuracy : 0.8725          
##                  95% CI : (0.7919, 0.9304)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : 9.423e-15       
##                                           
##                   Kappa : 0.7461          
##                                           
##  Mcnemar's Test P-Value : 0.005546        
##                                           
##             Sensitivity : 0.7692          
##             Specificity : 0.9800          
##          Pos Pred Value : 0.9756          
##          Neg Pred Value : 0.8033          
##              Prevalence : 0.5098          
##          Detection Rate : 0.3922          
##    Detection Prevalence : 0.4020          
##       Balanced Accuracy : 0.8746          
##                                           
##        'Positive' Class : 1               
## 
mean(pred$class != boston.test$crime01)
## [1] 0.127451

For the qda method we get a 0.07 test error for all predictors and a 0.12 test error when removing insignificant predictors. Note these values are still less than our logistic regression model. We will now try knn classification.

train.x = cbind(boston.train$zn, boston.train$indus, boston.train$chas, boston.train$nox, boston.train$rm, boston.train$age, boston.train$dis, boston.train$rad, boston.train$tax, boston.train$ptratio, boston.train$black, boston.train$lstat, boston.train$medv)

test.x = cbind(boston.test$zn, boston.test$indus, boston.test$chas, boston.test$nox, boston.test$rm, boston.test$age, boston.test$dis, boston.test$rad, boston.test$tax, boston.test$ptratio, boston.test$black, boston.test$lstat, boston.test$medv)

train.crime01 = boston.train$crime01

knn_test_error = rep(0,10)


for (i in 1:10){
set.seed(100)
knn.boston <- knn(train.x,test.x, train.crime01, k=i)



knn_test_error[i] <- mean(knn.boston!=boston.test$crime01)
print(c("k equals:",i))
print(c("test error rate:",knn_test_error[i]))
print(caret::confusionMatrix(as.factor(knn.boston),as.factor(boston.test$crime01), positive = '1'))
}
## [1] "k equals:" "1"        
## [1] "test error rate:"   "0.0490196078431373"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 48  3
##          1  2 49
##                                           
##                Accuracy : 0.951           
##                  95% CI : (0.8893, 0.9839)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.902           
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9423          
##             Specificity : 0.9600          
##          Pos Pred Value : 0.9608          
##          Neg Pred Value : 0.9412          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4804          
##    Detection Prevalence : 0.5000          
##       Balanced Accuracy : 0.9512          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "2"        
## [1] "test error rate:"   "0.0784313725490196"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 47  5
##          1  3 47
##                                           
##                Accuracy : 0.9216          
##                  95% CI : (0.8513, 0.9655)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.8432          
##                                           
##  Mcnemar's Test P-Value : 0.7237          
##                                           
##             Sensitivity : 0.9038          
##             Specificity : 0.9400          
##          Pos Pred Value : 0.9400          
##          Neg Pred Value : 0.9038          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4608          
##    Detection Prevalence : 0.4902          
##       Balanced Accuracy : 0.9219          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "3"        
## [1] "test error rate:"   "0.0392156862745098"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 49  3
##          1  1 49
##                                           
##                Accuracy : 0.9608          
##                  95% CI : (0.9026, 0.9892)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.9216          
##                                           
##  Mcnemar's Test P-Value : 0.6171          
##                                           
##             Sensitivity : 0.9423          
##             Specificity : 0.9800          
##          Pos Pred Value : 0.9800          
##          Neg Pred Value : 0.9423          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4804          
##    Detection Prevalence : 0.4902          
##       Balanced Accuracy : 0.9612          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "4"        
## [1] "test error rate:"   "0.0588235294117647"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 47  3
##          1  3 49
##                                           
##                Accuracy : 0.9412          
##                  95% CI : (0.8764, 0.9781)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.8823          
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9423          
##             Specificity : 0.9400          
##          Pos Pred Value : 0.9423          
##          Neg Pred Value : 0.9400          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4804          
##    Detection Prevalence : 0.5098          
##       Balanced Accuracy : 0.9412          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "5"        
## [1] "test error rate:"   "0.0490196078431373"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 48  3
##          1  2 49
##                                           
##                Accuracy : 0.951           
##                  95% CI : (0.8893, 0.9839)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.902           
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9423          
##             Specificity : 0.9600          
##          Pos Pred Value : 0.9608          
##          Neg Pred Value : 0.9412          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4804          
##    Detection Prevalence : 0.5000          
##       Balanced Accuracy : 0.9512          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "6"        
## [1] "test error rate:"   "0.0490196078431373"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 48  3
##          1  2 49
##                                           
##                Accuracy : 0.951           
##                  95% CI : (0.8893, 0.9839)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.902           
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9423          
##             Specificity : 0.9600          
##          Pos Pred Value : 0.9608          
##          Neg Pred Value : 0.9412          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4804          
##    Detection Prevalence : 0.5000          
##       Balanced Accuracy : 0.9512          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "7"        
## [1] "test error rate:"   "0.0490196078431373"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 48  3
##          1  2 49
##                                           
##                Accuracy : 0.951           
##                  95% CI : (0.8893, 0.9839)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.902           
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9423          
##             Specificity : 0.9600          
##          Pos Pred Value : 0.9608          
##          Neg Pred Value : 0.9412          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4804          
##    Detection Prevalence : 0.5000          
##       Balanced Accuracy : 0.9512          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "8"        
## [1] "test error rate:"   "0.0490196078431373"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 48  3
##          1  2 49
##                                           
##                Accuracy : 0.951           
##                  95% CI : (0.8893, 0.9839)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.902           
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9423          
##             Specificity : 0.9600          
##          Pos Pred Value : 0.9608          
##          Neg Pred Value : 0.9412          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4804          
##    Detection Prevalence : 0.5000          
##       Balanced Accuracy : 0.9512          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "9"        
## [1] "test error rate:"   "0.0490196078431373"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 48  3
##          1  2 49
##                                           
##                Accuracy : 0.951           
##                  95% CI : (0.8893, 0.9839)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.902           
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9423          
##             Specificity : 0.9600          
##          Pos Pred Value : 0.9608          
##          Neg Pred Value : 0.9412          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4804          
##    Detection Prevalence : 0.5000          
##       Balanced Accuracy : 0.9512          
##                                           
##        'Positive' Class : 1               
##                                           
## [1] "k equals:" "10"       
## [1] "test error rate:"   "0.0490196078431373"
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction  0  1
##          0 48  3
##          1  2 49
##                                           
##                Accuracy : 0.951           
##                  95% CI : (0.8893, 0.9839)
##     No Information Rate : 0.5098          
##     P-Value [Acc > NIR] : <2e-16          
##                                           
##                   Kappa : 0.902           
##                                           
##  Mcnemar's Test P-Value : 1               
##                                           
##             Sensitivity : 0.9423          
##             Specificity : 0.9600          
##          Pos Pred Value : 0.9608          
##          Neg Pred Value : 0.9412          
##              Prevalence : 0.5098          
##          Detection Rate : 0.4804          
##    Detection Prevalence : 0.5000          
##       Balanced Accuracy : 0.9512          
##                                           
##        'Positive' Class : 1               
## 

Based on knn classification with all predictors, we get a test score error of 0.039 for k=3. Note that this is the best performing model out of all that we have tried. It is performing slightly better than our logistic regression model with all predictors, based on the value of test score error. All models were providing adequate sensitivity and specificity scores so i was judging prediction power primarily on the test score error.