Chapter 04 (pg 189): 13, 14, 16
This question should be answered using the Weekly data set, which is part of the ISLR2 package. This data is similar in nature to the Smarket data from this chapter’s lab, except that it contains 1,089 weekly returns for 21 years, from the beginning of 1990 to the end of 2010.
library(ISLR2)
library(MASS)
##
## Attaching package: 'MASS'
## The following object is masked from 'package:ISLR2':
##
## Boston
library(e1071)
library(class)
attach(Weekly)
head(Weekly)
## Year Lag1 Lag2 Lag3 Lag4 Lag5 Volume Today Direction
## 1 1990 0.816 1.572 -3.936 -0.229 -3.484 0.1549760 -0.270 Down
## 2 1990 -0.270 0.816 1.572 -3.936 -0.229 0.1485740 -2.576 Down
## 3 1990 -2.576 -0.270 0.816 1.572 -3.936 0.1598375 3.514 Up
## 4 1990 3.514 -2.576 -0.270 0.816 1.572 0.1616300 0.712 Up
## 5 1990 0.712 3.514 -2.576 -0.270 0.816 0.1537280 1.178 Up
## 6 1990 1.178 0.712 3.514 -2.576 -0.270 0.1544440 -1.372 Down
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
Because the “Up’ direction occurs more frequently than”Down”, this data could suggest that there is a upward trend in the Weekly data during the 1990-2010 period. All the Lags have similar statistics, indicating that the market may be relatively stable during this period. Volume and Year are strongly correlated, however the lag variables and today’s returns have very little correlation.
full_model <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume, data = Weekly, family = binomial)
summary(full_model)
##
## 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
There is only one predictor that is significant at the 0.05 level, which is Lag2.
probs <- predict(full_model, type = "response")
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(Direction)
## Up
## Down 0
## Up 1
pred = rep("Down", 1089)
pred[probs >.5] ="Up"
table(pred, Direction)
## Direction
## pred Down Up
## Down 54 48
## Up 430 557
mean(pred == Direction)
## [1] 0.5610652
Based on the confusion matrix, the model has an accuracy of ~56%. It does a good job predicting true positives, however it does struggle with false positives. It seems there is a bias toward the “Up” direction.
train = (Year<2009)
Weekly.test = Weekly[!train,]
Direction.test = Direction[!train]
dim(Weekly.test)
## [1] 104 9
model2 <- glm(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
probs = predict(model2, Weekly.test, type = "response")
pred = rep("Down", 104)
pred[probs>.5]="Up"
table(pred, Direction.test)
## Direction.test
## pred Down Up
## Down 9 5
## Up 34 56
mean(pred == Direction.test)
## [1] 0.625
This means that this model performed better than the first model with a ~62.5 accuracy. This model still favors the “Up” direction and has more false positives. This suggests that even with delegated training period and only using the significant predictor, there is still a class imbalance causing bias.
ldamodel <- lda(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
ldamodel
## Call:
## lda(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
##
## Prior probabilities of groups:
## Down Up
## 0.4477157 0.5522843
##
## Group means:
## Lag2
## Down -0.03568254
## Up 0.26036581
##
## Coefficients of linear discriminants:
## LD1
## Lag2 0.4414162
lda.pred = predict(ldamodel, Weekly.test)
lda.class=lda.pred$class
table(lda.class, Direction.test)
## Direction.test
## lda.class Down Up
## Down 9 5
## Up 34 56
mean(lda.class == Direction.test)
## [1] 0.625
This means that this model performed better than the first model with a ~62.5% accuracy. It also performed exactly the same as the logistic regression with only Lag2. This model still favors the “Up” direction and has more false positives. This suggests that even with delegated training period and only using the significant predictor, there is still a class imbalance causing bias.
qdamodel <- qda(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
qdamodel
## Call:
## qda(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
##
## Prior probabilities of groups:
## Down Up
## 0.4477157 0.5522843
##
## Group means:
## Lag2
## Down -0.03568254
## Up 0.26036581
qda.class = predict(qdamodel, Weekly.test)$class
table(qda.class, Direction.test)
## Direction.test
## qda.class Down Up
## Down 0 0
## Up 43 61
mean(qda.class == Direction.test)
## [1] 0.5865385
This means that this model performed better than the first model with a ~58.7% accuracy. However, it did perform worse than the logistic regression with only Log2 and LDA. This model still favors the “Up” direction and has a lot of false positives. This suggests that even with delegated training period and only using the significant predictor, there is still a class imbalance causing bias.
train.X = as.matrix(Lag2[train])
test.X = as.matrix(Lag2[!train])
train.Direction = Direction[train]
test.Direction = Direction[!train]
set.seed(1)
knn.pred = knn(train.X, test.X, train.Direction, k=1)
table(knn.pred, test.Direction)
## test.Direction
## knn.pred Down Up
## Down 21 30
## Up 22 31
mean(knn.pred == test.Direction)
## [1] 0.5
This means that this model performed worse than any other model with a ~50% accuracy. This model still favors the “Up” direction and has the most of false negatives. This suggests that even with delegated training period and only using the significant predictor, there is still a class imbalance causing bias.
nb <- naiveBayes(Direction~Lag2, data = Weekly, subset=train)
nb
##
## Naive Bayes Classifier for Discrete Predictors
##
## Call:
## naiveBayes.default(x = X, y = Y, laplace = laplace)
##
## A-priori probabilities:
## Y
## Down Up
## 0.4477157 0.5522843
##
## Conditional probabilities:
## Lag2
## Y [,1] [,2]
## Down -0.03568254 2.199504
## Up 0.26036581 2.317485
pred.nb <- predict(nb, Weekly.test)
table(pred.nb, Direction.test)
## Direction.test
## pred.nb Down Up
## Down 0 0
## Up 43 61
mean(pred.nb == Direction.test)
## [1] 0.5865385
This means that this model performed better than the first model and the KNN model with a ~58.7 accuracy. It does the same as the QDA. It performs worse than the logistic regression with only Lag2 and the LDA. This model still favors the “Up” direction and has more false positives. This suggests that even with delegated training period and only using the significant predictor, there is still a class imbalance causing bias.
The logistic regression with only Lag2 and the LDA have the best results on this data and the highest accuracy. This is followed by the QDA and Naive Bayes, and lastly the full logistic model and KNN.
Here is an interaction term between Lag 2 and Lag 3:
model_interact1 <- glm(Direction ~ Lag2 * Lag3, data = Weekly, family = binomial, subset = train)
probs_interact1 = predict(model_interact1, Weekly.test, type = "response")
pred_interact1 = rep("Down", 104)
pred_interact1[probs_interact1 > .5] = "Up"
table(pred_interact1, Direction.test)
## Direction.test
## pred_interact1 Down Up
## Down 8 4
## Up 35 57
mean(pred_interact1 == Direction.test)
## [1] 0.625
This means that this model performed worse than the logistic model with only Lag2 with a ~60.58 accuracy. This model still favors the “Up” direction and has more false positives. This suggests that even with delegated training period and adding an interaction term between Lag2 and Lag3, there is still a class imbalance causing bias.
Here is KNN with K= 3, 10, 15:
set.seed(1)
knn_pred_3 = knn(train.X, test.X, train.Direction, k = 3)
mean(knn_pred_3 == test.Direction)
## [1] 0.5480769
knn_pred_10 = knn(train.X, test.X, train.Direction, k = 10)
mean(knn_pred_10 == test.Direction)
## [1] 0.5865385
knn_pred_15 = knn(train.X, test.X, train.Direction, k = 15)
mean(knn_pred_15 == test.Direction)
## [1] 0.5865385
This means that all 3 of these KNNs performed better than the k=1 model with the best being k=10 and k=15 a ~58.65% accuracy.
Here is an LDA with Lag1, Lag2, and Volume:
lda_model2 <- lda(Direction ~ Lag1 + Lag2 + Volume, data = Weekly, subset = train)
lda_pred2 = predict(lda_model2, Weekly.test)$class
table(lda_pred2, Direction.test)
## Direction.test
## lda_pred2 Down Up
## Down 27 33
## Up 16 28
mean(lda_pred2 == Direction.test)
## [1] 0.5288462
This means that this model performed worse than the first LDA model with a ~58.65% accuracy.. This model still favors the “Up” direction and has more false positives. This suggests that even with delegated training period and only using the significant predictor, there is still a class imbalance causing bias.
This proves that our original logistic regression with only Lag2 and the LDA have the best results on this data and the highest accuracy.
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.
attach(Auto)
mpg01<- median(Auto$mpg)
mpg01 <- ifelse(Auto$mpg > mpg01, 1, 0)
Auto<-data.frame(Auto,mpg01)
head(Auto)
## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name mpg01
## 1 chevrolet chevelle malibu 0
## 2 buick skylark 320 0
## 3 plymouth satellite 0
## 4 amc rebel sst 0
## 5 ford torino 0
## 6 ford galaxie 500 0
pairs(Auto)
cor(Auto[,-9])
## mpg cylinders displacement horsepower weight
## mpg 1.0000000 -0.7776175 -0.8051269 -0.7784268 -0.8322442
## cylinders -0.7776175 1.0000000 0.9508233 0.8429834 0.8975273
## displacement -0.8051269 0.9508233 1.0000000 0.8972570 0.9329944
## horsepower -0.7784268 0.8429834 0.8972570 1.0000000 0.8645377
## weight -0.8322442 0.8975273 0.9329944 0.8645377 1.0000000
## acceleration 0.4233285 -0.5046834 -0.5438005 -0.6891955 -0.4168392
## year 0.5805410 -0.3456474 -0.3698552 -0.4163615 -0.3091199
## origin 0.5652088 -0.5689316 -0.6145351 -0.4551715 -0.5850054
## mpg01 0.8369392 -0.7591939 -0.7534766 -0.6670526 -0.7577566
## acceleration year origin mpg01
## mpg 0.4233285 0.5805410 0.5652088 0.8369392
## cylinders -0.5046834 -0.3456474 -0.5689316 -0.7591939
## displacement -0.5438005 -0.3698552 -0.6145351 -0.7534766
## horsepower -0.6891955 -0.4163615 -0.4551715 -0.6670526
## weight -0.4168392 -0.3091199 -0.5850054 -0.7577566
## acceleration 1.0000000 0.2903161 0.2127458 0.3468215
## year 0.2903161 1.0000000 0.1815277 0.4299042
## origin 0.2127458 0.1815277 1.0000000 0.5136984
## mpg01 0.3468215 0.4299042 0.5136984 1.0000000
par(mfrow = c(2,2))
boxplot(cylinders ~ mpg01, data = Auto, main = "MPG01 by Cylinders")
boxplot(displacement ~ mpg01, data = Auto, main = "MPG01 by Displacement")
boxplot(horsepower ~ mpg01, data = Auto, main = "MPG01 by Horsepower")
boxplot(weight ~ mpg01, data = Auto, main = "MPG01 by Weight")
The correlation matrix for this dataset provides us insight that MPG01
is negatively correlated with variables: cylinders, displacement,
horsepower, and weight. After producing boxplots of these variables, we
can also tell that when the mpg is above the median (1), then these
variables will be lower than when mpg is below the median (0).
set.seed(1)
train <- sample(nrow(Auto), size = 0.7*nrow(Auto))
train_set <- Auto[train,]
test_set <- Auto[-train,]
mpg01.test<-mpg01[-train]
lda_mpg01 <- lda(mpg01 ~ cylinders + displacement + horsepower + weight, data = train_set)
lda_pred_mpg01<- predict(lda_mpg01, test_set)
lda_class_mpg01<-lda_pred_mpg01$class
table(lda_class_mpg01, mpg01.test)
## mpg01.test
## lda_class_mpg01 0 1
## 0 50 3
## 1 11 54
1-mean(lda_class_mpg01 == mpg01.test)
## [1] 0.1186441
There is an approximate 11.86% test error of the model.
qda_mpg01 <- qda(mpg01 ~ cylinders + displacement + horsepower + weight, data = train_set)
qda_pred_mpg01 <- predict(qda_mpg01, test_set)
qda_class_mpg01 <- qda_pred_mpg01$class
table(qda_class_mpg01, mpg01.test)
## mpg01.test
## qda_class_mpg01 0 1
## 0 52 5
## 1 9 52
1-mean(qda_class_mpg01 == mpg01.test)
## [1] 0.1186441
Same as the LDA, the QDA approximated an 11.86% test error of the model.
log_mpg01 <- glm(mpg01 ~ cylinders + displacement + horsepower + weight, data = train_set, family = binomial)
log_pred_mpg01 <- predict(log_mpg01, test_set, type = "response")
log_class_mpg01 <- ifelse(log_pred_mpg01 > 0.5, 1, 0)
table(log_class_mpg01,mpg01.test)
## mpg01.test
## log_class_mpg01 0 1
## 0 53 3
## 1 8 54
1-mean(log_class_mpg01 == mpg01.test)
## [1] 0.09322034
The logistic regression model approximated an 9.32% test error of the model.
nb_mpg01 <- naiveBayes(mpg01 ~ cylinders + displacement + horsepower + weight, data = train_set)
nb_pred_mpg01 <- predict(nb_mpg01, test_set)
table(nb_pred_mpg01, mpg01.test)
## mpg01.test
## nb_pred_mpg01 0 1
## 0 52 4
## 1 9 53
1-mean(nb_pred_mpg01 == mpg01.test)
## [1] 0.1101695
The naive Bayes model approximated an 11.02% test error of the model.
train.X <- cbind(cylinders, displacement, horsepower, weight)[train,]
test.X <- cbind(cylinders, displacement, horsepower, weight)[-train,]
train.mpg01 <- mpg01[train]
set.seed(1)
pred.knn <-knn(train.X, test.X, train.mpg01, k=1)
table(pred.knn, mpg01.test)
## mpg01.test
## pred.knn 0 1
## 0 51 6
## 1 10 51
1 - mean(pred.knn == mpg01.test)
## [1] 0.1355932
The KNN model for K = 1 has a test error rate of approximately 13.56%.
pred.knn <-knn(train.X, test.X, train.mpg01, k=100)
table(pred.knn, mpg01.test)
## mpg01.test
## pred.knn 0 1
## 0 50 7
## 1 11 50
1 - mean(pred.knn == mpg01.test)
## [1] 0.1525424
The KNN model for K = 100 has a test error rate of approximately 15.25%.
pred.knn <-knn(train.X, test.X, train.mpg01, k=50)
table(pred.knn, mpg01.test)
## mpg01.test
## pred.knn 0 1
## 0 49 7
## 1 12 50
1 - mean(pred.knn == mpg01.test)
## [1] 0.1610169
The KNN model for K = 50 has a test error rate of approximately 15.25%.
pred.knn <-knn(train.X, test.X, train.mpg01, k=15)
table(pred.knn, mpg01.test)
## mpg01.test
## pred.knn 0 1
## 0 49 5
## 1 12 52
1 - mean(pred.knn == mpg01.test)
## [1] 0.1440678
The KNN model for K = 15 has a test error rate of approximately 14.41%.
pred.knn <-knn(train.X, test.X, train.mpg01, k=5)
table(pred.knn, mpg01.test)
## mpg01.test
## pred.knn 0 1
## 0 51 5
## 1 10 52
1 - mean(pred.knn == mpg01.test)
## [1] 0.1271186
The KNN model for K = 5 has a test error rate of approximately 12.71%.
pred.knn <-knn(train.X, test.X, train.mpg01, k=3)
table(pred.knn, mpg01.test)
## mpg01.test
## pred.knn 0 1
## 0 52 4
## 1 9 53
1 - mean(pred.knn == mpg01.test)
## [1] 0.1101695
The KNN model for K = 3 has a test error rate of approximately 11.02%.
pred.knn <-knn(train.X, test.X, train.mpg01, k=2)
table(pred.knn, mpg01.test)
## mpg01.test
## pred.knn 0 1
## 0 52 6
## 1 9 51
1 - mean(pred.knn == mpg01.test)
## [1] 0.1271186
The KNN model for K = 3 has a test error rate of approximately 14.41%.
Answer: From the KNN models I tried, the K = 3 model had the lowest error rate of 11.02% making it the best model.
Using the Boston data set, fit classification models in order to predict whether a given census tract has a crime rate above or below the median. Explore logistic regression, LDA, naive Bayes, and KNN models using various subsets of the predictors. Describe your findings. Hint: You will have to create the response variable yourself, using the variables that are contained in the Boston data set
attach(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
high_crim<- median(Boston$crim)
high_crim <- ifelse(Boston$crim > high_crim, 1, 0)
Boston<-data.frame(Boston,high_crim)
head(Boston)
## crim zn indus chas nox rm age dis rad tax ptratio black lstat
## 1 0.00632 18 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98
## 2 0.02731 0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14
## 3 0.02729 0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03
## 4 0.03237 0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94
## 5 0.06905 0 2.18 0 0.458 7.147 54.2 6.0622 3 222 18.7 396.90 5.33
## 6 0.02985 0 2.18 0 0.458 6.430 58.7 6.0622 3 222 18.7 394.12 5.21
## medv high_crim
## 1 24.0 0
## 2 21.6 0
## 3 34.7 0
## 4 33.4 0
## 5 36.2 0
## 6 28.7 0
cor(Boston)
## crim zn indus chas nox
## crim 1.00000000 -0.20046922 0.40658341 -0.055891582 0.42097171
## zn -0.20046922 1.00000000 -0.53382819 -0.042696719 -0.51660371
## indus 0.40658341 -0.53382819 1.00000000 0.062938027 0.76365145
## chas -0.05589158 -0.04269672 0.06293803 1.000000000 0.09120281
## nox 0.42097171 -0.51660371 0.76365145 0.091202807 1.00000000
## rm -0.21924670 0.31199059 -0.39167585 0.091251225 -0.30218819
## age 0.35273425 -0.56953734 0.64477851 0.086517774 0.73147010
## dis -0.37967009 0.66440822 -0.70802699 -0.099175780 -0.76923011
## rad 0.62550515 -0.31194783 0.59512927 -0.007368241 0.61144056
## tax 0.58276431 -0.31456332 0.72076018 -0.035586518 0.66802320
## ptratio 0.28994558 -0.39167855 0.38324756 -0.121515174 0.18893268
## black -0.38506394 0.17552032 -0.35697654 0.048788485 -0.38005064
## lstat 0.45562148 -0.41299457 0.60379972 -0.053929298 0.59087892
## medv -0.38830461 0.36044534 -0.48372516 0.175260177 -0.42732077
## high_crim 0.40939545 -0.43615103 0.60326017 0.070096774 0.72323480
## rm age dis rad tax
## crim -0.21924670 0.35273425 -0.37967009 0.625505145 0.58276431
## zn 0.31199059 -0.56953734 0.66440822 -0.311947826 -0.31456332
## indus -0.39167585 0.64477851 -0.70802699 0.595129275 0.72076018
## chas 0.09125123 0.08651777 -0.09917578 -0.007368241 -0.03558652
## nox -0.30218819 0.73147010 -0.76923011 0.611440563 0.66802320
## rm 1.00000000 -0.24026493 0.20524621 -0.209846668 -0.29204783
## age -0.24026493 1.00000000 -0.74788054 0.456022452 0.50645559
## dis 0.20524621 -0.74788054 1.00000000 -0.494587930 -0.53443158
## rad -0.20984667 0.45602245 -0.49458793 1.000000000 0.91022819
## tax -0.29204783 0.50645559 -0.53443158 0.910228189 1.00000000
## ptratio -0.35550149 0.26151501 -0.23247054 0.464741179 0.46085304
## black 0.12806864 -0.27353398 0.29151167 -0.444412816 -0.44180801
## lstat -0.61380827 0.60233853 -0.49699583 0.488676335 0.54399341
## medv 0.69535995 -0.37695457 0.24992873 -0.381626231 -0.46853593
## high_crim -0.15637178 0.61393992 -0.61634164 0.619786249 0.60874128
## ptratio black lstat medv high_crim
## crim 0.2899456 -0.38506394 0.4556215 -0.3883046 0.40939545
## zn -0.3916785 0.17552032 -0.4129946 0.3604453 -0.43615103
## indus 0.3832476 -0.35697654 0.6037997 -0.4837252 0.60326017
## chas -0.1215152 0.04878848 -0.0539293 0.1752602 0.07009677
## nox 0.1889327 -0.38005064 0.5908789 -0.4273208 0.72323480
## rm -0.3555015 0.12806864 -0.6138083 0.6953599 -0.15637178
## age 0.2615150 -0.27353398 0.6023385 -0.3769546 0.61393992
## dis -0.2324705 0.29151167 -0.4969958 0.2499287 -0.61634164
## rad 0.4647412 -0.44441282 0.4886763 -0.3816262 0.61978625
## tax 0.4608530 -0.44180801 0.5439934 -0.4685359 0.60874128
## ptratio 1.0000000 -0.17738330 0.3740443 -0.5077867 0.25356836
## black -0.1773833 1.00000000 -0.3660869 0.3334608 -0.35121093
## lstat 0.3740443 -0.36608690 1.0000000 -0.7376627 0.45326273
## medv -0.5077867 0.33346082 -0.7376627 1.0000000 -0.26301673
## high_crim 0.2535684 -0.35121093 0.4532627 -0.2630167 1.00000000
crim, indus, nox, age, dis, rad, and tax seem somewhat correlated with high_crim.
set.seed(1)
train <- sample(nrow(Boston), size = 0.7*nrow(Boston))
train_set <- Boston[train,]
test_set <- Boston[-train,]
high_crim.test<-high_crim[-train]
log_high_crim <- glm(high_crim ~ . -high_crim - crim, data = train_set, family = binomial)
log_pred_high_crim <- predict(log_high_crim, test_set, type = "response")
log_class_high_crim <- ifelse(log_pred_high_crim > 0.5, 1, 0)
table(log_class_high_crim,high_crim.test)
## high_crim.test
## log_class_high_crim 0 1
## 0 62 5
## 1 11 74
1- mean(log_class_high_crim == high_crim.test)
## [1] 0.1052632
The full logistic regression model approximated an 10.53% test error of the model.
log_high_crim2 <- glm(high_crim ~ . -high_crim - crim -rm -lstat -medv, data = train_set, family = binomial)
log_pred_high_crim2 <- predict(log_high_crim2, test_set, type = "response")
log_class_high_crim2 <- ifelse(log_pred_high_crim2 > 0.5, 1, 0)
table(log_class_high_crim2,high_crim.test)
## high_crim.test
## log_class_high_crim2 0 1
## 0 61 5
## 1 12 74
1- mean(log_class_high_crim2 == high_crim.test)
## [1] 0.1118421
The logistic model without the variables approximated an 11.18% test error of the model, indicating that the full model had a slightly higher accuracy.
lda_high_crim <- lda(high_crim ~ . -high_crim - crim, data = train_set)
lda_pred_high_crim<- predict(lda_high_crim, test_set)
lda_class_high_crim<-lda_pred_high_crim$class
table(lda_class_high_crim, high_crim.test)
## high_crim.test
## lda_class_high_crim 0 1
## 0 70 20
## 1 3 59
1- mean(lda_class_high_crim == high_crim.test)
## [1] 0.1513158
The full LDA model approximated an 15.13% test error of the model.
lda_high_crim2 <- lda(high_crim ~ . -high_crim - crim -rm -lstat -medv, data = train_set)
lda_pred_high_crim2<- predict(lda_high_crim2, test_set)
lda_class_high_crim2<-lda_pred_high_crim2$class
table(lda_class_high_crim2, high_crim.test)
## high_crim.test
## lda_class_high_crim2 0 1
## 0 71 20
## 1 2 59
1- mean(lda_class_high_crim2 == high_crim.test)
## [1] 0.1447368
The LDA model without the weaker correlated variables approximated a 14.47% test error of the model. This indicates that this model is slightly better than the full model unlike the logistic model.
nb_high_crim <- naiveBayes(high_crim ~ . -high_crim - crim, data = train_set)
nb_pred_high_crim <- predict(nb_high_crim, test_set)
table(nb_pred_high_crim, high_crim.test)
## high_crim.test
## nb_pred_high_crim 0 1
## 0 68 20
## 1 5 59
1- mean(nb_pred_high_crim == high_crim.test)
## [1] 0.1644737
The full naive Bayes model approximated an 16.45% test error of the model.
nb_high_crim2 <- naiveBayes(high_crim ~ . -high_crim - crim -rm -lstat -medv, data = train_set)
nb_pred_high_crim2 <- predict(nb_high_crim2, test_set)
table(nb_pred_high_crim2, high_crim.test)
## high_crim.test
## nb_pred_high_crim2 0 1
## 0 65 22
## 1 8 57
1- mean(nb_pred_high_crim2 == high_crim.test)
## [1] 0.1973684
The naive Bayes model without the weaker correlated variables approximated a 19.73% test error of the model. This indicates that this model is slightly better than the full model unlike the logistic model.
train.X <- cbind(zn, indus, chas, nox, rm, age, dis, rad, tax,ptratio,black,lstat,medv)[train,]
test.X <- cbind(zn, indus, chas, nox, rm, age, dis, rad, tax,ptratio,black,lstat,medv)[-train,]
train.high_crim <- high_crim[train]
set.seed(1)
pred.knn_high_crim <-knn(train.X, test.X, train.high_crim, k=1)
table(pred.knn_high_crim, high_crim.test)
## high_crim.test
## pred.knn_high_crim 0 1
## 0 65 6
## 1 8 73
1 - mean(pred.knn_high_crim == high_crim.test)
## [1] 0.09210526
The full KNN model with k=1 gave us a test error rate of approximately 11.18%.
pred.knn_high_crim <-knn(train.X, test.X, train.high_crim, k=10)
table(pred.knn_high_crim, high_crim.test)
## high_crim.test
## pred.knn_high_crim 0 1
## 0 59 9
## 1 14 70
1 - mean(pred.knn_high_crim == high_crim.test)
## [1] 0.1513158
The full KNN model with k=10 gave us a test error rate of approximately 14.47%
pred.knn_high_crim <-knn(train.X, test.X, train.high_crim, k=6)
table(pred.knn_high_crim, high_crim.test)
## high_crim.test
## pred.knn_high_crim 0 1
## 0 61 6
## 1 12 73
1 - mean(pred.knn_high_crim == high_crim.test)
## [1] 0.1184211
The full KNN model with k=6 gave us a test error rate of approximately 11.84%.
train.X <- cbind(zn, indus, chas, nox, age, dis, rad, tax,ptratio,black)[train,]
test.X <- cbind(zn, indus, chas, nox, age, dis, rad, tax,ptratio,black)[-train,]
train.high_crim <- high_crim[train]
set.seed(1)
pred.knn_high_crim <-knn(train.X, test.X, train.high_crim, k=1)
table(pred.knn_high_crim, high_crim.test)
## high_crim.test
## pred.knn_high_crim 0 1
## 0 64 4
## 1 9 75
1 - mean(pred.knn_high_crim == high_crim.test)
## [1] 0.08552632
The KNN model with high correlated variables removed, produced a test error rate of approximately 8.55% at k=1.
pred.knn_high_crim <-knn(train.X, test.X, train.high_crim, k=5)
table(pred.knn_high_crim, high_crim.test)
## high_crim.test
## pred.knn_high_crim 0 1
## 0 62 5
## 1 11 74
1 - mean(pred.knn_high_crim == high_crim.test)
## [1] 0.1052632
The KNN model with high correlated variables removed, produced a test error rate of approximately 10.53% at k=5.
pred.knn_high_crim <-knn(train.X, test.X, train.high_crim, k=3)
table(pred.knn_high_crim, high_crim.test)
## high_crim.test
## pred.knn_high_crim 0 1
## 0 64 4
## 1 9 75
1 - mean(pred.knn_high_crim == high_crim.test)
## [1] 0.08552632
The KNN model with high correlated variables removed, produced a test error rate of approximately 8.55% at k=3 which is the same as k=1.