Chapter 09 (page 368): 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.

(a) Generate a data set with n = 500 and p = 2, such that the observations belong to two classes with a quadratic decision boundary between them. For instance, you can do this as follows:

x1=runif (500) -0.5 x2=runif (500) -0.5 y=1*(x12-x22 > 0)

set.seed(12345)
x1 = runif(500) - 0.5
x2 = runif(500) - 0.5
y = 1 * (x1^2 - x2^2 > 0)
(b) Plot the observations, colored according to their class labels. Your plot should display X1 on the x-axis, and X2 on the yaxis.
plot(x1[y == 0], x2[y == 0], col = "red", xlab = "X1", ylab = "X2", pch = "+")
points(x1[y == 1], x2[y == 1], col = "blue", pch = 4)

(c) Fit a logistic regression model to the data, using X1 and X2 as predictors.
lm.fit = glm(y ~ x1 + x2, family = binomial)
summary(lm.fit)
## 
## Call:
## glm(formula = y ~ x1 + x2, family = binomial)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -1.385  -1.207   1.011   1.130   1.328  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.07563    0.09032   0.837    0.402
## x1           0.46494    0.31237   1.488    0.137
## x2           0.41377    0.32373   1.278    0.201
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 692.18  on 499  degrees of freedom
## Residual deviance: 688.04  on 497  degrees of freedom
## AIC: 694.04
## 
## Number of Fisher Scoring iterations: 4
(d) Apply this model to the training data in order to obtain a predicted class label for each training observation. Plot the observations, colored according to the predicted class labels. The decision boundary should be linear.
data = data.frame(x1 = x1, x2 = x2, y = y)
lm.prob = predict(lm.fit, data, type = "response")
lm.pred = ifelse(lm.prob > 0.52, 1, 0)
data.pos = data[lm.pred == 1, ]
data.neg = data[lm.pred == 0, ]
plot(data.pos$x1, data.pos$x2, col = "red", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "blue", pch = 4)

(e) Now fit a logistic regression model to the data using non-linear functions of X1 and X2 as predictors (e.g. X2 1 , X1×X2, log(X2), and so forth).
require(ISLR)
## Loading required package: ISLR
## Warning: package 'ISLR' was built under R version 4.0.5
require(dplyr)
## Loading required package: dplyr
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
require(caret)
## Loading required package: caret
## Warning: package 'caret' was built under R version 4.0.5
## Loading required package: lattice
## Loading required package: ggplot2
require(ggplot2)
require(tidyr)
## Loading required package: tidyr
lm_fit = glm(y ~ poly(x1, 2) + poly(x2, 2) + I(x1 * x2), data = data, family = binomial)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(lm_fit)
## 
## Call:
## glm(formula = y ~ poly(x1, 2) + poly(x2, 2) + I(x1 * x2), family = binomial, 
##     data = data)
## 
## Deviance Residuals: 
##        Min          1Q      Median          3Q         Max  
## -2.547e-03  -2.000e-08   2.000e-08   2.000e-08   2.581e-03  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)
## (Intercept)      438.0     6185.7   0.071    0.944
## poly(x1, 2)1    5675.2    81328.4   0.070    0.944
## poly(x1, 2)2   90749.5  1264688.9   0.072    0.943
## poly(x2, 2)1    5656.4    80941.2   0.070    0.944
## poly(x2, 2)2  -86900.0  1210754.1  -0.072    0.943
## I(x1 * x2)       894.6    17259.9   0.052    0.959
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 6.9218e+02  on 499  degrees of freedom
## Residual deviance: 2.2685e-05  on 494  degrees of freedom
## AIC: 12
## 
## Number of Fisher Scoring iterations: 25
(f) Apply this model to the training data in order to obtain a predicted class label for each training observation. Plot the observations, colored according to the predicted class labels. The decision boundary should be obviously non-linear. If it is not, then repeat (a)-(e) until you come up with an example in which the predicted class labels are obviously non-linear.
lm.prob = predict(lm.fit, data, type = "response")
lm.pred = ifelse(lm.prob > 0.5, 1, 0)
data.pos = data[lm.pred == 1, ]
data.neg = data[lm.pred == 0, ]
plot(data.pos$x1, data.pos$x2, col = "red", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "blue", pch = 4)

(g) Fit a support vector classifier to the data with X1 and X2 as predictors. Obtain a class prediction for each training observation. Plot the observations, colored according to the predicted class labels.
library(e1071)
svm.fit = svm(as.factor(y) ~ x1 + x2, data, kernel = "linear", cost = 0.1)
svm.pred = predict(svm.fit, data)
data.pos = data[svm.pred == 1, ]
data.neg = data[svm.pred == 0, ]
plot(data.pos$x1, data.pos$x2, col = "blue", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "red", pch = 4)

(h) Fit a SVM using a non-linear kernel to the data. Obtain a class prediction for each training observation. Plot the observations, colored according to the predicted class labels.
svm.fit = svm(as.factor(y) ~ x1 + x2, data, gamma = 1)
svm.pred = predict(svm.fit, data)
data.pos = data[svm.pred == 1, ]
data.neg = data[svm.pred == 0, ]
plot(data.pos$x1, data.pos$x2, col = "red", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "blue", pch = 4)

(i) Comment on your results.
  • Using cross validation would be easier with the parameter of gamma
  • SVMS are important to use for finding non linear models

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.

(a) Create a binary variable that takes on a 1 for cars with gas mileage above the median, and a 0 for cars with gas mileage below the median.
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)

Auto %>% as_tibble() %>% mutate(above_median = as.factor( ifelse(mpg >= median(mpg), 1, 0) ) ) -> auto
(b) Fit a support vector classifier to the data with various values of cost, in order to predict whether a car gets high or low gas mileage. Report the cross-validation errors associated with different values of this parameter. Comment on your results.
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)

set.seed(1)
    auto %>%
    tune(svm, above_median ~ ., data = ., kernel = 'linear', ranges = list(cost = c(0.01, 0.1, 1, 10, 100))) -> auto_svc

summary(auto_svc)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##     1
## 
## - best performance: 0.01025641 
## 
## - Detailed performance results:
##    cost      error dispersion
## 1 1e-02 0.07653846 0.03617137
## 2 1e-01 0.04596154 0.03378238
## 3 1e+00 0.01025641 0.01792836
## 4 1e+01 0.02051282 0.02648194
## 5 1e+02 0.03076923 0.03151981
  • Lowest error is when cost = .1
(c) Now repeat (b), this time using SVMs with radial and polynomial basis kernels, with different values of gamma and degree and cost. Comment on your results.
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)

set.seed(1)
auto %>% tune(svm, above_median ~ ., data = ., kernel = 'radial', ranges = list(gamma = c(0.01, 0.1, 1, 10, 100), cost = c(.01, .1, 1, 10))) -> auto_svm_radial
summary(auto_svm_radial)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  gamma cost
##   0.01   10
## 
## - best performance: 0.02557692 
## 
## - Detailed performance results:
##    gamma  cost      error dispersion
## 1  1e-02  0.01 0.55115385 0.04366593
## 2  1e-01  0.01 0.21711538 0.09865227
## 3  1e+00  0.01 0.55115385 0.04366593
## 4  1e+01  0.01 0.55115385 0.04366593
## 5  1e+02  0.01 0.55115385 0.04366593
## 6  1e-02  0.10 0.08929487 0.04382379
## 7  1e-01  0.10 0.07903846 0.03874545
## 8  1e+00  0.10 0.55115385 0.04366593
## 9  1e+01  0.10 0.55115385 0.04366593
## 10 1e+02  0.10 0.55115385 0.04366593
## 11 1e-02  1.00 0.07403846 0.03522110
## 12 1e-01  1.00 0.05371795 0.03525162
## 13 1e+00  1.00 0.06384615 0.04375618
## 14 1e+01  1.00 0.51794872 0.05063697
## 15 1e+02  1.00 0.55115385 0.04366593
## 16 1e-02 10.00 0.02557692 0.02093679
## 17 1e-01 10.00 0.03076923 0.03375798
## 18 1e+00 10.00 0.05884615 0.04020934
## 19 1e+01 10.00 0.51794872 0.04917316
## 20 1e+02 10.00 0.55115385 0.04366593
  • Radial kernel: Lowest error when gamma = 0.01 and cost = 10.
auto %>% tune(svm, above_median ~ ., data = ., kernel = 'polynomial',ranges = list(degree = seq(2, 5), cost = c(.01, .1, 1, 10))) -> auto_svm_poly
summary(auto_svm_poly)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  degree cost
##       2   10
## 
## - best performance: 0.5841667 
## 
## - Detailed performance results:
##    degree  cost     error dispersion
## 1       2  0.01 0.6019231 0.06346118
## 2       3  0.01 0.6019231 0.06346118
## 3       4  0.01 0.6019231 0.06346118
## 4       5  0.01 0.6019231 0.06346118
## 5       2  0.10 0.6019231 0.06346118
## 6       3  0.10 0.6019231 0.06346118
## 7       4  0.10 0.6019231 0.06346118
## 8       5  0.10 0.6019231 0.06346118
## 9       2  1.00 0.6019231 0.06346118
## 10      3  1.00 0.6019231 0.06346118
## 11      4  1.00 0.6019231 0.06346118
## 12      5  1.00 0.6019231 0.06346118
## 13      2 10.00 0.5841667 0.07806609
## 14      3 10.00 0.6019231 0.06346118
## 15      4 10.00 0.6019231 0.06346118
## 16      5 10.00 0.6019231 0.06346118
    • Polynomial kernel: Lowest error is shown with degree = 2 and cost = 10.
(d) Make some plots to back up your assertions in (b) and (c). Hint: In the lab, we used the plot() function for svm objects only in cases with p = 2. When p > 2, you can use the plot() function to create plots displaying pairs of variables at a time. Essentially, instead of typing > plot(svmfit , dat) where svmfit contains your fitted model and dat is a data frame containing your data, you can type > plot(svmfit , dat , x1∼x4) in order to plot just the first and fourth variables. However, you must replace x1 and x4 with the correct variable names. To find out more, type ?plot.svm.
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)
#install.packages("stringr", repos='http://cran.us.r-project.org')
library("stringr")
## Warning: package 'stringr' was built under R version 4.0.5
svm_linr <- svm(above_median ~ ., data = auto, kernel = 'linear', cost = 1)
svm_poly <- svm(above_median ~ ., data = auto, kernel = 'polynomial', degree = 2, cost = 10)
svm_radl <- svm(above_median ~ ., data = auto, kernel = 'radial', gamma = 0.01, cost = 10)

plot_pairs <- function(fit, data, dependent, independents) {
    for (independent in independents) {
        formula = as.formula( str_c( dependent, '~', independent) )
        plot(fit, data, formula)
    }
}

plot_pairs(svm_linr, auto, 'mpg', c('acceleration', 'displacement', 'horsepower'))

8. This problem involves the OJ data set which is part of the ISLR package

(a) Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.
#install.packages("modelr")
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)
require(modelr)
## Loading required package: modelr
## Warning: package 'modelr' was built under R version 4.0.5
set.seed(1)
oj_train_samples <-OJ %>% resample_partition(c(train = .8, test = .2))
summary(oj_train_samples)
##       Length Class    Mode
## train 2      resample list
## test  2      resample list
(b) Fit a support vector classifier to the training data using cost=0.01, with Purchase as the response and the other variables as predictors. Use the summary() function to produce summary statistics, and describe the results obtained.
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)

oj_linr_svc <- svm(Purchase ~ ., data = oj_train_samples$train, kernel = 'linear', cost = 0.01)
summary(oj_linr_svc)
## 
## Call:
## svm(formula = Purchase ~ ., data = oj_train_samples$train, kernel = "linear", 
##     cost = 0.01)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  linear 
##        cost:  0.01 
## 
## Number of Support Vectors:  450
## 
##  ( 226 224 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
  • 471 Support Vectors
  • 226 are connected to class CH
  • 224 are connected to class MM
(c) What are the training and test error rates?
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)

oj_train_samples$train %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_linr_svc, newdata = .)) %>% summarize('Train Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Train Error Rate`
##                <dbl>
## 1              0.157
  • Train(0.1567251)
oj_train_samples$test %>%as_tibble() %>% mutate(Purchase_prime = predict(oj_linr_svc, newdata = .)) %>% summarize('Test Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Test Error Rate`
##               <dbl>
## 1             0.205
  • Test(0.2046512)
(d) Use the tune() function to select an optimal cost. Consider values in the range 0.01 to 10.
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)

set.seed(1)
tune(svm, Purchase ~ ., data = as_tibble( oj_train_samples$train ), kernel = 'linear', ranges = list(cost = 2^seq(-8,4))) -> oj_svc_tune

summary(oj_svc_tune)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##      cost
##  0.015625
## 
## - best performance: 0.1603557 
## 
## - Detailed performance results:
##           cost     error dispersion
## 1   0.00390625 0.1638577 0.03795702
## 2   0.00781250 0.1626813 0.03729402
## 3   0.01562500 0.1603557 0.03840728
## 4   0.03125000 0.1650205 0.03522022
## 5   0.06250000 0.1638988 0.03695773
## 6   0.12500000 0.1615458 0.03716560
## 7   0.25000000 0.1615595 0.03601742
## 8   0.50000000 0.1603967 0.03777655
## 9   1.00000000 0.1650479 0.03445874
## 10  2.00000000 0.1662244 0.03721419
## 11  4.00000000 0.1627086 0.03446296
## 12  8.00000000 0.1627223 0.03666065
## 13 16.00000000 0.1638851 0.03643699
  • Best parameters: Cost = 0.015625
(e) Compute the training and test error rates using this new value for cost.
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)

oj_linr_svc <- svm( Purchase ~ ., data = oj_train_samples$train,kernel = 'linear',cost = oj_svc_tune$best.parameters$cost)

oj_train_samples$train %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_linr_svc)) %>% summarize('Train Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Train Error Rate`
##                <dbl>
## 1              0.152
oj_train_samples$test %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_linr_svc, newdata = .)) %>% summarize('Test Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Test Error Rate`
##               <dbl>
## 1               0.2
(f) Repeat parts (b) through (e) using a support vector machine with a radial kernel. Use the default value for gamma.
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)

oj_radl_svc <- svm( Purchase ~ ., data = oj_train_samples$train, kernel = 'radial')
summary(oj_radl_svc)
## 
## Call:
## svm(formula = Purchase ~ ., data = oj_train_samples$train, kernel = "radial")
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  1 
## 
## Number of Support Vectors:  390
## 
##  ( 201 189 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
oj_train_samples$train %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_radl_svc, newdata = .)) %>% summarize('Train Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Train Error Rate`
##                <dbl>
## 1              0.145
oj_train_samples$test %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_radl_svc, newdata = .)) %>% summarize('Test Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Test Error Rate`
##               <dbl>
## 1             0.186
set.seed(1)
tune(svm,Purchase ~ .,data = as_tibble( oj_train_samples$train ),kernel = 'radial',ranges = list(cost = 2^seq(-8,4))) -> oj_radl_tune
summary(oj_radl_tune)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##     1
## 
## - best performance: 0.1673461 
## 
## - Detailed performance results:
##           cost     error dispersion
## 1   0.00390625 0.3764843 0.06228157
## 2   0.00781250 0.3764843 0.06228157
## 3   0.01562500 0.3764843 0.06228157
## 4   0.03125000 0.3366211 0.07569987
## 5   0.06250000 0.1918605 0.04001821
## 6   0.12500000 0.1755267 0.03617482
## 7   0.25000000 0.1755404 0.03986210
## 8   0.50000000 0.1720246 0.04204614
## 9   1.00000000 0.1673461 0.03585901
## 10  2.00000000 0.1720520 0.04109274
## 11  4.00000000 0.1731874 0.04013555
## 12  8.00000000 0.1825581 0.03589686
## 13 16.00000000 0.1837346 0.03185391
oj_radl_svc <- svm(Purchase ~ .,data = oj_train_samples$train,kernel = 'linear',cost = oj_radl_tune$best.parameters$cost)

oj_train_samples$train %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_radl_svc)) %>% summarize('Train Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Train Error Rate`
##                <dbl>
## 1              0.152
oj_train_samples$test %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_radl_svc, newdata = .)) %>% summarize('Test Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Test Error Rate`
##               <dbl>
## 1               0.2
(g) Repeat parts (b) through (e) using a support vector machine with a polynomial kernel. Set degree=2.
require(ISLR)
require(dplyr)
require(caret)
require(ggplot2)
require(tidyr)
oj_poly_svc <- svm(Purchase ~ ., data = oj_train_samples$train, kernel = 'polynomial',degree = 2)
summary(oj_poly_svc)
## 
## Call:
## svm(formula = Purchase ~ ., data = oj_train_samples$train, kernel = "polynomial", 
##     degree = 2)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  polynomial 
##        cost:  1 
##      degree:  2 
##      coef.0:  0 
## 
## Number of Support Vectors:  467
## 
##  ( 238 229 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
oj_train_samples$train %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_poly_svc, newdata = .)) %>% summarize('Train Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Train Error Rate`
##                <dbl>
## 1              0.174
oj_train_samples$test %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_poly_svc, newdata = .)) %>% summarize('Test Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Test Error Rate`
##               <dbl>
## 1             0.214
set.seed(1)
tune(svm,Purchase ~ .,data = as_tibble( oj_train_samples$train ),kernel = 'polynomial',ranges = list(cost = 2^seq(-8,4)),degree = 2) -> oj_poly_tune
summary(oj_poly_tune)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##     8
## 
## - best performance: 0.1708208 
## 
## - Detailed performance results:
##           cost     error dispersion
## 1   0.00390625 0.3764843 0.06228157
## 2   0.00781250 0.3776471 0.06301831
## 3   0.01562500 0.3495075 0.07242409
## 4   0.03125000 0.3389740 0.06929994
## 5   0.06250000 0.3179617 0.06929113
## 6   0.12500000 0.3016005 0.07149584
## 7   0.25000000 0.2268673 0.03547776
## 8   0.50000000 0.2012312 0.04937234
## 9   1.00000000 0.1918194 0.04481021
## 10  2.00000000 0.1848290 0.04773197
## 11  4.00000000 0.1801505 0.04354956
## 12  8.00000000 0.1708208 0.04408733
## 13 16.00000000 0.1732011 0.04348912
oj_poly_svc <- svm(Purchase ~ .,data = oj_train_samples$train,kernel = 'polynomial',cost = oj_poly_tune$best.parameters$cost)

oj_train_samples$train %>% as_tibble() %>%mutate(Purchase_prime = predict(oj_poly_svc)) %>%summarize('Train Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Train Error Rate`
##                <dbl>
## 1              0.137
oj_train_samples$test %>% as_tibble() %>% mutate(Purchase_prime = predict(oj_poly_svc, newdata = .)) %>% summarize('Test Error Rate' = mean(Purchase != Purchase_prime))
## # A tibble: 1 x 1
##   `Test Error Rate`
##               <dbl>
## 1             0.219
(h) Overall, which approach seems to give the best results on this data?
  • Radial kernal approach gives the better results. Our training and test errors rates are the lowest compared to Linear and Polynomial kernels.