a: Produce some numerical and graphical summaries of the Weekly data. Do there appear to be any patterns?
library(ISLR2)
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
##
##
##
##
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
pairs(Weekly[,-9])
plot(Weekly$Year, Weekly$Volume)
It seems the only significant correlation would be between Year and Volume. When observed graphically, volume increases substantially over time. Meanwhile no visible relationship can be noted between Year and Lag variables, therefore there is no noticeably strong evidence that past returns are used to predict future returns.
b. Use the full data set to perform a logistic Regression. Summary function. Do any of the predictors appear to be statistically significant? If so, which ones?
glm.fit <- glm(Direction ~ . -Today, data = Weekly, family = binomial)
summary(glm.fit)
##
## Call:
## glm(formula = Direction ~ . - Today, family = binomial, data = Weekly)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 17.225822 37.890522 0.455 0.6494
## Year -0.008500 0.018991 -0.448 0.6545
## Lag1 -0.040688 0.026447 -1.538 0.1239
## Lag2 0.059449 0.026970 2.204 0.0275 *
## Lag3 -0.015478 0.026703 -0.580 0.5622
## Lag4 -0.027316 0.026485 -1.031 0.3024
## Lag5 -0.014022 0.026409 -0.531 0.5955
## Volume 0.003256 0.068836 0.047 0.9623
## ---
## 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.2 on 1081 degrees of freedom
## AIC: 1502.2
##
## Number of Fisher Scoring iterations: 4
Based on the results, Lag2 appears to be statistically significant. Therefore if there was a positive return within the past 2 weeks, there is a higher likelihood that the market could go up today.
c. Compute the confusion matrix and overall fraction of correct predictions. Explain what the confusion matrix is telling you about the types of mistakes made by logistic regression
glm.prob <- predict(glm.fit, type = "response")
glm.prob[1:10]
## 1 2 3 4 5 6 7 8
## 0.6195813 0.6123878 0.5986601 0.4938106 0.6288351 0.5807001 0.5905994 0.5274874
## 9 10
## 0.5834212 0.5674230
contrasts(Weekly$Direction)
## Up
## Down 0
## Up 1
glm.pred <- rep("Down", length(glm.prob))
glm.pred[glm.prob > 0.5] = "Up"
table(glm.pred, Weekly$Direction)
##
## glm.pred Down Up
## Down 56 47
## Up 428 558
mean(glm.pred == Weekly$Direction)
## [1] 0.56382
The confusion matrix tells us 2 things, the first that the market is predicted to go up in about 558 days, and that the logistic regression model will predict market direction about 56.4% of the time. Here the confusion matrix shows that the linear regression model was more likely to mistake down predictions, showing a total of 428 down days that were missclassified.
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.
train <- (Weekly$Year < 2009)
weekly08 <- Weekly[!train,]
dim(weekly08)
## [1] 104 9
direction08 <- Weekly$Direction[!train]
glm.fit <- glm(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
glm.prob <- predict(glm.fit, weekly08, type = "response")
glm.pred <- rep("Down", 104)
glm.pred[glm.prob > 0.5] <- "Up"
table(glm.pred, direction08)
## direction08
## glm.pred Down Up
## Down 9 5
## Up 34 56
mean(glm.pred == direction08)
## [1] 0.625
e. d repeated using LDA
library(MASS)
lda.fit <- lda(Direction ~ Lag2, data = Weekly, subset = train)
lda.pred <- predict(lda.fit, weekly08)
names(lda.pred)
## [1] "class" "posterior" "x"
lda.class <- lda.pred$class
table(lda.class, direction08)
## direction08
## lda.class Down Up
## Down 9 5
## Up 34 56
mean(lda.class == direction08)
## [1] 0.625
f. d repeated using QDA
qda.fit <- qda(Direction ~ Lag1, data = weekly08, subset = train)
qda.class <- predict(qda.fit, weekly08)$class
table(qda.class, direction08)
## direction08
## qda.class Down Up
## Down 1 3
## Up 42 58
mean(qda.class == direction08)
## [1] 0.5673077
g. d repeated using KNN with K = 1
library(class)
train.X <- as.matrix(Weekly$Lag2[train])
test.X <- as.matrix(Weekly$Lag2[!train])
train.direction <- Weekly$Direction[train]
knn.pred <- knn(train = train.X,
test = test.X,
cl = train.direction,
k = 1)
table(knn.pred, direction08)
## direction08
## knn.pred Down Up
## Down 21 30
## Up 22 31
mean(knn.pred == direction08)
## [1] 0.5
h. d repeated using naive Bayes
library(e1071)
nb.fit <- naiveBayes(Direction ~ Lag1, data = weekly08, subset = train)
nb.class <- predict(nb.fit, weekly08)
table(nb.class, direction08)
## direction08
## nb.class Down Up
## Down 1 3
## Up 42 58
mean(nb.class == direction08)
## [1] 0.5673077
i. Which of these methods appears to provide the best results on this data?
Both Logistic Regression and LDA seem to provide the best results for the data both showing the highest prediction rate at 62.5%.
j. Experiment with different combination 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 KNN classifier.
logistic regression
glm.fit <- glm(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
glm.fit2 <- glm(Direction ~ Lag2 + Lag1, data = Weekly, family = binomial, subset = train)
glm.fit3 <- glm(Direction ~ Lag2 + Lag1 + Lag3, data = Weekly, family = binomial, subset = train)
glm.fit4 <- glm(Direction ~ Lag2 + Lag1 + Lag2 + Lag4, data = Weekly, family = binomial, subset = train)
glm.fit5 <- glm(Direction ~ Lag2 + Lag1 + Lag3 + Volume, data = Weekly, family = binomial, subset = train)
glm.fit6 <- glm(Direction ~ Lag2 + Volume, data = Weekly, family = binomial, subset = train)
train <- (Weekly$Year < 2009)
weekly.test <- Weekly[!train,]
models <- list(
glm1 = glm.fit,
glm2 = glm.fit2,
glm3 = glm.fit3,
glm4 = glm.fit4,
glm5 = glm.fit5,
glm6 = glm.fit6
)
summary_table <- data.frame(Model = character(), accuracy = numeric())
for(i in names(models)) {
prob <- predict(models[[i]], weekly.test, type = "response")
pred <- ifelse(prob > .5, "Up", "Down")
acc <- mean(pred == weekly.test$Direction)
summary_table <- rbind(summary_table, data.frame(Model = i, Accuracy = acc))
}
summary_table
## Model Accuracy
## 1 glm1 0.6250000
## 2 glm2 0.5769231
## 3 glm3 0.5769231
## 4 glm4 0.6057692
## 5 glm5 0.5192308
## 6 glm6 0.5384615
The best results for using the logistic regression is using Lag2 as the only variable.
LDA
lda.fit <- lda(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
lda.fit1 <- lda(Direction ~ Lag2 + Lag1, data = Weekly, family = binomial, subset = train)
lda.fit2 <- lda(Direction ~ Lag2 + Volume, data = Weekly, family = binomial, subset = train)
lda.fit3 <- lda(Direction ~ Lag2 + Lag1 +Lag3, data = Weekly, family = binomial, subset = train)
lda.fit4 <- lda(Direction ~ Lag2 + Lag1 + Lag3 + Lag4, data = Weekly, family = binomial, subset = train)
lda.fit5 <- lda(Direction ~ Lag2 + Lag1 + Lag3 + Lag4 + Lag5, data = Weekly, family = binomial, subset = train)
direction08 <- Weekly$Direction[!train]
models <- list (lda.fit, lda.fit1, lda.fit2, lda.fit3, lda.fit4, lda.fit5)
results <- data.frame()
for(i in seq_along(models)) {
lda.pred <- predict(models[[i]], weekly08)
lda.class <- lda.pred$class
cm <- table(lda.class, direction08)
acc <- mean(lda.class == direction08)
results <- rbind(results, data.frame(model = paste0("LDA", i), accuracy = acc))
print(cm)
}
## direction08
## lda.class Down Up
## Down 9 5
## Up 34 56
## direction08
## lda.class Down Up
## Down 7 8
## Up 36 53
## direction08
## lda.class Down Up
## Down 20 25
## Up 23 36
## direction08
## lda.class Down Up
## Down 8 9
## Up 35 52
## direction08
## lda.class Down Up
## Down 7 8
## Up 36 53
## direction08
## lda.class Down Up
## Down 9 13
## Up 34 48
results
## model accuracy
## 1 LDA1 0.6250000
## 2 LDA2 0.5769231
## 3 LDA3 0.5384615
## 4 LDA4 0.5769231
## 5 LDA5 0.5769231
## 6 LDA6 0.5480769
The best results for using the LDA model is using Lag2 as the only variable.
QDA
qda.fit <- qda(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
qda.fit1 <- qda(Direction ~ Lag2 + Lag1, data = Weekly, family = binomial, subset = train)
qda.fit2 <- qda(Direction ~ Lag2 + Volume, data = Weekly, family = binomial, subset = train)
qda.fit3 <- qda(Direction ~ Lag2 + Lag1 +Lag3, data = Weekly, family = binomial, subset = train)
qda.fit4 <- qda(Direction ~ Lag2 + Lag1 + Lag3 + Lag4, data = Weekly, family = binomial, subset = train)
qda.fit5 <- qda(Direction ~ Lag2 + Lag1 + Lag3 + Lag4 + Lag5, data = Weekly, family = binomial, subset = train)
direction08 <- Weekly$Direction[!train]
models <- list (qda.fit, qda.fit1, qda.fit2, qda.fit3, qda.fit4, qda.fit5)
results <- data.frame()
for(i in seq_along(models)) {
qda.pred <- predict(models[[i]], weekly08)
qda.class <- qda.pred$class
cm <- table(qda.class, direction08)
acc <- mean(qda.class == direction08)
results <- rbind(results, data.frame(model = paste0("QDA", i), accuracy = acc))
print(cm)
}
## direction08
## qda.class Down Up
## Down 0 0
## Up 43 61
## direction08
## qda.class Down Up
## Down 7 10
## Up 36 51
## direction08
## qda.class Down Up
## Down 32 44
## Up 11 17
## direction08
## qda.class Down Up
## Down 6 10
## Up 37 51
## direction08
## qda.class Down Up
## Down 9 16
## Up 34 45
## direction08
## qda.class Down Up
## Down 10 23
## Up 33 38
results
## model accuracy
## 1 QDA1 0.5865385
## 2 QDA2 0.5576923
## 3 QDA3 0.4711538
## 4 QDA4 0.5480769
## 5 QDA5 0.5192308
## 6 QDA6 0.4615385
KNN with different K values
train.X <- as.matrix(Weekly$Lag2[train])
test.X <- as.matrix(Weekly$Lag2[!train])
train.direction <- Weekly$Direction[train]
test.direction <- Weekly$Direction[!train]
kval <- c(1,3,5,7,9,11,13)
results <- data.frame()
for(k in kval){
knn.pred <- knn(train = train.X,
test = test.X,
cl = train.direction,
k = k)
cm <- table(knn.pred, test.direction)
acc <- mean(knn.pred == test.direction)
results <- rbind(results,
data.frame(
k=k,
accuracy = acc
))
print(cm)
}
## test.direction
## knn.pred Down Up
## Down 21 29
## Up 22 32
## test.direction
## knn.pred Down Up
## Down 16 19
## Up 27 42
## test.direction
## knn.pred Down Up
## Down 15 21
## Up 28 40
## test.direction
## knn.pred Down Up
## Down 16 19
## Up 27 42
## test.direction
## knn.pred Down Up
## Down 17 20
## Up 26 41
## test.direction
## knn.pred Down Up
## Down 19 21
## Up 24 40
## test.direction
## knn.pred Down Up
## Down 20 19
## Up 23 42
results
## k accuracy
## 1 1 0.5096154
## 2 3 0.5576923
## 3 5 0.5288462
## 4 7 0.5576923
## 5 9 0.5576923
## 6 11 0.5673077
## 7 13 0.5961538
a. Create a binary variable, mpg1, that contains a 1 if mpg contains a value above its median, and a 0 if mpg contains a value below its median.
library(ISLR2)
Auto$mpg01 <- ifelse(Auto$mpg > median(Auto$mpg), 1, 0)
Auto$horsepower[Auto$horsepower == "?"] <- NA
Auto$horsepower <- as.numeric(Auto$horsepower)
Auto <- na.omit(Auto)
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
table(Auto$mpg01)
##
## 0 1
## 196 196
b. Explore the data graphically in order to investigate the association between mpg01 and the 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[,c("mpg01", "cylinders", "displacement", "horsepower", "weight", "acceleration", "year")])
boxplot(cylinders ~ mpg01, data = Auto)
boxplot(displacement ~ mpg01, data = Auto)
boxplot(horsepower ~ mpg01, data = Auto)
boxplot(weight ~ mpg01, data = Auto)
boxplot(acceleration ~ mpg01, data = Auto)
boxplot(year ~ mpg01, data = Auto)
cor(Auto[,c("mpg01", "cylinders", "displacement", "horsepower", "weight", "acceleration", "year")])
## mpg01 cylinders displacement horsepower weight
## mpg01 1.0000000 -0.7591939 -0.7534766 -0.6670526 -0.7577566
## cylinders -0.7591939 1.0000000 0.9508233 0.8429834 0.8975273
## displacement -0.7534766 0.9508233 1.0000000 0.8972570 0.9329944
## horsepower -0.6670526 0.8429834 0.8972570 1.0000000 0.8645377
## weight -0.7577566 0.8975273 0.9329944 0.8645377 1.0000000
## acceleration 0.3468215 -0.5046834 -0.5438005 -0.6891955 -0.4168392
## year 0.4299042 -0.3456474 -0.3698552 -0.4163615 -0.3091199
## acceleration year
## mpg01 0.3468215 0.4299042
## cylinders -0.5046834 -0.3456474
## displacement -0.5438005 -0.3698552
## horsepower -0.6891955 -0.4163615
## weight -0.4168392 -0.3091199
## acceleration 1.0000000 0.2903161
## year 0.2903161 1.0000000
Autos with higher mpg, more fuel efficient, would be those with fewer cylinders, lower engine displacement, lower horsepower, lighter in weight, and newer models. While there doesn’t seem to be too much of an impact on mpg from acceleration.
c. Split the data into a training set and a test set.
set.seed(1)
train <- sample(1:nrow(Auto),nrow(Auto)/2)
auto.train <- Auto[train,]
auto.test <- Auto[-train,]
mpg01.test <- Auto$mpg01[-train]
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.fit <- lda(mpg01 ~ cylinders + displacement + horsepower + weight + year, data = auto.train)
lda.pred <- predict(lda.fit, auto.test)
table(lda.pred$class, mpg01.test)
## mpg01.test
## 0 1
## 0 81 4
## 1 21 90
mean(lda.pred$class != mpg01.test)
## [1] 0.127551
test error: 12.8%
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.fit <- qda(mpg01 ~ cylinders + displacement + horsepower + weight + year, data = auto.train)
qda.pred <- predict(qda.fit, auto.test)
table(qda.pred$class, mpg01.test)
## mpg01.test
## 0 1
## 0 89 8
## 1 13 86
mean(qda.pred$class != mpg01.test)
## [1] 0.1071429
test error: 10.7
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.fit <- glm(mpg01 ~ cylinders + displacement + horsepower + weight + year, data = auto.train, family = binomial)
glm.prob <- predict(glm.fit, auto.test, type = "response")
glm.pred <- ifelse(glm.prob > .5, 1, 0)
table(glm.pred, mpg01.test)
## mpg01.test
## glm.pred 0 1
## 0 91 7
## 1 11 87
mean(glm.pred != mpg01.test)
## [1] 0.09183673
test error: 9.2%
g. Perform naive Bayes 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?
nb.fit <- naiveBayes(as.factor(mpg01) ~ cylinders + displacement + horsepower + weight + year, data = auto.train)
nb.pred <- predict(nb.fit, auto.test)
table(nb.pred, mpg01.test)
## mpg01.test
## nb.pred 0 1
## 0 88 9
## 1 14 85
mean(nb.pred != mpg01.test)
## [1] 0.1173469
test error: 11.7%
h. Perform KNN on the training data, with several values of K, in order to predict mpg01. Use only the variables that seemed most associated with mpg01 in b. What test errors do you obtain? Which value of K seems to perform the best on this data set?
train.X <- scale(auto.train[,c("cylinders", "displacement", "horsepower", "weight", "year")])
test.X <- scale(auto.test[,c("cylinders", "displacement", "horsepower", "weight", "year")])
train.Y <- auto.train$mpg01
kval <- c(1,3,5,7,9,11,13)
err <- numeric(length(kval))
for(i in seq_along(kval)){
pred <- knn(train.X, test.X, train.Y, k = kval[i])
err[i] <- mean(pred != mpg01.test)
}
data.frame(K = kval, test_error = err)
## K test_error
## 1 1 0.10714286
## 2 3 0.10204082
## 3 5 0.09693878
## 4 7 0.10204082
## 5 9 0.10204082
## 6 11 0.10204082
## 7 13 0.10204082
From the test errors obtained, K = 5 would be the k value chosen to run the model since it gives the lowest test error with minimum smoothing.
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.
Boston$crim01 <- ifelse(Boston$crim > median(Boston$crim), 1,0)
table(Boston$crim01)
##
## 0 1
## 253 253
pairs(Boston,)
boxplot(zn ~ crim01, data = Boston)
boxplot(indus ~ crim01, data = Boston)
boxplot(nox ~ crim01, data = Boston)
boxplot(rm ~ crim01, data = Boston)
boxplot(dis ~ crim01, data = Boston)
boxplot(rad ~ crim01, data = Boston)
boxplot(tax ~ crim01, data = Boston)
boxplot(ptratio ~ crim01, data = Boston)
boxplot(lstat ~ crim01, data = Boston)
boxplot(medv ~ crim01, data = Boston)
Based on the plots, the variables that are significant in relation to crime rates are the following: index of accessibility to radial highways (rad), property tax, proportion of non-retail business acres (indus), nitrogen oxides concentration (nox), distances to employment centers (dis), and lower population status (lstat).
set.seed(1)
train <- sample(1:nrow(Boston), nrow(Boston)/2)
boston.train <- Boston[train,]
boston.test <- Boston[-train,]
crim01.test <- Boston$crim01[-train]
fitting logistic regression
glm.fit <- glm(crim01 ~ rad + tax + indus + nox + dis + lstat, data = boston.train, family = binomial)
glm.prob <- predict(glm.fit, newdata = boston.test, type = "response")
glm.pred <- ifelse(glm.prob > .5, 1, 0)
table(glm.pred, crim01.test)
## crim01.test
## glm.pred 0 1
## 0 109 17
## 1 17 110
mean(glm.pred != crim01.test)
## [1] 0.1343874
fitting LDA
lda.fit <- lda(crim01 ~ rad + tax + indus + nox + dis + lstat, data = boston.train)
lda.pred <- predict(lda.fit, boston.test)$class
table(lda.pred, crim01.test)
## crim01.test
## lda.pred 0 1
## 0 120 33
## 1 6 94
mean(lda.pred != crim01.test)
## [1] 0.1541502
fitting Naive Bayes
library(e1071)
nb.fit <- naiveBayes(as.factor(crim01) ~ rad + tax + indus + nox + dis + lstat, data = boston.train)
nb.pred <- predict(nb.fit, boston.test)
table(nb.pred, crim01.test)
## crim01.test
## nb.pred 0 1
## 0 119 39
## 1 7 88
mean(nb.pred != crim01.test)
## [1] 0.1818182
fitting KNN
train.X <- scale(boston.train[, c("rad", "tax", "indus", "nox", "dis", "lstat")])
test.X <- scale(boston.test[, c("rad", "tax", "indus", "nox", "dis", "lstat")], center = attr(train.X, "scaled:center"), scale = attr(train.X, "scaled:scale"))
train.Y <- boston.train$crim01
kval <- c(1,3,5,7,9,11,13,15)
err <- numeric(length(kval))
for(i in seq_along(kval)){
pred <- knn(train.X,
test.X,
train.Y,
k = kval[i])
err[i] <- mean(pred != crim01.test)
}
data.frame(K = kval, Test_Error = err)
## K Test_Error
## 1 1 0.07114625
## 2 3 0.05138340
## 3 5 0.07905138
## 4 7 0.09090909
## 5 9 0.09881423
## 6 11 0.07509881
## 7 13 0.09486166
## 8 15 0.11462451