We have seen that we can fit an SVM with a non-linear kernel in order to perform classification using a non-linear decision boundary. We will now see that we can also obtain a non-linear decision boundary by performing logistic regression using non-linear transformations of the features.
(a) Generate a data set with n = 500 and p = 2, such that the observations belong to two classes with a quadratic decision boundary between them. For instance, you can do this as follows:
set.seed(1)
x1 = runif(500)-0.5
x2 = runif(500)-0.5
y = 1 * (x1^2 - x2^2 > 0)
(b) 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[y==0], x2[y==0], col = 'green', xlab = 'X1', ylab = 'X2')
points(x1[y==1], x2[y==1], col = 'blue')
(c) Fit a logistic regression model to the data, using X1 and X2 as predictors.
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.179 -1.139 -1.112 1.206 1.257
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.087260 0.089579 -0.974 0.330
## x1 0.196199 0.316864 0.619 0.536
## x2 -0.002854 0.305712 -0.009 0.993
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 692.18 on 499 degrees of freedom
## Residual deviance: 691.79 on 497 degrees of freedom
## AIC: 697.79
##
## Number of Fisher Scoring iterations: 3
Based on the results, neither x1 or x2 are significant in predicting y.
(d) Apply this model to the training data in order to obtain a predicted class label for each training observation. Plot the observations, colored according to the predicted class labels. The decision boundary should be linear.
df <- data.frame(x1 = x1, x2 = x2, y = as.factor(y))
glm.prob <- predict(glm.fit, df, type = "response")
glm.pred = rep(0,500)
glm.pred[glm.prob>0.47] = 1
plot(df[glm.pred ==1,]$x1, df[glm.pred ==1,]$x2, col = "green", xlab = "X1", ylab = "X2")
points(df[glm.pred ==0,]$x1, df[glm.pred ==0,]$x2, col = "blue")
Based on the plot, the decision boundary is on barely shown on the left hand side.
(e) Now fit a logistic regression model to the data using non-linear functions of \(X_1\) and \(X_2\) as predictors (e.g. \(X^2_{1}\), \(X^1\) × \(X^2\), \(log(X_2)\), and so forth).
log.fit <- glm(y ~ poly(x1, 2) + log(x2) + I(x1 * x2), family = 'binomial')
summary(log.fit)
##
## Call:
## glm(formula = y ~ poly(x1, 2) + log(x2) + I(x1 * x2), family = "binomial")
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -3.07660 -0.30318 -0.07824 0.26039 1.78835
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -5.0989 0.7937 -6.424 1.33e-10 ***
## poly(x1, 2)1 22.0302 15.1692 1.452 0.146
## poly(x1, 2)2 70.5508 9.7403 7.243 4.38e-13 ***
## log(x2) -3.1893 0.4915 -6.489 8.63e-11 ***
## I(x1 * x2) -7.6014 7.2496 -1.049 0.294
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 344.97 on 249 degrees of freedom
## Residual deviance: 123.09 on 245 degrees of freedom
## (250 observations deleted due to missingness)
## AIC: 133.09
##
## Number of Fisher Scoring iterations: 7
(f) Apply this model to the training data in order to obtain a predicted class label for each training observation. Plot the observations, colored according to the predicted class labels. The decision boundary should be obviously non-linear. If it is not, then repeat (a)-(e) until you come up with an example in which the predicted class labels are obviously non-linear.
log.prob <- predict(log.fit, df, type = 'response')
log.pred <- rep(0,500)
log.pred[log.prob>0.47] = 1
plot(df[log.pred==1,]$x1, df[log.pred==1,]$x2, col = 'blue', xlab = 'X1', ylab = 'X2')
points(df[log.pred==0,]$x1, df[log.pred==0,]$x2, col = 'green')
Based on the plot, the non-linear decision boundary is shown.
(g) 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.
library(e1071)
svm.fit <- svm(as.factor(y)~ x1 + x2, df, kernel = 'linear', cost = 0.1)
svm.pred <- predict(svm.fit, df)
plot(df[svm.pred==0,]$x1, df[svm.pred==0,]$x2, col = "blue", xlab = "X1", ylab = "X2")
points(df[svm.pred==1,]$x1, df[svm.pred==1,]$x2, col = "green")
(h) Fit a SVM using a non-linear kernel to the data. Obtain a class prediction for each training observation. Plot the observations, colored according to the predicted class labels.
svm.fit2 <- svm(as.factor(y)~ x1 + x2, df, kernel = 'radial', gamma = 1)
svm.pred2 <- predict(svm.fit2, df)
plot(df[svm.pred2==0,]$x1, df[svm.pred2==0,]$x2, col = "red", xlab = "X1", ylab = "X2")
points(df[svm.pred2==1,]$x1, df[svm.pred2==1,]$x2, col = 'black')
(i) Comment on your results. Based on the plots, the SVMs with the non-linear kernels performed better at finding the non-linear boundary between the classes. The linear and non-linear logistic regressions poorly predicted the dataset.
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.
library(ISLR)
attach(Auto)
summary(Auto)
## mpg cylinders displacement horsepower weight
## Min. : 9.00 Min. :3.000 Min. : 68.0 Min. : 46.0 Min. :1613
## 1st Qu.:17.00 1st Qu.:4.000 1st Qu.:105.0 1st Qu.: 75.0 1st Qu.:2225
## Median :22.75 Median :4.000 Median :151.0 Median : 93.5 Median :2804
## Mean :23.45 Mean :5.472 Mean :194.4 Mean :104.5 Mean :2978
## 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:275.8 3rd Qu.:126.0 3rd Qu.:3615
## Max. :46.60 Max. :8.000 Max. :455.0 Max. :230.0 Max. :5140
##
## acceleration year origin name
## Min. : 8.00 Min. :70.00 Min. :1.000 amc matador : 5
## 1st Qu.:13.78 1st Qu.:73.00 1st Qu.:1.000 ford pinto : 5
## Median :15.50 Median :76.00 Median :1.000 toyota corolla : 5
## Mean :15.54 Mean :75.98 Mean :1.577 amc gremlin : 4
## 3rd Qu.:17.02 3rd Qu.:79.00 3rd Qu.:2.000 amc hornet : 4
## Max. :24.80 Max. :82.00 Max. :3.000 chevrolet chevette: 4
## (Other) :365
(a) Create a binary variable that takes on a 1 for cars with gas mileage above the median, and a 0 for cars with gas mileage below the median.
gas.var <- ifelse(Auto$mpg > median(Auto$mpg), 1, 0)
Auto$mpglevel <- as.factor(gas.var)
str(Auto$mpglevel)
## Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
(b) Fit a support vector classifier to the data with various values of cost, in order to predict whether a car gets high or low gas mileage. Report the cross-validation errors associated with different values of this parameter. Comment on your results.
set.seed(1)
tune.out <- tune(svm, mpglevel~., data = Auto, kernel = "linear", ranges = list(cost = c(0.01, 0.1, 1, 5, 10, 100)))
summary(tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 1
##
## - best performance: 0.01025641
##
## - Detailed performance results:
## cost error dispersion
## 1 1e-02 0.07653846 0.03617137
## 2 1e-01 0.04596154 0.03378238
## 3 1e+00 0.01025641 0.01792836
## 4 5e+00 0.02051282 0.02648194
## 5 1e+01 0.02051282 0.02648194
## 6 1e+02 0.03076923 0.03151981
tune.out$best.parameters
## cost
## 3 1
Based on the results of the linear SVM, cross-validation error is minimized for cost = 1.
(c) Now repeat (b), this time using SVMs with radial and polynomial basis kernels, with different values of gamma and degree and cost. Comment on your results.
set.seed(1)
tune.out.rad <- tune(svm, mpglevel~., data = Auto, kernel = "radial", ranges = list(cost = c(0.01, 0.1, 1, 5, 10, 100), gamma = c(0.01, 0.1, 1, 5, 10, 100)))
summary(tune.out.rad)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost gamma
## 100 0.01
##
## - best performance: 0.01282051
##
## - Detailed performance results:
## cost gamma error dispersion
## 1 1e-02 1e-02 0.55115385 0.04366593
## 2 1e-01 1e-02 0.08929487 0.04382379
## 3 1e+00 1e-02 0.07403846 0.03522110
## 4 5e+00 1e-02 0.04852564 0.03303346
## 5 1e+01 1e-02 0.02557692 0.02093679
## 6 1e+02 1e-02 0.01282051 0.01813094
## 7 1e-02 1e-01 0.21711538 0.09865227
## 8 1e-01 1e-01 0.07903846 0.03874545
## 9 1e+00 1e-01 0.05371795 0.03525162
## 10 5e+00 1e-01 0.02820513 0.03299190
## 11 1e+01 1e-01 0.03076923 0.03375798
## 12 1e+02 1e-01 0.03583333 0.02759051
## 13 1e-02 1e+00 0.55115385 0.04366593
## 14 1e-01 1e+00 0.55115385 0.04366593
## 15 1e+00 1e+00 0.06384615 0.04375618
## 16 5e+00 1e+00 0.05884615 0.04020934
## 17 1e+01 1e+00 0.05884615 0.04020934
## 18 1e+02 1e+00 0.05884615 0.04020934
## 19 1e-02 5e+00 0.55115385 0.04366593
## 20 1e-01 5e+00 0.55115385 0.04366593
## 21 1e+00 5e+00 0.49493590 0.04724924
## 22 5e+00 5e+00 0.48217949 0.05470903
## 23 1e+01 5e+00 0.48217949 0.05470903
## 24 1e+02 5e+00 0.48217949 0.05470903
## 25 1e-02 1e+01 0.55115385 0.04366593
## 26 1e-01 1e+01 0.55115385 0.04366593
## 27 1e+00 1e+01 0.51794872 0.05063697
## 28 5e+00 1e+01 0.51794872 0.04917316
## 29 1e+01 1e+01 0.51794872 0.04917316
## 30 1e+02 1e+01 0.51794872 0.04917316
## 31 1e-02 1e+02 0.55115385 0.04366593
## 32 1e-01 1e+02 0.55115385 0.04366593
## 33 1e+00 1e+02 0.55115385 0.04366593
## 34 5e+00 1e+02 0.55115385 0.04366593
## 35 1e+01 1e+02 0.55115385 0.04366593
## 36 1e+02 1e+02 0.55115385 0.04366593
tune.out.rad$best.parameters
## cost gamma
## 6 100 0.01
Based on the results of the SVM with radial basis kernel, cross-validation error is minimized for cost = 100 and gamma = 0.01.
set.seed(1)
tune.out.poly <- tune(svm, mpglevel~., data = Auto, kernel = "polynomial", ranges = list(cost = c(0.01, 0.1, 1, 5, 10, 100), degree = c(2,3,4)))
summary(tune.out.poly)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost degree
## 100 2
##
## - best performance: 0.3013462
##
## - Detailed performance results:
## cost degree error dispersion
## 1 1e-02 2 0.5511538 0.04366593
## 2 1e-01 2 0.5511538 0.04366593
## 3 1e+00 2 0.5511538 0.04366593
## 4 5e+00 2 0.5511538 0.04366593
## 5 1e+01 2 0.5130128 0.08963366
## 6 1e+02 2 0.3013462 0.09961961
## 7 1e-02 3 0.5511538 0.04366593
## 8 1e-01 3 0.5511538 0.04366593
## 9 1e+00 3 0.5511538 0.04366593
## 10 5e+00 3 0.5511538 0.04366593
## 11 1e+01 3 0.5511538 0.04366593
## 12 1e+02 3 0.3446154 0.09821588
## 13 1e-02 4 0.5511538 0.04366593
## 14 1e-01 4 0.5511538 0.04366593
## 15 1e+00 4 0.5511538 0.04366593
## 16 5e+00 4 0.5511538 0.04366593
## 17 1e+01 4 0.5511538 0.04366593
## 18 1e+02 4 0.5511538 0.04366593
tune.out.poly$best.parameters
## cost degree
## 6 100 2
Based on the results of the SVM with polynimial basis kernel, cross-validation error is minimized for cost = 100 and degree = 2.
(d) Make some plots to back up your assertions in (b) and (c). Hint: In the lab, we used the plot() function for svm objects only in cases with p = 2. When p > 2, you can use the plot() function to create plots displaying pairs of variables at a time. Essentially, instead of typing plot(svmfit , dat) where svmfit contains your fitted model and dat is a data frame containing your data, you can type plot(svmfit , dat , x1∼x4) in order to plot just the first and fourth variables. However, you must replace x1 and x4 with the correct variable names. To find out more, type ?plot.svm.
last.linear <- svm(mpglevel~., data=Auto, kernel="linear", cost=1)
last.rad <- svm(mpglevel~., data=Auto, kernel="radial", cost=10, gamma=0.01)
last.poly <- svm(mpglevel~., data=Auto, kernel="polynomial", cost=1, degree=2)
plotpairs <- function(autofit) {
for (name in names(Auto)[!(names(Auto) %in% c("mpg", "mpglevel", "name"))]) {
plot(autofit, Auto, as.formula(paste("mpg~", name, sep = "")))
}
}
plotpairs(last.linear)
plotpairs(last.rad)
plotpairs(last.poly)
detach(Auto)
This problem involves the OJ data set which is part of the ISLR package.
library(ISLR)
attach(OJ)
summary(OJ)
## Purchase WeekofPurchase StoreID PriceCH PriceMM
## CH:653 Min. :227.0 Min. :1.00 Min. :1.690 Min. :1.690
## MM:417 1st Qu.:240.0 1st Qu.:2.00 1st Qu.:1.790 1st Qu.:1.990
## Median :257.0 Median :3.00 Median :1.860 Median :2.090
## Mean :254.4 Mean :3.96 Mean :1.867 Mean :2.085
## 3rd Qu.:268.0 3rd Qu.:7.00 3rd Qu.:1.990 3rd Qu.:2.180
## Max. :278.0 Max. :7.00 Max. :2.090 Max. :2.290
## DiscCH DiscMM SpecialCH SpecialMM
## Min. :0.00000 Min. :0.0000 Min. :0.0000 Min. :0.0000
## 1st Qu.:0.00000 1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:0.0000
## Median :0.00000 Median :0.0000 Median :0.0000 Median :0.0000
## Mean :0.05186 Mean :0.1234 Mean :0.1477 Mean :0.1617
## 3rd Qu.:0.00000 3rd Qu.:0.2300 3rd Qu.:0.0000 3rd Qu.:0.0000
## Max. :0.50000 Max. :0.8000 Max. :1.0000 Max. :1.0000
## LoyalCH SalePriceMM SalePriceCH PriceDiff Store7
## Min. :0.000011 Min. :1.190 Min. :1.390 Min. :-0.6700 No :714
## 1st Qu.:0.325257 1st Qu.:1.690 1st Qu.:1.750 1st Qu.: 0.0000 Yes:356
## Median :0.600000 Median :2.090 Median :1.860 Median : 0.2300
## Mean :0.565782 Mean :1.962 Mean :1.816 Mean : 0.1465
## 3rd Qu.:0.850873 3rd Qu.:2.130 3rd Qu.:1.890 3rd Qu.: 0.3200
## Max. :0.999947 Max. :2.290 Max. :2.090 Max. : 0.6400
## PctDiscMM PctDiscCH ListPriceDiff STORE
## Min. :0.0000 Min. :0.00000 Min. :0.000 Min. :0.000
## 1st Qu.:0.0000 1st Qu.:0.00000 1st Qu.:0.140 1st Qu.:0.000
## Median :0.0000 Median :0.00000 Median :0.240 Median :2.000
## Mean :0.0593 Mean :0.02731 Mean :0.218 Mean :1.631
## 3rd Qu.:0.1127 3rd Qu.:0.00000 3rd Qu.:0.300 3rd Qu.:3.000
## Max. :0.4020 Max. :0.25269 Max. :0.440 Max. :4.000
(a) Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.
set.seed(1)
inTrain <- sample(nrow(OJ), 800)
train.oj <- OJ[inTrain,]
test.oj <- OJ[-inTrain,]
(b) Fit a support vector classifier to the training data using cost=0.01, with Purchase as the response and the other variables as predictors. Use the summary() function to produce summary statistics, and describe the results obtained.
library(e1071)
svm.lin.oj <- svm(Purchase ~., kernel = "linear", data = train.oj, cost = 0.01)
summary(svm.lin.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
Based on the results of the support vector classifier, there are 435 support vectors out of 800 training points. Among those, 219 belong to level CH and remaining 216 belong to level MM.
(c) What are the training and test error rates?
train.pred <- predict(svm.lin.oj, train.oj)
table(train.oj$Purchase, train.pred)
## train.pred
## CH MM
## CH 420 65
## MM 75 240
(75 + 65)/(420 + 65 + 75 + 240)
## [1] 0.175
test.pred <- predict(svm.lin.oj, test.oj)
table(test.oj$Purchase, test.pred)
## test.pred
## CH MM
## CH 153 15
## MM 33 69
(15 + 33)/(153 + 15 + 33 + 69)
## [1] 0.1777778
Based on the output, the training error rate is 17.5% and test error rate is about 17.8%.
(d) 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", ranges = list(cost=c(0.001, 0.01, 0.1, 1, 5, 10)))
summary(tune.oj)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.1
##
## - best performance: 0.1725
##
## - Detailed performance results:
## cost error dispersion
## 1 1e-03 0.31250 0.04124790
## 2 1e-02 0.17625 0.02853482
## 3 1e-01 0.17250 0.03162278
## 4 1e+00 0.17500 0.02946278
## 5 5e+00 0.17250 0.03162278
## 6 1e+01 0.17375 0.03197764
tune.oj$best.parameters
## cost
## 3 0.1
Based on the tuning, the optimal cost is 0.1.
(e) Compute the training and test error rates using this new value for cost.
svm.new.oj <- svm(Purchase ~ ., kernel = "linear", data = train.oj, cost = tune.oj$best.parameters$cost)
train.pred2 <- predict(svm.new.oj, train.oj)
table(train.oj$Purchase, train.pred2)
## train.pred2
## CH MM
## CH 422 63
## MM 69 246
(63 + 69)/(422 + 63 + 69 + 246)
## [1] 0.165
test.pred2 <- predict(svm.new.oj, test.oj)
table(test.oj$Purchase, test.pred2)
## test.pred2
## CH MM
## CH 155 13
## MM 31 71
(13 + 31)/(155 + 13 + 31 + 71)
## [1] 0.162963
When using the best cost, both training and test error rates decreased to 16.5% and 16.3% respectively.
(f) Repeat parts (b) through (e) using a support vector machine with a radial kernel. Use the default value for gamma.
set.seed(1)
svm.rad.oj <- svm(Purchase ~ ., data = train.oj, kernel = "radial")
summary(svm.rad.oj)
##
## 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
train.pred3 <- predict(svm.rad.oj, train.oj)
table(train.oj$Purchase, train.pred3)
## train.pred3
## CH MM
## CH 441 44
## MM 77 238
(44 + 77)/(441 + 44 + 77 + 238)
## [1] 0.15125
test.pred3 = predict(svm.rad.oj, test.oj)
table(test.oj$Purchase, test.pred3)
## test.pred3
## CH MM
## CH 151 17
## MM 33 69
(17 + 33)/(151 + 17 + 33 + 69)
## [1] 0.1851852
Based on these results, the radial basis kernel with default gamma creates 373 support vectors, out of which, 188 belong to level CH and remaining 185 belong to level MM. The classifier has a training error of 15.1% and a test error of 18.5%. We now use cross validation to find optimal gamma.
set.seed(1)
tune.out.rad2 = tune(svm, Purchase ~ ., data = train.oj, kernel = "radial", ranges = list(cost = c(0.001, 0.01, 0.1, 1, 5, 100)))
summary(tune.out.rad2)
##
## 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 1e-03 0.39375 0.04007372
## 2 1e-02 0.39375 0.04007372
## 3 1e-01 0.18625 0.02853482
## 4 1e+00 0.17125 0.02128673
## 5 5e+00 0.18000 0.02220485
## 6 1e+02 0.21750 0.04048319
tune.out.rad2$best.parameters
## cost
## 4 1
svm.rad.oj2 <- svm(Purchase ~ ., kernel = "radial", data = train.oj, cost = tune.out.rad2$best.parameters$cost)
train.pred4 <- predict(svm.rad.oj2, train.oj)
table(train.oj$Purchase, train.pred4)
## train.pred4
## CH MM
## CH 441 44
## MM 77 238
(44+77)/(441+44+77+238)
## [1] 0.15125
test.pred4 <- predict(svm.rad.oj2, test.oj)
table(test.oj$Purchase, test.pred4)
## test.pred4
## CH MM
## CH 151 17
## MM 33 69
(17+33)/(151+17+33+69)
## [1] 0.1851852
Based on the tuning, the training error rate decreased to 14.9%, and the test error rate decreased to 17.8%. This test error rate is still higher than the linear kernel.
(g) Repeat parts (b) through (e) using a support vector machine with a polynomial kernel. Set degree=2.
svm.poly.oj <- svm(Purchase ~ ., kernel = "poly", data = train.oj, degree=2)
summary(svm.poly.oj)
##
## Call:
## svm(formula = Purchase ~ ., data = train.oj, kernel = "poly", 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
train.pred5 <- predict(svm.poly.oj, train.oj)
table(train.oj$Purchase, train.pred5)
## train.pred5
## CH MM
## CH 449 36
## MM 110 205
(36+110)/(449+36+110+205)
## [1] 0.1825
test.pred5 <- predict(svm.poly.oj, test.oj)
table(test.oj$Purchase, test.pred5)
## test.pred5
## CH MM
## CH 153 15
## MM 45 57
(15+45)/(153+15+45+57)
## [1] 0.2222222
Based on these results, the polynomial basis kernel with degree 2, creates 447 support vectors, out of which, 225 belong to level CH and remaining 222 belong to level MM. The classifier has a training error of 18.25% and a test error of 22.2%.
set.seed(1)
tune.out.poly2 <- tune(svm, Purchase ~ ., data = train.oj, kernel = "poly", degree = 2, ranges = list(cost=c(0.001, 0.01, 0.1, 1, 5, 10)))
summary(tune.out.poly2)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 10
##
## - best performance: 0.18125
##
## - Detailed performance results:
## cost error dispersion
## 1 1e-03 0.39375 0.04007372
## 2 1e-02 0.39125 0.04210189
## 3 1e-01 0.32125 0.05001736
## 4 1e+00 0.20250 0.04116363
## 5 5e+00 0.18250 0.03496029
## 6 1e+01 0.18125 0.02779513
tune.out.poly2$best.parameters
## cost
## 6 10
svm.poly.oj2 = svm(Purchase~., data = train.oj, kernel = "poly", cost = 10, degree = 2)
summary(svm.poly.oj2)
##
## Call:
## svm(formula = Purchase ~ ., data = train.oj, kernel = "poly", cost = 10,
## degree = 2)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: polynomial
## cost: 10
## degree: 2
## coef.0: 0
##
## Number of Support Vectors: 340
##
## ( 171 169 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
train.pred6 <- predict(svm.poly.oj2, train.oj)
table(train.oj$Purchase, train.pred6)
## train.pred6
## CH MM
## CH 447 38
## MM 82 233
(38+82)/(447+38+82+233)
## [1] 0.15
test.pred6 <- predict(svm.poly.oj2, test.oj)
table(test.oj$Purchase, test.pred6)
## test.pred6
## CH MM
## CH 154 14
## MM 37 65
(14+37)/(154+14+37+65)
## [1] 0.1888889
(h) Overall, which approach seems to give the best results on this data?
Based on the results, the tuned Poly SVM with Cost = 10 resulted in the lowest Test Error Rate for the OJ data set.
detach(OJ)