Chapter 09 (page 368): 5, 7, 8
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:
> x1=runif(500)-0.5
> x2=runif(500)-0.5
> y=1*(x1^2-x2^2 > 0)
set.seed(1)
x1 = runif(500)-.5
x2 = runif(500)-.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, x2, xlab = "x1", ylab = "x2", col = (2 - y), pch = (4 - y))
(c) Fit a logistic regression model to the data, using X1 and X2 as predictors.
fit.l = glm(y ~ x1 + x2, family = 'binomial')
fit.l
##
## Call: glm(formula = y ~ x1 + x2, family = "binomial")
##
## Coefficients:
## (Intercept) x1 x2
## -0.087260 0.196199 -0.002854
##
## Degrees of Freedom: 499 Total (i.e. Null); 497 Residual
## Null Deviance: 692.2
## Residual Deviance: 691.8 AIC: 697.8
summary(fit.l)
##
## 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
(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.
probs.fit.l = predict(fit.l, newdata = data.frame(x1 = x1, x2 = x2), type = "response")
pred.fit.l = 1 * (probs.fit.l > 0.5)
plot(x1, x2, col = (pred.fit.l + 1), xlab = "x1", ylab = "x2")
(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).
fit.sqr = glm(y~x1+x2+I(x1^2)+I(x2^2)+I(x1*x2),family='binomial')
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(fit.sqr)
##
## Call:
## glm(formula = y ~ x1 + x2 + I(x1^2) + I(x2^2) + I(x1 * x2), family = "binomial")
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -8.240e-04 -2.000e-08 -2.000e-08 2.000e-08 1.163e-03
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -10.16 713.54 -0.014 0.989
## x1 42.10 15492.58 0.003 0.998
## x2 -66.81 14788.95 -0.005 0.996
## I(x1^2) 16757.98 519013.02 0.032 0.974
## I(x2^2) -16671.65 508668.89 -0.033 0.974
## I(x1 * x2) -206.38 41802.81 -0.005 0.996
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 6.9218e+02 on 499 degrees of freedom
## Residual deviance: 3.5810e-06 on 494 degrees of freedom
## AIC: 12
##
## Number of Fisher Scoring iterations: 25
(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 whichthe predicted class labels are obviously non-linear.
probs.fit.sqr = predict(fit.sqr, newdata = data.frame(x1 = x1, x2 = x2), type = "response")
pred.fit.sqr = 1 * (probs.fit.sqr > 0.5)
plot(x1, x2, col = (pred.fit.sqr + 1), xlab = "x1", ylab = "x2")
(g) Fit a support vector classifier to the data with \(X_{1}\) and \(X_{2}\) as predictors. Obtain a class prediction for each training observation. Plot the observations, colored according to the predicted class labels.
library(e1071)
data5 = data.frame(x1=x1, x2=x2, y=y)
tune.l = tune(svm,y~.,data=data5, kernel = 'linear', ranges = list(cost=c(.001,.01,.1,1,5,10)))
summary(tune.l)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.01
##
## - best performance: 0.4307734
##
## - Detailed performance results:
## cost error dispersion
## 1 1e-03 0.4324039 0.06767065
## 2 1e-02 0.4307734 0.07168132
## 3 1e-01 0.4379836 0.05571381
## 4 1e+00 0.4382838 0.05517638
## 5 5e+00 0.4382778 0.05518640
## 6 1e+01 0.4382901 0.05519332
#cost = .01 best.
svm.lin = svm(y~., data=data5, kernel="linear", cost=.01)
probs.fit.lin = predict(svm.lin, newdata = data5, type = "response")
pred.fit.lin = 1 * (probs.fit.lin > 0.5)
plot(x1, x2, col = (pred.fit.lin + 1), xlab = "x1", ylab = "x2")
(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.
tune.poly = tune(svm,y~.,data=data5, kernel = 'polynomial', ranges=list(cost=c(0.01,0.1,1,5,10,100),
degree = c(2,3,4)))
summary(tune.poly)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost degree
## 1 2
##
## - best performance: 0.09615208
##
## - Detailed performance results:
## cost degree error dispersion
## 1 1e-02 2 0.11073155 0.01424197
## 2 1e-01 2 0.09631148 0.01634807
## 3 1e+00 2 0.09615208 0.01746802
## 4 5e+00 2 0.09668382 0.01805566
## 5 1e+01 2 0.09671622 0.01803286
## 6 1e+02 2 0.09676759 0.01814352
## 7 1e-02 3 0.42461393 0.06465351
## 8 1e-01 3 0.40171091 0.08568122
## 9 1e+00 3 0.41489566 0.09163072
## 10 5e+00 3 0.41609853 0.09198800
## 11 1e+01 3 0.41609990 0.09199430
## 12 1e+02 3 0.41634232 0.09205323
## 13 1e-02 4 0.13410731 0.01476438
## 14 1e-01 4 0.15150868 0.03087332
## 15 1e+00 4 0.16751402 0.04036840
## 16 5e+00 4 0.16967131 0.04103773
## 17 1e+01 4 0.16975945 0.04089869
## 18 1e+02 4 0.17006533 0.04100641
#cost = .01, degree = 2 best.
svm.poly = svm(y~., data=data5, kernel="polynomial", cost=.01, degree = 2)
probs.fit.poly = predict(svm.poly, newdata = data5, type = "response")
pred.fit.poly = 1 * (probs.fit.poly > 0.5)
plot(x1, x2, col = (pred.fit.poly + 1), xlab = "x1", ylab = "x2")
(i) Comment on your results. In the linear SVM, I was unable to see the decision boundry between the differen classes. However, in the polynomial SVM, the decision boundry can be seen. There are two distinct boundries which are non-linear.
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)
## Warning: package 'ISLR' was built under R version 3.6.3
attach(Auto)
(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 mileag ebelow the median.
Auto$mpg1 = with(Auto,factor(mpg > median(mpg)))
(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 errwith different values of this parameter. Comment on your results.
library(e1071)
set.seed(1)
tune.out=tune(svm,mpg1~.,data=Auto,kernel ="linear",ranges=list(cost=c(0.001, 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-03 0.09442308 0.04519425
## 2 1e-02 0.07653846 0.03617137
## 3 1e-01 0.04596154 0.03378238
## 4 1e+00 0.01025641 0.01792836
## 5 5e+00 0.02051282 0.02648194
## 6 1e+01 0.02051282 0.02648194
## 7 1e+02 0.03076923 0.03151981
It seems that cost of 1 performs the best with an error of 0.01019231 and dispersion of 0.01786828.
(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.poly = tune(svm,mpg1~., 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.rad = tune(
svm, mpg1~., 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.01019231
##
## - Detailed performance results:
## cost gamma error dispersion
## 1 1e-02 1e-02 0.60192308 0.06346118
## 2 1e-01 1e-02 0.08653846 0.06488131
## 3 1e+00 1e-02 0.07134615 0.04769894
## 4 5e+00 1e-02 0.04846154 0.03905899
## 5 1e+01 1e-02 0.02294872 0.02534336
## 6 1e+02 1e-02 0.01019231 0.01786828
## 7 1e-02 1e-01 0.23974359 0.06938360
## 8 1e-01 1e-01 0.07891026 0.05147085
## 9 1e+00 1e-01 0.05096154 0.03995812
## 10 5e+00 1e-01 0.02301282 0.03069264
## 11 1e+01 1e-01 0.02294872 0.02807826
## 12 1e+02 1e-01 0.03064103 0.02651089
## 13 1e-02 1e+00 0.60192308 0.06346118
## 14 1e-01 1e+00 0.60192308 0.06346118
## 15 1e+00 1e+00 0.06365385 0.04845299
## 16 5e+00 1e+00 0.06121795 0.04387918
## 17 1e+01 1e+00 0.06121795 0.04387918
## 18 1e+02 1e+00 0.06121795 0.04387918
## 19 1e-02 5e+00 0.60192308 0.06346118
## 20 1e-01 5e+00 0.60192308 0.06346118
## 21 1e+00 5e+00 0.52814103 0.08413728
## 22 5e+00 5e+00 0.52814103 0.08238251
## 23 1e+01 5e+00 0.52814103 0.08238251
## 24 1e+02 5e+00 0.52814103 0.08238251
## 25 1e-02 1e+01 0.60192308 0.06346118
## 26 1e-01 1e+01 0.60192308 0.06346118
## 27 1e+00 1e+01 0.55615385 0.07526477
## 28 5e+00 1e+01 0.55358974 0.07343728
## 29 1e+01 1e+01 0.55358974 0.07343728
## 30 1e+02 1e+01 0.55358974 0.07343728
## 31 1e-02 1e+02 0.60192308 0.06346118
## 32 1e-01 1e+02 0.60192308 0.06346118
## 33 1e+00 1e+02 0.60192308 0.06346118
## 34 5e+00 1e+02 0.60192308 0.06346118
## 35 1e+01 1e+02 0.60192308 0.06346118
## 36 1e+02 1e+02 0.60192308 0.06346118
The best performing polynomial is the one with cost = 100 and degree = 2. The best performing radial SVM is the one with cost = 100 and gamma = .01.
(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 findout more, type ?plot.svm.
svm.lin = svm(mpg1~., data=Auto, kernel="linear", cost=1)
svm.poly = svm(mpg1~., data=Auto, kernel="polynomial", cost=100, degree=2)
svm.rad = svm(mpg1~., data=Auto, kernel="radial", cost=100, gamma=0.01)
plotpairs = function(fit) {
for (name in names(Auto)[!(names(Auto) %in% c("mpg", "mpg1", "name"))]) {
plot(fit, Auto, as.formula(paste("mpg~", name, sep = "")))
}
}
plotpairs(svm.lin)
plotpairs(svm.rad)
plotpairs(svm.poly)
This problem involves the OJ data set which is part of the ISLR package.
attach(OJ)
(a) Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.
set.seed(1)
train=sample(1:nrow(OJ), 800)
train.OJ=OJ[train,]
test=(-train)
test.OJ=OJ[test,]
purchase.test=OJ$Purchase[test]
(b) Fit a support vector classifier to the training data using cost=0.01, with Purchase as the response and the other variablesas predictors. Use the summary() function to produce summary statistics, and describe the results obtained.
svm.lin.oj = svm(Purchase~., data=train.OJ, kernel="linear", cost=.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
(c) What are the training and test error rates?
pred.train.oj = predict(svm.lin.oj, train.OJ)
table(pred.train.oj,train.OJ$Purchase)
##
## pred.train.oj CH MM
## CH 420 75
## MM 65 240
train.error = (75+65)/(420+75+65+240)
train.error
## [1] 0.175
pred.test.oj = predict(svm.lin.oj, test.OJ)
table(pred.test.oj,purchase.test)
## purchase.test
## pred.test.oj CH MM
## CH 153 33
## MM 15 69
test.error = (33+15)/(153+33+15+69)
test.error
## [1] 0.1777778
Training error rate is .175. Test error rate is 0.1777778.
(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,.05,0.1,1,2,3,4,5,6,7,8,9,10)))
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 1e-03 0.31250 0.04124790
## 2 1e-02 0.17625 0.02853482
## 3 5e-02 0.17625 0.02853482
## 4 1e-01 0.17250 0.03162278
## 5 1e+00 0.17500 0.02946278
## 6 2e+00 0.17250 0.02874698
## 7 3e+00 0.16875 0.03019037
## 8 4e+00 0.17000 0.02958040
## 9 5e+00 0.17250 0.03162278
## 10 6e+00 0.17500 0.03333333
## 11 7e+00 0.17500 0.03333333
## 12 8e+00 0.17375 0.03197764
## 13 9e+00 0.17375 0.03197764
## 14 1e+01 0.17375 0.03197764
The SVM fit with a cost = 4 has the lowest error - 0.16750.
(e) Compute the training and test error rates using this new value for cost.
svm.lin.tuned.oj = svm(Purchase~., data=train.OJ, kernel="linear", cost=4)
pred.train.tune.oj = predict(svm.lin.tuned.oj, train.OJ)
table(pred.train.tune.oj,train.OJ$Purchase)
##
## pred.train.tune.oj CH MM
## CH 423 70
## MM 62 245
tune.train.error = (70+62)/(423+70+62+245)
tune.train.error
## [1] 0.165
pred.test.tune.oj = predict(svm.lin.tuned.oj, test.OJ)
table(pred.test.tune.oj,purchase.test)
## purchase.test
## pred.test.tune.oj CH MM
## CH 155 29
## MM 13 73
tune.test.error = (29+13)/(155+29+13+73)
tune.test.error
## [1] 0.1555556
The training error rate for the SVM with optimal cost (4) is .165. The training error rate for this model is .1555556.
(f) Repeat parts (b) through (e) using a support vector machine with a radial kernel. Use the default value for gamma.
svm.rad.oj = svm(Purchase~., data=train.OJ, kernel="radial", cost=.01)
summary(svm.rad.oj)
##
## Call:
## svm(formula = Purchase ~ ., data = train.OJ, kernel = "radial",
## cost = 0.01)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 0.01
##
## Number of Support Vectors: 634
##
## ( 319 315 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
pred.train.oj.rad = predict(svm.rad.oj, train.OJ)
table(pred.train.oj.rad,train.OJ$Purchase)
##
## pred.train.oj.rad CH MM
## CH 485 315
## MM 0 0
train.error = (315)/(485+315)
train.error
## [1] 0.39375
pred.test.oj.rad = predict(svm.rad.oj, test.OJ)
table(pred.test.oj.rad,purchase.test)
## purchase.test
## pred.test.oj.rad CH MM
## CH 168 102
## MM 0 0
test.error = (102)/(102+168)
test.error
## [1] 0.3777778
tune.oj.rad=tune(svm,Purchase~.,data=train.OJ,kernel ="radial",ranges=list(cost=c(0.001, 0.01,.05,0.1,1,2,3,4,5,6,7,8,9,10)))
summary(tune.oj.rad)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 1
##
## - best performance: 0.17875
##
## - Detailed performance results:
## cost error dispersion
## 1 1e-03 0.39375 0.04723243
## 2 1e-02 0.39375 0.04723243
## 3 5e-02 0.20625 0.03131937
## 4 1e-01 0.19000 0.03270236
## 5 1e+00 0.17875 0.03537988
## 6 2e+00 0.18125 0.03596391
## 7 3e+00 0.18625 0.03143004
## 8 4e+00 0.18875 0.03408018
## 9 5e+00 0.19125 0.03537988
## 10 6e+00 0.19500 0.04133199
## 11 7e+00 0.19500 0.03827895
## 12 8e+00 0.19625 0.04084609
## 13 9e+00 0.20000 0.04124790
## 14 1e+01 0.19875 0.04143687
#cost = 1 is best
svm.rad.tuned.oj = svm(Purchase~., data=train.OJ, kernel="radial", cost=1)
pred.train.tune.oj.rad = predict(svm.rad.tuned.oj, train.OJ)
table(pred.train.tune.oj.rad,train.OJ$Purchase)
##
## pred.train.tune.oj.rad CH MM
## CH 441 77
## MM 44 238
tune.train.error.rad = (77+44)/(441+77+44+238)
tune.train.error.rad
## [1] 0.15125
pred.test.tune.oj.rad = predict(svm.rad.tuned.oj, test.OJ)
table(pred.test.tune.oj.rad,purchase.test)
## purchase.test
## pred.test.tune.oj.rad CH MM
## CH 151 33
## MM 17 69
tune.test.error.rad = (33+17)/(151+33+17+69)
tune.test.error.rad
## [1] 0.1851852
Training error = 0.15125. Test error = 0.1851852. These are higher than the training and test error rates for the linear SVM.
(g) Repeat parts (b) through (e) using a support vector machine with a polynomial kernel. Set degree=2.
svm.poly.oj = svm(Purchase~., data=train.OJ, kernel="polynomial", cost=.01, degree =2)
summary(svm.poly.oj)
##
## Call:
## svm(formula = Purchase ~ ., data = train.OJ, 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: 636
##
## ( 321 315 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
pred.train.oj.poly = predict(svm.poly.oj, train.OJ)
table(pred.train.oj.poly,train.OJ$Purchase)
##
## pred.train.oj.poly CH MM
## CH 484 297
## MM 1 18
train.error = (297+1)/(484+297+1+18)
train.error
## [1] 0.3725
pred.test.oj.poly = predict(svm.poly.oj, test.OJ)
table(pred.test.oj.rad,purchase.test)
## purchase.test
## pred.test.oj.rad CH MM
## CH 168 102
## MM 0 0
test.error = (102)/(102+168)
test.error
## [1] 0.3777778
tune.oj.poly=tune(svm,Purchase~.,data=train.OJ,kernel ="polynomial",ranges=list(cost=c(0.001, 0.01,.05,0.1,1,2,3,4,5,6,7,8,9,10)), degree = 2)
summary(tune.oj.poly)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 10
##
## - best performance: 0.17875
##
## - Detailed performance results:
## cost error dispersion
## 1 1e-03 0.39375 0.06461693
## 2 1e-02 0.38875 0.06807114
## 3 5e-02 0.33875 0.06626179
## 4 1e-01 0.32250 0.05197489
## 5 1e+00 0.20875 0.04489571
## 6 2e+00 0.19500 0.03545341
## 7 3e+00 0.19250 0.03016160
## 8 4e+00 0.19125 0.03438447
## 9 5e+00 0.18625 0.03030516
## 10 6e+00 0.18625 0.03087272
## 11 7e+00 0.18375 0.02949223
## 12 8e+00 0.18500 0.02415229
## 13 9e+00 0.18125 0.02716334
## 14 1e+01 0.17875 0.02638523
#cost = 9 is best.
svm.poly.tuned.oj = svm(Purchase~., data=train.OJ, kernel="polynomial", cost=9, degree =2)
summary(svm.poly.tuned.oj)
##
## Call:
## svm(formula = Purchase ~ ., data = train.OJ, kernel = "polynomial",
## cost = 9, degree = 2)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: polynomial
## cost: 9
## degree: 2
## coef.0: 0
##
## Number of Support Vectors: 346
##
## ( 174 172 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
pred.train.tune.oj.poly = predict(svm.poly.tuned.oj, train.OJ)
table(pred.train.tune.oj.poly,train.OJ$Purchase)
##
## pred.train.tune.oj.poly CH MM
## CH 448 83
## MM 37 232
tune.train.error.poly = (83+37)/(448+83+37+232)
tune.train.error.poly
## [1] 0.15
pred.test.tune.oj.poly = predict(svm.poly.tuned.oj, test.OJ)
table(pred.test.tune.oj.poly,purchase.test)
## purchase.test
## pred.test.tune.oj.poly CH MM
## CH 154 36
## MM 14 66
tune.test.error.poly = (36+14)/(151+36+14+66)
tune.test.error.poly
## [1] 0.1872659
Training error rate = .15. Test error rate = .1872659.
(h) Overall, which approach seems to give the best results on this data?
Overall, the linear SVM with cost = 4 has the best test error rate of .1555556.