R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

  1. 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.
  1. 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)
  1. Plot the observations, colored according to their class labels. Your plot should display X1 on the x-axis, and X2 on the yaxis.
plot(x1[y==0],x2[y==0],col="orange",xlab="X1",ylab="X2")
points(x1[y==1],x2[y==1],col="blue")

  1. Fit a logistic regression model to the data, using X1 and X2 as predictors.
dat=data.frame(x1 = x1, x2 = x2, y = as.factor(y))
glm.fit=glm(y~.,data=dat,family='binomial')
  1. 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.prob=predict(glm.fit,newdata=dat,type='response')
glm.pred=ifelse(glm.prob>0.5,1,0)
plot(dat$x1,dat$x2,col=glm.pred+2)

  1. 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).
glm.fit2=glm(y~poly(x1,2)+poly(x2,2),data=dat,family='binomial')
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(glm.fit2)
## 
## Call:
## glm(formula = y ~ poly(x1, 2) + poly(x2, 2), family = "binomial", 
##     data = dat)
## 
## Deviance Residuals: 
##        Min          1Q      Median          3Q         Max  
## -1.079e-03  -2.000e-08  -2.000e-08   2.000e-08   1.297e-03  
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)
## (Intercept)     -94.48    2963.78  -0.032    0.975
## poly(x1, 2)1   3442.52  104411.28   0.033    0.974
## poly(x1, 2)2  30110.74  858421.66   0.035    0.972
## poly(x2, 2)1    162.82   26961.99   0.006    0.995
## poly(x2, 2)2 -31383.76  895267.48  -0.035    0.972
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 6.9218e+02  on 499  degrees of freedom
## Residual deviance: 4.2881e-06  on 495  degrees of freedom
## AIC: 10
## 
## Number of Fisher Scoring iterations: 25
  1. 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.
glm.prob2=predict(glm.fit2,newdata=dat,type='response')
glm.pred2=ifelse(glm.prob2>0.5,1,0)
plot(dat$x1,dat$x2,col=glm.pred2+2)

  1. 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(y~. ,data=dat,kernel='linear',cost=0.01)
plot(svm.fit,dat)

  1. 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.
svm2=svm(y~.,data=dat,kernel='radial',gamma=1)
plot(svm2,data=dat)

(i) Comment on your results.

Nonlinear logistic regression and SVM both have positive impacts on nonlinear borders, although linear logistic regression has a poor effect and SVM linear kernels perform well with little cost.

  1. In this problem, you will use support vector approaches in order topredict whether a given car gets high or low gas mileage based on the Auto data set.
library(ISLR2)
attach(Auto)
  1. 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.
mileage.median=median(Auto$mpg)
Auto$mb=ifelse(Auto$mpg > mileage.median, 1, 0)
  1. 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. Note you will need to fit the classifier without the gas mileage variable to produce sensible results.
cost.grid=c(0.001,0.1,1,100)
set.seed(10)
tune.res=tune(svm,mpg~.-mpg,data=Auto,kernel='linear',ranges=list(cost=cost.grid))
summary(tune.res)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##   0.1
## 
## - best performance: 8.83414 
## 
## - Detailed performance results:
##    cost     error dispersion
## 1 1e-03 15.416960   6.251221
## 2 1e-01  8.834140   3.304068
## 3 1e+00  9.264512   2.274424
## 4 1e+02 11.123907   2.629105
  1. 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.
cost.grid=c(0.01,0.1,1,10,100)
gamma.grid=c(0.5,1,2,3,4)
tune.radial=tune(svm,mpg~.,data=Auto,kernel='radial',ranges=list(cost=cost.grid,gamma=gamma.grid))
summary(tune.radial)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost gamma
##    10   0.5
## 
## - best performance: 8.686356 
## 
## - Detailed performance results:
##     cost gamma     error dispersion
## 1  1e-02   0.5 53.325115  12.414531
## 2  1e-01   0.5 20.919402   7.853170
## 3  1e+00   0.5  9.394259   4.609831
## 4  1e+01   0.5  8.686356   4.127520
## 5  1e+02   0.5  8.705095   4.135771
## 6  1e-02   1.0 60.234057  13.066132
## 7  1e-01   1.0 46.768446  11.624934
## 8  1e+00   1.0 21.442077   7.796923
## 9  1e+01   1.0 20.164075   7.236470
## 10 1e+02   1.0 20.164075   7.236470
## 11 1e-02   2.0 61.820180  13.218297
## 12 1e-01   2.0 60.087904  13.081665
## 13 1e+00   2.0 50.593280  12.365866
## 14 1e+01   2.0 49.141492  12.044690
## 15 1e+02   2.0 49.141492  12.044690
## 16 1e-02   3.0 61.934458  13.229217
## 17 1e-01   3.0 61.158247  13.179454
## 18 1e+00   3.0 56.944766  13.145218
## 19 1e+01   3.0 56.312950  12.970294
## 20 1e+02   3.0 56.312950  12.970294
## 21 1e-02   4.0 61.955459  13.230305
## 22 1e-01   4.0 61.348654  13.183297
## 23 1e+00   4.0 58.034856  13.146808
## 24 1e+01   4.0 57.541527  12.969261
## 25 1e+02   4.0 57.541527  12.969261
deg.grid=c(1,2,3,4)
tune.degree=tune(svm,mpg~.,data=Auto,kernel='polynomial', ranges=list(cost=cost.grid,degree=deg.grid))
summary(tune.degree)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost degree
##    10      1
## 
## - best performance: 9.120906 
## 
## - Detailed performance results:
##     cost degree     error dispersion
## 1  1e-02      1 57.180757  18.561415
## 2  1e-01      1 28.487180  13.292568
## 3  1e+00      1 11.091162   7.207581
## 4  1e+01      1  9.120906   5.764613
## 5  1e+02      1  9.396378   5.141107
## 6  1e-02      2 62.081660  19.392508
## 7  1e-01      2 62.000430  19.401574
## 8  1e+00      2 61.225429  19.449762
## 9  1e+01      2 57.941389  21.646240
## 10 1e+02      2 44.066723  18.599758
## 11 1e-02      3 62.086397  19.391186
## 12 1e-01      3 62.045129  19.382222
## 13 1e+00      3 61.633682  19.291911
## 14 1e+01      3 57.701690  18.418796
## 15 1e+02      3 38.281435  13.471314
## 16 1e-02      4 62.090922  19.392180
## 17 1e-01      4 62.090359  19.392154
## 18 1e+00      4 62.084728  19.391890
## 19 1e+01      4 62.028565  19.389316
## 20 1e+02      4 61.482455  19.371276
  1. 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.
plot(tune.radial$best.model,Auto,mpg~horsepower)
  1. This problem involves the OJ data set which is part of the ISLR2 package.
  1. Create a training set containing a random sample of 800 observations, and a test set containing the remaining observations.
set.seed(13)
train = sample(dim(OJ)[1], 800)
OJ.train = OJ[train, ]
OJ.test = OJ[-train, ]
  1. Fit a support vector classifier to the training data usingcost = 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 = svm(Purchase ~ ., 
                 kernel = "linear", 
                 data = OJ.train, 
                 cost = 0.01)
summary(svm.linear)
## 
## 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:  439
## 
##  ( 219 220 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
  1. What are the training and test error rates?
train.pred = predict(svm.linear, 
                     OJ.train)
table(OJ.train$Purchase, 
      train.pred)
##     train.pred
##       CH  MM
##   CH 441  57
##   MM  78 224
(75+65)/800
## [1] 0.175
test.pred = predict(svm.linear,
                    OJ.test)
table(OJ.test$Purchase, 
      test.pred)
##     test.pred
##       CH  MM
##   CH 139  16
##   MM  25  90
(33+15)/800
## [1] 0.06
  1. Use the tune() function to select an optimal cost. Consider values in the range 0.01 to 10.
set.seed(83)
tune.out = tune(svm, 
                Purchase ~ ., 
                data = OJ.train, 
                kernel = "linear", 
                ranges = list(cost = 10^seq(-2, 1, by = 0.25)))
summary(tune.out)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##      cost
##  5.623413
## 
## - best performance: 0.16625 
## 
## - Detailed performance results:
##           cost   error dispersion
## 1   0.01000000 0.18000 0.04794383
## 2   0.01778279 0.17500 0.04330127
## 3   0.03162278 0.17625 0.03884174
## 4   0.05623413 0.17250 0.04322101
## 5   0.10000000 0.17250 0.04281744
## 6   0.17782794 0.17000 0.03917553
## 7   0.31622777 0.17250 0.04073969
## 8   0.56234133 0.17250 0.04281744
## 9   1.00000000 0.17125 0.04332131
## 10  1.77827941 0.17000 0.04090979
## 11  3.16227766 0.16875 0.04379958
## 12  5.62341325 0.16625 0.04528076
## 13 10.00000000 0.17125 0.04489571
  1. Compute the training and test error rates using this new value for cost.
svm.linear = svm(Purchase ~ ., 
                 kernel = "linear", 
                 data = OJ.train, 
                 cost = tune.out$best.parameters$cost)
train.pred = predict(svm.linear, 
                     OJ.train)

table(OJ.train$Purchase, 
      train.pred)
##     train.pred
##       CH  MM
##   CH 441  57
##   MM  72 230
(72 + 57)/(441 + 230 + 72 + 57)
## [1] 0.16125
test.pred = predict(svm.linear, 
                    OJ.test)
table(OJ.test$Purchase, 
      test.pred)
##     test.pred
##       CH  MM
##   CH 134  21
##   MM  23  92
(23 + 21)/(134 + 92 + 23 + 21)
## [1] 0.162963
  1. Repeat parts (b) through (e) using a support vector machine with a radial kernel. Use the default value for gamma.
set.seed(8)
svm.radial = svm(Purchase ~ .,
                 data = OJ.train, 
                 kernel = "radial")
summary(svm.radial)
## 
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "radial")
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  1 
## 
## Number of Support Vectors:  379
## 
##  ( 191 188 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
train.pred = predict(svm.radial, 
                     OJ.train)
table(OJ.train$Purchase,
      train.pred)
##     train.pred
##       CH  MM
##   CH 453  45
##   MM  84 218
(129)/(800)
## [1] 0.16125
test.pred = predict(svm.radial, 
                    OJ.test)
table(OJ.test$Purchase, 
      test.pred)
##     test.pred
##       CH  MM
##   CH 141  14
##   MM  25  90
(39)/(270)
## [1] 0.1444444
set.seed(11)
tune.out = tune(svm, Purchase ~ ., 
                data = OJ.train, 
                kernel = "radial",
                ranges = list(cost = 10^seq(-2, 1, by = 0.25)))
summary(tune.out)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##      cost
##  1.778279
## 
## - best performance: 0.18625 
## 
## - Detailed performance results:
##           cost   error dispersion
## 1   0.01000000 0.37750 0.06892024
## 2   0.01778279 0.37750 0.06892024
## 3   0.03162278 0.37375 0.07440738
## 4   0.05623413 0.22250 0.05027701
## 5   0.10000000 0.19500 0.04216370
## 6   0.17782794 0.19375 0.03691676
## 7   0.31622777 0.19250 0.04830459
## 8   0.56234133 0.18625 0.04427267
## 9   1.00000000 0.18750 0.04487637
## 10  1.77827941 0.18625 0.04619178
## 11  3.16227766 0.18875 0.04693746
## 12  5.62341325 0.19000 0.04706674
## 13 10.00000000 0.18875 0.04466309
svm.radial = svm(Purchase ~ ., 
                 data = OJ.train, 
                 kernel = "radial", 
                 cost = tune.out$best.parameters$cost)
train.pred = predict(svm.radial, 
                     OJ.train)
table(OJ.train$Purchase, 
      train.pred)
##     train.pred
##       CH  MM
##   CH 454  44
##   MM  82 220
(127)/(800)
## [1] 0.15875
test.pred = predict(svm.radial, 
                    OJ.test)
table(OJ.test$Purchase, 
      test.pred)
##     test.pred
##       CH  MM
##   CH 140  15
##   MM  25  90
(40/270)
## [1] 0.1481481
  1. Repeat parts (b) through (e) using a support vector machine with a polynomial kernel. Set degree = 2.
set.seed(10)
svm.poly = svm(Purchase ~ ., 
               data = OJ.train, 
               kernel = "poly", 
               degree = 2)
summary(svm.poly)
## 
## Call:
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "poly", degree = 2)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  polynomial 
##        cost:  1 
##      degree:  2 
##      coef.0:  0 
## 
## Number of Support Vectors:  446
## 
##  ( 219 227 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  CH MM
train.pred = predict(svm.poly, 
                     OJ.train)
table(OJ.train$Purchase, 
      train.pred)
##     train.pred
##       CH  MM
##   CH 461  37
##   MM 116 186
(153/800)
## [1] 0.19125
test.pred = predict(svm.poly, 
                    OJ.test)
table(OJ.test$Purchase, 
      test.pred)
##     test.pred
##       CH  MM
##   CH 144  11
##   MM  38  77
(49/270)
## [1] 0.1814815
set.seed(19)
tune.out = tune(svm, 
                Purchase ~ ., 
                data = OJ.train, 
                kernel = "poly", 
                degree = 2,
                ranges = list(cost = 10^seq(-2, 1, by = 0.25)))
summary(tune.out)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  cost
##    10
## 
## - best performance: 0.18 
## 
## - Detailed performance results:
##           cost   error dispersion
## 1   0.01000000 0.37750 0.03670453
## 2   0.01778279 0.35750 0.04609772
## 3   0.03162278 0.34625 0.04966904
## 4   0.05623413 0.32875 0.05622685
## 5   0.10000000 0.30625 0.06620937
## 6   0.17782794 0.25625 0.06515207
## 7   0.31622777 0.21125 0.07487258
## 8   0.56234133 0.21125 0.06958458
## 9   1.00000000 0.21375 0.06755913
## 10  1.77827941 0.19750 0.07090682
## 11  3.16227766 0.18625 0.07370408
## 12  5.62341325 0.18125 0.05929271
## 13 10.00000000 0.18000 0.05041494
svm.poly = svm(Purchase ~ ., 
               data = OJ.train, 
               kernel = "poly", 
               degree = 2, 
               cost = tune.out$best.parameters$cost)
train.pred = predict(svm.poly,
                     OJ.train)
table(OJ.train$Purchase, 
      train.pred)
##     train.pred
##       CH  MM
##   CH 456  42
##   MM  85 217
(127/800)
## [1] 0.15875
test.pred = predict(svm.poly, 
                    OJ.test)
table(OJ.test$Purchase, 
      test.pred)
##     test.pred
##       CH  MM
##   CH 140  15
##   MM  30  85
(45/270)
## [1] 0.1666667
  1. Overall, which approach seems to give the best results on this data? Radical is the best option with fewer number of errors results reported.