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

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

ANSWER: See below.

set.seed(123)
x1 <- runif (500) - 0.5
x2 <- runif (500) - 0.5
y <- 1 * (x1^2 - x2^2 > 0)
  1. Plot the observations, colored according to their class labels. Your plot should display X1 on the x-axis, and X2 on the y-axis.

ANSWER: See below.

  1. Fit a logistic regression model to the data, using X1 and X2 as predictors.

ANSWER: See below.

## 
## Call:
## glm(formula = y ~ x1 + x2, family = binomial)
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.04792    0.08949   0.535    0.592
## x1          -0.03999    0.31516  -0.127    0.899
## x2           0.11509    0.30829   0.373    0.709
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 692.86  on 499  degrees of freedom
## Residual deviance: 692.71  on 497  degrees of freedom
## AIC: 698.71
## 
## Number of Fisher Scoring iterations: 3
  1. 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.

ANSWER: See below.

  1. Now fit a logistic regression model to the data using non-linear functions of X1 and X2 as predictors (e.g. X21 , X1 ×X2, log(X2), and so forth).

ANSWER: See below.

## 
## Call:
## glm(formula = y ~ x1 + x2 + x1_sq + x2_sq + x1_x2 + log_x2, family = binomial, 
##     data = df2)
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)
## (Intercept)     16.764  13806.290   0.001    0.999
## x1            -149.246   9532.466  -0.016    0.988
## x2              47.861   7865.444   0.006    0.995
## x1_sq        12271.328 472913.778   0.026    0.979
## x2_sq       -12436.950 471735.546  -0.026    0.979
## x1_x2          580.189  35728.849   0.016    0.987
## log_x2           7.504   4300.380   0.002    0.999
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 6.9286e+02  on 499  degrees of freedom
## Residual deviance: 2.2577e-06  on 493  degrees of freedom
## AIC: 14
## 
## Number of Fisher Scoring iterations: 25
  1. 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.

ANSWER: See below.

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

ANSWER: See below.

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

ANSWER: See below.

  1. Comment on your results.

ANSWER: The linear svm model fails on th dataset because the true decision line is non-linear but we are telling it to use a linear method. It works with the second one because it uses a radial method that captures the true quadratic decision line.

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

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

ANSWER: See below.

Auto1 <- ISLR2::Auto

mpg_median <- median(Auto1$mpg)

Auto1$mpg_binary <- ifelse(Auto1$mpg > mpg_median, 1, 0)
  1. 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.

ANSWER: The SVM produced at an accuracy of about 89.8%, and the cross validation error is about 10.2%. The standard deviation across folds is 0.02, which I would say is pretty consistent.

##   C  Accuracy     Kappa AccuracySD    KappaSD
## 1 1 0.8978576 0.7956496 0.02094903 0.04215797
##   C  Accuracy
## 1 1 0.8978576
##   C  Accuracy     Kappa AccuracySD    KappaSD  CV_Error
## 1 1 0.8978576 0.7956496 0.02094903 0.04215797 0.1021424
  1. 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.

ANSWER: The radial model produced slightly less accurate results in the higher folds but peaks at C = 1. For the polynomial model, it performed only slighlty better than the radial model. Overall, the best-performing model is a degree‑2 polynomial SVM with scale = 1e−02 and C = 1.

Radial:

##        sigma    C  Accuracy     Kappa AccuracySD    KappaSD
## 1 0.00212946 0.25 0.8776046 0.7547022 0.05073164 0.10265777
## 2 0.00212946 0.50 0.8951012 0.7899617 0.05497537 0.11049960
## 3 0.00212946 1.00 0.8976653 0.7951952 0.04716699 0.09460053
## 4 0.00212946 2.00 0.8951012 0.7900026 0.05083290 0.10206794
## 5 0.00212946 4.00 0.8951012 0.7900026 0.05083290 0.10206794
##      C  Accuracy
## 1 0.25 0.8776046
## 2 0.50 0.8951012
## 3 1.00 0.8976653
## 4 2.00 0.8951012
## 5 4.00 0.8951012
##        sigma    C  Accuracy     Kappa AccuracySD    KappaSD  CV_Error
## 1 0.00212946 0.25 0.8776046 0.7547022 0.05073164 0.10265777 0.1223954
## 2 0.00212946 0.50 0.8951012 0.7899617 0.05497537 0.11049960 0.1048988
## 3 0.00212946 1.00 0.8976653 0.7951952 0.04716699 0.09460053 0.1023347
## 4 0.00212946 2.00 0.8951012 0.7900026 0.05083290 0.10206794 0.1048988
## 5 0.00212946 4.00 0.8951012 0.7900026 0.05083290 0.10206794 0.1048988

Polynomial:

##    degree scale    C  Accuracy     Kappa AccuracySD    KappaSD
## 1       1 1e-03 0.25 0.7648144 0.5331061 0.09629271 0.18701816
## 2       1 1e-03 0.50 0.8391802 0.6780622 0.04814076 0.09654416
## 3       1 1e-03 1.00 0.8908097 0.7814432 0.05720456 0.11439729
## 4       1 1e-03 2.00 0.8957422 0.7913728 0.05576213 0.11151552
## 5       1 1e-03 4.00 0.8957422 0.7913728 0.05053572 0.10106181
## 6       1 1e-02 0.25 0.8957422 0.7913728 0.05053572 0.10106181
## 7       1 1e-02 0.50 0.8983738 0.7966359 0.05115889 0.10231464
## 8       1 1e-02 1.00 0.8983738 0.7966359 0.05115889 0.10231464
## 9       1 1e-02 2.00 0.9008738 0.8016359 0.04618669 0.09237663
## 10      1 1e-02 4.00 0.9008738 0.8016359 0.04618669 0.09237663
## 11      1 1e-01 0.25 0.9008738 0.8016359 0.04618669 0.09237663
## 12      1 1e-01 0.50 0.9008738 0.8016359 0.04618669 0.09237663
## 13      1 1e-01 1.00 0.8932456 0.7863184 0.04167147 0.08334538
## 14      1 1e-01 2.00 0.8880499 0.7759985 0.04086593 0.08176279
## 15      1 1e-01 4.00 0.8828576 0.7653113 0.05365606 0.10807459
## 16      1 1e+00 0.25 0.8904858 0.7807380 0.04121326 0.08271422
## 17      1 1e+00 0.50 0.8803576 0.7603798 0.04599700 0.09244391
## 18      1 1e+00 1.00 0.8804217 0.7604366 0.05024123 0.10085139
## 19      1 1e+00 2.00 0.8852935 0.7701737 0.07047410 0.14159598
## 20      1 1e+00 4.00 0.8699696 0.7394272 0.07570431 0.15229550
## 21      1 1e+01 0.25 0.8802294 0.7600354 0.06696408 0.13455938
## 22      1 1e+01 0.50 0.8673381 0.7341641 0.07417320 0.14923142
## 23      1 1e+01 1.00 0.8724055 0.7443571 0.07924802 0.15932407
## 24      1 1e+01 2.00 0.8595816 0.7186382 0.06961202 0.13970471
## 25      1 1e+01 4.00 0.8595783 0.7187735 0.07275628 0.14598315
## 26      2 1e-03 0.25 0.8418117 0.6833254 0.04743414 0.09515314
## 27      2 1e-03 0.50 0.8908097 0.7814432 0.05720456 0.11439729
## 28      2 1e-03 1.00 0.8982422 0.7963728 0.05001125 0.10001896
## 29      2 1e-03 2.00 0.8957422 0.7913728 0.05053572 0.10106181
## 30      2 1e-03 4.00 0.8983738 0.7966359 0.05115889 0.10231464
## 31      2 1e-02 0.25 0.9008097 0.8014976 0.04130407 0.08259150
## 32      2 1e-02 0.50 0.9008097 0.8014976 0.04130407 0.08259150
## 33      2 1e-02 1.00 0.9033738 0.8066359 0.04680658 0.09362299
## 34      2 1e-02 2.00 0.8956781 0.7911936 0.04481848 0.08966943
## 35      2 1e-02 4.00 0.8931781 0.7861936 0.04675548 0.09353242
## 36      2 1e-01 0.25 0.8825270 0.7649454 0.05295359 0.10584522
## 37      2 1e-01 0.50 0.8773279 0.7544358 0.06293210 0.12600020
## 38      2 1e-01 1.00 0.8491161 0.6980349 0.05535240 0.11061097
## 39      2 1e-01 2.00 0.8464204 0.6926742 0.06902287 0.13800470
## 40      2 1e-01 4.00 0.8412247 0.6823533 0.07526486 0.15037445
## 41      2 1e+00 0.25 0.6355567 0.2716058 0.07389893 0.14378121
## 42      2 1e+00 0.50 0.6381208 0.2769247 0.07455173 0.14461832
## 43      2 1e+00 1.00 0.6381208 0.2769247 0.07455173 0.14461832
## 44      2 1e+00 2.00 0.6381208 0.2769247 0.07455173 0.14461832
## 45      2 1e+00 4.00 0.6381208 0.2769247 0.07455173 0.14461832
## 46      2 1e+01 0.25 0.6068117 0.2139797 0.07841051 0.15311610
## 47      2 1e+01 0.50 0.6068117 0.2139797 0.07841051 0.15311610
## 48      2 1e+01 1.00 0.6068117 0.2139797 0.07841051 0.15311610
## 49      2 1e+01 2.00 0.6068117 0.2139797 0.07841051 0.15311610
## 50      2 1e+01 4.00 0.6068117 0.2139797 0.07841051 0.15311610
## 51      3 1e-03 0.25 0.8830499 0.7659706 0.05098544 0.10196529
## 52      3 1e-03 0.50 0.8906781 0.7811936 0.05517254 0.11035507
## 53      3 1e-03 1.00 0.8957422 0.7913728 0.05053572 0.10106181
## 54      3 1e-03 2.00 0.9008738 0.8016359 0.04618669 0.09237663
## 55      3 1e-03 4.00 0.8983738 0.7966359 0.05115889 0.10231464
## 56      3 1e-02 0.25 0.9008097 0.8014976 0.04130407 0.08259150
## 57      3 1e-02 0.50 0.9008097 0.8014976 0.04295247 0.08588895
## 58      3 1e-02 1.00 0.9007422 0.8013728 0.04791255 0.09582788
## 59      3 1e-02 2.00 0.9007422 0.8013728 0.04492031 0.08984360
## 60      3 1e-02 4.00 0.8853543 0.7705020 0.05492544 0.11009125
## 61      3 1e-01 0.25 0.6462854 0.2929271 0.10026002 0.19747785
## 62      3 1e-01 0.50 0.6357591 0.2718744 0.07722661 0.15053797
## 63      3 1e-01 1.00 0.6357591 0.2718744 0.07722661 0.15053797
## 64      3 1e-01 2.00 0.6383907 0.2771376 0.08232997 0.16098016
## 65      3 1e-01 4.00 0.6383907 0.2771376 0.08232997 0.16098016
## 66      3 1e+00 0.25 0.6278644 0.2560850 0.06597084 0.12738520
## 67      3 1e+00 0.50 0.6278644 0.2560850 0.06597084 0.12738520
## 68      3 1e+00 1.00 0.6278644 0.2560850 0.06597084 0.12738520
## 69      3 1e+00 2.00 0.6278644 0.2560850 0.06597084 0.12738520
## 70      3 1e+00 4.00 0.6278644 0.2560850 0.06597084 0.12738520
## 71      3 1e+01 0.25 0.6278644 0.2560850 0.06597084 0.12738520
## 72      3 1e+01 0.50 0.6278644 0.2560850 0.06597084 0.12738520
## 73      3 1e+01 1.00 0.6278644 0.2560850 0.06597084 0.12738520
## 74      3 1e+01 2.00 0.6278644 0.2560850 0.06597084 0.12738520
## 75      3 1e+01 4.00 0.6278644 0.2560850 0.06597084 0.12738520
##       C  Accuracy
## 1  0.25 0.7648144
## 2  0.50 0.8391802
## 3  1.00 0.8908097
## 4  2.00 0.8957422
## 5  4.00 0.8957422
## 6  0.25 0.8957422
## 7  0.50 0.8983738
## 8  1.00 0.8983738
## 9  2.00 0.9008738
## 10 4.00 0.9008738
## 11 0.25 0.9008738
## 12 0.50 0.9008738
## 13 1.00 0.8932456
## 14 2.00 0.8880499
## 15 4.00 0.8828576
## 16 0.25 0.8904858
## 17 0.50 0.8803576
## 18 1.00 0.8804217
## 19 2.00 0.8852935
## 20 4.00 0.8699696
## 21 0.25 0.8802294
## 22 0.50 0.8673381
## 23 1.00 0.8724055
## 24 2.00 0.8595816
## 25 4.00 0.8595783
## 26 0.25 0.8418117
## 27 0.50 0.8908097
## 28 1.00 0.8982422
## 29 2.00 0.8957422
## 30 4.00 0.8983738
## 31 0.25 0.9008097
## 32 0.50 0.9008097
## 33 1.00 0.9033738
## 34 2.00 0.8956781
## 35 4.00 0.8931781
## 36 0.25 0.8825270
## 37 0.50 0.8773279
## 38 1.00 0.8491161
## 39 2.00 0.8464204
## 40 4.00 0.8412247
## 41 0.25 0.6355567
## 42 0.50 0.6381208
## 43 1.00 0.6381208
## 44 2.00 0.6381208
## 45 4.00 0.6381208
## 46 0.25 0.6068117
## 47 0.50 0.6068117
## 48 1.00 0.6068117
## 49 2.00 0.6068117
## 50 4.00 0.6068117
## 51 0.25 0.8830499
## 52 0.50 0.8906781
## 53 1.00 0.8957422
## 54 2.00 0.9008738
## 55 4.00 0.8983738
## 56 0.25 0.9008097
## 57 0.50 0.9008097
## 58 1.00 0.9007422
## 59 2.00 0.9007422
## 60 4.00 0.8853543
## 61 0.25 0.6462854
## 62 0.50 0.6357591
## 63 1.00 0.6357591
## 64 2.00 0.6383907
## 65 4.00 0.6383907
## 66 0.25 0.6278644
## 67 0.50 0.6278644
## 68 1.00 0.6278644
## 69 2.00 0.6278644
## 70 4.00 0.6278644
## 71 0.25 0.6278644
## 72 0.50 0.6278644
## 73 1.00 0.6278644
## 74 2.00 0.6278644
## 75 4.00 0.6278644
##    degree scale    C  Accuracy     Kappa AccuracySD    KappaSD   CV_Error
## 1       1 1e-03 0.25 0.7648144 0.5331061 0.09629271 0.18701816 0.23518556
## 2       1 1e-03 0.50 0.8391802 0.6780622 0.04814076 0.09654416 0.16081984
## 3       1 1e-03 1.00 0.8908097 0.7814432 0.05720456 0.11439729 0.10919028
## 4       1 1e-03 2.00 0.8957422 0.7913728 0.05576213 0.11151552 0.10425776
## 5       1 1e-03 4.00 0.8957422 0.7913728 0.05053572 0.10106181 0.10425776
## 6       1 1e-02 0.25 0.8957422 0.7913728 0.05053572 0.10106181 0.10425776
## 7       1 1e-02 0.50 0.8983738 0.7966359 0.05115889 0.10231464 0.10162618
## 8       1 1e-02 1.00 0.8983738 0.7966359 0.05115889 0.10231464 0.10162618
## 9       1 1e-02 2.00 0.9008738 0.8016359 0.04618669 0.09237663 0.09912618
## 10      1 1e-02 4.00 0.9008738 0.8016359 0.04618669 0.09237663 0.09912618
## 11      1 1e-01 0.25 0.9008738 0.8016359 0.04618669 0.09237663 0.09912618
## 12      1 1e-01 0.50 0.9008738 0.8016359 0.04618669 0.09237663 0.09912618
## 13      1 1e-01 1.00 0.8932456 0.7863184 0.04167147 0.08334538 0.10675439
## 14      1 1e-01 2.00 0.8880499 0.7759985 0.04086593 0.08176279 0.11195007
## 15      1 1e-01 4.00 0.8828576 0.7653113 0.05365606 0.10807459 0.11714238
## 16      1 1e+00 0.25 0.8904858 0.7807380 0.04121326 0.08271422 0.10951417
## 17      1 1e+00 0.50 0.8803576 0.7603798 0.04599700 0.09244391 0.11964238
## 18      1 1e+00 1.00 0.8804217 0.7604366 0.05024123 0.10085139 0.11957827
## 19      1 1e+00 2.00 0.8852935 0.7701737 0.07047410 0.14159598 0.11470648
## 20      1 1e+00 4.00 0.8699696 0.7394272 0.07570431 0.15229550 0.13003036
## 21      1 1e+01 0.25 0.8802294 0.7600354 0.06696408 0.13455938 0.11977058
## 22      1 1e+01 0.50 0.8673381 0.7341641 0.07417320 0.14923142 0.13266194
## 23      1 1e+01 1.00 0.8724055 0.7443571 0.07924802 0.15932407 0.12759447
## 24      1 1e+01 2.00 0.8595816 0.7186382 0.06961202 0.13970471 0.14041835
## 25      1 1e+01 4.00 0.8595783 0.7187735 0.07275628 0.14598315 0.14042173
## 26      2 1e-03 0.25 0.8418117 0.6833254 0.04743414 0.09515314 0.15818826
## 27      2 1e-03 0.50 0.8908097 0.7814432 0.05720456 0.11439729 0.10919028
## 28      2 1e-03 1.00 0.8982422 0.7963728 0.05001125 0.10001896 0.10175776
## 29      2 1e-03 2.00 0.8957422 0.7913728 0.05053572 0.10106181 0.10425776
## 30      2 1e-03 4.00 0.8983738 0.7966359 0.05115889 0.10231464 0.10162618
## 31      2 1e-02 0.25 0.9008097 0.8014976 0.04130407 0.08259150 0.09919028
## 32      2 1e-02 0.50 0.9008097 0.8014976 0.04130407 0.08259150 0.09919028
## 33      2 1e-02 1.00 0.9033738 0.8066359 0.04680658 0.09362299 0.09662618
## 34      2 1e-02 2.00 0.8956781 0.7911936 0.04481848 0.08966943 0.10432186
## 35      2 1e-02 4.00 0.8931781 0.7861936 0.04675548 0.09353242 0.10682186
## 36      2 1e-01 0.25 0.8825270 0.7649454 0.05295359 0.10584522 0.11747301
## 37      2 1e-01 0.50 0.8773279 0.7544358 0.06293210 0.12600020 0.12267206
## 38      2 1e-01 1.00 0.8491161 0.6980349 0.05535240 0.11061097 0.15088394
## 39      2 1e-01 2.00 0.8464204 0.6926742 0.06902287 0.13800470 0.15357962
## 40      2 1e-01 4.00 0.8412247 0.6823533 0.07526486 0.15037445 0.15877530
## 41      2 1e+00 0.25 0.6355567 0.2716058 0.07389893 0.14378121 0.36444332
## 42      2 1e+00 0.50 0.6381208 0.2769247 0.07455173 0.14461832 0.36187922
## 43      2 1e+00 1.00 0.6381208 0.2769247 0.07455173 0.14461832 0.36187922
## 44      2 1e+00 2.00 0.6381208 0.2769247 0.07455173 0.14461832 0.36187922
## 45      2 1e+00 4.00 0.6381208 0.2769247 0.07455173 0.14461832 0.36187922
## 46      2 1e+01 0.25 0.6068117 0.2139797 0.07841051 0.15311610 0.39318826
## 47      2 1e+01 0.50 0.6068117 0.2139797 0.07841051 0.15311610 0.39318826
## 48      2 1e+01 1.00 0.6068117 0.2139797 0.07841051 0.15311610 0.39318826
## 49      2 1e+01 2.00 0.6068117 0.2139797 0.07841051 0.15311610 0.39318826
## 50      2 1e+01 4.00 0.6068117 0.2139797 0.07841051 0.15311610 0.39318826
## 51      3 1e-03 0.25 0.8830499 0.7659706 0.05098544 0.10196529 0.11695007
## 52      3 1e-03 0.50 0.8906781 0.7811936 0.05517254 0.11035507 0.10932186
## 53      3 1e-03 1.00 0.8957422 0.7913728 0.05053572 0.10106181 0.10425776
## 54      3 1e-03 2.00 0.9008738 0.8016359 0.04618669 0.09237663 0.09912618
## 55      3 1e-03 4.00 0.8983738 0.7966359 0.05115889 0.10231464 0.10162618
## 56      3 1e-02 0.25 0.9008097 0.8014976 0.04130407 0.08259150 0.09919028
## 57      3 1e-02 0.50 0.9008097 0.8014976 0.04295247 0.08588895 0.09919028
## 58      3 1e-02 1.00 0.9007422 0.8013728 0.04791255 0.09582788 0.09925776
## 59      3 1e-02 2.00 0.9007422 0.8013728 0.04492031 0.08984360 0.09925776
## 60      3 1e-02 4.00 0.8853543 0.7705020 0.05492544 0.11009125 0.11464575
## 61      3 1e-01 0.25 0.6462854 0.2929271 0.10026002 0.19747785 0.35371457
## 62      3 1e-01 0.50 0.6357591 0.2718744 0.07722661 0.15053797 0.36424089
## 63      3 1e-01 1.00 0.6357591 0.2718744 0.07722661 0.15053797 0.36424089
## 64      3 1e-01 2.00 0.6383907 0.2771376 0.08232997 0.16098016 0.36160931
## 65      3 1e-01 4.00 0.6383907 0.2771376 0.08232997 0.16098016 0.36160931
## 66      3 1e+00 0.25 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
## 67      3 1e+00 0.50 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
## 68      3 1e+00 1.00 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
## 69      3 1e+00 2.00 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
## 70      3 1e+00 4.00 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
## 71      3 1e+01 0.25 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
## 72      3 1e+01 0.50 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
## 73      3 1e+01 1.00 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
## 74      3 1e+01 2.00 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
## 75      3 1e+01 4.00 0.6278644 0.2560850 0.06597084 0.12738520 0.37213563
  1. Make some plots to back up your assertions in (b) and (c).

ANSWER: See below.

Question 8

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

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

ANSWER: See below.

OJ <- ISLR2::OJ

n <- nrow(OJ)

train_index <- sample(1:n, 800)

train_set <- OJ[train_index, ]
test_set  <- OJ[-train_index, ]
  1. 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.

ANSWER: Because margin violations are “inexpensive,” the model used 431 support vectors (over half the training data) to define the decision boundary. The support vectors are nearly evenly split between the two classes, so both classes contribute similarly to defining the boundary.

## 
## Call:
## svm(formula = Purchase ~ ., data = train_set, kernel = "linear", 
##     cost = 0.01)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  linear 
##        cost:  0.01 
## 
## Number of Support Vectors:  431
## 
##  ( 215 216 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
  1. What are the training and test error rates?

ANSWER: The training error rate is 16.1%, while the test error rate is 17.7%.

## Accuracy 
##  0.16125
##  Accuracy 
## 0.1777778
  1. Use the tune() function to select an optimal cost. Consider values in the range 0.01 to 10.

ANSWER: The optimal cost is .1 based on it being the lowest error rate.

## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##   0.1
## 
## - best performance: 0.16875 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1  0.01 0.17250 0.05916080
## 2  0.10 0.16875 0.05245699
## 3  1.00 0.17125 0.05714565
## 4 10.00 0.17625 0.05935124
  1. Compute the training and test error rates using this new value for cost.

ANSWER: With this new cost, the training error went down to 15.8% while the test error reduced (slightly) to 17.4%.

## Accuracy 
##  0.15875
##  Accuracy 
## 0.1740741
  1. Repeat parts (b) through (e) using a support vector machine with a radial kernel. Use the default value for gamma.

ANSWER: For the initial cost of .01, the training error is 39.12% and teh test error is 38.5%. When comparing different C’s, it looks like C = 1 gives the best accuracy. Adjusting the model accordingly results in a train error of 16% and a test error of 16.6%.

## 
## Call:
## svm(formula = Purchase ~ ., data = train_set, kernel = "radial", 
##     cost = 0.01)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  0.01 
## 
## Number of Support Vectors:  630
## 
##  ( 313 317 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
## Accuracy 
##  0.39125
##  Accuracy 
## 0.3851852
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##     1
## 
## - best performance: 0.16625 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1  0.01 0.39125 0.05834821
## 2  0.10 0.17875 0.04752558
## 3  1.00 0.16625 0.03729108
## 4 10.00 0.17875 0.04896498
## Accuracy 
##     0.16
##  Accuracy 
## 0.1666667
  1. Repeat parts (b) through (e) using a support vector machine with a polynomial kernel. Set degree = 2.

ANSWER: With C = .01, the initial training error rate is 37.7% while the test error rate is 38.14%. Using the same comparison methodology, we see that C = 10 is optimal. Adjusting the model accordingly, the training error drops to 14.8% and the test error drops to 19.2%.

## 
## Call:
## svm(formula = Purchase ~ ., data = train_set, kernel = "polynomial", 
##     degree = 2, cost = 0.01)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  polynomial 
##        cost:  0.01 
##      degree:  2 
##      coef.0:  0 
## 
## Number of Support Vectors:  633
## 
##  ( 313 320 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
## Accuracy 
##   0.3775
##  Accuracy 
## 0.3814815
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##    10
## 
## - best performance: 0.17375 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1  0.01 0.38875 0.05510407
## 2  0.10 0.32625 0.04730589
## 3  1.00 0.20000 0.05335937
## 4 10.00 0.17375 0.04267529
## 5 20.00 0.17375 0.04693746
## 
## Call:
## svm(formula = Purchase ~ ., data = train_set, kernel = "polynomial", 
##     degree = 2, cost = 10)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  polynomial 
##        cost:  10 
##      degree:  2 
##      coef.0:  0 
## 
## Number of Support Vectors:  338
## 
##  ( 167 171 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
## Accuracy 
##  0.14875
##  Accuracy 
## 0.1925926
  1. Overall, which approach seems to give the best results on this data?

ANSWER: Overall, the best performing model appears to be the polynomial SVM with degree = 2 and C = 10.