Chapter 4

Exercise 10

This question should be answered using the Weekly dataset, 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?

data= Weekly
names(Weekly)
## [1] "Year"      "Lag1"      "Lag2"      "Lag3"      "Lag4"      "Lag5"     
## [7] "Volume"    "Today"     "Direction"
dim(Weekly)
## [1] 1089    9
summary(Weekly)
##       Year           Lag1               Lag2               Lag3         
##  Min.   :1990   Min.   :-18.1950   Min.   :-18.1950   Min.   :-18.1950  
##  1st Qu.:1995   1st Qu.: -1.1540   1st Qu.: -1.1540   1st Qu.: -1.1580  
##  Median :2000   Median :  0.2410   Median :  0.2410   Median :  0.2410  
##  Mean   :2000   Mean   :  0.1506   Mean   :  0.1511   Mean   :  0.1472  
##  3rd Qu.:2005   3rd Qu.:  1.4050   3rd Qu.:  1.4090   3rd Qu.:  1.4090  
##  Max.   :2010   Max.   : 12.0260   Max.   : 12.0260   Max.   : 12.0260  
##       Lag4               Lag5              Volume            Today         
##  Min.   :-18.1950   Min.   :-18.1950   Min.   :0.08747   Min.   :-18.1950  
##  1st Qu.: -1.1580   1st Qu.: -1.1660   1st Qu.:0.33202   1st Qu.: -1.1540  
##  Median :  0.2380   Median :  0.2340   Median :1.00268   Median :  0.2410  
##  Mean   :  0.1458   Mean   :  0.1399   Mean   :1.57462   Mean   :  0.1499  
##  3rd Qu.:  1.4090   3rd Qu.:  1.4050   3rd Qu.:2.05373   3rd Qu.:  1.4050  
##  Max.   : 12.0260   Max.   : 12.0260   Max.   :9.32821   Max.   : 12.0260  
##  Direction 
##  Down:484  
##  Up  :605  
##            
##            
##            
## 
pairs(Weekly)

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

The numerical and graphical data indicate that the data is normall distributed, there is no clear patterns among the data. From the summary we see that there is very little difference between the median and means between lags and the scatter plots indicate that there is no significant relationships between the variables with the exception of Year and Volume. Volume increase over time.

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

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

The smallest p-value here is associated with Lag2. The positive coefficient for this predictor suggests that if the market had a positive return yesterday, then it is likely to go up today. The p-value is 0.0296, demonstrates evidence of a real association between Lag2 and Direction.

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

logWeekly.probs=predict(weekly.fit, type="response")
logWeekly.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
contrasts(Weekly$Direction)
##      Up
## Down  0
## Up    1
logWeekly.pred=rep("Down" ,1089)
logWeekly.pred[logWeekly.probs >.5]="Up"
table(logWeekly.pred , Weekly$Direction )
##               
## logWeekly.pred Down  Up
##           Down   54  48
##           Up    430 557
mean(logWeekly.pred==Weekly$Direction)
## [1] 0.5610652

The diagonal elements of the confusion matrix indicate the correct predictions while the off-diagonals represent incorrect predictions. The model correctly predicted that the market would go down on 54 weeks and that it would go up on 557 weeks, for a total of 611 correct predictions. In this case, the logistic regression correctly predicted the movement of the market 56.1% of the time.

(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.0910<-Weekly[!train,]
Weekly.fit<-glm(Direction~Lag2, data=Weekly,family=binomial, subset=train)
logWeekly.prob= predict(Weekly.fit, Weekly.0910, type = "response")
logWeekly.pred = rep("Down", length(logWeekly.prob))
logWeekly.pred[logWeekly.prob > 0.5] = "Up"
Direction.0910 = Weekly$Direction[!train]
table(logWeekly.pred, Direction.0910)
##               Direction.0910
## logWeekly.pred Down Up
##           Down    9  5
##           Up     34 56
mean(logWeekly.pred == Direction.0910)
## [1] 0.625

When splitting up the whole Weekly datasheet into a training and test data set, the model correctly predicted weekly movement of the market 62.5% of the time. This is a moderate improvement from the model that utilized the whole data set.

(E) Repeat (d) using LDA.

ldaWeekly.fit<-lda(Direction~Lag2, data=Weekly,family=binomial, subset=train)
ldaWeekly.pred<-predict(ldaWeekly.fit, Weekly.0910)
table(ldaWeekly.pred$class, Direction.0910)
##       Direction.0910
##        Down Up
##   Down    9  5
##   Up     34 56
mean(ldaWeekly.pred$class==Direction.0910)
## [1] 0.625

The Linear Discriminant Analysis yielded similar results to the logistic regression analysis in section D. The model correctly predicted weekly movement of the market 62.5% of the time.

(F) Repeat (D) using QDA

qdaWeekly.fit = qda(Direction ~ Lag2, data = Weekly, subset = train)
qdaWeekly.pred = predict(qdaWeekly.fit, Weekly.0910)$class
table(qdaWeekly.pred, Direction.0910)
##               Direction.0910
## qdaWeekly.pred Down Up
##           Down    0  0
##           Up     43 61
mean(qdaWeekly.pred==Direction.0910)
## [1] 0.5865385

The Quadratic Linear Analysis results were lower than the Logistic Regression Analysis in section D. The model correctly predicted weekly movement of the market 58.6% of the time.

(G) Repeat (D) using KNN with K = 1

knnWeek.train=as.matrix(Weekly$Lag2[train])
knnWeek.test=as.matrix(Weekly$Lag2[!train])
train.Direction=Weekly$Direction[train]
set.seed(1)
knnWeek.pred=knn(knnWeek.train,knnWeek.test,train.Direction,k=1)
table(knnWeek.pred,Direction.0910)
##             Direction.0910
## knnWeek.pred Down Up
##         Down   21 30
##         Up     22 31
mean(knnWeek.pred == Direction.0910)
## [1] 0.5

This KNN analysis results were lower than the QDA, LDA, and the Logistic analysis. This model’s accuracy rate is 50%.

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

The method(s) that appear to provide the best results on this data are the Logistic Regression and Linear Discriminant Analyses. The methods both had an accuracy rate of 62.5%.

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

#Logistic Regression with interaction Lag2:Lag4
weekly.fit=glm(Direction~Lag2:Lag2+Lag4, data=Weekly,family=binomial)
logWeekly.probs=predict(weekly.fit, type="response")
logWeekly.pred=rep("Down" ,1089)
logWeekly.pred[logWeekly.probs >.5]="Up"
table(logWeekly.pred , Weekly$Direction )
##               
## logWeekly.pred Down  Up
##           Down   33  25
##           Up    451 580
mean(logWeekly.pred==Weekly$Direction)
## [1] 0.5629017

The accuracy of this model is 56.2%

#LDA with Interaction Lag2:Lag4
ldaWeekly.fit<-lda(Direction~Lag2:Lag2+Lag4, data=Weekly,family=binomial, subset=train)
ldaWeekly.pred<-predict(ldaWeekly.fit, Weekly.0910)
table(ldaWeekly.pred$class, Direction.0910)
##       Direction.0910
##        Down Up
##   Down    8  4
##   Up     35 57
mean(ldaWeekly.pred$class==Direction.0910)
## [1] 0.625

The accuracy of this model is 62.5%.

#QDA with interactions lag2:lag4
qdaWeekly.fit = qda(Direction ~ Lag2:Lag2+Lag4, data = Weekly, subset = train)
qdaWeekly.pred = predict(qdaWeekly.fit, Weekly.0910)$class
table(qdaWeekly.pred, Direction.0910)
##               Direction.0910
## qdaWeekly.pred Down Up
##           Down    9 14
##           Up     34 47
mean(qdaWeekly.pred==Direction.0910)
## [1] 0.5384615

The accuracy of this model is 53.8%.

#KNN with K=4
knnWeek.train=as.matrix(Weekly$Lag2[train])
knnWeek.test=as.matrix(Weekly$Lag2[!train])
train.Direction=Weekly$Direction[train]
set.seed(1)
knnWeek.pred=knn(knnWeek.train,knnWeek.test,train.Direction,k=4)
table(knnWeek.pred,Direction.0910)
##             Direction.0910
## knnWeek.pred Down Up
##         Down   20 17
##         Up     23 44
(mean(knnWeek.pred == Direction.0910))
## [1] 0.6153846

The accuracy of this model is 61.5%.

Using the Logistic Regression to test several transformations it was determined that using the interaction between Lag2 and Lag4 provided the best results. This interaction was also used in the LDA and QDA models. The KNN model produced the best result when K=4. Comparing the accuracy of these new models with the original accuracy results identified that none of the transformation results were better then the original results. In fact, the LDA model with Lag2 and Lag4 interactions provided the same accuracy as the LDA model without the interaction: 62.5%.

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

data=Auto
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

(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
Auto2 = data.frame(Auto, mpg01)

(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? Scatter plots and box plots may be useful tools to answer this question. Describe your findings.

corrplot(cor(Auto2[,-9]), method="circle")

There are three variables that have a strong negative correlation with mpg01: cylinders, displacement, and weight. Two variables have a moderate correlation with mpg01: horsepower(negative correlation) and origin (positive correlation).

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

# Split data into 80% training, 20% test and make sure my test data has same proportion 
set.seed(1)
split = initial_split(Auto2, strata = mpg01, prop = .8)
train.Auto2 = training(split)
test.Auto2 = testing (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?

ldaAuto2.fit <- lda(mpg01~displacement+horsepower+weight+year+cylinders+origin, data=train.Auto2)
ldaAuto2.pred <- predict(ldaAuto2.fit, test.Auto2)
lda_cm = table(ldaAuto2.pred$class, test.Auto2$mpg01)

(error= 1-(sum (diag(lda_cm))/sum(lda_cm)))
## [1] 0.1125

The test error rate is 11.25% for this model.

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

qdaAuto2.fit <- qda(mpg01~displacement+horsepower+weight+year+cylinders+origin, data=train.Auto2)
qdaAuto2.pred <- predict(qdaAuto2.fit, test.Auto2)
(qda_cm=table(qdaAuto2.pred$class, test.Auto2$mpg01))
##    
##      0  1
##   0 32  5
##   1  8 35
(error= 1-(sum (diag(qda_cm))/sum(qda_cm)))
## [1] 0.1625

The test error rate is 16.25% for this model.

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

logAuto2.fit<-glm(mpg01~displacement+horsepower+weight+year+cylinders+origin, data=train.Auto2,family=binomial)
logAuto2.probs = predict(logAuto2.fit, test.Auto2, type = "response")
logAuto2.pred = rep(0, length(logAuto2.probs))
logAuto2.pred[logAuto2.probs > 0.5] = 1
(log_cm=table(logAuto2.pred, test.Auto2$mpg01))
##              
## logAuto2.pred  0  1
##             0 33  6
##             1  7 34
(error= 1-(sum (diag(log_cm))/sum(log_cm)))
## [1] 0.1625

The test error rate is 16.25% for this model.

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

k_values = c(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)

my_accuracy = double()
for (i in k_values){
  kknn_Auto2=kknn(mpg01~cylinders + displacement + weight, train=train.Auto2, test=test.Auto2, k=i)
  CM_tempauto = table(kknn_Auto2$fitted, test.Auto2$mpg01)
  accuracy_tempauto = sum(diag(CM_tempauto))/sum(CM_tempauto)
  my_accuracy = c(my_accuracy,accuracy_tempauto)
}

(myresults = data.frame( k = k_values, accuracy = my_accuracy))
##     k accuracy
## 1   3   0.3750
## 2   4   0.3625
## 3   5   0.3625
## 4   6   0.3625
## 5   7   0.3500
## 6   8   0.3500
## 7   9   0.3375
## 8  10   0.3250
## 9  11   0.3250
## 10 12   0.3250
## 11 13   0.2750
## 12 14   0.2625
## 13 15   0.2625

The K Nearest Neighbors results identified k=3 has the highest accuracy at 37.5%. As K increases the accuracy decreases in this model.

##Exercise 13

Using the Boston data set, fit classification models in order to predict whether a given suburb has a crime rate above or below the median. Explore logistic regression, LDA, and KNN models using various subsets of the predictors. Describe your findings.

data=Boston
summary(Boston)
##       crim                zn             indus            chas        
##  Min.   : 0.00632   Min.   :  0.00   Min.   : 0.46   Min.   :0.00000  
##  1st Qu.: 0.08205   1st Qu.:  0.00   1st Qu.: 5.19   1st Qu.:0.00000  
##  Median : 0.25651   Median :  0.00   Median : 9.69   Median :0.00000  
##  Mean   : 3.61352   Mean   : 11.36   Mean   :11.14   Mean   :0.06917  
##  3rd Qu.: 3.67708   3rd Qu.: 12.50   3rd Qu.:18.10   3rd Qu.:0.00000  
##  Max.   :88.97620   Max.   :100.00   Max.   :27.74   Max.   :1.00000  
##       nox               rm             age              dis        
##  Min.   :0.3850   Min.   :3.561   Min.   :  2.90   Min.   : 1.130  
##  1st Qu.:0.4490   1st Qu.:5.886   1st Qu.: 45.02   1st Qu.: 2.100  
##  Median :0.5380   Median :6.208   Median : 77.50   Median : 3.207  
##  Mean   :0.5547   Mean   :6.285   Mean   : 68.57   Mean   : 3.795  
##  3rd Qu.:0.6240   3rd Qu.:6.623   3rd Qu.: 94.08   3rd Qu.: 5.188  
##  Max.   :0.8710   Max.   :8.780   Max.   :100.00   Max.   :12.127  
##       rad              tax           ptratio          black       
##  Min.   : 1.000   Min.   :187.0   Min.   :12.60   Min.   :  0.32  
##  1st Qu.: 4.000   1st Qu.:279.0   1st Qu.:17.40   1st Qu.:375.38  
##  Median : 5.000   Median :330.0   Median :19.05   Median :391.44  
##  Mean   : 9.549   Mean   :408.2   Mean   :18.46   Mean   :356.67  
##  3rd Qu.:24.000   3rd Qu.:666.0   3rd Qu.:20.20   3rd Qu.:396.23  
##  Max.   :24.000   Max.   :711.0   Max.   :22.00   Max.   :396.90  
##      lstat            medv      
##  Min.   : 1.73   Min.   : 5.00  
##  1st Qu.: 6.95   1st Qu.:17.02  
##  Median :11.36   Median :21.20  
##  Mean   :12.65   Mean   :22.53  
##  3rd Qu.:16.95   3rd Qu.:25.00  
##  Max.   :37.97   Max.   :50.00

Create the variable Crime.

crime01 <- rep(0, length(Boston$crim))
crime01[Boston$crim > median(Boston$crim)] <- 1
Boston= data.frame(Boston,crime01)

Split the data set into 80% training and 20% test.

set.seed(1)
split_Boston = initial_split(Boston, strata = crime01, prop = .8)
train_Boston = training(split_Boston)
test_Boston = testing (split_Boston)

Review and identify correlations to the variable crime01

corrplot(cor(Boston), method="circle")

The variables with the strongest correlation to crime01 are nox, rad, tax, indus, age and dis

###Logistic Regression

Boston_fit <-glm(crime01~indus+nox+age+dis+rad+tax, data=train_Boston, family=binomial)
Boston_probs = predict(Boston_fit, test_Boston, type = "response")
Boston_pred = rep(0, length(Boston_probs))
Boston_pred[Boston_probs > 0.5] = 1
(Boston_CM=table(Boston_pred, test_Boston$crime01))
##            
## Boston_pred  0  1
##           0 47 10
##           1  4 41
(sum (diag(Boston_CM))/sum(Boston_CM))
## [1] 0.8627451
summary(Boston_fit)
## 
## Call:
## glm(formula = crime01 ~ indus + nox + age + dis + rad + tax, 
##     family = binomial, data = train_Boston)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -2.03855  -0.29638  -0.01332   0.00746   2.72335  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -25.185374   4.178905  -6.027 1.67e-09 ***
## indus        -0.060518   0.046628  -1.298  0.19433    
## nox          44.361188   7.730513   5.738 9.55e-09 ***
## age           0.004101   0.009408   0.436  0.66293    
## dis           0.191673   0.159352   1.203  0.22904    
## rad           0.602396   0.131556   4.579 4.67e-06 ***
## tax          -0.006678   0.002483  -2.689  0.00716 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 560.06  on 403  degrees of freedom
## Residual deviance: 200.21  on 397  degrees of freedom
## AIC: 214.21
## 
## Number of Fisher Scoring iterations: 8

###Linear Discriminat Analysis

Boston_lda_fit <-lda(crime01~ indus+nox+age+dis+rad+tax, data=train_Boston,family=binomial)
Boston_lda_pred = predict(Boston_lda_fit, test_Boston)
(Boston_lda_CM=table(Boston_lda_pred$class, test_Boston$crime01))
##    
##      0  1
##   0 49 14
##   1  2 37
(sum (diag(Boston_lda_CM))/sum(Boston_lda_CM))
## [1] 0.8431373

###K Nearest Neighbors

k_values = c(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)

my_accuracy = double()
for (i in k_values){
  kknn_Boston=kknn(crime01~indus+nox+age+dis+rad+tax, train=train_Boston, test=test_Boston, k=i)
  Boston_temp_CM = table(kknn_Boston$fitted, test_Boston$crime01)
  Boston_temp_accuracy = sum(diag(Boston_temp_CM))/sum(Boston_temp_CM)
  my_accuracy = c(my_accuracy,Boston_temp_accuracy)
}

(myresults = data.frame( k = k_values, accuracy = my_accuracy))
##     k  accuracy
## 1   3 0.4607843
## 2   4 0.4411765
## 3   5 0.3921569
## 4   6 0.3823529
## 5   7 0.3431373
## 6   8 0.3333333
## 7   9 0.3137255
## 8  10 0.3039216
## 9  11 0.2647059
## 10 12 0.2450980
## 11 13 0.2352941
## 12 14 0.2352941
## 13 15 0.2352941

All of the modeling methods used the same variables. The variables were obtained using corrplot which provided a visual representation of all variables and their correlations to one another. In this case the focus was on significant correlations to the variable the crime01. Out of the Logistic Regression, LDA, and KNN models the one with the greatest accuracy is the Logistic Regression model. It achieved an accuracy of 86.27%. Looking closer at this model we see that variables that are statistically significant are tax, rad, and nox. For classification modeling, KNN identified that K=3 had the highest accuracy rate of 46.07%.