library(e1071)
## Warning: package 'e1071' was built under R version 4.0.3
library(caret)
## Warning: package 'caret' was built under R version 4.0.3
## Loading required package: lattice
## Loading required package: ggplot2
library(tidyverse)
## -- Attaching packages ------------------------------ tidyverse 1.3.0 --
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## v purrr   0.3.4
## -- Conflicts --------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## x purrr::lift()   masks caret::lift()
library(ISLR)
## Warning: package 'ISLR' was built under R version 4.0.3

Question 5

set.seed(421)
x1 = runif(500) - 0.5
x2 = runif(500) - 0.5
y = 1 * (x1^2 - x2^2 > 0)
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)

glm.fit = glm(y ~ x1 + x2, family = binomial)
summary(glm.fit)
## 
## Call:
## glm(formula = y ~ x1 + x2, family = binomial)
## 
## Deviance Residuals: 
##    Min      1Q  Median      3Q     Max  
## -1.278  -1.227   1.089   1.135   1.175  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.11999    0.08971   1.338    0.181
## x1          -0.16881    0.30854  -0.547    0.584
## x2          -0.08198    0.31476  -0.260    0.795
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 691.35  on 499  degrees of freedom
## Residual deviance: 690.99  on 497  degrees of freedom
## AIC: 696.99
## 
## Number of Fisher Scoring iterations: 3

x1 and x2 are insignificant

data = data.frame(x1 = x1, x2 = x2, y = y)
lm.prob = predict(glm.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 = "blue", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "red", pch = 4)

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  
## -0.003575   0.000000   0.000000   0.000000   0.003720  
## 
## Coefficients:
##                Estimate Std. Error z value Pr(>|z|)
## (Intercept)      236.09   34920.61   0.007    0.995
## poly(x1, 2)1    3608.97  246381.97   0.015    0.988
## poly(x1, 2)2   88150.22 1333540.93   0.066    0.947
## poly(x2, 2)1    3256.75  177352.91   0.018    0.985
## poly(x2, 2)2  -87128.37 1164195.57  -0.075    0.940
## I(x1 * x2)       -33.23  446735.64   0.000    1.000
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 6.9135e+02  on 499  degrees of freedom
## Residual deviance: 3.3069e-05  on 494  degrees of freedom
## AIC: 12
## 
## Number of Fisher Scoring iterations: 25
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 = "blue", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "red", pch = 4)

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)

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 = "blue", xlab = "X1", ylab = "X2", pch = "+")
points(data.neg$x1, data.neg$x2, col = "red", pch = 4)

SMVs with non-linear kernel are very powerful in founding a non-linear boundary. Both logistic regressions did not do well.

Question 7

data(Auto)
Auto$mpg = ifelse(Auto$mpg > median(Auto$mpg), 1,0)
Auto$mpg = as.factor(Auto$mpg)
str(Auto)
## 'data.frame':    392 obs. of  9 variables:
##  $ mpg         : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
##  $ cylinders   : num  8 8 8 8 8 8 8 8 8 8 ...
##  $ displacement: num  307 350 318 304 302 429 454 440 455 390 ...
##  $ horsepower  : num  130 165 150 150 140 198 220 215 225 190 ...
##  $ weight      : num  3504 3693 3436 3433 3449 ...
##  $ acceleration: num  12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
##  $ year        : num  70 70 70 70 70 70 70 70 70 70 ...
##  $ origin      : num  1 1 1 1 1 1 1 1 1 1 ...
##  $ name        : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
set.seed(13)

formula.1 = mpg ~.


tuned.svm = tune.svm(formula.1, data = Auto, kernel = "linear", gamma = seq(.01, .1, by = .01), cost = seq(.1, 1, by =.1))
summary(tuned.svm)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  gamma cost
##   0.01  0.3
## 
## - best performance: 0.08442308 
## 
## - Detailed performance results:
##     gamma cost      error dispersion
## 1    0.01  0.1 0.10467949 0.05479263
## 2    0.02  0.1 0.10467949 0.05479263
## 3    0.03  0.1 0.10467949 0.05479263
## 4    0.04  0.1 0.10467949 0.05479263
## 5    0.05  0.1 0.10467949 0.05479263
## 6    0.06  0.1 0.10467949 0.05479263
## 7    0.07  0.1 0.10467949 0.05479263
## 8    0.08  0.1 0.10467949 0.05479263
## 9    0.09  0.1 0.10467949 0.05479263
## 10   0.10  0.1 0.10467949 0.05479263
## 11   0.01  0.2 0.09205128 0.05586632
## 12   0.02  0.2 0.09205128 0.05586632
## 13   0.03  0.2 0.09205128 0.05586632
## 14   0.04  0.2 0.09205128 0.05586632
## 15   0.05  0.2 0.09205128 0.05586632
## 16   0.06  0.2 0.09205128 0.05586632
## 17   0.07  0.2 0.09205128 0.05586632
## 18   0.08  0.2 0.09205128 0.05586632
## 19   0.09  0.2 0.09205128 0.05586632
## 20   0.10  0.2 0.09205128 0.05586632
## 21   0.01  0.3 0.08442308 0.05561966
## 22   0.02  0.3 0.08442308 0.05561966
## 23   0.03  0.3 0.08442308 0.05561966
## 24   0.04  0.3 0.08442308 0.05561966
## 25   0.05  0.3 0.08442308 0.05561966
## 26   0.06  0.3 0.08442308 0.05561966
## 27   0.07  0.3 0.08442308 0.05561966
## 28   0.08  0.3 0.08442308 0.05561966
## 29   0.09  0.3 0.08442308 0.05561966
## 30   0.10  0.3 0.08442308 0.05561966
## 31   0.01  0.4 0.08705128 0.05837113
## 32   0.02  0.4 0.08705128 0.05837113
## 33   0.03  0.4 0.08705128 0.05837113
## 34   0.04  0.4 0.08705128 0.05837113
## 35   0.05  0.4 0.08705128 0.05837113
## 36   0.06  0.4 0.08705128 0.05837113
## 37   0.07  0.4 0.08705128 0.05837113
## 38   0.08  0.4 0.08705128 0.05837113
## 39   0.09  0.4 0.08705128 0.05837113
## 40   0.10  0.4 0.08705128 0.05837113
## 41   0.01  0.5 0.08448718 0.06182920
## 42   0.02  0.5 0.08448718 0.06182920
## 43   0.03  0.5 0.08448718 0.06182920
## 44   0.04  0.5 0.08448718 0.06182920
## 45   0.05  0.5 0.08448718 0.06182920
## 46   0.06  0.5 0.08448718 0.06182920
## 47   0.07  0.5 0.08448718 0.06182920
## 48   0.08  0.5 0.08448718 0.06182920
## 49   0.09  0.5 0.08448718 0.06182920
## 50   0.10  0.5 0.08448718 0.06182920
## 51   0.01  0.6 0.08698718 0.05962270
## 52   0.02  0.6 0.08698718 0.05962270
## 53   0.03  0.6 0.08698718 0.05962270
## 54   0.04  0.6 0.08698718 0.05962270
## 55   0.05  0.6 0.08698718 0.05962270
## 56   0.06  0.6 0.08698718 0.05962270
## 57   0.07  0.6 0.08698718 0.05962270
## 58   0.08  0.6 0.08698718 0.05962270
## 59   0.09  0.6 0.08698718 0.05962270
## 60   0.10  0.6 0.08698718 0.05962270
## 61   0.01  0.7 0.08698718 0.05962270
## 62   0.02  0.7 0.08698718 0.05962270
## 63   0.03  0.7 0.08698718 0.05962270
## 64   0.04  0.7 0.08698718 0.05962270
## 65   0.05  0.7 0.08698718 0.05962270
## 66   0.06  0.7 0.08698718 0.05962270
## 67   0.07  0.7 0.08698718 0.05962270
## 68   0.08  0.7 0.08698718 0.05962270
## 69   0.09  0.7 0.08698718 0.05962270
## 70   0.10  0.7 0.08698718 0.05962270
## 71   0.01  0.8 0.08698718 0.05962270
## 72   0.02  0.8 0.08698718 0.05962270
## 73   0.03  0.8 0.08698718 0.05962270
## 74   0.04  0.8 0.08698718 0.05962270
## 75   0.05  0.8 0.08698718 0.05962270
## 76   0.06  0.8 0.08698718 0.05962270
## 77   0.07  0.8 0.08698718 0.05962270
## 78   0.08  0.8 0.08698718 0.05962270
## 79   0.09  0.8 0.08698718 0.05962270
## 80   0.10  0.8 0.08698718 0.05962270
## 81   0.01  0.9 0.09211538 0.05964107
## 82   0.02  0.9 0.09211538 0.05964107
## 83   0.03  0.9 0.09211538 0.05964107
## 84   0.04  0.9 0.09211538 0.05964107
## 85   0.05  0.9 0.09211538 0.05964107
## 86   0.06  0.9 0.09211538 0.05964107
## 87   0.07  0.9 0.09211538 0.05964107
## 88   0.08  0.9 0.09211538 0.05964107
## 89   0.09  0.9 0.09211538 0.05964107
## 90   0.10  0.9 0.09211538 0.05964107
## 91   0.01  1.0 0.09467949 0.06532038
## 92   0.02  1.0 0.09467949 0.06532038
## 93   0.03  1.0 0.09467949 0.06532038
## 94   0.04  1.0 0.09467949 0.06532038
## 95   0.05  1.0 0.09467949 0.06532038
## 96   0.06  1.0 0.09467949 0.06532038
## 97   0.07  1.0 0.09467949 0.06532038
## 98   0.08  1.0 0.09467949 0.06532038
## 99   0.09  1.0 0.09467949 0.06532038
## 100  0.10  1.0 0.09467949 0.06532038
tuned.svm$best.parameters
##    gamma cost
## 21  0.01  0.3

Using cross-validation the error is lowest with a gamma of 0.01 and cost of 0.6

set.seed(13)

formula.1 = mpg ~. 


tuned.svm.radial = tune.svm(formula.1, data = Auto, kernel = "radial", gamma = seq(.01, .1, by = .01), cost = seq(.1, 1, by =.1), degree = c(2,3,4))
summary(tuned.svm.radial)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  degree gamma cost
##       2  0.08  0.5
## 
## - best performance: 0.08429487 
## 
## - Detailed performance results:
##     degree gamma cost      error dispersion
## 1        2  0.01  0.1 0.11250000 0.06776250
## 2        3  0.01  0.1 0.11250000 0.06776250
## 3        4  0.01  0.1 0.11250000 0.06776250
## 4        2  0.02  0.1 0.10230769 0.06419025
## 5        3  0.02  0.1 0.10230769 0.06419025
## 6        4  0.02  0.1 0.10230769 0.06419025
## 7        2  0.03  0.1 0.09205128 0.06206093
## 8        3  0.03  0.1 0.09205128 0.06206093
## 9        4  0.03  0.1 0.09205128 0.06206093
## 10       2  0.04  0.1 0.08942308 0.05716381
## 11       3  0.04  0.1 0.08942308 0.05716381
## 12       4  0.04  0.1 0.08942308 0.05716381
## 13       2  0.05  0.1 0.08942308 0.05716381
## 14       3  0.05  0.1 0.08942308 0.05716381
## 15       4  0.05  0.1 0.08942308 0.05716381
## 16       2  0.06  0.1 0.08942308 0.05716381
## 17       3  0.06  0.1 0.08942308 0.05716381
## 18       4  0.06  0.1 0.08942308 0.05716381
## 19       2  0.07  0.1 0.08942308 0.05716381
## 20       3  0.07  0.1 0.08942308 0.05716381
## 21       4  0.07  0.1 0.08942308 0.05716381
## 22       2  0.08  0.1 0.09192308 0.05445256
## 23       3  0.08  0.1 0.09192308 0.05445256
## 24       4  0.08  0.1 0.09192308 0.05445256
## 25       2  0.09  0.1 0.09192308 0.05445256
## 26       3  0.09  0.1 0.09192308 0.05445256
## 27       4  0.09  0.1 0.09192308 0.05445256
## 28       2  0.10  0.1 0.09192308 0.05445256
## 29       3  0.10  0.1 0.09192308 0.05445256
## 30       4  0.10  0.1 0.09192308 0.05445256
## 31       2  0.01  0.2 0.10480769 0.06238822
## 32       3  0.01  0.2 0.10480769 0.06238822
## 33       4  0.01  0.2 0.10480769 0.06238822
## 34       2  0.02  0.2 0.08942308 0.06206539
## 35       3  0.02  0.2 0.08942308 0.06206539
## 36       4  0.02  0.2 0.08942308 0.06206539
## 37       2  0.03  0.2 0.08942308 0.05587127
## 38       3  0.03  0.2 0.08942308 0.05587127
## 39       4  0.03  0.2 0.08942308 0.05587127
## 40       2  0.04  0.2 0.08935897 0.05449950
## 41       3  0.04  0.2 0.08935897 0.05449950
## 42       4  0.04  0.2 0.08935897 0.05449950
## 43       2  0.05  0.2 0.08935897 0.05449950
## 44       3  0.05  0.2 0.08935897 0.05449950
## 45       4  0.05  0.2 0.08935897 0.05449950
## 46       2  0.06  0.2 0.08935897 0.05449950
## 47       3  0.06  0.2 0.08935897 0.05449950
## 48       4  0.06  0.2 0.08935897 0.05449950
## 49       2  0.07  0.2 0.09192308 0.05445256
## 50       3  0.07  0.2 0.09192308 0.05445256
## 51       4  0.07  0.2 0.09192308 0.05445256
## 52       2  0.08  0.2 0.09192308 0.05445256
## 53       3  0.08  0.2 0.09192308 0.05445256
## 54       4  0.08  0.2 0.09192308 0.05445256
## 55       2  0.09  0.2 0.08942308 0.05587127
## 56       3  0.09  0.2 0.08942308 0.05587127
## 57       4  0.09  0.2 0.08942308 0.05587127
## 58       2  0.10  0.2 0.08942308 0.05587127
## 59       3  0.10  0.2 0.08942308 0.05587127
## 60       4  0.10  0.2 0.08942308 0.05587127
## 61       2  0.01  0.3 0.09205128 0.06206093
## 62       3  0.01  0.3 0.09205128 0.06206093
## 63       4  0.01  0.3 0.09205128 0.06206093
## 64       2  0.02  0.3 0.08935897 0.05449950
## 65       3  0.02  0.3 0.08935897 0.05449950
## 66       4  0.02  0.3 0.08935897 0.05449950
## 67       2  0.03  0.3 0.08935897 0.05449950
## 68       3  0.03  0.3 0.08935897 0.05449950
## 69       4  0.03  0.3 0.08935897 0.05449950
## 70       2  0.04  0.3 0.08935897 0.05449950
## 71       3  0.04  0.3 0.08935897 0.05449950
## 72       4  0.04  0.3 0.08935897 0.05449950
## 73       2  0.05  0.3 0.08935897 0.05449950
## 74       3  0.05  0.3 0.08935897 0.05449950
## 75       4  0.05  0.3 0.08935897 0.05449950
## 76       2  0.06  0.3 0.08935897 0.05449950
## 77       3  0.06  0.3 0.08935897 0.05449950
## 78       4  0.06  0.3 0.08935897 0.05449950
## 79       2  0.07  0.3 0.08935897 0.05449950
## 80       3  0.07  0.3 0.08935897 0.05449950
## 81       4  0.07  0.3 0.08935897 0.05449950
## 82       2  0.08  0.3 0.08685897 0.05578949
## 83       3  0.08  0.3 0.08685897 0.05578949
## 84       4  0.08  0.3 0.08685897 0.05578949
## 85       2  0.09  0.3 0.08685897 0.05578949
## 86       3  0.09  0.3 0.08685897 0.05578949
## 87       4  0.09  0.3 0.08685897 0.05578949
## 88       2  0.10  0.3 0.08942308 0.05587127
## 89       3  0.10  0.3 0.08942308 0.05587127
## 90       4  0.10  0.3 0.08942308 0.05587127
## 91       2  0.01  0.4 0.09198718 0.06318810
## 92       3  0.01  0.4 0.09198718 0.06318810
## 93       4  0.01  0.4 0.09198718 0.06318810
## 94       2  0.02  0.4 0.08935897 0.05449950
## 95       3  0.02  0.4 0.08935897 0.05449950
## 96       4  0.02  0.4 0.08935897 0.05449950
## 97       2  0.03  0.4 0.08935897 0.05449950
## 98       3  0.03  0.4 0.08935897 0.05449950
## 99       4  0.03  0.4 0.08935897 0.05449950
## 100      2  0.04  0.4 0.08935897 0.05449950
## 101      3  0.04  0.4 0.08935897 0.05449950
## 102      4  0.04  0.4 0.08935897 0.05449950
## 103      2  0.05  0.4 0.08935897 0.05449950
## 104      3  0.05  0.4 0.08935897 0.05449950
## 105      4  0.05  0.4 0.08935897 0.05449950
## 106      2  0.06  0.4 0.08679487 0.05830100
## 107      3  0.06  0.4 0.08679487 0.05830100
## 108      4  0.06  0.4 0.08679487 0.05830100
## 109      2  0.07  0.4 0.08679487 0.05830100
## 110      3  0.07  0.4 0.08679487 0.05830100
## 111      4  0.07  0.4 0.08679487 0.05830100
## 112      2  0.08  0.4 0.08679487 0.05830100
## 113      3  0.08  0.4 0.08679487 0.05830100
## 114      4  0.08  0.4 0.08679487 0.05830100
## 115      2  0.09  0.4 0.08679487 0.05830100
## 116      3  0.09  0.4 0.08679487 0.05830100
## 117      4  0.09  0.4 0.08679487 0.05830100
## 118      2  0.10  0.4 0.08685897 0.05578949
## 119      3  0.10  0.4 0.08685897 0.05578949
## 120      4  0.10  0.4 0.08685897 0.05578949
## 121      2  0.01  0.5 0.08935897 0.05449950
## 122      3  0.01  0.5 0.08935897 0.05449950
## 123      4  0.01  0.5 0.08935897 0.05449950
## 124      2  0.02  0.5 0.08935897 0.05449950
## 125      3  0.02  0.5 0.08935897 0.05449950
## 126      4  0.02  0.5 0.08935897 0.05449950
## 127      2  0.03  0.5 0.08935897 0.05449950
## 128      3  0.03  0.5 0.08935897 0.05449950
## 129      4  0.03  0.5 0.08935897 0.05449950
## 130      2  0.04  0.5 0.08679487 0.05830100
## 131      3  0.04  0.5 0.08679487 0.05830100
## 132      4  0.04  0.5 0.08679487 0.05830100
## 133      2  0.05  0.5 0.08679487 0.05830100
## 134      3  0.05  0.5 0.08679487 0.05830100
## 135      4  0.05  0.5 0.08679487 0.05830100
## 136      2  0.06  0.5 0.08679487 0.05830100
## 137      3  0.06  0.5 0.08679487 0.05830100
## 138      4  0.06  0.5 0.08679487 0.05830100
## 139      2  0.07  0.5 0.08935897 0.05449950
## 140      3  0.07  0.5 0.08935897 0.05449950
## 141      4  0.07  0.5 0.08935897 0.05449950
## 142      2  0.08  0.5 0.08429487 0.05938883
## 143      3  0.08  0.5 0.08429487 0.05938883
## 144      4  0.08  0.5 0.08429487 0.05938883
## 145      2  0.09  0.5 0.08429487 0.05938883
## 146      3  0.09  0.5 0.08429487 0.05938883
## 147      4  0.09  0.5 0.08429487 0.05938883
## 148      2  0.10  0.5 0.08429487 0.05938883
## 149      3  0.10  0.5 0.08429487 0.05938883
## 150      4  0.10  0.5 0.08429487 0.05938883
## 151      2  0.01  0.6 0.08935897 0.05449950
## 152      3  0.01  0.6 0.08935897 0.05449950
## 153      4  0.01  0.6 0.08935897 0.05449950
## 154      2  0.02  0.6 0.08935897 0.05449950
## 155      3  0.02  0.6 0.08935897 0.05449950
## 156      4  0.02  0.6 0.08935897 0.05449950
## 157      2  0.03  0.6 0.08935897 0.05449950
## 158      3  0.03  0.6 0.08935897 0.05449950
## 159      4  0.03  0.6 0.08935897 0.05449950
## 160      2  0.04  0.6 0.08679487 0.05830100
## 161      3  0.04  0.6 0.08679487 0.05830100
## 162      4  0.04  0.6 0.08679487 0.05830100
## 163      2  0.05  0.6 0.08679487 0.05830100
## 164      3  0.05  0.6 0.08679487 0.05830100
## 165      4  0.05  0.6 0.08679487 0.05830100
## 166      2  0.06  0.6 0.08935897 0.05449950
## 167      3  0.06  0.6 0.08935897 0.05449950
## 168      4  0.06  0.6 0.08935897 0.05449950
## 169      2  0.07  0.6 0.08685897 0.05578949
## 170      3  0.07  0.6 0.08685897 0.05578949
## 171      4  0.07  0.6 0.08685897 0.05578949
## 172      2  0.08  0.6 0.08685897 0.05578949
## 173      3  0.08  0.6 0.08685897 0.05578949
## 174      4  0.08  0.6 0.08685897 0.05578949
## 175      2  0.09  0.6 0.08685897 0.05578949
## 176      3  0.09  0.6 0.08685897 0.05578949
## 177      4  0.09  0.6 0.08685897 0.05578949
## 178      2  0.10  0.6 0.08429487 0.05938883
## 179      3  0.10  0.6 0.08429487 0.05938883
## 180      4  0.10  0.6 0.08429487 0.05938883
## 181      2  0.01  0.7 0.08935897 0.05449950
## 182      3  0.01  0.7 0.08935897 0.05449950
## 183      4  0.01  0.7 0.08935897 0.05449950
## 184      2  0.02  0.7 0.08935897 0.05449950
## 185      3  0.02  0.7 0.08935897 0.05449950
## 186      4  0.02  0.7 0.08935897 0.05449950
## 187      2  0.03  0.7 0.08679487 0.05830100
## 188      3  0.03  0.7 0.08679487 0.05830100
## 189      4  0.03  0.7 0.08679487 0.05830100
## 190      2  0.04  0.7 0.08679487 0.05830100
## 191      3  0.04  0.7 0.08679487 0.05830100
## 192      4  0.04  0.7 0.08679487 0.05830100
## 193      2  0.05  0.7 0.08935897 0.05449950
## 194      3  0.05  0.7 0.08935897 0.05449950
## 195      4  0.05  0.7 0.08935897 0.05449950
## 196      2  0.06  0.7 0.08685897 0.05578949
## 197      3  0.06  0.7 0.08685897 0.05578949
## 198      4  0.06  0.7 0.08685897 0.05578949
## 199      2  0.07  0.7 0.08685897 0.05578949
## 200      3  0.07  0.7 0.08685897 0.05578949
## 201      4  0.07  0.7 0.08685897 0.05578949
## 202      2  0.08  0.7 0.08685897 0.05578949
## 203      3  0.08  0.7 0.08685897 0.05578949
## 204      4  0.08  0.7 0.08685897 0.05578949
## 205      2  0.09  0.7 0.08685897 0.05578949
## 206      3  0.09  0.7 0.08685897 0.05578949
## 207      4  0.09  0.7 0.08685897 0.05578949
## 208      2  0.10  0.7 0.08685897 0.05578949
## 209      3  0.10  0.7 0.08685897 0.05578949
## 210      4  0.10  0.7 0.08685897 0.05578949
## 211      2  0.01  0.8 0.08935897 0.05449950
## 212      3  0.01  0.8 0.08935897 0.05449950
## 213      4  0.01  0.8 0.08935897 0.05449950
## 214      2  0.02  0.8 0.08935897 0.05449950
## 215      3  0.02  0.8 0.08935897 0.05449950
## 216      4  0.02  0.8 0.08935897 0.05449950
## 217      2  0.03  0.8 0.08679487 0.05830100
## 218      3  0.03  0.8 0.08679487 0.05830100
## 219      4  0.03  0.8 0.08679487 0.05830100
## 220      2  0.04  0.8 0.08935897 0.05449950
## 221      3  0.04  0.8 0.08935897 0.05449950
## 222      4  0.04  0.8 0.08935897 0.05449950
## 223      2  0.05  0.8 0.08935897 0.05449950
## 224      3  0.05  0.8 0.08935897 0.05449950
## 225      4  0.05  0.8 0.08935897 0.05449950
## 226      2  0.06  0.8 0.08685897 0.05578949
## 227      3  0.06  0.8 0.08685897 0.05578949
## 228      4  0.06  0.8 0.08685897 0.05578949
## 229      2  0.07  0.8 0.08685897 0.05578949
## 230      3  0.07  0.8 0.08685897 0.05578949
## 231      4  0.07  0.8 0.08685897 0.05578949
## 232      2  0.08  0.8 0.08685897 0.05578949
## 233      3  0.08  0.8 0.08685897 0.05578949
## 234      4  0.08  0.8 0.08685897 0.05578949
## 235      2  0.09  0.8 0.08685897 0.05578949
## 236      3  0.09  0.8 0.08685897 0.05578949
## 237      4  0.09  0.8 0.08685897 0.05578949
## 238      2  0.10  0.8 0.08685897 0.05578949
## 239      3  0.10  0.8 0.08685897 0.05578949
## 240      4  0.10  0.8 0.08685897 0.05578949
## 241      2  0.01  0.9 0.08935897 0.05449950
## 242      3  0.01  0.9 0.08935897 0.05449950
## 243      4  0.01  0.9 0.08935897 0.05449950
## 244      2  0.02  0.9 0.08935897 0.05449950
## 245      3  0.02  0.9 0.08935897 0.05449950
## 246      4  0.02  0.9 0.08935897 0.05449950
## 247      2  0.03  0.9 0.08679487 0.05830100
## 248      3  0.03  0.9 0.08679487 0.05830100
## 249      4  0.03  0.9 0.08679487 0.05830100
## 250      2  0.04  0.9 0.08935897 0.05449950
## 251      3  0.04  0.9 0.08935897 0.05449950
## 252      4  0.04  0.9 0.08935897 0.05449950
## 253      2  0.05  0.9 0.08685897 0.05578949
## 254      3  0.05  0.9 0.08685897 0.05578949
## 255      4  0.05  0.9 0.08685897 0.05578949
## 256      2  0.06  0.9 0.08685897 0.05578949
## 257      3  0.06  0.9 0.08685897 0.05578949
## 258      4  0.06  0.9 0.08685897 0.05578949
## 259      2  0.07  0.9 0.08685897 0.05578949
## 260      3  0.07  0.9 0.08685897 0.05578949
## 261      4  0.07  0.9 0.08685897 0.05578949
## 262      2  0.08  0.9 0.08685897 0.05578949
## 263      3  0.08  0.9 0.08685897 0.05578949
## 264      4  0.08  0.9 0.08685897 0.05578949
## 265      2  0.09  0.9 0.08685897 0.05578949
## 266      3  0.09  0.9 0.08685897 0.05578949
## 267      4  0.09  0.9 0.08685897 0.05578949
## 268      2  0.10  0.9 0.08685897 0.05578949
## 269      3  0.10  0.9 0.08685897 0.05578949
## 270      4  0.10  0.9 0.08685897 0.05578949
## 271      2  0.01  1.0 0.08935897 0.05449950
## 272      3  0.01  1.0 0.08935897 0.05449950
## 273      4  0.01  1.0 0.08935897 0.05449950
## 274      2  0.02  1.0 0.08679487 0.05830100
## 275      3  0.02  1.0 0.08679487 0.05830100
## 276      4  0.02  1.0 0.08679487 0.05830100
## 277      2  0.03  1.0 0.08935897 0.05449950
## 278      3  0.03  1.0 0.08935897 0.05449950
## 279      4  0.03  1.0 0.08935897 0.05449950
## 280      2  0.04  1.0 0.08935897 0.05449950
## 281      3  0.04  1.0 0.08935897 0.05449950
## 282      4  0.04  1.0 0.08935897 0.05449950
## 283      2  0.05  1.0 0.09198718 0.05711587
## 284      3  0.05  1.0 0.09198718 0.05711587
## 285      4  0.05  1.0 0.09198718 0.05711587
## 286      2  0.06  1.0 0.09198718 0.05711587
## 287      3  0.06  1.0 0.09198718 0.05711587
## 288      4  0.06  1.0 0.09198718 0.05711587
## 289      2  0.07  1.0 0.08942308 0.05716381
## 290      3  0.07  1.0 0.08942308 0.05716381
## 291      4  0.07  1.0 0.08942308 0.05716381
## 292      2  0.08  1.0 0.08685897 0.05578949
## 293      3  0.08  1.0 0.08685897 0.05578949
## 294      4  0.08  1.0 0.08685897 0.05578949
## 295      2  0.09  1.0 0.08685897 0.05578949
## 296      3  0.09  1.0 0.08685897 0.05578949
## 297      4  0.09  1.0 0.08685897 0.05578949
## 298      2  0.10  1.0 0.08685897 0.05578949
## 299      3  0.10  1.0 0.08685897 0.05578949
## 300      4  0.10  1.0 0.08685897 0.05578949

Using cross validation, we get degree 2, gamma of 0.08, and cost of 0.5 for the radial SVM

set.seed(13)

formula.1 = mpg ~.



tuned.svm.poly = tune.svm(formula.1, data = Auto, kernel = "polynomial", gamma = seq(.01, .1, by = .01), cost = seq(.1, 1, by =.1), degree = c(2,3,4))
summary(tuned.svm.poly)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  degree gamma cost
##       3  0.09  0.8
## 
## - best performance: 0.08429487 
## 
## - Detailed performance results:
##     degree gamma cost      error dispersion
## 1        2  0.01  0.1 0.55621795 0.04719084
## 2        3  0.01  0.1 0.55621795 0.04719084
## 3        4  0.01  0.1 0.55621795 0.04719084
## 4        2  0.02  0.1 0.55621795 0.04719084
## 5        3  0.02  0.1 0.55621795 0.04719084
## 6        4  0.02  0.1 0.55621795 0.04719084
## 7        2  0.03  0.1 0.53833333 0.05502857
## 8        3  0.03  0.1 0.44878205 0.12058571
## 9        4  0.03  0.1 0.55621795 0.04719084
## 10       2  0.04  0.1 0.48474359 0.08298184
## 11       3  0.04  0.1 0.34160256 0.07961485
## 12       4  0.04  0.1 0.52044872 0.06198235
## 13       2  0.05  0.1 0.42858974 0.09936389
## 14       3  0.05  0.1 0.28576923 0.05095766
## 15       4  0.05  0.1 0.47698718 0.07687347
## 16       2  0.06  0.1 0.39538462 0.04487057
## 17       3  0.06  0.1 0.26288462 0.06063743
## 18       4  0.06  0.1 0.41820513 0.06010255
## 19       2  0.07  0.1 0.36224359 0.04740167
## 20       3  0.07  0.1 0.26038462 0.06722984
## 21       4  0.07  0.1 0.40044872 0.05298805
## 22       2  0.08  0.1 0.34955128 0.05495571
## 23       3  0.08  0.1 0.25782051 0.06895177
## 24       4  0.08  0.1 0.38256410 0.04209868
## 25       2  0.09  0.1 0.32660256 0.04317218
## 26       3  0.09  0.1 0.25782051 0.06895177
## 27       4  0.09  0.1 0.36217949 0.05109385
## 28       2  0.10  0.1 0.32153846 0.04710577
## 29       3  0.10  0.1 0.25788462 0.06604745
## 30       4  0.10  0.1 0.33929487 0.06210716
## 31       2  0.01  0.2 0.55621795 0.04719084
## 32       3  0.01  0.2 0.55621795 0.04719084
## 33       4  0.01  0.2 0.55621795 0.04719084
## 34       2  0.02  0.2 0.54602564 0.04803255
## 35       3  0.02  0.2 0.50756410 0.11042929
## 36       4  0.02  0.2 0.55621795 0.04719084
## 37       2  0.03  0.2 0.47705128 0.07538454
## 38       3  0.03  0.2 0.37230769 0.05540479
## 39       4  0.03  0.2 0.53320513 0.06677191
## 40       2  0.04  0.2 0.42608974 0.08395236
## 41       3  0.04  0.2 0.28326923 0.05474295
## 42       4  0.04  0.2 0.47955128 0.08044663
## 43       2  0.05  0.2 0.35967949 0.05262699
## 44       3  0.05  0.2 0.26032051 0.05780335
## 45       4  0.05  0.2 0.42076923 0.06027247
## 46       2  0.06  0.2 0.33429487 0.04289633
## 47       3  0.06  0.2 0.25782051 0.06895177
## 48       4  0.06  0.2 0.39282051 0.04781820
## 49       2  0.07  0.2 0.32153846 0.04710577
## 50       3  0.07  0.2 0.25782051 0.06895177
## 51       4  0.07  0.2 0.37237179 0.04730139
## 52       2  0.08  0.2 0.30115385 0.05734845
## 53       3  0.08  0.2 0.25788462 0.06604745
## 54       4  0.08  0.2 0.34442308 0.06011687
## 55       2  0.09  0.2 0.28846154 0.05708688
## 56       3  0.09  0.2 0.25532051 0.06876495
## 57       4  0.09  0.2 0.32653846 0.05579845
## 58       2  0.10  0.2 0.27320513 0.06555185
## 59       3  0.10  0.2 0.25275641 0.06705185
## 60       4  0.10  0.2 0.29602564 0.05815249
## 61       2  0.01  0.3 0.55621795 0.04719084
## 62       3  0.01  0.3 0.55621795 0.04719084
## 63       4  0.01  0.3 0.55621795 0.04719084
## 64       2  0.02  0.3 0.50000000 0.08547009
## 65       3  0.02  0.3 0.47185897 0.07297842
## 66       4  0.02  0.3 0.55621795 0.04719084
## 67       2  0.03  0.3 0.42858974 0.08510778
## 68       3  0.03  0.3 0.31121795 0.06089650
## 69       4  0.03  0.3 0.52294872 0.06268650
## 70       2  0.04  0.3 0.36724359 0.04858887
## 71       3  0.04  0.3 0.27051282 0.05841116
## 72       4  0.04  0.3 0.43858974 0.07472757
## 73       2  0.05  0.3 0.33429487 0.04289633
## 74       3  0.05  0.3 0.26038462 0.06722984
## 75       4  0.05  0.3 0.40807692 0.05142004
## 76       2  0.06  0.3 0.31134615 0.05221905
## 77       3  0.06  0.3 0.25782051 0.06895177
## 78       4  0.06  0.3 0.38256410 0.04209868
## 79       2  0.07  0.3 0.29352564 0.05698398
## 80       3  0.07  0.3 0.25788462 0.06604745
## 81       4  0.07  0.3 0.35711538 0.05687170
## 82       2  0.08  0.3 0.27320513 0.06555185
## 83       3  0.08  0.3 0.25532051 0.06876495
## 84       4  0.08  0.3 0.32910256 0.05924640
## 85       2  0.09  0.3 0.27320513 0.06665694
## 86       3  0.09  0.3 0.24769231 0.06897653
## 87       4  0.09  0.3 0.29602564 0.05815249
## 88       2  0.10  0.3 0.27064103 0.06892528
## 89       3  0.10  0.3 0.16589744 0.10826095
## 90       4  0.10  0.3 0.28083333 0.05598001
## 91       2  0.01  0.4 0.55621795 0.04719084
## 92       3  0.01  0.4 0.55621795 0.04719084
## 93       4  0.01  0.4 0.55621795 0.04719084
## 94       2  0.02  0.4 0.48474359 0.08298184
## 95       3  0.02  0.4 0.40788462 0.08695087
## 96       4  0.02  0.4 0.55621795 0.04719084
## 97       2  0.03  0.4 0.39538462 0.04487057
## 98       3  0.03  0.4 0.28576923 0.05095766
## 99       4  0.03  0.4 0.51538462 0.06067774
## 100      2  0.04  0.4 0.34955128 0.05495571
## 101      3  0.04  0.4 0.26032051 0.05780335
## 102      4  0.04  0.4 0.42846154 0.06359859
## 103      2  0.05  0.4 0.32153846 0.04710577
## 104      3  0.05  0.4 0.25782051 0.06895177
## 105      4  0.05  0.4 0.39794872 0.04955865
## 106      2  0.06  0.4 0.29865385 0.06059375
## 107      3  0.06  0.4 0.25788462 0.06604745
## 108      4  0.06  0.4 0.37237179 0.04730139
## 109      2  0.07  0.4 0.27320513 0.06555185
## 110      3  0.07  0.4 0.25788462 0.06604745
## 111      4  0.07  0.4 0.33929487 0.06210716
## 112      2  0.08  0.4 0.27320513 0.07090528
## 113      3  0.08  0.4 0.25275641 0.06705185
## 114      4  0.08  0.4 0.30365385 0.05938945
## 115      2  0.09  0.4 0.27583333 0.06675978
## 116      3  0.09  0.4 0.16846154 0.10688922
## 117      4  0.09  0.4 0.28583333 0.05256797
## 118      2  0.10  0.4 0.28352564 0.07478758
## 119      3  0.10  0.4 0.13544872 0.11029287
## 120      4  0.10  0.4 0.27314103 0.05715199
## 121      2  0.01  0.5 0.55621795 0.04719084
## 122      3  0.01  0.5 0.55621795 0.04719084
## 123      4  0.01  0.5 0.55621795 0.04719084
## 124      2  0.02  0.5 0.45666667 0.07956021
## 125      3  0.02  0.5 0.39006410 0.08033009
## 126      4  0.02  0.5 0.55621795 0.04719084
## 127      2  0.03  0.5 0.37237179 0.04882135
## 128      3  0.03  0.5 0.28326923 0.05474295
## 129      4  0.03  0.5 0.48461538 0.08564086
## 130      2  0.04  0.5 0.32660256 0.04317218
## 131      3  0.04  0.5 0.25782051 0.06227142
## 132      4  0.04  0.5 0.41820513 0.06010255
## 133      2  0.05  0.5 0.30115385 0.05734845
## 134      3  0.05  0.5 0.25782051 0.06895177
## 135      4  0.05  0.5 0.38769231 0.04373280
## 136      2  0.06  0.5 0.27320513 0.06555185
## 137      3  0.06  0.5 0.25788462 0.06604745
## 138      4  0.06  0.5 0.36217949 0.05109385
## 139      2  0.07  0.5 0.27320513 0.06665694
## 140      3  0.07  0.5 0.25532051 0.06876495
## 141      4  0.07  0.5 0.32910256 0.05924640
## 142      2  0.08  0.5 0.27070513 0.06488979
## 143      3  0.08  0.5 0.20679487 0.08431874
## 144      4  0.08  0.5 0.29096154 0.05723182
## 145      2  0.09  0.5 0.28352564 0.07478758
## 146      3  0.09  0.5 0.14307692 0.10570589
## 147      4  0.09  0.5 0.28083333 0.05465948
## 148      2  0.10  0.5 0.28102564 0.07454491
## 149      3  0.10  0.5 0.09461538 0.07946604
## 150      4  0.10  0.5 0.26544872 0.05318964
## 151      2  0.01  0.6 0.55621795 0.04719084
## 152      3  0.01  0.6 0.55621795 0.04719084
## 153      4  0.01  0.6 0.55621795 0.04719084
## 154      2  0.02  0.6 0.43621795 0.08893985
## 155      3  0.02  0.6 0.37993590 0.06526583
## 156      4  0.02  0.6 0.55621795 0.04719084
## 157      2  0.03  0.6 0.35724359 0.06022310
## 158      3  0.03  0.6 0.27307692 0.05572296
## 159      4  0.03  0.6 0.48461538 0.07944581
## 160      2  0.04  0.6 0.31897436 0.04551252
## 161      3  0.04  0.6 0.26038462 0.06722984
## 162      4  0.04  0.6 0.41314103 0.05734403
## 163      2  0.05  0.6 0.28846154 0.05446748
## 164      3  0.05  0.6 0.25782051 0.06895177
## 165      4  0.05  0.6 0.38256410 0.04209868
## 166      2  0.06  0.6 0.27320513 0.06555185
## 167      3  0.06  0.6 0.25788462 0.06604745
## 168      4  0.06  0.6 0.34698718 0.06252040
## 169      2  0.07  0.6 0.27064103 0.06892528
## 170      3  0.07  0.6 0.25275641 0.06705185
## 171      4  0.07  0.6 0.32141026 0.05615082
## 172      2  0.08  0.6 0.28096154 0.07129459
## 173      3  0.08  0.6 0.16339744 0.11022264
## 174      4  0.08  0.6 0.28583333 0.05256797
## 175      2  0.09  0.6 0.28102564 0.07454491
## 176      3  0.09  0.6 0.11500000 0.09318209
## 177      4  0.09  0.6 0.27314103 0.05715199
## 178      2  0.10  0.6 0.28352564 0.07380433
## 179      3  0.10  0.6 0.08942308 0.05319204
## 180      4  0.10  0.6 0.26038462 0.05530664
## 181      2  0.01  0.7 0.55621795 0.04719084
## 182      3  0.01  0.7 0.55621795 0.04719084
## 183      4  0.01  0.7 0.55621795 0.04719084
## 184      2  0.02  0.7 0.43371795 0.09815822
## 185      3  0.02  0.7 0.36974359 0.05794405
## 186      4  0.02  0.7 0.55115385 0.04522834
## 187      2  0.03  0.7 0.35467949 0.05822801
## 188      3  0.03  0.7 0.27051282 0.05841116
## 189      4  0.03  0.7 0.47698718 0.07875110
## 190      2  0.04  0.7 0.31134615 0.05221905
## 191      3  0.04  0.7 0.25782051 0.06895177
## 192      4  0.04  0.7 0.40807692 0.05142004
## 193      2  0.05  0.7 0.27833333 0.06498102
## 194      3  0.05  0.7 0.25788462 0.06604745
## 195      4  0.05  0.7 0.37750000 0.04692149
## 196      2  0.06  0.7 0.27064103 0.06566876
## 197      3  0.06  0.7 0.25532051 0.06876495
## 198      4  0.06  0.7 0.33929487 0.06210716
## 199      2  0.07  0.7 0.27839744 0.06858678
## 200      3  0.07  0.7 0.22980769 0.07690364
## 201      4  0.07  0.7 0.30115385 0.05650875
## 202      2  0.08  0.7 0.28352564 0.07478758
## 203      3  0.08  0.7 0.14814103 0.10605975
## 204      4  0.08  0.7 0.28333333 0.05746950
## 205      2  0.09  0.7 0.28352564 0.07380433
## 206      3  0.09  0.7 0.08948718 0.06985788
## 207      4  0.09  0.7 0.26544872 0.05318964
## 208      2  0.10  0.7 0.28352564 0.07179745
## 209      3  0.10  0.7 0.08685897 0.04865723
## 210      4  0.10  0.7 0.26038462 0.05530664
## 211      2  0.01  0.8 0.54602564 0.04803255
## 212      3  0.01  0.8 0.55621795 0.04719084
## 213      4  0.01  0.8 0.55621795 0.04719084
## 214      2  0.02  0.8 0.42608974 0.08395236
## 215      3  0.02  0.8 0.34160256 0.07961485
## 216      4  0.02  0.8 0.53064103 0.08176689
## 217      2  0.03  0.8 0.33429487 0.04289633
## 218      3  0.03  0.8 0.26288462 0.06063743
## 219      4  0.03  0.8 0.47698718 0.07687347
## 220      2  0.04  0.8 0.30115385 0.05734845
## 221      3  0.04  0.8 0.25782051 0.06895177
## 222      4  0.04  0.8 0.40551282 0.04752936
## 223      2  0.05  0.8 0.27320513 0.06555185
## 224      3  0.05  0.8 0.25788462 0.06604745
## 225      4  0.05  0.8 0.37237179 0.04730139
## 226      2  0.06  0.8 0.27064103 0.06892528
## 227      3  0.06  0.8 0.25532051 0.06876495
## 228      4  0.06  0.8 0.33673077 0.06173258
## 229      2  0.07  0.8 0.28096154 0.07129459
## 230      3  0.07  0.8 0.18128205 0.10011353
## 231      4  0.07  0.8 0.29602564 0.05815249
## 232      2  0.08  0.8 0.28102564 0.07454491
## 233      3  0.08  0.8 0.13288462 0.10397360
## 234      4  0.08  0.8 0.28083333 0.05465948
## 235      2  0.09  0.8 0.28096154 0.07331523
## 236      3  0.09  0.8 0.08429487 0.06524988
## 237      4  0.09  0.8 0.26288462 0.05008070
## 238      2  0.10  0.8 0.27583333 0.07303071
## 239      3  0.10  0.8 0.09455128 0.04997191
## 240      4  0.10  0.8 0.26038462 0.05530664
## 241      2  0.01  0.9 0.53833333 0.05502857
## 242      3  0.01  0.9 0.55621795 0.04719084
## 243      4  0.01  0.9 0.55621795 0.04719084
## 244      2  0.02  0.9 0.39538462 0.04487057
## 245      3  0.02  0.9 0.32647436 0.06763221
## 246      4  0.02  0.9 0.53320513 0.07501540
## 247      2  0.03  0.9 0.32660256 0.04317218
## 248      3  0.03  0.9 0.26288462 0.06063743
## 249      4  0.03  0.9 0.43852564 0.08174380
## 250      2  0.04  0.9 0.29865385 0.06059375
## 251      3  0.04  0.9 0.25782051 0.06895177
## 252      4  0.04  0.9 0.40044872 0.05298805
## 253      2  0.05  0.9 0.27064103 0.06677191
## 254      3  0.05  0.9 0.25788462 0.06604745
## 255      4  0.05  0.9 0.37237179 0.04730139
## 256      2  0.06  0.9 0.27583333 0.06675978
## 257      3  0.06  0.9 0.25275641 0.06705185
## 258      4  0.06  0.9 0.33166667 0.05999991
## 259      2  0.07  0.9 0.28352564 0.07478758
## 260      3  0.07  0.9 0.16339744 0.11022264
## 261      4  0.07  0.9 0.29096154 0.05723182
## 262      2  0.08  0.9 0.28352564 0.07380433
## 263      3  0.08  0.9 0.10474359 0.08423911
## 264      4  0.08  0.9 0.27570513 0.05816242
## 265      2  0.09  0.9 0.28096154 0.07528166
## 266      3  0.09  0.9 0.08942308 0.05454811
## 267      4  0.09  0.9 0.26038462 0.05530664
## 268      2  0.10  0.9 0.27839744 0.07470450
## 269      3  0.10  0.9 0.10211538 0.04201319
## 270      4  0.10  0.9 0.26551282 0.05724087
## 271      2  0.01  1.0 0.52551282 0.08712175
## 272      3  0.01  1.0 0.55621795 0.04719084
## 273      4  0.01  1.0 0.55621795 0.04719084
## 274      2  0.02  1.0 0.38000000 0.05337674
## 275      3  0.02  1.0 0.31121795 0.06089650
## 276      4  0.02  1.0 0.53320513 0.06677191
## 277      2  0.03  1.0 0.32153846 0.04710577
## 278      3  0.03  1.0 0.26032051 0.05780335
## 279      4  0.03  1.0 0.43602564 0.07330137
## 280      2  0.04  1.0 0.29102564 0.05860235
## 281      3  0.04  1.0 0.25782051 0.06895177
## 282      4  0.04  1.0 0.39538462 0.04801639
## 283      2  0.05  1.0 0.27064103 0.06566876
## 284      3  0.05  1.0 0.25788462 0.06604745
## 285      4  0.05  1.0 0.36474359 0.05295439
## 286      2  0.06  1.0 0.28096154 0.07129459
## 287      3  0.06  1.0 0.25025641 0.06659486
## 288      4  0.06  1.0 0.32653846 0.05579845
## 289      2  0.07  1.0 0.28102564 0.07454491
## 290      3  0.07  1.0 0.15070513 0.10788480
## 291      4  0.07  1.0 0.28583333 0.05256797
## 292      2  0.08  1.0 0.27839744 0.06964373
## 293      3  0.08  1.0 0.08948718 0.06985788
## 294      4  0.08  1.0 0.27057692 0.05728285
## 295      2  0.09  1.0 0.27839744 0.07372015
## 296      3  0.09  1.0 0.09198718 0.05300459
## 297      4  0.09  1.0 0.26038462 0.05530664
## 298      2  0.10  1.0 0.27326923 0.07520642
## 299      3  0.10  1.0 0.10211538 0.04371740
## 300      4  0.10  1.0 0.26807692 0.05736199

Using cross-validation, we get degree 3, gamma 0.09, and cost 0.8 for the polynomial SVM

svm.linear = svm(formula.1, data = Auto, kernel = "linear", cost = 0.6, gamma = 0.01)

svm.radial = svm(formula.1, data = Auto, kernel = "radial", cost =0.5, gamma = 0.08, degree = 2 )

svm.poly = svm(formula.1, data = Auto, kernel = "polynomial", cost = 0.8, gamma = 0.09, degree = 3)
plot(svm.linear, Auto, weight ~ horsepower)

plot(svm.radial, Auto, weight ~ horsepower)

plot(svm.poly, Auto, weight ~ horsepower)

Question 8

data("OJ")
set.seed(13)

train = sample(nrow(OJ), 800)
oj.train = OJ[train,]
oj.test = OJ[-train,]

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.

svm.linear.oj = svm(Purchase ~., data = oj.train, kernel = "linear", cost = 0.01)
summary(svm.linear.oj)
## 
## 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:  439
## 
##  ( 219 220 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM

The SVM creates 432 support vectors. 219 belong to CH and 220 belong to MM.

Training error rate

preds.train = predict(svm.linear.oj, oj.train)
table(oj.train$Purchase, preds.train)
##     preds.train
##       CH  MM
##   CH 441  57
##   MM  78 224
(78 + 57) / (441 + 57 + 78 +224)
## [1] 0.16875

Test error rate

preds.test = predict(svm.linear.oj, oj.test)
table(oj.test$Purchase, preds.test)
##     preds.test
##       CH  MM
##   CH 139  16
##   MM  25  90
(25 + 16)/(139 + 16 + 25 +90)
## [1] 0.1518519

The training rate is 16.89% and the test error rate is 15.19%

set.seed(13)

tuned.linear.svm.oj = tune.svm(Purchase ~., data = oj.train, kernel = "linear", cost = seq(.1, 10, by =.5))
summary(tuned.linear.svm.oj)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##   2.1
## 
## - best performance: 0.16875 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1   0.1 0.17000 0.05309844
## 2   0.6 0.17500 0.04602234
## 3   1.1 0.17500 0.04602234
## 4   1.6 0.17000 0.04647281
## 5   2.1 0.16875 0.04759858
## 6   2.6 0.17250 0.04518481
## 7   3.1 0.17375 0.04543387
## 8   3.6 0.17000 0.04721405
## 9   4.1 0.17375 0.04839436
## 10  4.6 0.17625 0.04980866
## 11  5.1 0.17250 0.05163978
## 12  5.6 0.17250 0.05163978
## 13  6.1 0.17250 0.05163978
## 14  6.6 0.17250 0.05163978
## 15  7.1 0.17250 0.04923018
## 16  7.6 0.17125 0.04966904
## 17  8.1 0.17250 0.04887626
## 18  8.6 0.17250 0.04887626
## 19  9.1 0.17375 0.05084358
## 20  9.6 0.17250 0.04887626

The optimal cost is 2.1

svm.linear.oj.cv = svm(Purchase ~., kernel = "linear", data = oj.train, cost = 2.1)

training error

preds.train.2 = predict(svm.linear.oj.cv, oj.train)
table(oj.train$Purchase, preds.train.2)
##     preds.train.2
##       CH  MM
##   CH 441  57
##   MM  69 233
(69 + 57) / (441 +57 +69 + 233)
## [1] 0.1575

testing error

preds.test.2 = predict(svm.linear.oj.cv, oj.test)
table(oj.test$Purchase, preds.test.2)
##     preds.test.2
##       CH  MM
##   CH 134  21
##   MM  24  91
(24 + 21)/(134 + 21 + 24 + 91)
## [1] 0.1666667

The training error rate is 15.75% and the testing error is 16.67% using the optimal cost.

svm.radial.oj = svm(Purchase ~., kernel = "radial", data = oj.train)
summary(svm.radial.oj)
## 
## Call:
## svm(formula = Purchase ~ ., data = oj.train, kernel = "radial")
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  1 
## 
## Number of Support Vectors:  379
## 
##  ( 191 188 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM

379 support vectors were created. 191 belong to CH and 188 belong to MM

preds.train = predict(svm.radial.oj, oj.train)
table(oj.train$Purchase, preds.train)
##     preds.train
##       CH  MM
##   CH 453  45
##   MM  84 218
(84 + 45)/(453 + 45 + 84 + 218)
## [1] 0.16125
preds.test = predict(svm.radial.oj, oj.test)
table(oj.test$Purchase, preds.test)
##     preds.test
##       CH  MM
##   CH 141  14
##   MM  25  90
(25 + 14)/(141 + 14 + 25 +90)
## [1] 0.1444444

The test error is 16.13% and the training is 14.44%.

set.seed(13)

tuned.radial.svm.oj = tune.svm(Purchase ~., data = oj.train, kernel = "radial", cost = seq(.1, 10, by =.5))
summary(tuned.radial.svm.oj)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##   2.6
## 
## - best performance: 0.18 
## 
## - Detailed performance results:
##    cost   error dispersion
## 1   0.1 0.18625 0.04767147
## 2   0.6 0.18250 0.04758034
## 3   1.1 0.18625 0.04767147
## 4   1.6 0.18375 0.04604120
## 5   2.1 0.18125 0.04903584
## 6   2.6 0.18000 0.05075814
## 7   3.1 0.18125 0.04832256
## 8   3.6 0.18125 0.04497299
## 9   4.1 0.18625 0.05350558
## 10  4.6 0.18625 0.05185785
## 11  5.1 0.18375 0.05402224
## 12  5.6 0.18375 0.05402224
## 13  6.1 0.18500 0.05361903
## 14  6.6 0.18750 0.05137012
## 15  7.1 0.18875 0.04980866
## 16  7.6 0.18875 0.04980866
## 17  8.1 0.18750 0.04823265
## 18  8.6 0.18750 0.04639804
## 19  9.1 0.18625 0.04505013
## 20  9.6 0.18750 0.04409586
svm.radial.cv.oj = svm(Purchase ~., kernal = "radial", cost = 2.6, data = oj.train)

summary(svm.radial.cv.oj)
## 
## Call:
## svm(formula = Purchase ~ ., data = oj.train, kernal = "radial", cost = 2.6)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  2.6 
## 
## Number of Support Vectors:  351
## 
##  ( 174 177 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM

351 vectors were made. 174 belong to CH and 177 belong to MM.

preds.train.2 = predict(svm.radial.cv.oj, oj.train)
table(oj.train$Purchase, preds.train.2)
##     preds.train.2
##       CH  MM
##   CH 455  43
##   MM  81 221
(69 + 57)/(441 + 57 + 69 +233)
## [1] 0.1575
preds.test.2 = predict(svm.radial.cv.oj, oj.test)
table(oj.test$Purchase, preds.test.2)
##     preds.test.2
##       CH  MM
##   CH 140  15
##   MM  25  90
(25 + 15)/ (140 + 15 + 12 + 90)
## [1] 0.155642

The training error rate is 15.75% and the testing error rate is 15.56%.

svm.poly = svm(Purchase ~., kernel = "polynomial", data = oj.train, degree = 2)
summary(svm.poly)
## 
## Call:
## svm(formula = Purchase ~ ., data = oj.train, kernel = "polynomial", 
##     degree = 2)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  polynomial 
##        cost:  1 
##      degree:  2 
##      coef.0:  0 
## 
## Number of Support Vectors:  446
## 
##  ( 219 227 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM

446 vectors are created. 219 belong to CC and 227 belong to MM.

training error rate

preds.train = predict(svm.poly, oj.train)
table(oj.train$Purchase, preds.train)
##     preds.train
##       CH  MM
##   CH 461  37
##   MM 116 186
(84 + 45)/(453 + 45 + 84 +218)
## [1] 0.16125

testing error rate

preds.test = predict(svm.poly, oj.test)
table(oj.test$Purchase, preds.test)
##     preds.test
##       CH  MM
##   CH 144  11
##   MM  38  77
(38 + 11)/(144 + 11 + 38 +77)
## [1] 0.1814815

The training error rate is 16.13% and the testing error rate is 18.15%

set.seed(13)

tuned.poly.svm.oj = tune.svm(Purchase ~., data = oj.train, kernel = "polynomial", degree = 2, cost = seq(.1, 10, by =.5))

summary(tuned.poly.svm.oj)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  degree cost
##       2  7.6
## 
## - best performance: 0.1825 
## 
## - Detailed performance results:
##    degree cost   error dispersion
## 1       2  0.1 0.31000 0.05614960
## 2       2  0.6 0.21500 0.05489890
## 3       2  1.1 0.21125 0.05084358
## 4       2  1.6 0.19750 0.04923018
## 5       2  2.1 0.19125 0.04715886
## 6       2  2.6 0.19125 0.04896498
## 7       2  3.1 0.19125 0.05104804
## 8       2  3.6 0.19375 0.05440652
## 9       2  4.1 0.19125 0.05239076
## 10      2  4.6 0.18750 0.04965156
## 11      2  5.1 0.19000 0.05197489
## 12      2  5.6 0.18625 0.05252314
## 13      2  6.1 0.18500 0.05130248
## 14      2  6.6 0.18375 0.05272110
## 15      2  7.1 0.18375 0.05001736
## 16      2  7.6 0.18250 0.04866267
## 17      2  8.1 0.18375 0.05001736
## 18      2  8.6 0.18375 0.05001736
## 19      2  9.1 0.18250 0.05075814
## 20      2  9.6 0.18375 0.05070681
svm.poly.cv.oj = svm(Purchase ~., kernel = "polynomial", degree = 2, cost = 7.6, data = oj.train)
summary(svm.poly.cv.oj)
## 
## Call:
## svm(formula = Purchase ~ ., data = oj.train, kernel = "polynomial", 
##     degree = 2, cost = 7.6)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  polynomial 
##        cost:  7.6 
##      degree:  2 
##      coef.0:  0 
## 
## Number of Support Vectors:  359
## 
##  ( 175 184 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM

359 support vectors were made. 175 belong to CH and 184 belong to MM.

training error rate

preds.train.2 = predict(svm.poly.cv.oj, oj.train)
table(oj.train$Purchase, preds.train.2)
##     preds.train.2
##       CH  MM
##   CH 457  41
##   MM  90 212
(90+41)/(457+41+90+212)
## [1] 0.16375

testing error rate

preds.test.2 = predict(svm.poly.cv.oj, oj.test)
table(oj.test$Purchase, preds.test.2)
##     preds.test.2
##       CH  MM
##   CH 142  13
##   MM  30  85
(30+13)/(142+13+30+85)
## [1] 0.1592593

The training error is 16.38% and the testing error rate is 15.93%

Overall the radial SVM had the lowest train and test error rate.