5

a)

x1 = runif(500) -0.5
x2 = runif(500) -0.5

y = 1 * (x1^2 - x2^2 > 0)

b)

plot(x1[y==1], x2[y==1], col = "blue", xlab = "x1", ylab = "x2")
points(x1[y==0], x2[y==0])

c)

log.model = glm(y ~ x1 + x2, family = binomial)

d)

log.PredProb = predict.glm(log.model, type = "response")
log.PredSur = ifelse(log.PredProb >= .5,1,0)
plot(x1[log.PredSur == 1], x2[log.PredSur == 1], col = "blue", xlab = "x1", ylab = "x2")
points(x1[log.PredSur == 0], x2[log.PredSur == 0])

e)

log.model2 = glm(y ~ x1* x2, family = binomial)

f)

log.PredProb = predict.glm(log.model2, type = "response")
log.PredSur = ifelse(log.PredProb >= .5,1,0)
plot(x1[log.PredSur == 1], x2[log.PredSur == 1], col = "blue", xlab = "x1", ylab = "x2")
points(x1[log.PredSur == 0], x2[log.PredSur == 0])

g)

library(e1071)

svm.lin = svm(as.factor(y)~ x1 + x2, kernel = "linear")

svm.PredSur = predict(svm.lin)
plot(x1[svm.PredSur == 1], x2[svm.PredSur == 1], col = "blue", xlab = "x1", ylab = "x2")
points(x1[svm.PredSur == 0], x2[svm.PredSur == 0])

h)

svm.rad = svm(as.factor(y)~ x1 + x2, kernel = "radial")

svm.PredSur = predict(svm.rad)
plot(x1[svm.PredSur == 1], x2[svm.PredSur == 1], col = "blue", xlab = "x1", ylab = "x2")
points(x1[svm.PredSur == 0], x2[svm.PredSur == 0])

i)

It is clear that the linear models are incapable of finding the pattern of this dataset. The right model will change depending on the data, for this case using a radial SVM model can almost perfectly classify the data.

7

a)

library(ISLR)
car = Auto

median.mil = median(car$mpg)
car$med = 1
car$med[car$mpg < median.mil] = 0 
car = subset(car, select = -c(mpg, name))
car$med = as.factor(car$med)

b)

library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
set.seed(42)

train_control = trainControl(method="repeatedcv", number=10, repeats=3)

svm.lin = train(med ~., data = car, method = "svmLinear", trControl = train_control,  preProcess = c("center","scale"), tuneGrid = expand.grid(C = seq(0, 2, length = 20)))
svm.lin
## Support Vector Machines with Linear Kernel 
## 
## 392 samples
##   7 predictor
##   2 classes: '0', '1' 
## 
## Pre-processing: centered (7), scaled (7) 
## Resampling: Cross-Validated (10 fold, repeated 3 times) 
## Summary of sample sizes: 353, 352, 354, 352, 352, 353, ... 
## Resampling results across tuning parameters:
## 
##   C          Accuracy   Kappa    
##   0.0000000        NaN        NaN
##   0.1052632  0.9071041  0.8141414
##   0.2105263  0.9062494  0.8123786
##   0.3157895  0.9037494  0.8073741
##   0.4210526  0.9037281  0.8073010
##   0.5263158  0.9097368  0.8193231
##   0.6315789  0.9088821  0.8175923
##   0.7368421  0.9097368  0.8192960
##   0.8421053  0.9097368  0.8192960
##   0.9473684  0.9089035  0.8176294
##   1.0526316  0.9106129  0.8210459
##   1.1578947  0.9106129  0.8210459
##   1.2631579  0.9089024  0.8176249
##   1.3684211  0.9089238  0.8176846
##   1.4736842  0.9106118  0.8210685
##   1.5789474  0.9106556  0.8211562
##   1.6842105  0.9123662  0.8245773
##   1.7894737  0.9115115  0.8228509
##   1.8947368  0.9132220  0.8262720
##   2.0000000  0.9132220  0.8262720
## 
## Accuracy was used to select the optimal model using the largest value.
## The final value used for the model was C = 1.894737.
svm.lin$bestTune
##           C
## 19 1.894737

The tuning functions found that using a cost of 1.894737 would be best and return to lowest error.

c)

svm.rad = train(med ~., data = car, method = "svmRadial", trControl = train_control, preProcess = c("center","scale"), tuneGrid = expand.grid(C = seq(0, 2, length = 20), sigma = seq(0, 1, length = 5)))
svm.rad$bestTune
##    sigma        C
## 64  0.75 1.263158
svm.poly = train(med ~., data = car, method = "svmPoly", trControl = train_control, preProcess = c("center","scale"), tuneGrid = expand.grid(C = seq(0, 2, length = 20), scale = seq(0, 1, length = 5), degree = seq(0,1, length = 5)))
svm.poly$bestTune    
##     degree scale        C
## 275      1     1 1.052632

The radial model chooses a sigma of .75 and a cost of 1.263158. The polynomial model decides to go with degree =1, scale =1 and cost = 1.052632

d)

plot(svm.lin)

plot(svm.rad)

plot(svm.poly)

8

a)

OJ = OJ

train = sample(nrow(OJ), 800)

OJ.train = OJ[train,]
OJ.test = OJ[-train,]

b)

set.seed(42)

OJ.lin = svm(Purchase ~., OJ.train, kernel = "linear", cost = .01)
summary(OJ.lin)
## 
## 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:  442
## 
##  ( 222 220 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM

c)

set.seed(42)

OJ.train.pred = predict(OJ.lin, OJ.train)
OJ.test.pred = predict(OJ.lin, OJ.test)

table(OJ.train$Purchase, OJ.train.pred)
##     OJ.train.pred
##       CH  MM
##   CH 415  64
##   MM  73 248
table(OJ.test$Purchase, OJ.test.pred)
##     OJ.test.pred
##       CH  MM
##   CH 154  20
##   MM  25  71
train.error = 1 - (428 + 241)/800
train.error
## [1] 0.16375
test.error = 1 - (143 + 76)/270
test.error
## [1] 0.1888889

The training error is .1637 and the testing error is .1889

d)

set.seed(42)

OJ.tune = tune(svm, Purchase ~ ., data = OJ.train, kernel = "linear", ranges=list(cost=seq(.01, 10, by = .1)))
summary(OJ.tune)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##  0.71
## 
## - best performance: 0.1675 
## 
## - Detailed performance results:
##     cost   error dispersion
## 1   0.01 0.17500 0.03584302
## 2   0.11 0.16875 0.03076005
## 3   0.21 0.17000 0.03291403
## 4   0.31 0.17000 0.03291403
## 5   0.41 0.17000 0.03291403
## 6   0.51 0.17000 0.03291403
## 7   0.61 0.16875 0.03498512
## 8   0.71 0.16750 0.03496029
## 9   0.81 0.17000 0.03917553
## 10  0.91 0.17000 0.03917553
## 11  1.01 0.17000 0.03917553
## 12  1.11 0.17000 0.03917553
## 13  1.21 0.17000 0.03917553
## 14  1.31 0.17125 0.03729108
## 15  1.41 0.17125 0.03729108
## 16  1.51 0.17125 0.03729108
## 17  1.61 0.17125 0.03729108
## 18  1.71 0.17125 0.03729108
## 19  1.81 0.17125 0.03729108
## 20  1.91 0.17125 0.03729108
## 21  2.01 0.17000 0.03872983
## 22  2.11 0.17000 0.03872983
## 23  2.21 0.17000 0.03872983
## 24  2.31 0.17000 0.03872983
## 25  2.41 0.17000 0.03872983
## 26  2.51 0.17000 0.03872983
## 27  2.61 0.17000 0.03872983
## 28  2.71 0.17000 0.03872983
## 29  2.81 0.17000 0.03872983
## 30  2.91 0.16875 0.03830162
## 31  3.01 0.16875 0.03830162
## 32  3.11 0.16875 0.03830162
## 33  3.21 0.16875 0.03830162
## 34  3.31 0.16875 0.03830162
## 35  3.41 0.16875 0.03830162
## 36  3.51 0.16875 0.03830162
## 37  3.61 0.16875 0.03830162
## 38  3.71 0.16875 0.03830162
## 39  3.81 0.17000 0.04090979
## 40  3.91 0.17000 0.04090979
## 41  4.01 0.17000 0.04090979
## 42  4.11 0.17000 0.04090979
## 43  4.21 0.17000 0.04090979
## 44  4.31 0.17000 0.04090979
## 45  4.41 0.17000 0.04090979
## 46  4.51 0.17000 0.04090979
## 47  4.61 0.16875 0.03919768
## 48  4.71 0.16875 0.03919768
## 49  4.81 0.16875 0.03919768
## 50  4.91 0.16875 0.03919768
## 51  5.01 0.16875 0.03919768
## 52  5.11 0.16875 0.03919768
## 53  5.21 0.16875 0.03919768
## 54  5.31 0.16875 0.03919768
## 55  5.41 0.17000 0.03961621
## 56  5.51 0.17000 0.03961621
## 57  5.61 0.17000 0.03961621
## 58  5.71 0.17000 0.03961621
## 59  5.81 0.17000 0.03961621
## 60  5.91 0.17000 0.03961621
## 61  6.01 0.17000 0.03961621
## 62  6.11 0.17000 0.03961621
## 63  6.21 0.17000 0.03961621
## 64  6.31 0.17000 0.03961621
## 65  6.41 0.17000 0.03961621
## 66  6.51 0.17000 0.03961621
## 67  6.61 0.17000 0.03961621
## 68  6.71 0.17000 0.03961621
## 69  6.81 0.17000 0.03961621
## 70  6.91 0.17000 0.03961621
## 71  7.01 0.17000 0.03961621
## 72  7.11 0.17000 0.03961621
## 73  7.21 0.17000 0.03961621
## 74  7.31 0.17000 0.03961621
## 75  7.41 0.16875 0.03830162
## 76  7.51 0.16875 0.03830162
## 77  7.61 0.16875 0.03830162
## 78  7.71 0.16875 0.03830162
## 79  7.81 0.16875 0.03830162
## 80  7.91 0.16875 0.03830162
## 81  8.01 0.16875 0.03830162
## 82  8.11 0.16875 0.03830162
## 83  8.21 0.16750 0.03782269
## 84  8.31 0.16750 0.03782269
## 85  8.41 0.16750 0.03782269
## 86  8.51 0.16750 0.03782269
## 87  8.61 0.16750 0.03782269
## 88  8.71 0.16750 0.03782269
## 89  8.81 0.16750 0.03782269
## 90  8.91 0.16750 0.03782269
## 91  9.01 0.16750 0.03782269
## 92  9.11 0.16750 0.03782269
## 93  9.21 0.16750 0.03782269
## 94  9.31 0.16750 0.03782269
## 95  9.41 0.16750 0.03782269
## 96  9.51 0.16750 0.03782269
## 97  9.61 0.16750 0.03782269
## 98  9.71 0.16750 0.03782269
## 99  9.81 0.16750 0.03782269
## 100 9.91 0.16750 0.03782269

e)

set.seed(42)

OJ.lin2 = svm(Purchase ~., OJ.train, kernel = "linear", cost = 6.31)

OJ.train.pred = predict(OJ.lin2, OJ.train)
OJ.test.pred = predict(OJ.lin2, OJ.test)

table(OJ.train$Purchase, OJ.train.pred)
##     OJ.train.pred
##       CH  MM
##   CH 420  59
##   MM  74 247
table(OJ.test$Purchase, OJ.test.pred)
##     OJ.test.pred
##       CH  MM
##   CH 154  20
##   MM  24  72
train.error = 1 - (433 + 250)/800
train.error
## [1] 0.14625
test.error = 1 - (140 + 78)/270
test.error
## [1] 0.1925926

f)

set.seed(42)

OJ.rad = svm(Purchase ~., OJ.train, kernel = "radial", cost = .01)
summary(OJ.rad)
## 
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "radial", cost = 0.01)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  0.01 
## 
## Number of Support Vectors:  645
## 
##  ( 324 321 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
OJ.train.pred = predict(OJ.rad, OJ.train)
OJ.test.pred = predict(OJ.rad, OJ.test)

table(OJ.train$Purchase, OJ.train.pred)
##     OJ.train.pred
##       CH  MM
##   CH 479   0
##   MM 321   0
table(OJ.test$Purchase, OJ.test.pred)
##     OJ.test.pred
##       CH  MM
##   CH 174   0
##   MM  96   0
train.error = 1 - (490 + 0)/800
train.error
## [1] 0.3875
test.error = 1 - (163 + 0)/270
test.error
## [1] 0.3962963
OJ.tune = tune(svm, Purchase ~ ., data = OJ.train, kernel = "radial", ranges=list(cost=seq(.01, 10, by = .1)))
summary(OJ.tune)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##  0.91
## 
## - best performance: 0.1725 
## 
## - Detailed performance results:
##     cost   error dispersion
## 1   0.01 0.40125 0.04348132
## 2   0.11 0.17875 0.03634805
## 3   0.21 0.17375 0.03251602
## 4   0.31 0.17875 0.03866254
## 5   0.41 0.18250 0.03961621
## 6   0.51 0.17625 0.04466309
## 7   0.61 0.17750 0.04362084
## 8   0.71 0.17625 0.04348132
## 9   0.81 0.17500 0.04526159
## 10  0.91 0.17250 0.04594683
## 11  1.01 0.17375 0.04656611
## 12  1.11 0.17375 0.04656611
## 13  1.21 0.17625 0.04619178
## 14  1.31 0.17625 0.04875178
## 15  1.41 0.17875 0.04528076
## 16  1.51 0.17875 0.04528076
## 17  1.61 0.18000 0.04647281
## 18  1.71 0.18125 0.04868051
## 19  1.81 0.18125 0.04973890
## 20  1.91 0.18250 0.04972145
## 21  2.01 0.18250 0.04972145
## 22  2.11 0.18250 0.04972145
## 23  2.21 0.18250 0.04972145
## 24  2.31 0.18500 0.04958158
## 25  2.41 0.18625 0.05152197
## 26  2.51 0.18625 0.05478810
## 27  2.61 0.18625 0.05478810
## 28  2.71 0.18625 0.05478810
## 29  2.81 0.18750 0.05368374
## 30  2.91 0.18625 0.05604128
## 31  3.01 0.18750 0.05368374
## 32  3.11 0.18750 0.05368374
## 33  3.21 0.18750 0.05368374
## 34  3.31 0.18750 0.05368374
## 35  3.41 0.18625 0.05185785
## 36  3.51 0.18625 0.05185785
## 37  3.61 0.18500 0.05197489
## 38  3.71 0.18500 0.05197489
## 39  3.81 0.18500 0.05197489
## 40  3.91 0.18625 0.05152197
## 41  4.01 0.18625 0.05152197
## 42  4.11 0.18625 0.05152197
## 43  4.21 0.18625 0.05152197
## 44  4.31 0.18500 0.05394184
## 45  4.41 0.18500 0.05394184
## 46  4.51 0.18500 0.05394184
## 47  4.61 0.18375 0.05434266
## 48  4.71 0.18375 0.05434266
## 49  4.81 0.18500 0.05394184
## 50  4.91 0.18625 0.05415064
## 51  5.01 0.18500 0.05552777
## 52  5.11 0.18625 0.05318012
## 53  5.21 0.18625 0.05318012
## 54  5.31 0.18625 0.05318012
## 55  5.41 0.18625 0.05318012
## 56  5.51 0.18750 0.05103104
## 57  5.61 0.18750 0.05103104
## 58  5.71 0.18750 0.05103104
## 59  5.81 0.18750 0.05103104
## 60  5.91 0.18750 0.05103104
## 61  6.01 0.18750 0.05103104
## 62  6.11 0.18750 0.05103104
## 63  6.21 0.18625 0.05219155
## 64  6.31 0.18750 0.05034602
## 65  6.41 0.18625 0.04875178
## 66  6.51 0.18750 0.04714045
## 67  6.61 0.18750 0.04714045
## 68  6.71 0.18750 0.04714045
## 69  6.81 0.18750 0.04714045
## 70  6.91 0.18625 0.04910660
## 71  7.01 0.18625 0.04910660
## 72  7.11 0.18750 0.04930066
## 73  7.21 0.18750 0.04930066
## 74  7.31 0.18625 0.04910660
## 75  7.41 0.18625 0.04910660
## 76  7.51 0.18625 0.04910660
## 77  7.61 0.18750 0.05068969
## 78  7.71 0.18750 0.05068969
## 79  7.81 0.18875 0.05050096
## 80  7.91 0.18875 0.05050096
## 81  8.01 0.18875 0.05050096
## 82  8.11 0.18875 0.05050096
## 83  8.21 0.18875 0.05050096
## 84  8.31 0.18875 0.05050096
## 85  8.41 0.18875 0.05050096
## 86  8.51 0.18875 0.05050096
## 87  8.61 0.18875 0.05050096
## 88  8.71 0.18875 0.05050096
## 89  8.81 0.18875 0.05050096
## 90  8.91 0.18750 0.04859127
## 91  9.01 0.18750 0.04859127
## 92  9.11 0.18750 0.04859127
## 93  9.21 0.18750 0.04859127
## 94  9.31 0.18750 0.04859127
## 95  9.41 0.18750 0.04859127
## 96  9.51 0.18750 0.04859127
## 97  9.61 0.18750 0.04859127
## 98  9.71 0.18750 0.04859127
## 99  9.81 0.18750 0.04859127
## 100 9.91 0.18500 0.05230785
OJ.rad2 = svm(Purchase ~., OJ.train, kernel = "radial", cost = 7.61)

OJ.train.pred = predict(OJ.rad2, OJ.train)
OJ.test.pred = predict(OJ.rad2, OJ.test)

table(OJ.train$Purchase, OJ.train.pred)
##     OJ.train.pred
##       CH  MM
##   CH 441  38
##   MM  80 241
table(OJ.test$Purchase, OJ.test.pred)
##     OJ.test.pred
##       CH  MM
##   CH 152  22
##   MM  25  71
train.error = 1 - (446 + 246)/800
train.error
## [1] 0.135
test.error = 1 - (140 + 74)/270
test.error
## [1] 0.2074074

g)

set.seed(42)

OJ.poly = svm(Purchase ~., OJ.train, kernel = "polynomial", cost = .01, degree = 2)
summary(OJ.poly)
## 
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "polynomial", 
##     cost = 0.01, degree = 2)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  polynomial 
##        cost:  0.01 
##      degree:  2 
##      coef.0:  0 
## 
## Number of Support Vectors:  647
## 
##  ( 326 321 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
OJ.train.pred = predict(OJ.poly, OJ.train)
OJ.test.pred = predict(OJ.poly, OJ.test)

table(OJ.train$Purchase, OJ.train.pred)
##     OJ.train.pred
##       CH  MM
##   CH 474   5
##   MM 299  22
table(OJ.test$Purchase, OJ.test.pred)
##     OJ.test.pred
##       CH  MM
##   CH 172   2
##   MM  89   7
train.error = 1 - (488 + 12)/800
train.error
## [1] 0.375
test.error = 1 - (163 + 9)/270
test.error
## [1] 0.362963
OJ.tune = tune(svm, Purchase ~ ., data = OJ.train, kernel = "polynomial", ranges=list(cost=seq(.01, 10, by = .1)), degree = 2)
summary(OJ.tune)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##  7.71
## 
## - best performance: 0.1875 
## 
## - Detailed performance results:
##     cost   error dispersion
## 1   0.01 0.39875 0.04581439
## 2   0.11 0.33000 0.04609772
## 3   0.21 0.22875 0.05036326
## 4   0.31 0.21500 0.05263871
## 5   0.41 0.21125 0.05050096
## 6   0.51 0.21125 0.05084358
## 7   0.61 0.21000 0.05489890
## 8   0.71 0.20750 0.05374838
## 9   0.81 0.20625 0.05145454
## 10  0.91 0.21000 0.05062114
## 11  1.01 0.20875 0.05434266
## 12  1.11 0.21000 0.06003471
## 13  1.21 0.21250 0.05682576
## 14  1.31 0.21125 0.05478810
## 15  1.41 0.21125 0.05510407
## 16  1.51 0.21125 0.05635022
## 17  1.61 0.20750 0.05719120
## 18  1.71 0.20750 0.05779514
## 19  1.81 0.20500 0.05596378
## 20  1.91 0.20250 0.05394184
## 21  2.01 0.20375 0.05529278
## 22  2.11 0.20250 0.05614960
## 23  2.21 0.20000 0.05464532
## 24  2.31 0.19750 0.05614960
## 25  2.41 0.19500 0.05957022
## 26  2.51 0.19625 0.05864500
## 27  2.61 0.19625 0.05864500
## 28  2.71 0.19500 0.05957022
## 29  2.81 0.19625 0.05923412
## 30  2.91 0.19625 0.05923412
## 31  3.01 0.19750 0.05737305
## 32  3.11 0.19875 0.05573063
## 33  3.21 0.19875 0.05350558
## 34  3.31 0.20000 0.05464532
## 35  3.41 0.19875 0.05350558
## 36  3.51 0.19750 0.05361903
## 37  3.61 0.19750 0.05361903
## 38  3.71 0.19375 0.05566829
## 39  3.81 0.19375 0.05566829
## 40  3.91 0.19250 0.05719120
## 41  4.01 0.19125 0.05622685
## 42  4.11 0.19125 0.05622685
## 43  4.21 0.19250 0.05719120
## 44  4.31 0.19125 0.05952649
## 45  4.41 0.19250 0.05809475
## 46  4.51 0.19375 0.05690208
## 47  4.61 0.19500 0.05596378
## 48  4.71 0.19250 0.05596378
## 49  4.81 0.19250 0.05596378
## 50  4.91 0.19250 0.05596378
## 51  5.01 0.19375 0.05472469
## 52  5.11 0.19375 0.05472469
## 53  5.21 0.19375 0.05472469
## 54  5.31 0.19375 0.05472469
## 55  5.41 0.19375 0.05472469
## 56  5.51 0.19375 0.05472469
## 57  5.61 0.19375 0.05472469
## 58  5.71 0.19250 0.05342440
## 59  5.81 0.19125 0.05466120
## 60  5.91 0.19125 0.05466120
## 61  6.01 0.19125 0.05337563
## 62  6.11 0.19000 0.05361903
## 63  6.21 0.19000 0.05361903
## 64  6.31 0.19125 0.05337563
## 65  6.41 0.19125 0.05337563
## 66  6.51 0.19125 0.05337563
## 67  6.61 0.19125 0.05337563
## 68  6.71 0.19000 0.05130248
## 69  6.81 0.19000 0.05130248
## 70  6.91 0.19000 0.05130248
## 71  7.01 0.19000 0.05130248
## 72  7.11 0.19000 0.05130248
## 73  7.21 0.19125 0.05239076
## 74  7.31 0.18875 0.05382908
## 75  7.41 0.18875 0.05382908
## 76  7.51 0.18875 0.05382908
## 77  7.61 0.18875 0.05382908
## 78  7.71 0.18750 0.05204165
## 79  7.81 0.18875 0.05185785
## 80  7.91 0.18875 0.05185785
## 81  8.01 0.18875 0.05185785
## 82  8.11 0.18875 0.05185785
## 83  8.21 0.18875 0.05185785
## 84  8.31 0.18875 0.05185785
## 85  8.41 0.19000 0.05296750
## 86  8.51 0.19000 0.05296750
## 87  8.61 0.19000 0.05296750
## 88  8.71 0.19125 0.05304937
## 89  8.81 0.19125 0.05304937
## 90  8.91 0.19125 0.05304937
## 91  9.01 0.18875 0.05185785
## 92  9.11 0.18875 0.05185785
## 93  9.21 0.18875 0.05185785
## 94  9.31 0.18875 0.05185785
## 95  9.41 0.18875 0.05185785
## 96  9.51 0.19000 0.05361903
## 97  9.61 0.19125 0.05369991
## 98  9.71 0.18875 0.05185785
## 99  9.81 0.18875 0.05185785
## 100 9.91 0.18875 0.05185785
OJ.poly2 = svm(Purchase ~., OJ.train, kernel = "radial", cost = 4.21, degree = 2)

OJ.train.pred = predict(OJ.poly2, OJ.train)
OJ.test.pred = predict(OJ.poly2, OJ.test)

table(OJ.train$Purchase, OJ.train.pred)
##     OJ.train.pred
##       CH  MM
##   CH 439  40
##   MM  79 242
table(OJ.test$Purchase, OJ.test.pred)
##     OJ.test.pred
##       CH  MM
##   CH 151  23
##   MM  22  74
train.error = 1 - (449 + 245)/800
train.error
## [1] 0.1325
test.error = 1 - (140 + 73)/270
test.error
## [1] 0.2111111

h)

Definitely the worst model was the radial svm. The testing error wasn’t too different from the other models, but it was not able to make any predictions on MM. Even though the linear model performed worst than the polynomial for making training predictions, it achieved a better error when making predictions on the testing dataset over the polynomial model.