Load Libraries and attach datasets
library(ISLR2)
library(GGally)
library(MASS)
library(class)
library(e1071)
library(tidyverse)
library(corrplot)
attach(Weekly)
attach(Auto)
attach(Boston)
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.
Weekly data. Do there appear to be any patterns? Looking at the below plots, the only relevant correlation appears to be between Volume and Year. It is worth noting that there is a correlation between Today and Direction (as shown in the colors in the last plot); however, this is a pattern you would expect to see based on the fact that Direction is a representation (up or down) of the value in Today.
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
##
##
##
##
#Draw a correlation of Weekly, with Direction removed (since it is not numerical).
corrplot(cor(Weekly[,-9]), method = 'number', type = 'lower')
#Plotting the data using direction as a color to look for patterns/correlation.
plot(Weekly[,-9],col=Weekly$Direction)
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? Looking at the summary below, the only predictor that is significant (using an alpha of .05) is Lag2 based off the p-value of .0296. The next closest predictor is Lag1, which is .11.
weekly_lrm <- glm(Direction ~ Lag1 + Lag2 + Lag3 + Lag4 + Lag5 + Volume,
data = Weekly,
family = 'binomial')
summary(weekly_lrm)
##
## 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 table below indicates we predicted “Up” correctly 557 times and “Down” correctly 54 times. That results in a total of 611 correct predictions out of 1089 attempts. This equates to an accuracy rate of 56%. This is not very accurate, overall. At first glimpse, the model appears to predict “Up” fairly well with a 92% accuracy rate (557 correct, 48 wrong); however, this is due to the large amount of “Up” predictions in the model. In fact in this particular data set, if you just assumed “Up” for all observations, you would have an overall prediction rate of 55.5% (slightly less than our model) and you would predict “Up” correctly 100% of the time. With that in mind, I would say the model is not very effective.
weekly_probs <- predict(weekly_lrm, type='response')
weekly_preds <- rep('Down', length(weekly_probs))
weekly_preds[weekly_probs > 0.5] = 'Up'
table(weekly_preds, Weekly$Direction)
##
## weekly_preds Down Up
## Down 54 48
## Up 430 557
mean(weekly_preds == Weekly$Direction)
## [1] 0.5610652
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). weekly_train <- (Weekly$Year<2009)
weekly_test <- Weekly[!weekly_train,]
weekly_glm <-glm(Direction ~ Lag2, data = Weekly, family = 'binomial',
subset = weekly_train)
glm_probs <- predict(weekly_glm, weekly_test, type = 'response')
glm_preds <- rep('Down', length(glm_probs))
glm_preds[glm_probs > 0.5] = 'Up'
direction_test = Weekly$Direction[!weekly_train]
table(glm_preds, direction_test)
## direction_test
## glm_preds Down Up
## Down 9 5
## Up 34 56
mean(glm_preds == direction_test)
## [1] 0.625
weekly_lda <- lda(Direction ~ Lag2, data = Weekly, family = 'binomial',
subset = weekly_train)
lda_preds <- predict(weekly_lda, weekly_test)
table(lda_preds$class, direction_test)
## direction_test
## Down Up
## Down 9 5
## Up 34 56
mean(lda_preds$class == direction_test)
## [1] 0.625
weekly_qda <- qda(Direction ~ Lag2, data = Weekly, family = 'binomial',
subset = weekly_train)
qda_preds <- predict(weekly_qda, weekly_test)
table(qda_preds$class, direction_test)
## direction_test
## Down Up
## Down 0 0
## Up 43 61
mean(qda_preds$class == direction_test)
## [1] 0.5865385
weekly_train2 <- as.matrix(Weekly$Lag2[weekly_train])
weekly_test <- as.matrix(Weekly$Lag2[!weekly_train])
direction_train <- Weekly$Direction[weekly_train]
set.seed(22)
knn_preds <- knn(weekly_train2, weekly_test, direction_train, k = 1)
table(knn_preds,direction_test)
## direction_test
## knn_preds Down Up
## Down 21 29
## Up 22 32
mean(knn_preds == direction_test)
## [1] 0.5
weekly_NB <- naiveBayes(Direction ~ Lag2, data = Weekly, subset = weekly_train)
NB_preds <- predict(weekly_NB, weekly_test)
table(NB_preds, direction_test)
## direction_test
## NB_preds Down Up
## Down 0 0
## Up 43 61
mean(NB_preds == direction_test)
## [1] 0.5865385
From an overall perspective, both the LR and the LDA have an accuracy rate of 62.5%. However, much like the model in problem 13c, the best results are dependent on whether or not you would prefer an overall accuracy rate or a more favorable rate of predicting “Up” or “Down”. For instance, in the LDA and LR models both only achieved about a 20% accuracy rating when predicting “Down”. If you were more concerned about accurately predicting “Down” you might choose to use the KNN model which predicts down correctly about 50% of the time.
Adding Lag1 and Year with an interaction actually made the model’s overall accuracy worse. Dropping the accuracy in which it predicted “Down” while not affecting the “Up” prediction accuracy. On a positive note, increasing the K from 1 to 3 positively impacted the model. In exchange for a slight decrease in accuracy in predicting “Down” (from 50% to 46.5%), there was a significant increase in predicting “Up”, from 50% to 72%. Depending on the priority placed predicting either “Up” or “Down” more accurately, this is likely a significant improvement to the model
weekly_train <- (Weekly$Year<2009)
weekly_test <- Weekly[!weekly_train,]
weekly_glm <-glm(Direction ~ Lag1:Lag2+Year, data = Weekly, family = 'binomial',
subset = weekly_train)
glm_probs <- predict(weekly_glm, weekly_test, type = 'response')
glm_preds <- rep('Down', length(glm_probs))
glm_preds[glm_probs > 0.5] = 'Up'
direction_test = Weekly$Direction[!weekly_train]
table(glm_preds, direction_test)
## direction_test
## glm_preds Down Up
## Down 6 5
## Up 37 56
mean(glm_preds == direction_test)
## [1] 0.5865385
weekly_train2 <- as.matrix(Weekly$Lag2[weekly_train])
weekly_test <- as.matrix(Weekly$Lag2[!weekly_train])
direction_train <- Weekly$Direction[weekly_train]
set.seed(22)
knn_preds2 <- knn(weekly_train2, weekly_test, direction_train, k = 4)
table(knn_preds2, direction_test)
## direction_test
## knn_preds2 Down Up
## Down 19 20
## Up 24 41
mean(knn_preds2 == direction_test)
## [1] 0.6153846
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.
mpg01, that contains a 1 if mpg contains a value above its median, and a 0 if mpg contains a value below its median. You can compute the median using the median() function. Note you may find it helpful to use the data.frame() function to create a single data set containing both mpg01 and the other Auto variables.mpg01 <- rep(0, length(Auto$mpg))
mpg01[Auto$mpg > median(Auto$mpg)] <- 1
Auto <- data.frame(Auto, mpg01)
summary(Auto)
## mpg cylinders displacement horsepower weight
## Min. : 9.00 Min. :3.000 Min. : 68.0 Min. : 46.0 Min. :1613
## 1st Qu.:17.00 1st Qu.:4.000 1st Qu.:105.0 1st Qu.: 75.0 1st Qu.:2225
## Median :22.75 Median :4.000 Median :151.0 Median : 93.5 Median :2804
## Mean :23.45 Mean :5.472 Mean :194.4 Mean :104.5 Mean :2978
## 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:275.8 3rd Qu.:126.0 3rd Qu.:3615
## Max. :46.60 Max. :8.000 Max. :455.0 Max. :230.0 Max. :5140
##
## acceleration year origin name
## Min. : 8.00 Min. :70.00 Min. :1.000 amc matador : 5
## 1st Qu.:13.78 1st Qu.:73.00 1st Qu.:1.000 ford pinto : 5
## Median :15.50 Median :76.00 Median :1.000 toyota corolla : 5
## Mean :15.54 Mean :75.98 Mean :1.577 amc gremlin : 4
## 3rd Qu.:17.02 3rd Qu.:79.00 3rd Qu.:2.000 amc hornet : 4
## Max. :24.80 Max. :82.00 Max. :3.000 chevrolet chevette: 4
## (Other) :365
## mpg01
## Min. :0.0
## 1st Qu.:0.0
## Median :0.5
## Mean :0.5
## 3rd Qu.:1.0
## Max. :1.0
##
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.The below charts indicate that cylinders, displacement, horsepower, and weight all appear to have a significant correlation to mpg01 and might be useful as predictors.
#Draw a correlation of Auto with name removed (since it is not numerical).
corrplot(cor(Auto[-9]), method = 'number', type = 'lower')
#Plotting the data using to look for patterns/correlation.
plot(Auto[-c(9,10)], col=as.factor(Auto$mpg01))
set.seed(22)
train_num <- sample(nrow(Auto), size = (nrow(Auto) * .75))
test_num <- -train_num
auto_train <- Auto[train_num, ]
auto_test <- Auto[test_num, ]
mpg01_test <- Auto$mpg01[test_num]
mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained?auto_lda <- lda(mpg01 ~ cylinders + displacement + horsepower + weight,
data = auto_train)
lda_preds <- predict(auto_lda, auto_test)
table(lda_preds$class, mpg01_test)
## mpg01_test
## 0 1
## 0 49 2
## 1 6 41
mean(lda_preds$class == mpg01_test)
## [1] 0.9183673
1 - mean(lda_preds$class == mpg01_test)
## [1] 0.08163265
mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained? The QDA test error rate is 7.1%, slightly better than the LDA model’s test error rate of 8.2%.
auto_qda <- qda(mpg01 ~ cylinders + displacement + horsepower + weight,
data = auto_train)
qda_preds <- predict(auto_qda, auto_test)
table(qda_preds$class, mpg01_test)
## mpg01_test
## 0 1
## 0 52 4
## 1 3 39
mean(qda_preds$class == mpg01_test)
## [1] 0.9285714
1-mean(qda_preds$class == mpg01_test)
## [1] 0.07142857
mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained? The LR test error rate is 8.2%, identical to the LDA error rate.
auto_glm <- glm(mpg01 ~ cylinders + displacement + horsepower + weight,
data = auto_train)
glm_probs <- predict(auto_glm, auto_test, type = 'response')
glm_preds <- round(glm_probs)
table(glm_preds, mpg01_test)
## mpg01_test
## glm_preds 0 1
## 0 49 2
## 1 6 41
mean(glm_preds == mpg01_test)
## [1] 0.9183673
1-mean(glm_preds == mpg01_test)
## [1] 0.08163265
mpg01 using the variables that seemed most associated with mpg01 in (b). What is the test error of the model obtained? The NB test error rate is 8.2%, identical to the LDA error rate.
auto_NB <- naiveBayes(mpg01 ~ cylinders + displacement + horsepower + weight,
data = auto_train)
NB_preds <- predict(auto_NB, auto_test)
table(NB_preds, mpg01_test)
## mpg01_test
## NB_preds 0 1
## 0 52 3
## 1 3 40
mean(NB_preds == mpg01_test)
## [1] 0.9387755
1-mean(glm_preds == mpg01_test)
## [1] 0.08163265
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?I used both the square root method and a k iteration loop to determine the best value of K. Looking at the results below, it appears that the K value of 8 appears to perform the best on this data set.
#Determine optimal K using square root method.
round(sqrt(98))
## [1] 10
#Running a KNN with a k value of 10
auto_train2 <- cbind(Auto$cylinders, Auto$displacement, Auto$horsepower,
Auto$weight)[train_num,]
auto_test2 <- cbind(Auto$cylinders, Auto$displacement, Auto$horsepower,
Auto$weight)[test_num,]
mpg01_train <- Auto$mpg01[train_num]
set.seed(22)
knn_preds2 <- knn(auto_train2, auto_test2, mpg01_train, k = 10)
table(knn_preds2, mpg01_test)
## mpg01_test
## knn_preds2 0 1
## 0 50 4
## 1 5 39
mean(knn_preds2 == mpg01_test)
## [1] 0.877551
1-mean(knn_preds2 == mpg01_test)
## [1] 0.122449
#Using a for loop to determine the optimal k value.
acc <- list()
for (i in 1:20) {
knn_preds2 <- knn(auto_train2, auto_test2, mpg01_train, k = i)
acc[as.character(i)] = 1-mean(knn_preds2 == mpg01_test)
}
acc <- unlist(acc)
#Displaying the error rate for each iteration of k value. The lowest error rate is found with a k value of 8, which results in an 8.1% error rate.
acc
## 1 2 3 4 5 6 7
## 0.10204082 0.14285714 0.11224490 0.10204082 0.11224490 0.09183673 0.09183673
## 8 9 10 11 12 13 14
## 0.08163265 0.09183673 0.08163265 0.10204082 0.09183673 0.10204082 0.09183673
## 15 16 17 18 19 20
## 0.10204082 0.10204082 0.10204082 0.10204082 0.10204082 0.11224490
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.Boston data set.#Creating a response variable where 1 equals a crime rate above the median and 0 equals a rate below the median.
crim01 <- rep(0, length(Boston$crim))
crim01[Boston$crim > median(Boston$crim)] <- 1
Boston <- data.frame(Boston, crim01)
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 crim01
## Min. : 1.73 Min. : 5.00 Min. :0.0
## 1st Qu.: 6.95 1st Qu.:17.02 1st Qu.:0.0
## Median :11.36 Median :21.20 Median :0.5
## Mean :12.65 Mean :22.53 Mean :0.5
## 3rd Qu.:16.95 3rd Qu.:25.00 3rd Qu.:1.0
## Max. :37.97 Max. :50.00 Max. :1.0
#Exploring data to find correlating variables
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
## crim01 0.40939545 -0.43615103 0.60326017 0.070096774 0.72323480
## rm age dis rad tax ptratio
## crim -0.21924670 0.35273425 -0.37967009 0.625505145 0.58276431 0.2899456
## zn 0.31199059 -0.56953734 0.66440822 -0.311947826 -0.31456332 -0.3916785
## indus -0.39167585 0.64477851 -0.70802699 0.595129275 0.72076018 0.3832476
## chas 0.09125123 0.08651777 -0.09917578 -0.007368241 -0.03558652 -0.1215152
## nox -0.30218819 0.73147010 -0.76923011 0.611440563 0.66802320 0.1889327
## rm 1.00000000 -0.24026493 0.20524621 -0.209846668 -0.29204783 -0.3555015
## age -0.24026493 1.00000000 -0.74788054 0.456022452 0.50645559 0.2615150
## dis 0.20524621 -0.74788054 1.00000000 -0.494587930 -0.53443158 -0.2324705
## rad -0.20984667 0.45602245 -0.49458793 1.000000000 0.91022819 0.4647412
## tax -0.29204783 0.50645559 -0.53443158 0.910228189 1.00000000 0.4608530
## ptratio -0.35550149 0.26151501 -0.23247054 0.464741179 0.46085304 1.0000000
## black 0.12806864 -0.27353398 0.29151167 -0.444412816 -0.44180801 -0.1773833
## lstat -0.61380827 0.60233853 -0.49699583 0.488676335 0.54399341 0.3740443
## medv 0.69535995 -0.37695457 0.24992873 -0.381626231 -0.46853593 -0.5077867
## crim01 -0.15637178 0.61393992 -0.61634164 0.619786249 0.60874128 0.2535684
## black lstat medv crim01
## crim -0.38506394 0.4556215 -0.3883046 0.40939545
## zn 0.17552032 -0.4129946 0.3604453 -0.43615103
## indus -0.35697654 0.6037997 -0.4837252 0.60326017
## chas 0.04878848 -0.0539293 0.1752602 0.07009677
## nox -0.38005064 0.5908789 -0.4273208 0.72323480
## rm 0.12806864 -0.6138083 0.6953599 -0.15637178
## age -0.27353398 0.6023385 -0.3769546 0.61393992
## dis 0.29151167 -0.4969958 0.2499287 -0.61634164
## rad -0.44441282 0.4886763 -0.3816262 0.61978625
## tax -0.44180801 0.5439934 -0.4685359 0.60874128
## ptratio -0.17738330 0.3740443 -0.5077867 0.25356836
## black 1.00000000 -0.3660869 0.3334608 -0.35121093
## lstat -0.36608690 1.0000000 -0.7376627 0.45326273
## medv 0.33346082 -0.7376627 1.0000000 -0.26301673
## crim01 -0.35121093 0.4532627 -0.2630167 1.00000000
#Drawing a plot with the variables that have the highest correlation with Crim01.
corrplot(cor(Boston[c(3,5,7:10,15)]), method = 'number', type = 'lower')
plot(Boston[c(3,5,7:10,15)], col = 'dark blue')
I am electing to build my model using predictors that have correlation of .61 or higher. I will be using tax, rad, dis, age, and nox. Additionally, there appears to be some relation between nox and dis so I will use that interaction as well
#split data into test and train data
set.seed(22)
train_num <- sample(nrow(Boston), size = (nrow(Boston) * .75))
test_num <- -train_num
boston_train <- Boston[train_num, ]
boston_test <- Boston[test_num, ]
crim01_test <- Boston$crim01[test_num]
boston_lda <- lda(crim01 ~ tax + rad + dis + age + nox + nox:dis,
data = boston_train)
lda_preds <- predict(boston_lda, boston_test)
table(lda_preds$class, crim01_test)
## crim01_test
## 0 1
## 0 64 15
## 1 3 45
mean(lda_preds$class == crim01_test)
## [1] 0.8582677
1 - mean(lda_preds$class == crim01_test)
## [1] 0.1417323
boston_qda <- qda(crim01 ~ tax + rad + dis + age + nox + nox:dis,
data = boston_train)
qda_preds <- predict(boston_qda, boston_test)
table(qda_preds$class, crim01_test)
## crim01_test
## 0 1
## 0 65 14
## 1 2 46
mean(qda_preds$class == crim01_test)
## [1] 0.8740157
1-mean(qda_preds$class == crim01_test)
## [1] 0.1259843
boston_glm <- glm(crim01 ~ tax + rad + dis + age + nox + nox:dis,
data = boston_train)
glm_probs <- predict(boston_glm, boston_test, type = 'response')
glm_preds <- round(glm_probs)
table(glm_preds, crim01_test)
## crim01_test
## glm_preds 0 1
## 0 64 15
## 1 3 45
mean(glm_preds == crim01_test)
## [1] 0.8582677
1-mean(glm_preds == crim01_test)
## [1] 0.1417323
boston_NB <- naiveBayes(crim01 ~ tax + rad + dis + age + nox,
data = boston_train)
NB_preds <- predict(boston_NB, boston_test)
table(NB_preds, crim01_test)
## crim01_test
## NB_preds 0 1
## 0 63 17
## 1 4 43
mean(NB_preds == crim01_test)
## [1] 0.8346457
1-mean(glm_preds == crim01_test)
## [1] 0.1417323
boston_train2 <- cbind(Boston$tax, Boston$rad, Boston$dis,
Boston$age, Boston$nox)[train_num,]
boston_test2 <- cbind(Boston$tax, Boston$rad, Boston$dis,
Boston$age, Boston$nox)[test_num,]
crim01_train <- Boston$crim01[train_num]
set.seed(22)
acc <- list()
for (i in 1:20) {
knn_preds2 <- knn(boston_train2, boston_test2, crim01_train, k = i)
acc[as.character(i)] = 1-mean(knn_preds2 == crim01_test)
}
acc <- unlist(acc)
#Displaying the error rate for each iteration of k value. The lowest error rate is found with a k value of 8, which results in an 8.1% error rate.
acc
## 1 2 3 4 5 6 7
## 0.08661417 0.10236220 0.10236220 0.11811024 0.10236220 0.11023622 0.10236220
## 8 9 10 11 12 13 14
## 0.11811024 0.11811024 0.12598425 0.11811024 0.11811024 0.12598425 0.14173228
## 15 16 17 18 19 20
## 0.14173228 0.17322835 0.14960630 0.14960630 0.14173228 0.14173228
I ran several different models, some with the interaction term and some without. In the end, the KNN model with a K of 1 proved to be the most accurate model with an error rate of 8.1%. I also added additional predictors on several of the models without any significant gain in accuracy. However, when I removed any of the 5 predictors I was using I noticed a decline in accuracy