Load Libraries and attach data sets

library(ISLR2)
library(caret)
library(e1071)
library(pander)
library(tidyverse)
attach(Auto)
attach(OJ)





Problem 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.


Generate Data Set

set.seed(22)

x1 <- runif (500) - 0.5
x2 <- runif (500) - 0.5
y <- 1 * (x1^2 - x2^2 > 0)
data <- data.frame(x1,x2,as.factor(y))

(b) Plot the observations, colored according to their class labels. Your plot should display \(X_{1}\) on the x-axis, and \(X_{2}\) on the yaxis.


Plot the observations

plot(x1, x2, xlab = 'X1', ylab = 'X2', col = (2 - y), pch = (2 - y), col.lab = 'Dark Blue', col.axis = 'Dark Blue')
box(col = 'Dark Blue')


(c) Fit a logistic regression model to the data, using \(X_{1}\) and \(X_{2}\) as predictors.


Fit the LR model

set.seed(22)

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.26894  -1.17215  -0.00249   1.15428   1.28833  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.01184    0.09013  -0.131    0.895
## x1          -0.40179    0.31621  -1.271    0.204
## x2           0.09526    0.31674   0.301    0.764
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 693.15  on 499  degrees of freedom
## Residual deviance: 691.38  on 497  degrees of freedom
## AIC: 697.38
## 
## Number of Fisher Scoring iterations: 3


(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.


Apply the model and plot the observations

set.seed(22)

lm_prob <- predict(lm_fit, data, type = 'response')
lm_pred <- rep(0,500)
lm_pred[lm_prob > .50] = 1
plot(x1,x2, col = lm_pred+1, pch = lm_pred+1, xlab = 'X1', ylab = 'X2', col.lab = 'Dark Blue', col.axis = 'Dark Blue')
box(col = 'Dark Blue')


(e) Now fit a logistic regression model to the data using non-linear functions of \(X_{1}\) and \(X_{2}\) as predictors (e.g. \(X_{1}^{2}\), \(X_{1}1 × X_{2}\), log(\(X_{2}\)), and so forth).


Fit LR Model using non-linear functions

set.seed(22)

lm_fit2 <- glm(y ~ poly(x1, 2) + poly(x2, 2) + I(x1 * x2), family = "binomial")
summary(lm_fit2)
## 
## Call:
## glm(formula = y ~ poly(x1, 2) + poly(x2, 2) + I(x1 * x2), family = "binomial")
## 
## Deviance Residuals: 
##        Min          1Q      Median          3Q         Max  
## -1.573e-03  -2.000e-08   0.000e+00   2.000e-08   1.779e-03  
## 
## Coefficients:
##                Estimate Std. Error z value Pr(>|z|)
## (Intercept)       70.85    2799.12   0.025    0.980
## poly(x1, 2)1   -2031.61   56762.00  -0.036    0.971
## poly(x1, 2)2   50414.44 1026786.76   0.049    0.961
## poly(x2, 2)1    1855.89   52122.00   0.036    0.972
## poly(x2, 2)2  -49699.85 1013198.45  -0.049    0.961
## I(x1 * x2)       -32.95   28816.48  -0.001    0.999
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 6.9315e+02  on 499  degrees of freedom
## Residual deviance: 9.2417e-06  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.


Apply model and plot observations

set.seed(22)

lm_prob2 <- predict(lm_fit2, data, type = 'response')
lm_pred2 <- rep(0,500)
lm_pred2[lm_prob2 > .50] = 1
plot(x1,x2, col = lm_pred2+1, pch = lm_pred2+1, xlab = 'X1', ylab = 'X2', col.lab = 'Dark Blue', col.axis = 'Dark Blue')
box(col = 'Dark Blue')


(g) Fit a support vector classifier to the data with \(X_{1}\) and \(X_{2}\) as predictors. Obtain a class prediction for each training observation. Plot the observations, colored according to the predicted class labels.


set.seed(22)

svm_fit <- svm(y ~ x1 + x2, data, kernel = 'linear', cost = .001)
svm_pred <- predict(svm_fit, data)
plot(x1,x2, col = svm_pred+1, pch = svm_pred+1, xlab = 'X1', ylab = 'X2', col.lab = 'Dark Blue', col.axis = 'Dark Blue')
box(col = 'Dark Blue')


(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.


Fit non-linear SVM and plot observations

set.seed(22)

svm_fit2 <- svm(y ~ x1 + x2, data, kernel = 'radial', gamma = 1)
svm_pred2 <- predict(svm_fit2, data)
plot(x1,x2, col = svm_pred2+1, pch = svm_pred2+1, xlab = 'X1', ylab = 'X2', col.lab = 'Dark Blue', col.axis = 'Dark Blue')
box(col = 'Dark Blue')


(i) Comment on your results.

The linear functions, as expected, did not do well predicting boundaries for non-linear data; both the Logistic Regression and the Non-Linear SVM performed significantly better in predicting boundaries. The Non-Linear SVM was easier to use as we didn’t have to recode any of the data in order to plot the model.







Problem 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.


Display median

median(Auto$mpg)
## [1] 22.75


Create variable

Auto$mpg_high = as.factor(ifelse(Auto$mpg > median(Auto$mpg), 1, 0))
pander(Auto[,c(1,10)]%>%slice(15:30))
mpg mpg_high
24 1
22 0
18 0
21 0
27 1
26 1
25 1
24 1
25 1
26 1
21 0
10 0
10 0
11 0
9 0
27 1


Remove original mpg column

Auto_mpg <- Auto
Auto <- subset(Auto, select = -mpg)


(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. Note you will need to fit the classifier without the gas mileage variable to produce sensible results.

Based off the values I selected, using 10 fold cross-validation, cost was the best parameter and .01 was the best value with a cross-validation error of .0896.


set.seed(22)

tune_svm <- tune(svm, mpg_high ~ ., data = Auto, kernel = "linear", ranges = list(cost = c(.001, .01, 
    0.1, 1, 10, 100)))
(summary(tune_svm))
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##  0.01
## 
## - best performance: 0.08955128 
## 
## - Detailed performance results:
##    cost      error dispersion
## 1 1e-03 0.13275641 0.05262526
## 2 1e-02 0.08955128 0.05183291
## 3 1e-01 0.09967949 0.05873951
## 4 1e+00 0.09717949 0.04670864
## 5 1e+01 0.10737179 0.05269185
## 6 1e+02 0.12512821 0.04923051


(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.

Based off the values I selected, using 10 fold cross-validation, cost was still the best parameter for both polynomial and radial SVMs and they both found 100 to be the best value. For the polynomial SVM, the best degree of 2 resulted in a cross-validation error rate of .3167. The radial SVM found a gamma of .01 to be best and a cross-validation error of .0869. Out of all three models, the polynomial performed the best by a small margin.


Fitting an polynomial SVM with various degrees

set.seed(22)

tune_SVM2 <- tune(svm, mpg_high ~ ., data = Auto, kernel = 'polynomial', ranges = list(cost = c(.001, 0.1, 
    1, 10, 100), degree = c(2, 3, 4, 5)))

summary(tune_SVM2)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost degree
##   100      2
## 
## - best performance: 0.3166667 
## 
## - Detailed performance results:
##     cost degree     error dispersion
## 1  1e-03      2 0.5741026 0.02415229
## 2  1e-01      2 0.5741026 0.02415229
## 3  1e+00      2 0.5741026 0.02415229
## 4  1e+01      2 0.5510897 0.04591189
## 5  1e+02      2 0.3166667 0.06985644
## 6  1e-03      3 0.5741026 0.02415229
## 7  1e-01      3 0.5741026 0.02415229
## 8  1e+00      3 0.5741026 0.02415229
## 9  1e+01      3 0.5741026 0.02415229
## 10 1e+02      3 0.3905128 0.12677759
## 11 1e-03      4 0.5741026 0.02415229
## 12 1e-01      4 0.5741026 0.02415229
## 13 1e+00      4 0.5741026 0.02415229
## 14 1e+01      4 0.5741026 0.02415229
## 15 1e+02      4 0.5741026 0.02415229
## 16 1e-03      5 0.5741026 0.02415229
## 17 1e-01      5 0.5741026 0.02415229
## 18 1e+00      5 0.5741026 0.02415229
## 19 1e+01      5 0.5741026 0.02415229
## 20 1e+02      5 0.5741026 0.02415229


Fitting a radial SVM with various gammas

set.seed(22)

tune_svm3 <- tune(svm, mpg_high ~ ., data = Auto, kernel = 'radial', ranges = list(cost = c(.001, 0.1, 
    1, 10, 100), gamma = c(.01, 1, 2, 4, 6, 8, 10)))

summary(tune_svm3)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost gamma
##   100  0.01
## 
## - best performance: 0.08692308 
## 
## - Detailed performance results:
##     cost gamma      error dispersion
## 1  1e-03  0.01 0.57410256 0.02415229
## 2  1e-01  0.01 0.11237179 0.04563259
## 3  1e+00  0.01 0.08955128 0.05183291
## 4  1e+01  0.01 0.09711538 0.05915775
## 5  1e+02  0.01 0.08692308 0.04406913
## 6  1e-03  1.00 0.57410256 0.02415229
## 7  1e-01  1.00 0.57410256 0.02415229
## 8  1e+00  1.00 0.09198718 0.05031609
## 9  1e+01  1.00 0.08942308 0.05319204
## 10 1e+02  1.00 0.08942308 0.05319204
## 11 1e-03  2.00 0.57410256 0.02415229
## 12 1e-01  2.00 0.57410256 0.02415229
## 13 1e+00  2.00 0.14820513 0.06177380
## 14 1e+01  2.00 0.13801282 0.05608073
## 15 1e+02  2.00 0.13801282 0.05608073
## 16 1e-03  4.00 0.57410256 0.02415229
## 17 1e-01  4.00 0.57410256 0.02415229
## 18 1e+00  4.00 0.49743590 0.04482497
## 19 1e+01  4.00 0.49493590 0.04832505
## 20 1e+02  4.00 0.49493590 0.04832505
## 21 1e-03  6.00 0.57410256 0.02415229
## 22 1e-01  6.00 0.57410256 0.02415229
## 23 1e+00  6.00 0.51532051 0.03330380
## 24 1e+01  6.00 0.50769231 0.03493312
## 25 1e+02  6.00 0.50769231 0.03493312
## 26 1e-03  8.00 0.57410256 0.02415229
## 27 1e-01  8.00 0.57410256 0.02415229
## 28 1e+00  8.00 0.53320513 0.02838874
## 29 1e+01  8.00 0.51782051 0.03078377
## 30 1e+02  8.00 0.51782051 0.03078377
## 31 1e-03 10.00 0.57410256 0.02415229
## 32 1e-01 10.00 0.57410256 0.02415229
## 33 1e+00 10.00 0.53833333 0.02892408
## 34 1e+01 10.00 0.53576923 0.03002696
## 35 1e+02 10.00 0.53576923 0.03002696


(d) Make some plots to back up your assertions in (b) and (c).


svm_linear <- svm(mpg_high ~ ., data = Auto, kernel = 'linear', cost = 1)
svm_poly <- svm(mpg_high ~ ., data = Auto, kernel = 'polynomial', degree = 2, cost = 100)
svm_radial <- svm(mpg_high ~ ., data = Auto, kernel = 'radial', gamma = .01, cost = 100)

Auto$mpg <- Auto_mpg$mpg


Linear SVM Scatter Plots

plot(svm_linear, Auto, mpg ~ acceleration, svSymbol = 19, dataSymbol = 1)

plot(svm_linear, Auto, mpg ~ displacement, svSymbol = 19, dataSymbol = 1)

plot(svm_linear, Auto, mpg ~ horsepower, svSymbol = 19, dataSymbol = 1)


Polynomial SVM Scatter Plots

plot(svm_poly, Auto, mpg ~ acceleration, svSymbol = 19, dataSymbol = 1)

plot(svm_linear, Auto, mpg ~ displacement, svSymbol = 19, dataSymbol = 1)

plot(svm_linear, Auto, mpg ~ horsepower, svSymbol = 19, dataSymbol = 1)


Radial SVM Scatter Plots

plot(svm_radial, Auto, mpg ~ acceleration, svSymbol = 19, dataSymbol = 1)

plot(svm_radial, Auto, mpg ~ displacement, svSymbol = 19, dataSymbol = 1)

plot(svm_radial, Auto, mpg ~ horsepower, svSymbol = 19, dataSymbol = 1)







Problem 8.

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

(a) Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.


Split dataset into test and train

set.seed(22)

in_train <- sample(nrow(OJ), 800)
train <- OJ[in_train, ]
test <- OJ[-in_train, ]


(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.


Train the linear SVM model

svm_linear <- svm(Purchase ~ ., data = train, kernel = 'linear', cost = 0.01)

summary(svm_linear)
## 
## Call:
## svm(formula = Purchase ~ ., data = train, kernel = "linear", cost = 0.01)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  linear 
##        cost:  0.01 
## 
## Number of Support Vectors:  449
## 
##  ( 224 225 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM


(c) What are the training and test error rates?

The training error rate is 18.1%, unexpectedly the test error rate decreased to 15.9%.


Find the train error rate

svm_pred_train <- predict(svm_linear, train)
confusionMatrix(table(train$Purchase, svm_pred_train))
## Confusion Matrix and Statistics
## 
##     svm_pred_train
##       CH  MM
##   CH 428  57
##   MM  88 227
##                                           
##                Accuracy : 0.8188          
##                  95% CI : (0.7903, 0.8449)
##     No Information Rate : 0.645           
##     P-Value [Acc > NIR] : < 2e-16         
##                                           
##                   Kappa : 0.6137          
##                                           
##  Mcnemar's Test P-Value : 0.01273         
##                                           
##             Sensitivity : 0.8295          
##             Specificity : 0.7993          
##          Pos Pred Value : 0.8825          
##          Neg Pred Value : 0.7206          
##              Prevalence : 0.6450          
##          Detection Rate : 0.5350          
##    Detection Prevalence : 0.6062          
##       Balanced Accuracy : 0.8144          
##                                           
##        'Positive' Class : CH              
## 


Find the test error rate

svm_pred_test <- predict(svm_linear, test)
confusionMatrix(table(test$Purchase, svm_pred_test))
## Confusion Matrix and Statistics
## 
##     svm_pred_test
##       CH  MM
##   CH 149  19
##   MM  24  78
##                                           
##                Accuracy : 0.8407          
##                  95% CI : (0.7915, 0.8823)
##     No Information Rate : 0.6407          
##     P-Value [Acc > NIR] : 2.551e-13       
##                                           
##                   Kappa : 0.6579          
##                                           
##  Mcnemar's Test P-Value : 0.5419          
##                                           
##             Sensitivity : 0.8613          
##             Specificity : 0.8041          
##          Pos Pred Value : 0.8869          
##          Neg Pred Value : 0.7647          
##              Prevalence : 0.6407          
##          Detection Rate : 0.5519          
##    Detection Prevalence : 0.6222          
##       Balanced Accuracy : 0.8327          
##                                           
##        'Positive' Class : CH              
## 


(d) Use the tune() function to select an optimal cost. Consider values in the range \(0.01\) to \(10\).


Tune the linear SVM

set.seed(22)

tune_svm <- tune(svm, Purchase ~ ., data = train, kernel = 'linear', ranges = list(cost = c(.001, .01, .1, 1, 10)))

summary(tune_svm)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##   0.1
## 
## - best performance: 0.18375 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1 1e-03 0.34500 0.05342440
## 2 1e-02 0.18625 0.03304563
## 3 1e-01 0.18375 0.03866254
## 4 1e+00 0.18375 0.03775377
## 5 1e+01 0.18750 0.03952847


(e) Compute the training and test error rates using this new value for cost.

Using the new values for cost, the training error rate is 17.62% and the test error rate is 14.07%%; once again the test error rate was slightly better.


Train the linear SVM using the new best cost values

svm_linear <- svm(Purchase ~ ., kernel = 'linear', data = train, cost = .1)
train_pred <- predict(svm_linear, train)


Find the train error rate

svm_pred_train <- predict(svm_linear, train)
confusionMatrix(table(train$Purchase, svm_pred_train))
## Confusion Matrix and Statistics
## 
##     svm_pred_train
##       CH  MM
##   CH 426  59
##   MM  82 233
##                                           
##                Accuracy : 0.8238          
##                  95% CI : (0.7955, 0.8495)
##     No Information Rate : 0.635           
##     P-Value [Acc > NIR] : < 2e-16         
##                                           
##                   Kappa : 0.626           
##                                           
##  Mcnemar's Test P-Value : 0.06392         
##                                           
##             Sensitivity : 0.8386          
##             Specificity : 0.7979          
##          Pos Pred Value : 0.8784          
##          Neg Pred Value : 0.7397          
##              Prevalence : 0.6350          
##          Detection Rate : 0.5325          
##    Detection Prevalence : 0.6062          
##       Balanced Accuracy : 0.8183          
##                                           
##        'Positive' Class : CH              
## 


Find the test error rate

svm_pred_test <- predict(svm_linear, test)
confusionMatrix(table(test$Purchase, svm_pred_test))
## Confusion Matrix and Statistics
## 
##     svm_pred_test
##       CH  MM
##   CH 148  20
##   MM  18  84
##                                          
##                Accuracy : 0.8593         
##                  95% CI : (0.812, 0.8984)
##     No Information Rate : 0.6148         
##     P-Value [Acc > NIR] : <2e-16         
##                                          
##                   Kappa : 0.7018         
##                                          
##  Mcnemar's Test P-Value : 0.8711         
##                                          
##             Sensitivity : 0.8916         
##             Specificity : 0.8077         
##          Pos Pred Value : 0.8810         
##          Neg Pred Value : 0.8235         
##              Prevalence : 0.6148         
##          Detection Rate : 0.5481         
##    Detection Prevalence : 0.6222         
##       Balanced Accuracy : 0.8496         
##                                          
##        'Positive' Class : CH             
## 


(f) Repeat parts (b) through (e) using a support vector machine with a radial kernel. Use the default value for gamma.


Train the radial SVM model

svm_radial <- svm(Purchase ~ ., data = train, kernel = 'radial', cost = 0.01)


Find the train error rate

svm_pred_train <- predict(svm_radial, train)
confusionMatrix(table(train$Purchase, svm_pred_train))
## Confusion Matrix and Statistics
## 
##     svm_pred_train
##       CH  MM
##   CH 485   0
##   MM 315   0
##                                           
##                Accuracy : 0.6062          
##                  95% CI : (0.5714, 0.6403)
##     No Information Rate : 1               
##     P-Value [Acc > NIR] : 1               
##                                           
##                   Kappa : 0               
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.6062          
##             Specificity :     NA          
##          Pos Pred Value :     NA          
##          Neg Pred Value :     NA          
##              Prevalence : 1.0000          
##          Detection Rate : 0.6062          
##    Detection Prevalence : 0.6062          
##       Balanced Accuracy :     NA          
##                                           
##        'Positive' Class : CH              
## 


Find the test error rate

svm_pred_test <- predict(svm_radial, test)
confusionMatrix(table(test$Purchase, svm_pred_test))
## Confusion Matrix and Statistics
## 
##     svm_pred_test
##       CH  MM
##   CH 168   0
##   MM 102   0
##                                           
##                Accuracy : 0.6222          
##                  95% CI : (0.5615, 0.6803)
##     No Information Rate : 1               
##     P-Value [Acc > NIR] : 1               
##                                           
##                   Kappa : 0               
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.6222          
##             Specificity :     NA          
##          Pos Pred Value :     NA          
##          Neg Pred Value :     NA          
##              Prevalence : 1.0000          
##          Detection Rate : 0.6222          
##    Detection Prevalence : 0.6222          
##       Balanced Accuracy :     NA          
##                                           
##        'Positive' Class : CH              
## 


Tune the radial SVM

set.seed(22)
tune_svm <- tune(svm, Purchase ~ ., data = train, kernel = 'radial', ranges = list(cost = c(.001, .01, .1, 1, 10)))

summary(tune_svm)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##     1
## 
## - best performance: 0.18625 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1 1e-03 0.39375 0.04535738
## 2 1e-02 0.39375 0.04535738
## 3 1e-01 0.19875 0.03087272
## 4 1e+00 0.18625 0.03458584
## 5 1e+01 0.19750 0.03425801


Train the linear SVM using the new best cost values

svm_radial <- svm(Purchase ~ ., kernel = 'radial', data = train, cost = 1)
train_pred <- predict(svm_radial, train)


Find the train error rate

svm_pred_train <- predict(svm_radial, train)
confusionMatrix(table(train$Purchase, svm_pred_train))
## Confusion Matrix and Statistics
## 
##     svm_pred_train
##       CH  MM
##   CH 442  43
##   MM  86 229
##                                           
##                Accuracy : 0.8388          
##                  95% CI : (0.8114, 0.8636)
##     No Information Rate : 0.66            
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.654           
##                                           
##  Mcnemar's Test P-Value : 0.0002174       
##                                           
##             Sensitivity : 0.8371          
##             Specificity : 0.8419          
##          Pos Pred Value : 0.9113          
##          Neg Pred Value : 0.7270          
##              Prevalence : 0.6600          
##          Detection Rate : 0.5525          
##    Detection Prevalence : 0.6062          
##       Balanced Accuracy : 0.8395          
##                                           
##        'Positive' Class : CH              
## 


Find the test error rate

svm_pred_test <- predict(svm_radial, test)
confusionMatrix(table(test$Purchase, svm_pred_test))
## Confusion Matrix and Statistics
## 
##     svm_pred_test
##       CH  MM
##   CH 150  18
##   MM  23  79
##                                           
##                Accuracy : 0.8481          
##                  95% CI : (0.7997, 0.8888)
##     No Information Rate : 0.6407          
##     P-Value [Acc > NIR] : 2.733e-14       
##                                           
##                   Kappa : 0.6739          
##                                           
##  Mcnemar's Test P-Value : 0.5322          
##                                           
##             Sensitivity : 0.8671          
##             Specificity : 0.8144          
##          Pos Pred Value : 0.8929          
##          Neg Pred Value : 0.7745          
##              Prevalence : 0.6407          
##          Detection Rate : 0.5556          
##    Detection Prevalence : 0.6222          
##       Balanced Accuracy : 0.8407          
##                                           
##        'Positive' Class : CH              
## 


(g) Repeat parts (b) through (e) using a support vector machine with a polynomial kernel. Set degree = 2.


Train the Polynomial SVM

svm_poly <- svm(Purchase ~ ., data = train, kernel = 'polynomial', cost = 0.01, degree = 2)


Find the train error rate

svm_pred_train <- predict(svm_poly, train)
confusionMatrix(table(train$Purchase, svm_pred_train))
## Confusion Matrix and Statistics
## 
##     svm_pred_train
##       CH  MM
##   CH 485   0
##   MM 315   0
##                                           
##                Accuracy : 0.6062          
##                  95% CI : (0.5714, 0.6403)
##     No Information Rate : 1               
##     P-Value [Acc > NIR] : 1               
##                                           
##                   Kappa : 0               
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.6062          
##             Specificity :     NA          
##          Pos Pred Value :     NA          
##          Neg Pred Value :     NA          
##              Prevalence : 1.0000          
##          Detection Rate : 0.6062          
##    Detection Prevalence : 0.6062          
##       Balanced Accuracy :     NA          
##                                           
##        'Positive' Class : CH              
## 


Find the test error rate

svm_pred_test <- predict(svm_poly, test)
confusionMatrix(table(test$Purchase, svm_pred_test))
## Confusion Matrix and Statistics
## 
##     svm_pred_test
##       CH  MM
##   CH 168   0
##   MM 102   0
##                                           
##                Accuracy : 0.6222          
##                  95% CI : (0.5615, 0.6803)
##     No Information Rate : 1               
##     P-Value [Acc > NIR] : 1               
##                                           
##                   Kappa : 0               
##                                           
##  Mcnemar's Test P-Value : <2e-16          
##                                           
##             Sensitivity : 0.6222          
##             Specificity :     NA          
##          Pos Pred Value :     NA          
##          Neg Pred Value :     NA          
##              Prevalence : 1.0000          
##          Detection Rate : 0.6222          
##    Detection Prevalence : 0.6222          
##       Balanced Accuracy :     NA          
##                                           
##        'Positive' Class : CH              
## 


Tune the polynomial SVM

set.seed(22)
tune_svm <- tune(svm, Purchase ~ ., data = train, kernel = 'polynomial', ranges = list(cost = c(.001, .01, .1, 1, 10)), degree = 2)

summary(tune_svm)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##    10
## 
## - best performance: 0.20125 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1 1e-03 0.39375 0.04535738
## 2 1e-02 0.39375 0.04535738
## 3 1e-01 0.31500 0.04779877
## 4 1e+00 0.21500 0.02874698
## 5 1e+01 0.20125 0.03030516


Train the linear SVM using the new best cost values

svm_poly <- svm(Purchase ~ ., kernel = 'polynomial', data = train, cost = 10, degree = 2)
train_pred <- predict(svm_poly, train)


Find the train error rate

svm_pred_train <- predict(svm_poly, train)
confusionMatrix(table(train$Purchase, svm_pred_train))
## Confusion Matrix and Statistics
## 
##     svm_pred_train
##       CH  MM
##   CH 447  38
##   MM  87 228
##                                           
##                Accuracy : 0.8438          
##                  95% CI : (0.8167, 0.8682)
##     No Information Rate : 0.6675          
##     P-Value [Acc > NIR] : < 2.2e-16       
##                                           
##                   Kappa : 0.6635          
##                                           
##  Mcnemar's Test P-Value : 1.761e-05       
##                                           
##             Sensitivity : 0.8371          
##             Specificity : 0.8571          
##          Pos Pred Value : 0.9216          
##          Neg Pred Value : 0.7238          
##              Prevalence : 0.6675          
##          Detection Rate : 0.5587          
##    Detection Prevalence : 0.6062          
##       Balanced Accuracy : 0.8471          
##                                           
##        'Positive' Class : CH              
## 


Find the test error rate

svm_pred_test <- predict(svm_poly, test)
confusionMatrix(table(test$Purchase, svm_pred_test))
## Confusion Matrix and Statistics
## 
##     svm_pred_test
##       CH  MM
##   CH 154  14
##   MM  24  78
##                                          
##                Accuracy : 0.8593         
##                  95% CI : (0.812, 0.8984)
##     No Information Rate : 0.6593         
##     P-Value [Acc > NIR] : 7.968e-14      
##                                          
##                   Kappa : 0.6948         
##                                          
##  Mcnemar's Test P-Value : 0.1443         
##                                          
##             Sensitivity : 0.8652         
##             Specificity : 0.8478         
##          Pos Pred Value : 0.9167         
##          Neg Pred Value : 0.7647         
##              Prevalence : 0.6593         
##          Detection Rate : 0.5704         
##    Detection Prevalence : 0.6222         
##       Balanced Accuracy : 0.8565         
##                                          
##        'Positive' Class : CH             
## 


(h) Overall, which approach seems to give the best results on this data?

Looking at how each model performed on the test data (after being tuned) both radial and linear reached an accuracy of 85.93%. However, the linear model better predicted CH and poly better predicted MM; so depending on which factor you were more interested in predicting, one model may be a better option over the other.