Chapter 9: 5,7,8
5.) We have seen that we can fit an SVM with a non-linear kernel in order to perform classification using a non-linear decision boundary. We will now see that we can also obtain a non-linear decision boundary by performing logistic regression using non-linear transformations of the features.
set.seed(8)
x1 = runif(500) - 0.5
x2 = runif(500) - 0.5
y = as.integer(x1 ^ 2 - x2 ^ 2 > 0)
plot(x1[y == 0], x2[y == 0], col = "green", xlab = "X1", ylab = "X2")
points(x1[y == 1], x2[y == 1], col = "purple")
dat = data.frame(x1 = x1, x2 = x2, y = as.factor(y))
lr.fit = glm(y ~ ., data = dat, family = 'binomial')
lr.prob = predict(lr.fit, newdata = dat, type = 'response')
lr.pred = ifelse(lr.prob > 0.5, 1, 0)
plot(dat$x1, dat$x2, col = lr.pred + 2)
lr.nl = glm(y ~ poly(x1, 2) + poly(x2, 2), data = dat, family = 'binomial')
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(lr.nl)
##
## Call:
## glm(formula = y ~ poly(x1, 2) + poly(x2, 2), family = "binomial",
## data = dat)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.171e-03 -2.000e-08 2.000e-08 2.000e-08 1.201e-03
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 32.11 1699.25 0.019 0.985
## poly(x1, 2)1 461.37 68674.33 0.007 0.995
## poly(x1, 2)2 26770.48 858491.07 0.031 0.975
## poly(x2, 2)1 1207.17 79361.74 0.015 0.988
## poly(x2, 2)2 -27213.11 875178.35 -0.031 0.975
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 6.9179e+02 on 499 degrees of freedom
## Residual deviance: 3.3839e-06 on 495 degrees of freedom
## AIC: 10
##
## Number of Fisher Scoring iterations: 25
lr.prob.nl = predict(lr.nl, newdata = dat, type = 'response')
lr.pred.nl = ifelse(lr.prob.nl > 0.5, 1, 0)
plot(dat$x1, dat$x2, col = lr.pred.nl + 2)
library(e1071)
svm.lin = svm(y ~ ., data = dat, kernel = 'linear', cost = 0.01)
plot(svm.lin, dat)
svm.nl = svm(y ~ ., data = dat, kernel = 'radial', gamma = 1)
plot(svm.nl, data = dat)
SVM with non-linear kernel and logistic regression with interaction terms are both very effective for finding non-linear decision boundaries. SVM with linear kernel and logistic regression without any interaction term are not very useful when it comes to finding non-linear decision boundaries. One argument in favor of SVM is that when using logistic regression, it requires some manual tuning to find the right interaction terms, although when using SVM only the gamma needs to be tuned.
7.) In this problem, you will use support vector approaches in order to predict whether a given car gets high or low gas mileage based on the Auto data set.
library(ISLR)
## Warning: package 'ISLR' was built under R version 4.1.3
gas.med = median(Auto$mpg)
new.var = ifelse(Auto$mpg > gas.med, 1, 0)
Auto$mpglevel = as.factor(new.var)
library(e1071)
set.seed(9)
tune.out = tune(svm, mpglevel ~ ., data = Auto, kernel = "linear", ranges = list(cost = c(0.01, 0.1, 1, 5, 10, 100)))
summary(tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 1
##
## - best performance: 0.01019231
##
## - Detailed performance results:
## cost error dispersion
## 1 1e-02 0.07653846 0.05100638
## 2 1e-01 0.05102564 0.03586588
## 3 1e+00 0.01019231 0.01315951
## 4 5e+00 0.02044872 0.01619554
## 5 1e+01 0.02044872 0.01619554
## 6 1e+02 0.03839744 0.03872235
set.seed(77)
tune.out = tune(svm, mpglevel ~ ., data = Auto, kernel = "polynomial", ranges = list(cost = c(0.1, 1, 5, 10), degree = c(2, 3, 4)))
summary(tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost degree
## 10 2
##
## - best performance: 0.5096795
##
## - Detailed performance results:
## cost degree error dispersion
## 1 0.1 2 0.5635256 0.05199157
## 2 1.0 2 0.5635256 0.05199157
## 3 5.0 2 0.5635256 0.05199157
## 4 10.0 2 0.5096795 0.11240472
## 5 0.1 3 0.5635256 0.05199157
## 6 1.0 3 0.5635256 0.05199157
## 7 5.0 3 0.5635256 0.05199157
## 8 10.0 3 0.5635256 0.05199157
## 9 0.1 4 0.5635256 0.05199157
## 10 1.0 4 0.5635256 0.05199157
## 11 5.0 4 0.5635256 0.05199157
## 12 10.0 4 0.5635256 0.05199157
tune.out = tune(svm, mpglevel ~ ., data = Auto, kernel = "radial", ranges = list(cost = c(0.1, 1, 5, 10), gamma = c(0.01, 0.1, 1, 5, 10, 100)))
summary(tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost gamma
## 10 0.01
##
## - best performance: 0.02557692
##
## - Detailed performance results:
## cost gamma error dispersion
## 1 0.1 1e-02 0.08955128 0.05590133
## 2 1.0 1e-02 0.07416667 0.05062800
## 3 5.0 1e-02 0.05371795 0.04603578
## 4 10.0 1e-02 0.02557692 0.02093679
## 5 0.1 1e-01 0.07673077 0.05142612
## 6 1.0 1e-01 0.05628205 0.04500063
## 7 5.0 1e-01 0.02814103 0.02246020
## 8 10.0 1e-01 0.03326923 0.03211170
## 9 0.1 1e+00 0.56365385 0.04979325
## 10 1.0 1e+00 0.06391026 0.03485206
## 11 5.0 1e+00 0.06647436 0.03680434
## 12 10.0 1e+00 0.06647436 0.03680434
## 13 0.1 5e+00 0.56365385 0.04979325
## 14 1.0 5e+00 0.51512821 0.05521196
## 15 5.0 5e+00 0.51000000 0.05649372
## 16 10.0 5e+00 0.51000000 0.05649372
## 17 0.1 1e+01 0.56365385 0.04979325
## 18 1.0 1e+01 0.52788462 0.05428129
## 19 5.0 1e+01 0.52532051 0.05013610
## 20 10.0 1e+01 0.52532051 0.05013610
## 21 0.1 1e+02 0.56365385 0.04979325
## 22 1.0 1e+02 0.56365385 0.04979325
## 23 5.0 1e+02 0.56365385 0.04979325
## 24 10.0 1e+02 0.56365385 0.04979325
svm.linear = svm(mpglevel ~ ., data = Auto, kernel = "linear", cost = 1)
svm.poly = svm(mpglevel ~ ., data = Auto, kernel = "polynomial", cost = 10,
degree = 2)
svm.radial = svm(mpglevel ~ ., data = Auto, kernel = "radial", cost = 10, gamma = 0.01)
plotpairs = function(fit) {
for (name in names(Auto)[!(names(Auto) %in% c("mpg", "mpglevel", "name"))]) {
plot(fit, Auto, as.formula(paste("mpg~", name, sep = "")))
}
}
plotpairs(svm.linear)
plotpairs(svm.poly)
plotpairs(svm.radial)
8.) This problem involves the OJ data set which is part of the ISLR2 package.
library(ISLR)
set.seed(78)
train = sample(dim(OJ)[1], 800)
OJ.train = OJ[train, ]
OJ.test = OJ[-train, ]
library(e1071)
svm.linear = svm(Purchase ~ ., kernel = "linear", data = OJ.train, cost = 0.01)
summary(svm.linear)
##
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "linear", cost = 0.01)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: linear
## cost: 0.01
##
## Number of Support Vectors: 434
##
## ( 218 216 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
train.pred = predict(svm.linear, OJ.train)
table(OJ.train$Purchase, train.pred)
## train.pred
## CH MM
## CH 422 53
## MM 75 250
(75+53)/(422+53+75+250)
## [1] 0.16
test.pred = predict(svm.linear, OJ.test)
table(OJ.test$Purchase, test.pred)
## test.pred
## CH MM
## CH 150 28
## MM 22 70
(28+22)/(150+28+22+70)
## [1] 0.1851852
Training test error rate is 16%. Testing test error rate is 18.5%.
set.seed(777)
tune.out = tune(svm, Purchase ~ ., data = OJ.train, kernel = "linear", ranges = list(cost = 10^seq(-2, 1, by = 0.25)))
summary(tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.03162278
##
## - best performance: 0.15875
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01000000 0.16625 0.03775377
## 2 0.01778279 0.16250 0.03773077
## 3 0.03162278 0.15875 0.03682259
## 4 0.05623413 0.16250 0.03773077
## 5 0.10000000 0.16500 0.03622844
## 6 0.17782794 0.16500 0.03374743
## 7 0.31622777 0.16375 0.03747684
## 8 0.56234133 0.16625 0.03729108
## 9 1.00000000 0.16625 0.03682259
## 10 1.77827941 0.16500 0.03670453
## 11 3.16227766 0.16000 0.03525699
## 12 5.62341325 0.16000 0.03763863
## 13 10.00000000 0.16250 0.03435921
svm.linear = svm(Purchase ~ ., kernel = "linear", data = OJ.train, cost = tune.out$best.parameters$cost)
train.pred = predict(svm.linear, OJ.train)
table(OJ.train$Purchase, train.pred)
## train.pred
## CH MM
## CH 417 58
## MM 68 257
(68+58)/(417+58+68+257)
## [1] 0.1575
test.pred = predict(svm.linear, OJ.test)
table(OJ.test$Purchase, test.pred)
## test.pred
## CH MM
## CH 147 31
## MM 21 71
(21+31)/(147+31+21+71)
## [1] 0.1925926
Training test error rate is 15.75%. Testing test error rate is 19.26%.
set.seed(1604)
svm.radial = svm(Purchase ~ ., data = OJ.train, kernel = "radial")
summary(svm.radial)
##
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "radial")
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 1
##
## Number of Support Vectors: 363
##
## ( 183 180 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
train.pred = predict(svm.radial, OJ.train)
table(OJ.train$Purchase, train.pred)
## train.pred
## CH MM
## CH 438 37
## MM 80 245
(37+80)/(438+37+80+245)
## [1] 0.14625
test.pred = predict(svm.radial, OJ.test)
table(OJ.test$Purchase, test.pred)
## test.pred
## CH MM
## CH 152 26
## MM 25 67
(25+26)/(152+26+25+67)
## [1] 0.1888889
Training test error rate is 14.6%. Testing test error rate is 18.9%.
set.seed(110)
tune.out = tune(svm, Purchase ~ ., data = OJ.train, kernel = "radial", ranges = list(cost = 10^seq(-2, 1, by = 0.25)))
summary(tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.5623413
##
## - best performance: 0.16375
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01000000 0.40625 0.06488505
## 2 0.01778279 0.40625 0.06488505
## 3 0.03162278 0.29375 0.06488505
## 4 0.05623413 0.19625 0.04411554
## 5 0.10000000 0.18750 0.04526159
## 6 0.17782794 0.17500 0.05400617
## 7 0.31622777 0.17000 0.05627314
## 8 0.56234133 0.16375 0.04945888
## 9 1.00000000 0.16750 0.04866267
## 10 1.77827941 0.16875 0.04938862
## 11 3.16227766 0.17375 0.05118390
## 12 5.62341325 0.17875 0.05070681
## 13 10.00000000 0.18875 0.05219155
svm.radial = svm(Purchase ~ ., data = OJ.train, kernel = "radial", cost = tune.out$best.parameters$cost)
train.pred = predict(svm.radial, OJ.train)
table(OJ.train$Purchase, train.pred)
## train.pred
## CH MM
## CH 437 38
## MM 82 243
(38+82)/(437+38+82+243)
## [1] 0.15
test.pred = predict(svm.radial, OJ.test)
table(OJ.test$Purchase, test.pred)
## test.pred
## CH MM
## CH 150 28
## MM 24 68
(28+24)/(150+28+24+68)
## [1] 0.1925926
Training test error rate is 15%. Testing test error rate is 19.3%.
set.seed(2022)
svm.poly = svm(Purchase ~ ., data = OJ.train, kernel = "poly", degree = 2)
summary(svm.poly)
##
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "poly", degree = 2)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: polynomial
## cost: 1
## degree: 2
## coef.0: 0
##
## Number of Support Vectors: 441
##
## ( 225 216 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
train.pred = predict(svm.poly, OJ.train)
table(OJ.train$Purchase, train.pred)
## train.pred
## CH MM
## CH 448 27
## MM 112 213
(27+112)/(448+27+112+213)
## [1] 0.17375
test.pred = predict(svm.poly, OJ.test)
table(OJ.test$Purchase, test.pred)
## test.pred
## CH MM
## CH 156 22
## MM 40 52
(22+40)/(156+22+40+52)
## [1] 0.2296296
Training test error rate is 17.4%. Testing test error rate is 23.0%
set.seed(1883)
tune.out = tune(svm, Purchase ~ ., data = OJ.train, kernel = "poly", degree = 2,
ranges = list(cost = 10^seq(-2, 1, by = 0.25)))
summary(tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 3.162278
##
## - best performance: 0.17125
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01000000 0.37875 0.05304937
## 2 0.01778279 0.37000 0.05407043
## 3 0.03162278 0.34625 0.05529278
## 4 0.05623413 0.32125 0.05894029
## 5 0.10000000 0.30250 0.05552777
## 6 0.17782794 0.22250 0.06118052
## 7 0.31622777 0.19375 0.05008673
## 8 0.56234133 0.19000 0.04594683
## 9 1.00000000 0.18750 0.05527708
## 10 1.77827941 0.18250 0.05439056
## 11 3.16227766 0.17125 0.05894029
## 12 5.62341325 0.17125 0.05560588
## 13 10.00000000 0.17375 0.05252314
svm.poly = svm(Purchase ~ ., data = OJ.train, kernel = "poly", degree = 2, cost = tune.out$best.parameters$cost)
train.pred = predict(svm.poly, OJ.train)
table(OJ.train$Purchase, train.pred)
## train.pred
## CH MM
## CH 445 30
## MM 92 233
(30+92)/(445+30+92+233)
## [1] 0.1525
test.pred = predict(svm.poly, OJ.test)
table(OJ.test$Purchase, test.pred)
## test.pred
## CH MM
## CH 156 22
## MM 30 62
(22+30)/(156+22+30+62)
## [1] 0.1925926
Training test error rate is 15.3%. Testing test error rate is 19.3%.
Radial appears to be the best option with lowest error rates.