Q10(a) Using the “Weekly” data set, it contains 1,089 weekly returns for 21 years, from the beginning of 1990 to the end of 2010. Produce some numerical and graphical summaries of the “Weekly” data. Do there appear to be any patterns ?
library(ISLR)
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
## Min. :-18.1950 Min. :-18.1950 Min. :0.08747
## 1st Qu.: -1.1580 1st Qu.: -1.1660 1st Qu.:0.33202
## Median : 0.2380 Median : 0.2340 Median :1.00268
## Mean : 0.1458 Mean : 0.1399 Mean :1.57462
## 3rd Qu.: 1.4090 3rd Qu.: 1.4050 3rd Qu.:2.05373
## Max. : 12.0260 Max. : 12.0260 Max. :9.32821
## Today Direction
## Min. :-18.1950 Down:484
## 1st Qu.: -1.1540 Up :605
## Median : 0.2410
## Mean : 0.1499
## 3rd Qu.: 1.4050
## Max. : 12.0260
484/1089
## [1] 0.4444444
The data shows 44.4% “Down” weekly returns and 55.6% “Up” weekly returns. No other patterns.
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 only substantial positive correlation is between “Year” and “Volume”. In addition, the correlations between the “lag” variables and today’s returns are very low, it does not show any unusual patterns.
attach(Weekly)
plot(Year, Volume)
When we plot “Year” against “Volume”, there is an increasing trend from Year 1990 to 2008, and it started decreasing from Year 2009 to 2010.
(b) Use the full data set to perform a logistic regression with “Direction” as the response and the five lag variables plus “Volume” as predictors. Use the summary function to print the results. Do any of the predictors appear to be statistically significant ? If so, which ones ?
glm.fit = glm(Direction ~ . -Today, data = Weekly, family = binomial)
summary(glm.fit)$coef
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 17.225822231 37.89052190 0.45462088 0.6493820
## Year -0.008499918 0.01899083 -0.44758011 0.6544563
## Lag1 -0.040687571 0.02644652 -1.53848459 0.1239302
## Lag2 0.059448637 0.02697031 2.20422531 0.0275085
## Lag3 -0.015477987 0.02670309 -0.57963289 0.5621622
## Lag4 -0.027316278 0.02648478 -1.03139539 0.3023554
## Lag5 -0.014022185 0.02640947 -0.53095285 0.5954515
## Volume 0.003256253 0.06883640 0.04730423 0.9622708
It would seem that “Lag2” is the only predictor statistically significant as its p-value is less than 0.05.
(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.probs = predict(glm.fit, Weekly, type = "response")
glm.pred = rep("Down", 1089)
glm.pred[glm.probs > .5] = "Up"
table(glm.pred, Direction)
## Direction
## glm.pred Down Up
## Down 56 47
## Up 428 558
Using Logistic regression, we may conclude that the percentage of correct predictions on the training data is 56.3820018%. In other words, 43.6179982% is the training error rate, which is often overly optimistic. Because we are not concern with the training error rate but instead the test error rate. We could say for weeks when the market goes up, the model is right 92.231405% of the time. For weeks when the market goes down, the model is right only 11.5702479% 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 to 2010).
train = (Year < 2009)
Weekly.2009.2010 = Weekly[!train,]
Direction.2009.2010 = Direction[!train] # test y
glm.fit = glm(Direction ~ Lag2, data = Weekly, family = binomial, subset = train)
summary(glm.fit)$coef
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.20325743 0.06428036 3.162046 0.00156665
## Lag2 0.05809527 0.02870446 2.023911 0.04297934
glm.probs = predict(glm.fit, Weekly.2009.2010, type = "response")
glm.pred = rep("Down", length(glm.probs))
glm.pred[glm.probs > .5] = "Up"
table(glm.pred, Direction.2009.2010)
## Direction.2009.2010
## glm.pred Down Up
## Down 9 5
## Up 34 56
Using Logistic regression, we may conclude that the percentage of correct predictions on the test data is 62.5%. In other words, 37.5% is the test error rate. We could also say for weeks when the market is going up, the model is right 91.8032787% of the time. For weeks when the market goes down, the model is right about 20.9302326% of the time.
(e) Repeat (d) using LDA.
library(MASS)
lda.fit = lda(Direction ~ Lag2, data = Weekly, subset = train)
lda.fit
## Call:
## lda(Direction ~ Lag2, data = Weekly, 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(lda.fit, Weekly.2009.2010)
lda.class = lda.pred$class
table(lda.class, Direction.2009.2010)
## Direction.2009.2010
## lda.class Down Up
## Down 9 5
## Up 34 56
Using LDA, we may conclude that the percentage of correct predictions on the test data is 62.5%. In other words, 37.5% is the test error rate. We could also say for weeks when the market is going up, the model is right 91.8032787% of the time. For weeks when the market goes down, the model is right about 20.9302326% of the time.
(f) Repeat (d) using QDA.
qda.fit = qda(Direction ~ Lag2, data = Weekly, subset = train)
qda.fit
## Call:
## qda(Direction ~ Lag2, data = Weekly, subset = train)
##
## Prior probabilities of groups:
## Down Up
## 0.4477157 0.5522843
##
## Group means:
## Lag2
## Down -0.03568254
## Up 0.26036581
qda.pred = predict(qda.fit, Weekly.2009.2010)
qda.class = qda.pred$class
table(qda.class, Direction.2009.2010)
## Direction.2009.2010
## qda.class Down Up
## Down 0 0
## Up 43 61
Using QDA, we may conclude that the percentage of correct predictions on the test data is 58.6538462%. In other words, 41.3461538% is the test error rate. We could also say for weeks when the market is going up, the model is right 100% of the time. For weeks when the market goes down, the model is right about 0% of the time.
(g) Repeat (d) using KNN with K = 1
library(class)
train.X <- as.matrix(Lag2[train])
test.X <- as.matrix(Lag2[!train])
train.Direction = Direction[train]
set.seed(1)
knn.pred = knn(train.X, test.X, train.Direction, k = 1)
table(knn.pred, Direction.2009.2010)
## Direction.2009.2010
## knn.pred Down Up
## Down 21 30
## Up 22 31
Using KNN with k = 1, we may conclude that the percentage of correct predictions on the test data is 50%. In other words, 50% is the test error rate. We could also say for weeks when the market is going up, the model is right 50.8196721% of the time. For weeks when the market goes down, the model is right about 48.8372093% of the time.
(h) Which of these methods appears to provide the best results on this data ?
If we compare the overall test error rates, we see that logistic regression and LDA have the minimum error rates, followed by QDA and KNN with k = 1.
Q11(a) Develop a model to predict whether a given car gets high or low gas mileage based on the “Auto” data set. 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.
attach(Auto)
mpg01 = rep(0, length(mpg))
mpg01[mpg > median(mpg)] = 1
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
(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 predictiong “mpg01” ? Scatterplots and boxplots may be useful tools to answer this question. Describe your findings.
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
pairs(Auto)
boxplot(cylinders ~ mpg01, data = Auto, main = "Cylinders vs mpg01")
boxplot(displacement ~ mpg01, data = Auto, main = "Displacement vs mpg01")
boxplot(horsepower ~ mpg01, data = Auto, main = "Horsepower vs mpg01")
boxplot(weight ~ mpg01, data = Auto, main = "Weight vs mpg01")
boxplot(acceleration ~ mpg01, data = Auto, main = "Acceleration vs mpg01")
boxplot(year ~ mpg01, data = Auto, main = "Year vs mpg01")
We may conclude that there exists some association between “mpg01” with the following 4 variables: “cylinders”, “displacement”, “horsepower” and “weight”.
(c) Split the data into a training set and a test set.
train = (year%%2 == 0) # if the year is even
Auto.train = Auto[train, ]
Auto.test = Auto[!train, ]
mpg01.test = 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, data = Auto, subset = train)
lda.fit
## Call:
## lda(mpg01 ~ cylinders + displacement + horsepower + weight, data = Auto,
## subset = train)
##
## Prior probabilities of groups:
## 0 1
## 0.4571429 0.5428571
##
## Group means:
## cylinders displacement horsepower weight
## 0 6.812500 271.7396 133.14583 3604.823
## 1 4.070175 111.6623 77.92105 2314.763
##
## Coefficients of linear discriminants:
## LD1
## cylinders -0.6741402638
## displacement 0.0004481325
## horsepower 0.0059035377
## weight -0.0011465750
lda.pred = predict(lda.fit, Auto.test)
lda.class = lda.pred$class
table(lda.class, mpg01.test)
## mpg01.test
## lda.class 0 1
## 0 86 9
## 1 14 73
mean(lda.class != mpg01.test)
## [1] 0.1263736
We may conclude that we have a test error rate of 12.6373626%.
(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, data = Auto, subset = train)
qda.fit
## Call:
## qda(mpg01 ~ cylinders + displacement + horsepower + weight, data = Auto,
## subset = train)
##
## Prior probabilities of groups:
## 0 1
## 0.4571429 0.5428571
##
## Group means:
## cylinders displacement horsepower weight
## 0 6.812500 271.7396 133.14583 3604.823
## 1 4.070175 111.6623 77.92105 2314.763
qda.pred = predict(qda.fit, Auto.test)
qda.class = qda.pred$class
table(qda.class, mpg01.test)
## mpg01.test
## qda.class 0 1
## 0 89 13
## 1 11 69
mean(qda.class != mpg01.test)
## [1] 0.1318681
We may conclude that we have a test error rate of 13.1868132%.
(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, data = Auto, subset = train, family = binomial)
summary(glm.fit)$coef
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 17.658730372 3.409012230 5.1800138 2.218695e-07
## cylinders -1.028031664 0.653606999 -1.5728590 1.157515e-01
## displacement 0.002461740 0.015029620 0.1637926 8.698944e-01
## horsepower -0.050610857 0.025209015 -2.0076491 4.468060e-02
## weight -0.002922352 0.001137367 -2.5694006 1.018746e-02
glm.probs = predict(glm.fit, Auto.test, type = "response")
glm.pred = rep(0, length(glm.probs))
glm.pred[glm.probs > .5] = 1
table(glm.pred, mpg01.test)
## mpg01.test
## glm.pred 0 1
## 0 89 11
## 1 11 71
mean(glm.pred != mpg01.test)
## [1] 0.1208791
We may conclude that we have a test error rate of 12.0879121%.
(g) Perform KNN on the training data, with several values of KK, in order to predict “mpg01” using the variables that seemed most associated with “mpg01” in (b). What test errors do you obtain ? Which value of K seems to perform the best on this data set ?
train.X = cbind(cylinders, displacement, horsepower, weight)[train, ]
test.X = cbind(cylinders, displacement, horsepower, weight)[!train, ]
train.mpg01 = mpg01[train]
set.seed(1)
knn.pred = knn(train.X, test.X, train.mpg01, k = 1)
table(knn.pred, mpg01.test )
## mpg01.test
## knn.pred 0 1
## 0 83 11
## 1 17 71
mean(knn.pred != mpg01.test)
## [1] 0.1538462
We may conclude that we have a test error rate of 15.3846154% for K = 1.
knn.pred = knn(train.X, test.X, train.mpg01, k = 10)
table(knn.pred, mpg01.test )
## mpg01.test
## knn.pred 0 1
## 0 77 7
## 1 23 75
mean(knn.pred != mpg01.test)
## [1] 0.1648352
We may conclude that we have a test error rate of 16.4835165% for K = 10.
knn.pred = knn(train.X, test.X, train.mpg01, k = 100)
table(knn.pred, mpg01.test )
## mpg01.test
## knn.pred 0 1
## 0 81 7
## 1 19 75
mean(knn.pred != mpg01.test)
## [1] 0.1428571
We may conclude that we have a test error rate of 14.2857143% for K = 100. a K value of 100 seems to perform the best.
Q12(a) Write a function, Power(), that prints out the result of raising 2 to the 3rd power. In other words, your function should compute \(2^3\) and print out the results.
Power <- function() {
2^3
}
Power()
## [1] 8
(b) Create a new function, Power2(), that allows you to pass any two numbers, “x” and “a”, and prints out the value of “x^a”.
Power2 <- function(x, a) {
x^a
}
Power2(2, 5)
## [1] 32
(c) Using the Power2() function that you just wrote, compute \(10^3\), \(8^{17}\), and \(131^3\).
Power2(10, 3)
## [1] 1000
Power2(8, 17)
## [1] 2.2518e+15
Power2(131, 3)
## [1] 2248091
(d) Now create a new function, Power3(), that actually returns the result “x^a” as an R object, rather than simply printing it to the screen. That is, if you store the value “x^a” in an object called “result” within your function, then you can simply return() this result.
Power3 <- function(x, a) {
result <- x^a
return(result)
}
(e) Now using the Power3() function, create a plot of \(f(x) = x^2\). The x-axis should display a range of integers from 1 to 10, and the y-axis should display \(x^2\). Label the axes appropriately, and use an appropriate title for the figure. Consider displaying either the x-axis, the y-axis, or both on the log-scale.
x <- 1:10
plot(x, Power3(x, 2), log = "xy", xlab = "log of x", ylab = "log of x^2", main = "Log of x^2 vs Log of x")
(f) Create a function, PlotPower(), that allows you to create a plot of “x” against “x^a” for a fixed “a” and for a range of values of “x”.
PlotPower <- function(x, a){
plot(x, Power3(x, a))
}
PlotPower(1:10, 3)
Q13(a) 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 the logistic regression, LDA, and KNN models using various subsets of the predictors. Describe your findings.
library(MASS)
attach(Boston)
crim01 <- rep(0, length(crim)) # create binary variable for "crim"
crim01[crim > median(crim)] <- 1
Boston <- data.frame(Boston, crim01)
train <- 1:(length(crim) / 2) # split first half of data to train set
test <- (length(crim) / 2 + 1):length(crim)
Boston.train <- Boston[train, ]
Boston.test <- Boston[test, ]
crim01.test <- crim01[test]
# logistic regression
glm.fit <- glm(crim01 ~ . - crim01 - crim, data = Boston, family = binomial, subset = train)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(glm.fit)
##
## Call:
## glm(formula = crim01 ~ . - crim01 - crim, family = binomial,
## data = Boston, subset = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.83229 -0.06593 0.00000 0.06181 2.61513
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -91.319906 19.490273 -4.685 2.79e-06 ***
## zn -0.815573 0.193373 -4.218 2.47e-05 ***
## indus 0.354172 0.173862 2.037 0.04164 *
## chas 0.167396 0.991922 0.169 0.86599
## nox 93.706326 21.202008 4.420 9.88e-06 ***
## rm -4.719108 1.788765 -2.638 0.00833 **
## age 0.048634 0.024199 2.010 0.04446 *
## dis 4.301493 0.979996 4.389 1.14e-05 ***
## rad 3.039983 0.719592 4.225 2.39e-05 ***
## tax -0.006546 0.007855 -0.833 0.40461
## ptratio 1.430877 0.359572 3.979 6.91e-05 ***
## black -0.017552 0.006734 -2.606 0.00915 **
## lstat 0.190439 0.086722 2.196 0.02809 *
## medv 0.598533 0.185514 3.226 0.00125 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 329.367 on 252 degrees of freedom
## Residual deviance: 69.568 on 239 degrees of freedom
## AIC: 97.568
##
## Number of Fisher Scoring iterations: 10
glm.probs <- predict(glm.fit, Boston.test, type = "response")
glm.pred <- rep(0, length(glm.probs))
glm.pred[glm.probs > 0.5] <- 1
table(glm.pred, crim01.test)
## crim01.test
## glm.pred 0 1
## 0 68 24
## 1 22 139
mean(glm.pred != crim01.test)
## [1] 0.1818182
We may conclude that, for this logistic regression, we have a test error rate of 18.1818182%.
# logistic regression (-chas -nox -tax)
glm.fit <- glm(crim01 ~ . - crim01 - crim -chas -nox -tax, data = Boston, family = binomial, subset = train)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(glm.fit)
##
## Call:
## glm(formula = crim01 ~ . - crim01 - crim - chas - nox - tax,
## family = binomial, data = Boston, subset = train)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -3.04443 -0.24461 -0.00114 0.38919 2.72999
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -17.291707 6.019497 -2.873 0.004071 **
## zn -0.478891 0.104276 -4.593 4.38e-06 ***
## indus 0.362719 0.082969 4.372 1.23e-05 ***
## rm -2.364642 0.967625 -2.444 0.014535 *
## age 0.063371 0.015457 4.100 4.14e-05 ***
## dis 1.494535 0.397249 3.762 0.000168 ***
## rad 1.756498 0.357330 4.916 8.85e-07 ***
## ptratio 0.575045 0.161917 3.551 0.000383 ***
## black -0.018916 0.006754 -2.801 0.005102 **
## lstat 0.057632 0.053051 1.086 0.277326
## medv 0.237282 0.081326 2.918 0.003527 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 329.37 on 252 degrees of freedom
## Residual deviance: 139.59 on 242 degrees of freedom
## AIC: 161.59
##
## Number of Fisher Scoring iterations: 9
glm.probs <- predict(glm.fit, Boston.test, type = "response")
glm.pred <- rep(0, length(glm.probs))
glm.pred[glm.probs > 0.5] <- 1
table(glm.pred, crim01.test)
## crim01.test
## glm.pred 0 1
## 0 78 28
## 1 12 135
mean(glm.pred != crim01.test)
## [1] 0.1581028
We may conclude that, for this logistic regression, we have a test error rate of 15.8102767%.
# LDA
lda.fit <- lda(crim01 ~ . - crim01 - crim, data = Boston, subset = train)
lda.pred <- predict(lda.fit, Boston.test)
table(lda.pred$class, crim01.test)
## crim01.test
## 0 1
## 0 80 24
## 1 10 139
mean(lda.pred$class != crim01.test)
## [1] 0.1343874
We may conclude that, for this LDA, we have a test error rate of 13.4387352%.
# LDA (-chas -nox -tax)
lda.fit <- lda(crim01 ~ . - crim01 - crim - chas - nox - tax, data = Boston, subset = train)
lda.pred <- predict(lda.fit, Boston.test)
table(lda.pred$class, crim01.test)
## crim01.test
## 0 1
## 0 83 28
## 1 7 135
mean(lda.pred$class != crim01.test)
## [1] 0.1383399
We may conclude that, for this LDA, we have a test error rate of 13.8339921%.
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)[test, ]
train.crim01 <- crim01[train]
set.seed(1)
knn.pred <- knn(train.X, test.X, train.crim01, k = 1)
table(knn.pred, crim01.test)
## crim01.test
## knn.pred 0 1
## 0 85 111
## 1 5 52
We may conclude that, for this KNN (\(k = 1\)), we have a test error rate of 45.8498024%.
knn.pred <- knn(train.X, test.X, train.crim01, k = 10)
table(knn.pred, crim01.test)
## crim01.test
## knn.pred 0 1
## 0 83 23
## 1 7 140
We may conclude that, for this KNN (\(k = 10\)), we have a test error rate of 11.8577075%.
knn.pred <- knn(train.X, test.X, train.crim01, k = 100)
table(knn.pred, crim01.test)
## crim01.test
## knn.pred 0 1
## 0 86 120
## 1 4 43
We may conclude that, for this KNN (\(k = 100\)), we have a test error rate of 49.0118577%. Proceed to picking the model with the lowest test error rate, but there are more exploration left to do.