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.
library(e1071)
(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(5)
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="red",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.
log_fit<-glm(y~x1+x2,family="binomial")
summary(log_fit)
##
## Call:
## glm(formula = y ~ x1 + x2, family = "binomial")
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.200 -1.161 -1.131 1.190 1.223
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.03150 0.08949 -0.352 0.725
## x1 -0.06176 0.30506 -0.202 0.840
## x2 -0.11509 0.31086 -0.370 0.711
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 693.02 on 499 degrees of freedom
## Residual deviance: 692.85 on 497 degrees of freedom
## AIC: 698.85
##
## 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.
dat<-data.frame(x1=x1,x2=x2,y=as.factor(y))
log_prob<-predict(log_fit,dat,type="response")
log_pred<-ifelse(log_prob>0.50,1,0)
data_pos<-dat[log_pred == 1, ]
data_neg<-dat[log_pred == 0, ]
plot(data_pos$x1,data_pos$x2,col="blue",xlab="X1",ylab="X2")
points(data_neg$x1,data_neg$x2,col="red")
(e) Now fit a logistic regression model to the data using non-linear functions of X1 and X2 as predictors (e.g. X21 , X1×X2, log(X2), and so forth).
log_fit2<-glm(y~poly(x1,2)+poly(x2,2),data=dat,family="binomial")
summary(log_fit2)
##
## Call:
## glm(formula = y ~ poly(x1, 2) + poly(x2, 2), family = "binomial",
## data = dat)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.003739 0.000000 0.000000 0.000000 0.003504
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 252.0 3439.0 0.073 0.942
## poly(x1, 2)1 2792.7 40700.9 0.069 0.945
## poly(x1, 2)2 100456.5 1343613.6 0.075 0.940
## poly(x2, 2)1 -792.9 16782.0 -0.047 0.962
## poly(x2, 2)2 -99176.6 1325929.2 -0.075 0.940
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 6.9302e+02 on 499 degrees of freedom
## Residual deviance: 2.8367e-05 on 495 degrees of freedom
## AIC: 10
##
## 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 which the predicted class labels are obviously non-linear.
log_prob2<-predict(log_fit2,dat,type="response")
log_pred2<-ifelse(log_prob2>0.50,1,0)
data_pos2<-dat[log_pred2 == 1, ]
data_neg2<-dat[log_pred2 == 0, ]
plot(data_pos2$x1,data_pos2$x2,col="blue",xlab="X1",ylab="X2")
points(data_neg2$x1,data_neg2$x2,col="red")
(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.
svm_fit<-svm(y~.,data=dat,kernel="linear",cost=0.01,scale=FALSE)
plot(svm_fit,dat)
(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(y~.,data=dat,kernel="radial",gamma=1)
plot(svm_fit2,dat)
(i) Comment on your results.
The results of this experiment demonstrate the ability to use support vector machines (SVMs) with nonlinear kernels and logistic regression with non-linear functions of X1 and X2 for identifying non linear models. It appears that a SVM with a nonlinear kernel and logistic regression with non-linear functions of X1 and X2 are both equally capable of finding non-linear decision boundaries. A SVM with a linear kernel and a logistic regression without functions applied to the predictors both perform very poorly when applied to non linear models.
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.
Auto$mpglevel<-as.factor(ifelse(Auto$mpg>median(Auto$mpg),1,0))
str(Auto)
## 'data.frame': 392 obs. of 10 variables:
## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : num 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : num 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : num 3504 3693 3436 3433 3449 ...
## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : num 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : num 1 1 1 1 1 1 1 1 1 1 ...
## $ name : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
## $ 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.
From the list below, we can see that a cost equal to one would yield the lowest error rate of 0.01026. A cost equal to 0.01 has the highest error rate of 0.07654.
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
(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.
For a polynomial kernel, the lowest error rate of 0.51301 is yielded
with a degree of 2 and a cost of 10.
set.seed(1)
tune_out2<-tune(svm,mpglevel~.,data=Auto,kernel="polynomial",ranges=list(cost=c(0.1,1,5,10),degree=c(2, 3, 4)))
summary(tune_out2)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost degree
## 10 2
##
## - best performance: 0.5130128
##
## - Detailed performance results:
## cost degree error dispersion
## 1 0.1 2 0.5511538 0.04366593
## 2 1.0 2 0.5511538 0.04366593
## 3 5.0 2 0.5511538 0.04366593
## 4 10.0 2 0.5130128 0.08963366
## 5 0.1 3 0.5511538 0.04366593
## 6 1.0 3 0.5511538 0.04366593
## 7 5.0 3 0.5511538 0.04366593
## 8 10.0 3 0.5511538 0.04366593
## 9 0.1 4 0.5511538 0.04366593
## 10 1.0 4 0.5511538 0.04366593
## 11 5.0 4 0.5511538 0.04366593
## 12 10.0 4 0.5511538 0.04366593
For a radial kernel, the lowest error rate of 0.02558 is yielded with a gamma of 0.01 and a cost of 10.
set.seed(1)
tune_out3<-tune(svm,mpglevel~.,data=Auto,kernel="radial",ranges=list(cost=c(0.1,1,5,10),gamma=c(0.01,0.1,1,5,10,100)))
summary(tune_out3)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost gamma
## 10 0.01
##
## - best performance: 0.02557692
##
## - Detailed performance results:
## cost gamma error dispersion
## 1 0.1 1e-02 0.08929487 0.04382379
## 2 1.0 1e-02 0.07403846 0.03522110
## 3 5.0 1e-02 0.04852564 0.03303346
## 4 10.0 1e-02 0.02557692 0.02093679
## 5 0.1 1e-01 0.07903846 0.03874545
## 6 1.0 1e-01 0.05371795 0.03525162
## 7 5.0 1e-01 0.02820513 0.03299190
## 8 10.0 1e-01 0.03076923 0.03375798
## 9 0.1 1e+00 0.55115385 0.04366593
## 10 1.0 1e+00 0.06384615 0.04375618
## 11 5.0 1e+00 0.05884615 0.04020934
## 12 10.0 1e+00 0.05884615 0.04020934
## 13 0.1 5e+00 0.55115385 0.04366593
## 14 1.0 5e+00 0.49493590 0.04724924
## 15 5.0 5e+00 0.48217949 0.05470903
## 16 10.0 5e+00 0.48217949 0.05470903
## 17 0.1 1e+01 0.55115385 0.04366593
## 18 1.0 1e+01 0.51794872 0.05063697
## 19 5.0 1e+01 0.51794872 0.04917316
## 20 10.0 1e+01 0.51794872 0.04917316
## 21 0.1 1e+02 0.55115385 0.04366593
## 22 1.0 1e+02 0.55115385 0.04366593
## 23 5.0 1e+02 0.55115385 0.04366593
## 24 10.0 1e+02 0.55115385 0.04366593
(d) Make some plots to back up your assertions in (b) and (c).
svm_linear<-svm(mpglevel~.,data=Auto,kernel="linear",cost=1)
svm_poly<-svm(mpglevel~.,data=Auto,kernel="polynomial",cost=100,degree=2)
svm_radial<-svm(mpglevel~.,data=Auto,kernel="radial",cost=10,gamma=0.01)
plotpairs = function(fit) {
for (name in names(Auto)[!(names(Auto) %in% c("mpg", "mpglevel", "name"))]) {
plot(fit, Auto, as.formula(paste("mpg~", name, sep = "")))
}
}
plotpairs(svm_linear)
plotpairs(svm_poly)
plotpairs(svm_radial)
detach(Auto)
This problem involves the OJ data set which is
part of the ISLR package.
library(ISLR)
attach(OJ)
str(OJ)
## 'data.frame': 1070 obs. of 18 variables:
## $ Purchase : Factor w/ 2 levels "CH","MM": 1 1 1 2 1 1 1 1 1 1 ...
## $ WeekofPurchase: num 237 239 245 227 228 230 232 234 235 238 ...
## $ StoreID : num 1 1 1 1 7 7 7 7 7 7 ...
## $ PriceCH : num 1.75 1.75 1.86 1.69 1.69 1.69 1.69 1.75 1.75 1.75 ...
## $ PriceMM : num 1.99 1.99 2.09 1.69 1.69 1.99 1.99 1.99 1.99 1.99 ...
## $ DiscCH : num 0 0 0.17 0 0 0 0 0 0 0 ...
## $ DiscMM : num 0 0.3 0 0 0 0 0.4 0.4 0.4 0.4 ...
## $ SpecialCH : num 0 0 0 0 0 0 1 1 0 0 ...
## $ SpecialMM : num 0 1 0 0 0 1 1 0 0 0 ...
## $ LoyalCH : num 0.5 0.6 0.68 0.4 0.957 ...
## $ SalePriceMM : num 1.99 1.69 2.09 1.69 1.69 1.99 1.59 1.59 1.59 1.59 ...
## $ SalePriceCH : num 1.75 1.75 1.69 1.69 1.69 1.69 1.69 1.75 1.75 1.75 ...
## $ PriceDiff : num 0.24 -0.06 0.4 0 0 0.3 -0.1 -0.16 -0.16 -0.16 ...
## $ Store7 : Factor w/ 2 levels "No","Yes": 1 1 1 1 2 2 2 2 2 2 ...
## $ PctDiscMM : num 0 0.151 0 0 0 ...
## $ PctDiscCH : num 0 0 0.0914 0 0 ...
## $ ListPriceDiff : num 0.24 0.24 0.23 0 0 0.3 0.3 0.24 0.24 0.24 ...
## $ STORE : num 1 1 1 1 0 0 0 0 0 0 ...
(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(dim(OJ)[1], 800)
OJ_train<-OJ[train, ]
OJ_test<-OJ[-train, ]
(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.
svm_linear_oj<-svm(Purchase~.,data=OJ_train,kernel="linear",cost=0.01)
summary(svm_linear_oj)
##
## Call:
## svm(formula = Purchase ~ ., data = OJ_train, kernel = "linear", cost = 0.01)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: linear
## cost: 0.01
##
## Number of Support Vectors: 435
##
## ( 219 216 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
(c) What are the training and test error rates?
From the confusion matrix below, the training error rate is shown to be \((75+65)/800\) or 17.5%.
train_pred<-predict(svm_linear_oj, OJ_train)
table(OJ_train$Purchase,train_pred)
## train_pred
## CH MM
## CH 420 65
## MM 75 240
From the confusion matrix below, the test error rate is shown to be \((33+15)/270\) or 17.778%.
test_pred<-predict(svm_linear_oj, OJ_test)
table(OJ_test$Purchase,test_pred)
## test_pred
## CH MM
## CH 153 15
## MM 33 69
(d) Use the tune() function to select an optimal
cost. Consider values in the range 0.01 to 10.
The tune() function identifies the optimal cost as
3.16228 with an error rate of 0.16875.
set.seed(1)
tune_out_oj<-tune(svm,Purchase~.,data=OJ_train,kernel="linear",ranges=list(cost=10^seq(-2,1,by=0.25)))
summary(tune_out_oj)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 3.162278
##
## - best performance: 0.16875
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01000000 0.17625 0.02853482
## 2 0.01778279 0.17625 0.03143004
## 3 0.03162278 0.17125 0.02829041
## 4 0.05623413 0.17625 0.02853482
## 5 0.10000000 0.17250 0.03162278
## 6 0.17782794 0.17125 0.02829041
## 7 0.31622777 0.17125 0.02889757
## 8 0.56234133 0.17125 0.02703521
## 9 1.00000000 0.17500 0.02946278
## 10 1.77827941 0.17375 0.02729087
## 11 3.16227766 0.16875 0.03019037
## 12 5.62341325 0.17375 0.03304563
## 13 10.00000000 0.17375 0.03197764
(e) Compute the training and test error rates using this new
value for cost.
svm_linear_oj2<-svm(Purchase~.,kernel="linear",data=OJ_train,cost=tune_out_oj$best.parameter$cost)
From the confusion matrix below, the training error rate using the
optimal cost value is shown to be \((70+62)/800\) or 16.5%. This is an
improvement on the original training error rate of 17.5% yielded when
cost was equal to 0.01.
train_pred2<-predict(svm_linear_oj2,OJ_train)
table(OJ_train$Purchase,train_pred2)
## train_pred2
## CH MM
## CH 423 62
## MM 70 245
From the confusion matrix below, the test error rate using the
optimal cost value is shown to be \((29+12)/270\) or 15.185%. This is an
improvement on the original test error rate of 17.778% yielded when
cost was equal to 0.01.
test_pred2<-predict(svm_linear_oj2,OJ_test)
table(OJ_test$Purchase,test_pred2)
## test_pred2
## CH MM
## CH 156 12
## MM 29 73
(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_radial_oj<-svm(Purchase~.,kernel="radial",data=OJ_train)
summary(svm_radial_oj)
##
## Call:
## svm(formula = Purchase ~ ., data = OJ_train, kernel = "radial")
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 1
##
## Number of Support Vectors: 373
##
## ( 188 185 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
From the confusion matrix below, the training error rate using the
default value for gamma is shown to be \((77+44)/800\) or 15.125%. This is a slight
improvement on the test error rate of 15.185% yielded by a linear kernel
with an optimal cost value.
train_pred3<-predict(svm_radial_oj,OJ_train)
table(OJ_train$Purchase,train_pred3)
## train_pred3
## CH MM
## CH 441 44
## MM 77 238
From the confusion matrix below, the test error rate using the
default value for gamma is shown to be \((33+17)/270\) or 18.519%. Even though there
was an improvement in training error rate, the test error rate yielded
with a radial kernel is the highest seen so far.
test_pred3<-predict(svm_radial_oj,OJ_test)
table(OJ_test$Purchase,test_pred3)
## test_pred3
## CH MM
## CH 151 17
## MM 33 69
Using the tune() function to identify the
optimal values for cost with a radial kernel and default
value for gamma.
The tune() function identifies the optimal cost for a
radial kernel as 0.56234 with an error rate of 0.16875.
set.seed(1)
tune_out_oj2<-tune(svm,Purchase~.,data=OJ_train,kernel="radial",ranges=list(cost=10^seq(-2,1,by=0.25)))
summary(tune_out_oj2)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.5623413
##
## - best performance: 0.16875
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01000000 0.39375 0.04007372
## 2 0.01778279 0.39375 0.04007372
## 3 0.03162278 0.35750 0.05927806
## 4 0.05623413 0.19500 0.02443813
## 5 0.10000000 0.18625 0.02853482
## 6 0.17782794 0.18250 0.03291403
## 7 0.31622777 0.17875 0.03230175
## 8 0.56234133 0.16875 0.02651650
## 9 1.00000000 0.17125 0.02128673
## 10 1.77827941 0.17625 0.02079162
## 11 3.16227766 0.17750 0.02266912
## 12 5.62341325 0.18000 0.02220485
## 13 10.00000000 0.18625 0.02853482
svm_radial_oj2<-svm(Purchase~.,kernel="radial",data=OJ_train,cost=tune_out_oj2$best.parameter$cost)
summary(svm_radial_oj2)
##
## Call:
## svm(formula = Purchase ~ ., data = OJ_train, kernel = "radial", cost = tune_out_oj2$best.parameter$cost)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 0.5623413
##
## Number of Support Vectors: 397
##
## ( 200 197 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
From the confusion matrix below, the training error rate using the
optimal cost value with radial kernel is shown to be \((71+48)/800\) or 14.875%.
train_pred4<-predict(svm_radial_oj2,OJ_train)
table(OJ_train$Purchase,train_pred4)
## train_pred4
## CH MM
## CH 437 48
## MM 71 244
From the confusion matrix below, the test error rate using the
optimal cost value with radial kernel is shown to be \((30+18)/270\) or 17.778%. While this does
not improve on the test error rate of 15.185% yielded by a linear kernel
with an optimal value for cost, it is an improvement on the
test error rate of 18.519% yielded by a radial kernel with no
consideration for the optimal value of cost.
test_pred4<-predict(svm_radial_oj2,OJ_test)
table(OJ_test$Purchase,test_pred4)
## test_pred4
## CH MM
## CH 150 18
## MM 30 72
(g) Repeat parts (b) through (e) using a support vector
machine with a polynomial kernel. Set
degree=2.
svm_poly_oj<-svm(Purchase~.,kernel="polynomial",data=OJ_train,degree=2)
summary(svm_poly_oj)
##
## Call:
## svm(formula = Purchase ~ ., data = OJ_train, kernel = "polynomial",
## degree = 2)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: polynomial
## cost: 1
## degree: 2
## coef.0: 0
##
## Number of Support Vectors: 447
##
## ( 225 222 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
From the confusion matrix below, the training error rate with a polynomial kernel and degree equal to two is shown to be \((110+36)/800\) or 18.25%.
train_pred5<-predict(svm_poly_oj,OJ_train)
table(OJ_train$Purchase,train_pred5)
## train_pred5
## CH MM
## CH 449 36
## MM 110 205
From the confusion matrix below, the test error rate with a polynomial kernel and degree equal to two is shown to be \((45+15)/270\) or 22.223%. This is the highest test and training error rate seen so far.
test_pred5<-predict(svm_poly_oj,OJ_test)
table(OJ_test$Purchase,test_pred5)
## test_pred5
## CH MM
## CH 153 15
## MM 45 57
Using the tune() function to identify the
optimal values for cost with a polynomial kernel and
degree=2.
The tune() function identifies the optimal cost for a
polynomial kernel as 3.16228 with an error rate of 0.1775.
set.seed(1)
tune_out_oj3<-tune(svm,Purchase~.,data=OJ_train,kernel="polynomial",degree=2,ranges=list(cost=10^seq(-2,1,by=0.25)))
summary(tune_out_oj3)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 3.162278
##
## - best performance: 0.1775
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01000000 0.39125 0.04210189
## 2 0.01778279 0.37125 0.03537988
## 3 0.03162278 0.36500 0.03476109
## 4 0.05623413 0.33750 0.04714045
## 5 0.10000000 0.32125 0.05001736
## 6 0.17782794 0.24500 0.04758034
## 7 0.31622777 0.19875 0.03972562
## 8 0.56234133 0.20500 0.03961621
## 9 1.00000000 0.20250 0.04116363
## 10 1.77827941 0.18500 0.04199868
## 11 3.16227766 0.17750 0.03670453
## 12 5.62341325 0.18375 0.03064696
## 13 10.00000000 0.18125 0.02779513
svm_poly_oj2<-svm(Purchase~.,kernel="polynomial",degree=2,data=OJ_train,cost=tune_out_oj3$best.parameter$cost)
summary(svm_poly_oj2)
##
## Call:
## svm(formula = Purchase ~ ., data = OJ_train, kernel = "polynomial",
## degree = 2, cost = tune_out_oj3$best.parameter$cost)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: polynomial
## cost: 3.162278
## degree: 2
## coef.0: 0
##
## Number of Support Vectors: 385
##
## ( 197 188 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
From the confusion matrix below, the training error rate with a
polynomial kernel, degree equal to two, and optimal value for
cost is shown to be \((90+34)/800\) or 15.5%.
train_pred6<-predict(svm_poly_oj2,OJ_train)
table(OJ_train$Purchase,train_pred6)
## train_pred6
## CH MM
## CH 451 34
## MM 90 225
From the confusion matrix below, the test error rate with a
polynomial kernel, degree equal to two, and optimal value for
cost is shown to be \((41+14)/270\) or 20.37%. Using the
tune() function to identify the optimal value for
cost with a polynomial kernel does appear to reduce both
the training and test error rates.
test_pred6<-predict(svm_poly_oj2,OJ_test)
table(OJ_test$Purchase,test_pred6)
## test_pred6
## CH MM
## CH 154 14
## MM 41 61
(h) Overall, which approach seems to give the best results on this data?
Overall a SVM using a linear kernel with an optimal cost identified
by the tune() function of 3.16228 yields the lowest test
error rate of 15.185%.