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.
Generate a data set with n = 500 and p = 2, such that the observations belong to two classes with a quadratic decision boundary between them. For instance, you can do this as follows:
x1=runif (500) -0.5
x2=runif (500) -0.5
y=1*(x1^2-x2^2 > 0)
Plot the observations, colored according to their class labels. Your plot should display X1 on the x-axis, and X2 on the y-axis.
plot(x1, x2, xlab = "x1", ylab = "x2", col = (2 - y), pch = (3 - y))
log.x = glm(y ~ x1 + x2, family = 'binomial')
summary(log.x)
##
## Call:
## glm(formula = y ~ x1 + x2, family = "binomial")
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.282 -1.148 -1.041 1.154 1.361
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.06915 0.08995 -0.769 0.4420
## x1 0.68016 0.31971 2.127 0.0334 *
## x2 -0.04831 0.31364 -0.154 0.8776
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 692.64 on 499 degrees of freedom
## Residual deviance: 687.96 on 497 degrees of freedom
## AIC: 693.96
##
## Number of Fisher Scoring iterations: 3
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.
glm.pred = predict(log.x,data.frame(x1,x2))
plot(x1,x2,col=ifelse(glm.pred>0,'red','black'),pch=ifelse(as.integer(glm.pred>0) == y,1,4))
### (e) Now fit a logistic regression model to the data using non-linear functions of X1 and X2 as predictors (e.g. X2 1 , X1×X2, log(X2), and so forth).
log.non = glm(y ~ log(x2) + x1*x2, data=data.frame(x1,x2,y), family = 'binomial')
summary(log.non)
##
## Call:
## glm(formula = y ~ log(x2) + x1 * x2, family = "binomial", data = data.frame(x1,
## x2, y))
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.4826 -0.7489 -0.3014 0.7827 2.3835
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.0003 2.1272 -0.470 0.6382
## log(x2) -1.2451 0.7828 -1.591 0.1117
## x1 -1.2124 1.3313 -0.911 0.3624
## x2 -4.2499 3.8560 -1.102 0.2704
## x1:x2 8.0103 4.7487 1.687 0.0916 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 342.09 on 246 degrees of freedom
## Residual deviance: 235.72 on 242 degrees of freedom
## (253 observations deleted due to missingness)
## AIC: 245.72
##
## Number of Fisher Scoring iterations: 6
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.
non.pred = predict(log.non,data.frame(x1,x2))
plot(x1,x2,col=ifelse(non.pred>0,'red','blue'),pch=ifelse(as.integer(non.pred>0) == y,1,4))
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.
svm.x = svm(y ~ x1 + x2,data = data.frame(x1,x2,y=as.factor(y)), kernel ='linear')
svm.pred = predict(svm.x, data.frame(x1,x2), type='response')
plot(x1,x2,col=ifelse(svm.pred!=0,'red','pink'),pch=ifelse(svm.pred == y,1,4))
Fit a SVM using a non-linear kernel to the data. Obtain a class prediction for each training observation. Plot the observations, colored according to the predicted class labels.
svm.non = svm(y~., data = data.frame(x1,x2,y=as.factor(y)), kernel = 'polynomial', degree = 2)
svm.pred.non = predict(svm.non, data.frame(x1,x2), type = 'response')
plot(x1,x2,col=ifelse(svm.pred.non!= 0,'red','blue'),pch=ifelse(svm.pred.non == y,1,4))
### (i) Comment on your results.
##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.
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.
head(Auto)
## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name
## 1 chevrolet chevelle malibu
## 2 buick skylark 320
## 3 plymouth satellite
## 4 amc rebel sst
## 5 ford torino
## 6 ford galaxie 500
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.
Looking at the summary of the tuned variable we return a best performance of 0.01025641 at a cost of 61.
Auto$mpg=ifelse(Auto$mpg>median(Auto$mpg),1,0)
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.
Looking at the summary of the tuned variable we return a best performance of 0.01025641 at a cost of 61.
set.seed(1)
tuned = tune.svm(mpg ~ ., data = Auto, cost = seq(1,300, by = 10))
summary(tuned)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 151
##
## - best performance: 0.08027368
##
## - Detailed performance results:
## cost error dispersion
## 1 1 0.09803436 0.02818832
## 2 11 0.08966653 0.03678376
## 3 21 0.08771550 0.03754872
## 4 31 0.08664265 0.03796920
## 5 41 0.08583445 0.03802468
## 6 51 0.08521585 0.03798571
## 7 61 0.08457807 0.03788690
## 8 71 0.08385876 0.03777741
## 9 81 0.08321047 0.03763776
## 10 91 0.08243934 0.03709781
## 11 101 0.08169968 0.03639614
## 12 111 0.08116540 0.03585953
## 13 121 0.08069894 0.03534477
## 14 131 0.08043757 0.03490563
## 15 141 0.08033816 0.03457919
## 16 151 0.08027368 0.03429429
## 17 161 0.08028669 0.03427671
## 18 171 0.08050120 0.03455322
## 19 181 0.08063484 0.03455296
## 20 191 0.08070245 0.03453761
## 21 201 0.08066474 0.03446414
## 22 211 0.08062178 0.03441694
## 23 221 0.08056807 0.03436599
## 24 231 0.08052403 0.03431236
## 25 241 0.08050212 0.03427968
## 26 251 0.08048468 0.03427810
## 27 261 0.08045788 0.03429389
## 28 271 0.08043432 0.03432569
## 29 281 0.08040802 0.03436037
## 30 291 0.08037717 0.03439934
tuned$best.parameters
## cost
## 16 151
set.seed(1)
tune.again = tune.svm(mpg ~ ., data = Auto, kernal = 'polynomial', gamma = seq(1,100, by =10), cost = seq(1,100, by = 10), degree = c(2,5,9),scale = T)
summary(tune.again)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## degree gamma cost
## 2 1 1
##
## - best performance: 0.09918732
##
## - Detailed performance results:
## degree gamma cost error dispersion
## 1 2 1 1 0.09918732 0.020523476
## 2 5 1 1 0.09918732 0.020523476
## 3 9 1 1 0.09918732 0.020523476
## 4 2 11 1 0.24450608 0.004156713
## 5 5 11 1 0.24450608 0.004156713
## 6 9 11 1 0.24450608 0.004156713
## 7 2 21 1 0.24842981 0.001604909
## 8 5 21 1 0.24842981 0.001604909
## 9 9 21 1 0.24842981 0.001604909
## 10 2 31 1 0.24989798 0.001087359
## 11 5 31 1 0.24989798 0.001087359
## 12 9 31 1 0.24989798 0.001087359
## 13 2 41 1 0.25048683 0.001184628
## 14 5 41 1 0.25048683 0.001184628
## 15 9 41 1 0.25048683 0.001184628
## 16 2 51 1 0.25072826 0.001291715
## 17 5 51 1 0.25072826 0.001291715
## 18 9 51 1 0.25072826 0.001291715
## 19 2 61 1 0.25082910 0.001351053
## 20 5 61 1 0.25082910 0.001351053
## 21 9 61 1 0.25082910 0.001351053
## 22 2 71 1 0.25087197 0.001380488
## 23 5 71 1 0.25087197 0.001380488
## 24 9 71 1 0.25087197 0.001380488
## 25 2 81 1 0.25089049 0.001394609
## 26 5 81 1 0.25089049 0.001394609
## 27 9 81 1 0.25089049 0.001394609
## 28 2 91 1 0.25089861 0.001401299
## 29 5 91 1 0.25089861 0.001401299
## 30 9 91 1 0.25089861 0.001401299
## 31 2 1 11 0.10442540 0.020692046
## 32 5 1 11 0.10442540 0.020692046
## 33 9 1 11 0.10442540 0.020692046
## 34 2 11 11 0.24450279 0.004158802
## 35 5 11 11 0.24450279 0.004158802
## 36 9 11 11 0.24450279 0.004158802
## 37 2 21 11 0.24842966 0.001605023
## 38 5 21 11 0.24842966 0.001605023
## 39 9 21 11 0.24842966 0.001605023
## 40 2 31 11 0.24989801 0.001087394
## 41 5 31 11 0.24989801 0.001087394
## 42 9 31 11 0.24989801 0.001087394
## 43 2 41 11 0.25048687 0.001184665
## 44 5 41 11 0.25048687 0.001184665
## 45 9 41 11 0.25048687 0.001184665
## 46 2 51 11 0.25072827 0.001291711
## 47 5 51 11 0.25072827 0.001291711
## 48 9 51 11 0.25072827 0.001291711
## 49 2 61 11 0.25082910 0.001351050
## 50 5 61 11 0.25082910 0.001351050
## 51 9 61 11 0.25082910 0.001351050
## 52 2 71 11 0.25087198 0.001380485
## 53 5 71 11 0.25087198 0.001380485
## 54 9 71 11 0.25087198 0.001380485
## 55 2 81 11 0.25089049 0.001394608
## 56 5 81 11 0.25089049 0.001394608
## 57 9 81 11 0.25089049 0.001394608
## 58 2 91 11 0.25089861 0.001401298
## 59 5 91 11 0.25089861 0.001401298
## 60 9 91 11 0.25089861 0.001401298
## 61 2 1 21 0.10442540 0.020692046
## 62 5 1 21 0.10442540 0.020692046
## 63 9 1 21 0.10442540 0.020692046
## 64 2 11 21 0.24450279 0.004158802
## 65 5 11 21 0.24450279 0.004158802
## 66 9 11 21 0.24450279 0.004158802
## 67 2 21 21 0.24842966 0.001605023
## 68 5 21 21 0.24842966 0.001605023
## 69 9 21 21 0.24842966 0.001605023
## 70 2 31 21 0.24989801 0.001087394
## 71 5 31 21 0.24989801 0.001087394
## 72 9 31 21 0.24989801 0.001087394
## 73 2 41 21 0.25048687 0.001184665
## 74 5 41 21 0.25048687 0.001184665
## 75 9 41 21 0.25048687 0.001184665
## 76 2 51 21 0.25072827 0.001291711
## 77 5 51 21 0.25072827 0.001291711
## 78 9 51 21 0.25072827 0.001291711
## 79 2 61 21 0.25082910 0.001351050
## 80 5 61 21 0.25082910 0.001351050
## 81 9 61 21 0.25082910 0.001351050
## 82 2 71 21 0.25087198 0.001380485
## 83 5 71 21 0.25087198 0.001380485
## 84 9 71 21 0.25087198 0.001380485
## 85 2 81 21 0.25089049 0.001394608
## 86 5 81 21 0.25089049 0.001394608
## 87 9 81 21 0.25089049 0.001394608
## 88 2 91 21 0.25089861 0.001401298
## 89 5 91 21 0.25089861 0.001401298
## 90 9 91 21 0.25089861 0.001401298
## 91 2 1 31 0.10442540 0.020692046
## 92 5 1 31 0.10442540 0.020692046
## 93 9 1 31 0.10442540 0.020692046
## 94 2 11 31 0.24450279 0.004158802
## 95 5 11 31 0.24450279 0.004158802
## 96 9 11 31 0.24450279 0.004158802
## 97 2 21 31 0.24842966 0.001605023
## 98 5 21 31 0.24842966 0.001605023
## 99 9 21 31 0.24842966 0.001605023
## 100 2 31 31 0.24989801 0.001087394
## 101 5 31 31 0.24989801 0.001087394
## 102 9 31 31 0.24989801 0.001087394
## 103 2 41 31 0.25048687 0.001184665
## 104 5 41 31 0.25048687 0.001184665
## 105 9 41 31 0.25048687 0.001184665
## 106 2 51 31 0.25072827 0.001291711
## 107 5 51 31 0.25072827 0.001291711
## 108 9 51 31 0.25072827 0.001291711
## 109 2 61 31 0.25082910 0.001351050
## 110 5 61 31 0.25082910 0.001351050
## 111 9 61 31 0.25082910 0.001351050
## 112 2 71 31 0.25087198 0.001380485
## 113 5 71 31 0.25087198 0.001380485
## 114 9 71 31 0.25087198 0.001380485
## 115 2 81 31 0.25089049 0.001394608
## 116 5 81 31 0.25089049 0.001394608
## 117 9 81 31 0.25089049 0.001394608
## 118 2 91 31 0.25089861 0.001401298
## 119 5 91 31 0.25089861 0.001401298
## 120 9 91 31 0.25089861 0.001401298
## 121 2 1 41 0.10442540 0.020692046
## 122 5 1 41 0.10442540 0.020692046
## 123 9 1 41 0.10442540 0.020692046
## 124 2 11 41 0.24450279 0.004158802
## 125 5 11 41 0.24450279 0.004158802
## 126 9 11 41 0.24450279 0.004158802
## 127 2 21 41 0.24842966 0.001605023
## 128 5 21 41 0.24842966 0.001605023
## 129 9 21 41 0.24842966 0.001605023
## 130 2 31 41 0.24989801 0.001087394
## 131 5 31 41 0.24989801 0.001087394
## 132 9 31 41 0.24989801 0.001087394
## 133 2 41 41 0.25048687 0.001184665
## 134 5 41 41 0.25048687 0.001184665
## 135 9 41 41 0.25048687 0.001184665
## 136 2 51 41 0.25072827 0.001291711
## 137 5 51 41 0.25072827 0.001291711
## 138 9 51 41 0.25072827 0.001291711
## 139 2 61 41 0.25082910 0.001351050
## 140 5 61 41 0.25082910 0.001351050
## 141 9 61 41 0.25082910 0.001351050
## 142 2 71 41 0.25087198 0.001380485
## 143 5 71 41 0.25087198 0.001380485
## 144 9 71 41 0.25087198 0.001380485
## 145 2 81 41 0.25089049 0.001394608
## 146 5 81 41 0.25089049 0.001394608
## 147 9 81 41 0.25089049 0.001394608
## 148 2 91 41 0.25089861 0.001401298
## 149 5 91 41 0.25089861 0.001401298
## 150 9 91 41 0.25089861 0.001401298
## 151 2 1 51 0.10442540 0.020692046
## 152 5 1 51 0.10442540 0.020692046
## 153 9 1 51 0.10442540 0.020692046
## 154 2 11 51 0.24450279 0.004158802
## 155 5 11 51 0.24450279 0.004158802
## 156 9 11 51 0.24450279 0.004158802
## 157 2 21 51 0.24842966 0.001605023
## 158 5 21 51 0.24842966 0.001605023
## 159 9 21 51 0.24842966 0.001605023
## 160 2 31 51 0.24989801 0.001087394
## 161 5 31 51 0.24989801 0.001087394
## 162 9 31 51 0.24989801 0.001087394
## 163 2 41 51 0.25048687 0.001184665
## 164 5 41 51 0.25048687 0.001184665
## 165 9 41 51 0.25048687 0.001184665
## 166 2 51 51 0.25072827 0.001291711
## 167 5 51 51 0.25072827 0.001291711
## 168 9 51 51 0.25072827 0.001291711
## 169 2 61 51 0.25082910 0.001351050
## 170 5 61 51 0.25082910 0.001351050
## 171 9 61 51 0.25082910 0.001351050
## 172 2 71 51 0.25087198 0.001380485
## 173 5 71 51 0.25087198 0.001380485
## 174 9 71 51 0.25087198 0.001380485
## 175 2 81 51 0.25089049 0.001394608
## 176 5 81 51 0.25089049 0.001394608
## 177 9 81 51 0.25089049 0.001394608
## 178 2 91 51 0.25089861 0.001401298
## 179 5 91 51 0.25089861 0.001401298
## 180 9 91 51 0.25089861 0.001401298
## 181 2 1 61 0.10442540 0.020692046
## 182 5 1 61 0.10442540 0.020692046
## 183 9 1 61 0.10442540 0.020692046
## 184 2 11 61 0.24450279 0.004158802
## 185 5 11 61 0.24450279 0.004158802
## 186 9 11 61 0.24450279 0.004158802
## 187 2 21 61 0.24842966 0.001605023
## 188 5 21 61 0.24842966 0.001605023
## 189 9 21 61 0.24842966 0.001605023
## 190 2 31 61 0.24989801 0.001087394
## 191 5 31 61 0.24989801 0.001087394
## 192 9 31 61 0.24989801 0.001087394
## 193 2 41 61 0.25048687 0.001184665
## 194 5 41 61 0.25048687 0.001184665
## 195 9 41 61 0.25048687 0.001184665
## 196 2 51 61 0.25072827 0.001291711
## 197 5 51 61 0.25072827 0.001291711
## 198 9 51 61 0.25072827 0.001291711
## 199 2 61 61 0.25082910 0.001351050
## 200 5 61 61 0.25082910 0.001351050
## 201 9 61 61 0.25082910 0.001351050
## 202 2 71 61 0.25087198 0.001380485
## 203 5 71 61 0.25087198 0.001380485
## 204 9 71 61 0.25087198 0.001380485
## 205 2 81 61 0.25089049 0.001394608
## 206 5 81 61 0.25089049 0.001394608
## 207 9 81 61 0.25089049 0.001394608
## 208 2 91 61 0.25089861 0.001401298
## 209 5 91 61 0.25089861 0.001401298
## 210 9 91 61 0.25089861 0.001401298
## 211 2 1 71 0.10442540 0.020692046
## 212 5 1 71 0.10442540 0.020692046
## 213 9 1 71 0.10442540 0.020692046
## 214 2 11 71 0.24450279 0.004158802
## 215 5 11 71 0.24450279 0.004158802
## 216 9 11 71 0.24450279 0.004158802
## 217 2 21 71 0.24842966 0.001605023
## 218 5 21 71 0.24842966 0.001605023
## 219 9 21 71 0.24842966 0.001605023
## 220 2 31 71 0.24989801 0.001087394
## 221 5 31 71 0.24989801 0.001087394
## 222 9 31 71 0.24989801 0.001087394
## 223 2 41 71 0.25048687 0.001184665
## 224 5 41 71 0.25048687 0.001184665
## 225 9 41 71 0.25048687 0.001184665
## 226 2 51 71 0.25072827 0.001291711
## 227 5 51 71 0.25072827 0.001291711
## 228 9 51 71 0.25072827 0.001291711
## 229 2 61 71 0.25082910 0.001351050
## 230 5 61 71 0.25082910 0.001351050
## 231 9 61 71 0.25082910 0.001351050
## 232 2 71 71 0.25087198 0.001380485
## 233 5 71 71 0.25087198 0.001380485
## 234 9 71 71 0.25087198 0.001380485
## 235 2 81 71 0.25089049 0.001394608
## 236 5 81 71 0.25089049 0.001394608
## 237 9 81 71 0.25089049 0.001394608
## 238 2 91 71 0.25089861 0.001401298
## 239 5 91 71 0.25089861 0.001401298
## 240 9 91 71 0.25089861 0.001401298
## 241 2 1 81 0.10442540 0.020692046
## 242 5 1 81 0.10442540 0.020692046
## 243 9 1 81 0.10442540 0.020692046
## 244 2 11 81 0.24450279 0.004158802
## 245 5 11 81 0.24450279 0.004158802
## 246 9 11 81 0.24450279 0.004158802
## 247 2 21 81 0.24842966 0.001605023
## 248 5 21 81 0.24842966 0.001605023
## 249 9 21 81 0.24842966 0.001605023
## 250 2 31 81 0.24989801 0.001087394
## 251 5 31 81 0.24989801 0.001087394
## 252 9 31 81 0.24989801 0.001087394
## 253 2 41 81 0.25048687 0.001184665
## 254 5 41 81 0.25048687 0.001184665
## 255 9 41 81 0.25048687 0.001184665
## 256 2 51 81 0.25072827 0.001291711
## 257 5 51 81 0.25072827 0.001291711
## 258 9 51 81 0.25072827 0.001291711
## 259 2 61 81 0.25082910 0.001351050
## 260 5 61 81 0.25082910 0.001351050
## 261 9 61 81 0.25082910 0.001351050
## 262 2 71 81 0.25087198 0.001380485
## 263 5 71 81 0.25087198 0.001380485
## 264 9 71 81 0.25087198 0.001380485
## 265 2 81 81 0.25089049 0.001394608
## 266 5 81 81 0.25089049 0.001394608
## 267 9 81 81 0.25089049 0.001394608
## 268 2 91 81 0.25089861 0.001401298
## 269 5 91 81 0.25089861 0.001401298
## 270 9 91 81 0.25089861 0.001401298
## 271 2 1 91 0.10442540 0.020692046
## 272 5 1 91 0.10442540 0.020692046
## 273 9 1 91 0.10442540 0.020692046
## 274 2 11 91 0.24450279 0.004158802
## 275 5 11 91 0.24450279 0.004158802
## 276 9 11 91 0.24450279 0.004158802
## 277 2 21 91 0.24842966 0.001605023
## 278 5 21 91 0.24842966 0.001605023
## 279 9 21 91 0.24842966 0.001605023
## 280 2 31 91 0.24989801 0.001087394
## 281 5 31 91 0.24989801 0.001087394
## 282 9 31 91 0.24989801 0.001087394
## 283 2 41 91 0.25048687 0.001184665
## 284 5 41 91 0.25048687 0.001184665
## 285 9 41 91 0.25048687 0.001184665
## 286 2 51 91 0.25072827 0.001291711
## 287 5 51 91 0.25072827 0.001291711
## 288 9 51 91 0.25072827 0.001291711
## 289 2 61 91 0.25082910 0.001351050
## 290 5 61 91 0.25082910 0.001351050
## 291 9 61 91 0.25082910 0.001351050
## 292 2 71 91 0.25087198 0.001380485
## 293 5 71 91 0.25087198 0.001380485
## 294 9 71 91 0.25087198 0.001380485
## 295 2 81 91 0.25089049 0.001394608
## 296 5 81 91 0.25089049 0.001394608
## 297 9 81 91 0.25089049 0.001394608
## 298 2 91 91 0.25089861 0.001401298
## 299 5 91 91 0.25089861 0.001401298
## 300 9 91 91 0.25089861 0.001401298
tune.again$best.parameters
## degree gamma cost
## 1 2 1 1
set.seed(1)
tune.again.rad = tune.svm(mpg ~ ., data = Auto, kernal = 'radial', gamma = seq(1,1000, by =100), cost = seq(1,1000, by = 100), degree = c(3,4,5))
summary(tune.again.rad)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## degree gamma cost
## 3 1 1
##
## - best performance: 0.09918732
##
## - Detailed performance results:
## degree gamma cost error dispersion
## 1 3 1 1 0.09918732 0.020523476
## 2 4 1 1 0.09918732 0.020523476
## 3 5 1 1 0.09918732 0.020523476
## 4 3 101 1 0.25090220 0.001404450
## 5 4 101 1 0.25090220 0.001404450
## 6 5 101 1 0.25090220 0.001404450
## 7 3 201 1 0.25090515 0.001407231
## 8 4 201 1 0.25090515 0.001407231
## 9 5 201 1 0.25090515 0.001407231
## 10 3 301 1 0.25090515 0.001407233
## 11 4 301 1 0.25090515 0.001407233
## 12 5 301 1 0.25090515 0.001407233
## 13 3 401 1 0.25090515 0.001407233
## 14 4 401 1 0.25090515 0.001407233
## 15 5 401 1 0.25090515 0.001407233
## 16 3 501 1 0.25090515 0.001407233
## 17 4 501 1 0.25090515 0.001407233
## 18 5 501 1 0.25090515 0.001407233
## 19 3 601 1 0.25090515 0.001407233
## 20 4 601 1 0.25090515 0.001407233
## 21 5 601 1 0.25090515 0.001407233
## 22 3 701 1 0.25090515 0.001407233
## 23 4 701 1 0.25090515 0.001407233
## 24 5 701 1 0.25090515 0.001407233
## 25 3 801 1 0.25090515 0.001407233
## 26 4 801 1 0.25090515 0.001407233
## 27 5 801 1 0.25090515 0.001407233
## 28 3 901 1 0.25090515 0.001407233
## 29 4 901 1 0.25090515 0.001407233
## 30 5 901 1 0.25090515 0.001407233
## 31 3 1 101 0.10442540 0.020692046
## 32 4 1 101 0.10442540 0.020692046
## 33 5 1 101 0.10442540 0.020692046
## 34 3 101 101 0.25090220 0.001404450
## 35 4 101 101 0.25090220 0.001404450
## 36 5 101 101 0.25090220 0.001404450
## 37 3 201 101 0.25090515 0.001407231
## 38 4 201 101 0.25090515 0.001407231
## 39 5 201 101 0.25090515 0.001407231
## 40 3 301 101 0.25090515 0.001407233
## 41 4 301 101 0.25090515 0.001407233
## 42 5 301 101 0.25090515 0.001407233
## 43 3 401 101 0.25090515 0.001407233
## 44 4 401 101 0.25090515 0.001407233
## 45 5 401 101 0.25090515 0.001407233
## 46 3 501 101 0.25090515 0.001407233
## 47 4 501 101 0.25090515 0.001407233
## 48 5 501 101 0.25090515 0.001407233
## 49 3 601 101 0.25090515 0.001407233
## 50 4 601 101 0.25090515 0.001407233
## 51 5 601 101 0.25090515 0.001407233
## 52 3 701 101 0.25090515 0.001407233
## 53 4 701 101 0.25090515 0.001407233
## 54 5 701 101 0.25090515 0.001407233
## 55 3 801 101 0.25090515 0.001407233
## 56 4 801 101 0.25090515 0.001407233
## 57 5 801 101 0.25090515 0.001407233
## 58 3 901 101 0.25090515 0.001407233
## 59 4 901 101 0.25090515 0.001407233
## 60 5 901 101 0.25090515 0.001407233
## 61 3 1 201 0.10442540 0.020692046
## 62 4 1 201 0.10442540 0.020692046
## 63 5 1 201 0.10442540 0.020692046
## 64 3 101 201 0.25090220 0.001404450
## 65 4 101 201 0.25090220 0.001404450
## 66 5 101 201 0.25090220 0.001404450
## 67 3 201 201 0.25090515 0.001407231
## 68 4 201 201 0.25090515 0.001407231
## 69 5 201 201 0.25090515 0.001407231
## 70 3 301 201 0.25090515 0.001407233
## 71 4 301 201 0.25090515 0.001407233
## 72 5 301 201 0.25090515 0.001407233
## 73 3 401 201 0.25090515 0.001407233
## 74 4 401 201 0.25090515 0.001407233
## 75 5 401 201 0.25090515 0.001407233
## 76 3 501 201 0.25090515 0.001407233
## 77 4 501 201 0.25090515 0.001407233
## 78 5 501 201 0.25090515 0.001407233
## 79 3 601 201 0.25090515 0.001407233
## 80 4 601 201 0.25090515 0.001407233
## 81 5 601 201 0.25090515 0.001407233
## 82 3 701 201 0.25090515 0.001407233
## 83 4 701 201 0.25090515 0.001407233
## 84 5 701 201 0.25090515 0.001407233
## 85 3 801 201 0.25090515 0.001407233
## 86 4 801 201 0.25090515 0.001407233
## 87 5 801 201 0.25090515 0.001407233
## 88 3 901 201 0.25090515 0.001407233
## 89 4 901 201 0.25090515 0.001407233
## 90 5 901 201 0.25090515 0.001407233
## 91 3 1 301 0.10442540 0.020692046
## 92 4 1 301 0.10442540 0.020692046
## 93 5 1 301 0.10442540 0.020692046
## 94 3 101 301 0.25090220 0.001404450
## 95 4 101 301 0.25090220 0.001404450
## 96 5 101 301 0.25090220 0.001404450
## 97 3 201 301 0.25090515 0.001407231
## 98 4 201 301 0.25090515 0.001407231
## 99 5 201 301 0.25090515 0.001407231
## 100 3 301 301 0.25090515 0.001407233
## 101 4 301 301 0.25090515 0.001407233
## 102 5 301 301 0.25090515 0.001407233
## 103 3 401 301 0.25090515 0.001407233
## 104 4 401 301 0.25090515 0.001407233
## 105 5 401 301 0.25090515 0.001407233
## 106 3 501 301 0.25090515 0.001407233
## 107 4 501 301 0.25090515 0.001407233
## 108 5 501 301 0.25090515 0.001407233
## 109 3 601 301 0.25090515 0.001407233
## 110 4 601 301 0.25090515 0.001407233
## 111 5 601 301 0.25090515 0.001407233
## 112 3 701 301 0.25090515 0.001407233
## 113 4 701 301 0.25090515 0.001407233
## 114 5 701 301 0.25090515 0.001407233
## 115 3 801 301 0.25090515 0.001407233
## 116 4 801 301 0.25090515 0.001407233
## 117 5 801 301 0.25090515 0.001407233
## 118 3 901 301 0.25090515 0.001407233
## 119 4 901 301 0.25090515 0.001407233
## 120 5 901 301 0.25090515 0.001407233
## 121 3 1 401 0.10442540 0.020692046
## 122 4 1 401 0.10442540 0.020692046
## 123 5 1 401 0.10442540 0.020692046
## 124 3 101 401 0.25090220 0.001404450
## 125 4 101 401 0.25090220 0.001404450
## 126 5 101 401 0.25090220 0.001404450
## 127 3 201 401 0.25090515 0.001407231
## 128 4 201 401 0.25090515 0.001407231
## 129 5 201 401 0.25090515 0.001407231
## 130 3 301 401 0.25090515 0.001407233
## 131 4 301 401 0.25090515 0.001407233
## 132 5 301 401 0.25090515 0.001407233
## 133 3 401 401 0.25090515 0.001407233
## 134 4 401 401 0.25090515 0.001407233
## 135 5 401 401 0.25090515 0.001407233
## 136 3 501 401 0.25090515 0.001407233
## 137 4 501 401 0.25090515 0.001407233
## 138 5 501 401 0.25090515 0.001407233
## 139 3 601 401 0.25090515 0.001407233
## 140 4 601 401 0.25090515 0.001407233
## 141 5 601 401 0.25090515 0.001407233
## 142 3 701 401 0.25090515 0.001407233
## 143 4 701 401 0.25090515 0.001407233
## 144 5 701 401 0.25090515 0.001407233
## 145 3 801 401 0.25090515 0.001407233
## 146 4 801 401 0.25090515 0.001407233
## 147 5 801 401 0.25090515 0.001407233
## 148 3 901 401 0.25090515 0.001407233
## 149 4 901 401 0.25090515 0.001407233
## 150 5 901 401 0.25090515 0.001407233
## 151 3 1 501 0.10442540 0.020692046
## 152 4 1 501 0.10442540 0.020692046
## 153 5 1 501 0.10442540 0.020692046
## 154 3 101 501 0.25090220 0.001404450
## 155 4 101 501 0.25090220 0.001404450
## 156 5 101 501 0.25090220 0.001404450
## 157 3 201 501 0.25090515 0.001407231
## 158 4 201 501 0.25090515 0.001407231
## 159 5 201 501 0.25090515 0.001407231
## 160 3 301 501 0.25090515 0.001407233
## 161 4 301 501 0.25090515 0.001407233
## 162 5 301 501 0.25090515 0.001407233
## 163 3 401 501 0.25090515 0.001407233
## 164 4 401 501 0.25090515 0.001407233
## 165 5 401 501 0.25090515 0.001407233
## 166 3 501 501 0.25090515 0.001407233
## 167 4 501 501 0.25090515 0.001407233
## 168 5 501 501 0.25090515 0.001407233
## 169 3 601 501 0.25090515 0.001407233
## 170 4 601 501 0.25090515 0.001407233
## 171 5 601 501 0.25090515 0.001407233
## 172 3 701 501 0.25090515 0.001407233
## 173 4 701 501 0.25090515 0.001407233
## 174 5 701 501 0.25090515 0.001407233
## 175 3 801 501 0.25090515 0.001407233
## 176 4 801 501 0.25090515 0.001407233
## 177 5 801 501 0.25090515 0.001407233
## 178 3 901 501 0.25090515 0.001407233
## 179 4 901 501 0.25090515 0.001407233
## 180 5 901 501 0.25090515 0.001407233
## 181 3 1 601 0.10442540 0.020692046
## 182 4 1 601 0.10442540 0.020692046
## 183 5 1 601 0.10442540 0.020692046
## 184 3 101 601 0.25090220 0.001404450
## 185 4 101 601 0.25090220 0.001404450
## 186 5 101 601 0.25090220 0.001404450
## 187 3 201 601 0.25090515 0.001407231
## 188 4 201 601 0.25090515 0.001407231
## 189 5 201 601 0.25090515 0.001407231
## 190 3 301 601 0.25090515 0.001407233
## 191 4 301 601 0.25090515 0.001407233
## 192 5 301 601 0.25090515 0.001407233
## 193 3 401 601 0.25090515 0.001407233
## 194 4 401 601 0.25090515 0.001407233
## 195 5 401 601 0.25090515 0.001407233
## 196 3 501 601 0.25090515 0.001407233
## 197 4 501 601 0.25090515 0.001407233
## 198 5 501 601 0.25090515 0.001407233
## 199 3 601 601 0.25090515 0.001407233
## 200 4 601 601 0.25090515 0.001407233
## 201 5 601 601 0.25090515 0.001407233
## 202 3 701 601 0.25090515 0.001407233
## 203 4 701 601 0.25090515 0.001407233
## 204 5 701 601 0.25090515 0.001407233
## 205 3 801 601 0.25090515 0.001407233
## 206 4 801 601 0.25090515 0.001407233
## 207 5 801 601 0.25090515 0.001407233
## 208 3 901 601 0.25090515 0.001407233
## 209 4 901 601 0.25090515 0.001407233
## 210 5 901 601 0.25090515 0.001407233
## 211 3 1 701 0.10442540 0.020692046
## 212 4 1 701 0.10442540 0.020692046
## 213 5 1 701 0.10442540 0.020692046
## 214 3 101 701 0.25090220 0.001404450
## 215 4 101 701 0.25090220 0.001404450
## 216 5 101 701 0.25090220 0.001404450
## 217 3 201 701 0.25090515 0.001407231
## 218 4 201 701 0.25090515 0.001407231
## 219 5 201 701 0.25090515 0.001407231
## 220 3 301 701 0.25090515 0.001407233
## 221 4 301 701 0.25090515 0.001407233
## 222 5 301 701 0.25090515 0.001407233
## 223 3 401 701 0.25090515 0.001407233
## 224 4 401 701 0.25090515 0.001407233
## 225 5 401 701 0.25090515 0.001407233
## 226 3 501 701 0.25090515 0.001407233
## 227 4 501 701 0.25090515 0.001407233
## 228 5 501 701 0.25090515 0.001407233
## 229 3 601 701 0.25090515 0.001407233
## 230 4 601 701 0.25090515 0.001407233
## 231 5 601 701 0.25090515 0.001407233
## 232 3 701 701 0.25090515 0.001407233
## 233 4 701 701 0.25090515 0.001407233
## 234 5 701 701 0.25090515 0.001407233
## 235 3 801 701 0.25090515 0.001407233
## 236 4 801 701 0.25090515 0.001407233
## 237 5 801 701 0.25090515 0.001407233
## 238 3 901 701 0.25090515 0.001407233
## 239 4 901 701 0.25090515 0.001407233
## 240 5 901 701 0.25090515 0.001407233
## 241 3 1 801 0.10442540 0.020692046
## 242 4 1 801 0.10442540 0.020692046
## 243 5 1 801 0.10442540 0.020692046
## 244 3 101 801 0.25090220 0.001404450
## 245 4 101 801 0.25090220 0.001404450
## 246 5 101 801 0.25090220 0.001404450
## 247 3 201 801 0.25090515 0.001407231
## 248 4 201 801 0.25090515 0.001407231
## 249 5 201 801 0.25090515 0.001407231
## 250 3 301 801 0.25090515 0.001407233
## 251 4 301 801 0.25090515 0.001407233
## 252 5 301 801 0.25090515 0.001407233
## 253 3 401 801 0.25090515 0.001407233
## 254 4 401 801 0.25090515 0.001407233
## 255 5 401 801 0.25090515 0.001407233
## 256 3 501 801 0.25090515 0.001407233
## 257 4 501 801 0.25090515 0.001407233
## 258 5 501 801 0.25090515 0.001407233
## 259 3 601 801 0.25090515 0.001407233
## 260 4 601 801 0.25090515 0.001407233
## 261 5 601 801 0.25090515 0.001407233
## 262 3 701 801 0.25090515 0.001407233
## 263 4 701 801 0.25090515 0.001407233
## 264 5 701 801 0.25090515 0.001407233
## 265 3 801 801 0.25090515 0.001407233
## 266 4 801 801 0.25090515 0.001407233
## 267 5 801 801 0.25090515 0.001407233
## 268 3 901 801 0.25090515 0.001407233
## 269 4 901 801 0.25090515 0.001407233
## 270 5 901 801 0.25090515 0.001407233
## 271 3 1 901 0.10442540 0.020692046
## 272 4 1 901 0.10442540 0.020692046
## 273 5 1 901 0.10442540 0.020692046
## 274 3 101 901 0.25090220 0.001404450
## 275 4 101 901 0.25090220 0.001404450
## 276 5 101 901 0.25090220 0.001404450
## 277 3 201 901 0.25090515 0.001407231
## 278 4 201 901 0.25090515 0.001407231
## 279 5 201 901 0.25090515 0.001407231
## 280 3 301 901 0.25090515 0.001407233
## 281 4 301 901 0.25090515 0.001407233
## 282 5 301 901 0.25090515 0.001407233
## 283 3 401 901 0.25090515 0.001407233
## 284 4 401 901 0.25090515 0.001407233
## 285 5 401 901 0.25090515 0.001407233
## 286 3 501 901 0.25090515 0.001407233
## 287 4 501 901 0.25090515 0.001407233
## 288 5 501 901 0.25090515 0.001407233
## 289 3 601 901 0.25090515 0.001407233
## 290 4 601 901 0.25090515 0.001407233
## 291 5 601 901 0.25090515 0.001407233
## 292 3 701 901 0.25090515 0.001407233
## 293 4 701 901 0.25090515 0.001407233
## 294 5 701 901 0.25090515 0.001407233
## 295 3 801 901 0.25090515 0.001407233
## 296 4 801 901 0.25090515 0.001407233
## 297 5 801 901 0.25090515 0.001407233
## 298 3 901 901 0.25090515 0.001407233
## 299 4 901 901 0.25090515 0.001407233
## 300 5 901 901 0.25090515 0.001407233
tune.again.rad$best.parameters
## degree gamma cost
## 1 3 1 1
Make some plots to back up your assertions in (b) and (c).
dim(tune.again$performances)
## [1] 300 5
plot(tune.again$performances[,c(1,5)])
This problem involves the OJ data set which is part of the ISLR package.
Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.
set.seed(1)
index = sample(nrow(OJ), 800)
train.oj = OJ[index, ]
test.oj = OJ[-index, ]
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.
The summary of the svm tells us that the svm chose 435 variables out of the 800 in the training set. 219 belong to CH awhile 216 belong to MM.
svm.oj <- svm(Purchase ~ ., data = train.oj, kernel = "linear", cost = 0.01)
summary(svm.oj)
##
## Call:
## svm(formula = Purchase ~ ., data = train.oj, kernel = "linear", cost = 0.01)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: linear
## cost: 0.01
##
## Number of Support Vectors: 435
##
## ( 219 216 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
What are the training and test error rates?
Training error rate: .175 Test error rate: .177
pred.svm.train = predict(svm.oj, train.oj)
table(train.oj$Purchase, pred.svm.train)
## pred.svm.train
## CH MM
## CH 420 65
## MM 75 240
(75 + 65) / (420 + 240 + 75 + 65)
## [1] 0.175
pred.svm.test = predict(svm.oj, test.oj)
table(test.oj$Purchase, pred.svm.test)
## pred.svm.test
## CH MM
## CH 153 15
## MM 33 69
(33 + 15) / (153 + 69 + 33 + 15)
## [1] 0.1777778
Use the tune() function to select an optimal cost. Consider values in the range 0.01 to 10.
set.seed(1)
tune.oj <- tune.svm(Purchase ~ ., data = train.oj, kernel = "linear", cost = seq(1,10, by = 1))
summary(tune.oj)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 3
##
## - best performance: 0.16875
##
## - Detailed performance results:
## cost error dispersion
## 1 1 0.17500 0.02946278
## 2 2 0.17250 0.02874698
## 3 3 0.16875 0.03019037
## 4 4 0.17000 0.02958040
## 5 5 0.17250 0.03162278
## 6 6 0.17500 0.03333333
## 7 7 0.17500 0.03333333
## 8 8 0.17375 0.03197764
## 9 9 0.17375 0.03197764
## 10 10 0.17375 0.03197764
Compute the training and test error rates using this new value for cost.
Training error: .166 Test error: .159
svm.tune.oj = svm(Purchase ~ ., kernel = "linear", data = train.oj, cost = tune.oj$best.parameter$cost)
pred.train.tune = predict(svm.tune.oj, train.oj)
table(train.oj$Purchase, pred.train.tune)
## pred.train.tune
## CH MM
## CH 422 63
## MM 70 245
(70 + 63) / (422 + 245 + 63 +70)
## [1] 0.16625
svm.tune.oj = svm(Purchase ~ ., kernel = "linear", data = test.oj, cost = tune.oj$best.parameter$cost)
pred.test.tune = predict(svm.tune.oj, test.oj)
table(test.oj$Purchase, pred.test.tune)
## pred.test.tune
## CH MM
## CH 153 15
## MM 28 74
(28 + 15) / (153 + 74 + 28 +15)
## [1] 0.1592593
373 variables chosen. 188 to CH 185 to MM
svm.radial = svm(Purchase ~ ., kernel = "radial", data = train.oj)
summary(svm.radial)
##
## Call:
## svm(formula = Purchase ~ ., data = train.oj, kernel = "radial")
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 1
##
## Number of Support Vectors: 373
##
## ( 188 185 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
Training error: .151 Test error: .185
pred.radial = predict(svm.radial, train.oj)
table(train.oj$Purchase, pred.radial)
## pred.radial
## CH MM
## CH 441 44
## MM 77 238
(77 + 44) / (441 + 238 + 77 + 44)
## [1] 0.15125
pred.radial.test = predict(svm.radial, test.oj)
table(test.oj$Purchase, pred.radial.test)
## pred.radial.test
## CH MM
## CH 151 17
## MM 33 69
(33 + 17) / (151 + 69 + 33 + 17)
## [1] 0.1851852
We get a best performance of .171 with a cost of 1.
set.seed(1)
tune.radial = tune.svm(Purchase ~ ., data = train.oj, kernel = "radial", cost = seq(1,10, by = 1))
summary(tune.radial)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 1
##
## - best performance: 0.17125
##
## - Detailed performance results:
## cost error dispersion
## 1 1 0.17125 0.02128673
## 2 2 0.17750 0.02188988
## 3 3 0.17625 0.02239947
## 4 4 0.18125 0.02301117
## 5 5 0.18000 0.02220485
## 6 6 0.18000 0.02220485
## 7 7 0.18375 0.02503470
## 8 8 0.18250 0.02648375
## 9 9 0.18375 0.02703521
## 10 10 0.18625 0.02853482
The svm chooses 373 variables. 188 to CH 185 to MM
svm.radial2 = svm(Purchase ~ ., kernel = "radial", data = train.oj, cost = tune.radial$best.parameter$cost)
summary(svm.radial2)
##
## Call:
## svm(formula = Purchase ~ ., data = train.oj, kernel = "radial", cost = tune.radial$best.parameter$cost)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 1
##
## Number of Support Vectors: 373
##
## ( 188 185 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
Training error: .151 Test error: .185
train.pred = predict(svm.radial2, train.oj)
table(train.oj$Purchase, train.pred)
## train.pred
## CH MM
## CH 441 44
## MM 77 238
(77 + 44) / (441 + 238 + 77 + 44)
## [1] 0.15125
test.pred = predict(svm.radial2, test.oj)
table(test.oj$Purchase, test.pred)
## test.pred
## CH MM
## CH 151 17
## MM 33 69
(33 + 17) / (151 + 69 + 33 +17)
## [1] 0.1851852
Repeat parts (b) through (e) using a support vector machine with a polynomial kernel. Set degree=2.
The svm chooses 447 variables 225 to CH and 222 to MM
svm.poly = svm(Purchase ~ ., kernel = "polynomial", data = train.oj, degree = 2)
summary(svm.poly)
##
## Call:
## svm(formula = Purchase ~ ., data = train.oj, kernel = "polynomial",
## degree = 2)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: polynomial
## cost: 1
## degree: 2
## coef.0: 0
##
## Number of Support Vectors: 447
##
## ( 225 222 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
Training error: .182 Test error: .22
train.pred.poly = predict(svm.poly, train.oj)
table(train.oj$Purchase, train.pred.poly)
## train.pred.poly
## CH MM
## CH 449 36
## MM 110 205
(110 + 36) / (449 + 205 + 110 + 36)
## [1] 0.1825
test.pred.poly = predict(svm.poly, test.oj)
table(test.oj$Purchase, test.pred.poly)
## test.pred.poly
## CH MM
## CH 153 15
## MM 45 57
(45 + 15) / (153 + 57 + 45 + 15)
## [1] 0.2222222
With a cost variable we get a best performance of .176 with a cost of 3
set.seed(1)
tune.poly = tune.svm(Purchase ~ ., data = train.oj, kernel = "polynomial", degree = 2, cost = seq(1,10, by = 1))
summary(tune.poly)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## degree cost
## 2 3
##
## - best performance: 0.17625
##
## - Detailed performance results:
## degree cost error dispersion
## 1 2 1 0.20250 0.04116363
## 2 2 2 0.18125 0.04177070
## 3 2 3 0.17625 0.03793727
## 4 2 4 0.18250 0.03395258
## 5 2 5 0.18250 0.03496029
## 6 2 6 0.18625 0.03304563
## 7 2 7 0.18500 0.03162278
## 8 2 8 0.18000 0.03395258
## 9 2 9 0.17750 0.02751262
## 10 2 10 0.18125 0.02779513
With the best parameters the svm selects 384 variables. 197 to CH and 187 to MM
svm.poly2 = svm(Purchase ~ ., kernel = "polynomial", degree = 2, data = train.oj, cost = tune.poly$best.parameter$cost)
summary(svm.poly2)
##
## Call:
## svm(formula = Purchase ~ ., data = train.oj, kernel = "polynomial",
## degree = 2, cost = tune.poly$best.parameter$cost)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: polynomial
## cost: 3
## degree: 2
## coef.0: 0
##
## Number of Support Vectors: 384
##
## ( 197 187 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
Training error: .153 Test error: .203
train.pred2 = predict(svm.poly2, train.oj)
table(train.oj$Purchase, train.pred2)
## train.pred2
## CH MM
## CH 452 33
## MM 90 225
(90 + 33) / (452 + 225 + 90 + 33)
## [1] 0.15375
test.pred2 = predict(svm.poly2, test.oj)
table(test.oj$Purchase, test.pred2)
## test.pred2
## CH MM
## CH 153 15
## MM 40 62
(40 + 15) / (153 + 62 + 40 + 15)
## [1] 0.2037037
Overall, which approach seems to give the best results on this data? The best over all test and training error rates came when we computed the training and test error rates using the linear kernal with the cost function.