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.
x1 <- runif(500) - 0.5
x2 <- runif(500) - 0.5
y <- 1 * (x1^2 - x2^2 > 0)
data <- data.frame(x1 = x1, x2 = x2, y = as.factor(y))
library(ggplot2)
data_plot <- ggplot(data,
aes(
x = x1,
y = x2,
color = y
)) +
geom_point()
data_plot
set.seed(1)
data_glm <- glm(y ~., data, family = 'binomial')
summary(data_glm)
##
## Call:
## glm(formula = y ~ ., family = "binomial", data = data)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.03364 0.08977 0.375 0.708
## x1 0.09568 0.31439 0.304 0.761
## x2 0.47105 0.31456 1.498 0.134
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 692.95 on 499 degrees of freedom
## Residual deviance: 690.59 on 497 degrees of freedom
## AIC: 696.59
##
## Number of Fisher Scoring iterations: 3
set.seed(1)
glm_preds <- predict(data_glm, type = "response")
glm_probs <- ifelse(glm_preds > 0.5, 1, 0)
data$glm_preds <- as.factor(glm_probs)
data_coef <- coef(data_glm)
ggplot(
data,
aes(
x = x1,
y = x2,
color = glm_probs
)) +
geom_point() +
geom_abline(intercept = -data_coef[1] / data_coef[3], slope = -data_coef[2] / data_coef[3])
set.seed(1)
square_glm <- glm(y ~ poly(x1, 2) + x1*x2, data, family = "binomial")
summary(square_glm)
##
## Call:
## glm(formula = y ~ poly(x1, 2) + x1 * x2, family = "binomial",
## data = data)
##
## Coefficients: (1 not defined because of singularities)
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.2293 0.1179 1.945 0.0518 .
## poly(x1, 2)1 2.0903 2.8383 0.736 0.4614
## poly(x1, 2)2 37.4311 3.4377 10.888 <2e-16 ***
## x1 NA NA NA NA
## x2 0.1757 0.3896 0.451 0.6520
## x1:x2 -0.2041 1.5816 -0.129 0.8973
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 692.95 on 499 degrees of freedom
## Residual deviance: 492.23 on 495 degrees of freedom
## AIC: 502.23
##
## Number of Fisher Scoring iterations: 5
set.seed(1)
square_glm_preds <- predict(square_glm, type = "response")
square_glm_probs <- ifelse(square_glm_preds > 0.5, 1, 0)
data$square_glm_preds <- as.factor(square_glm_probs)
square_coef <- coef(square_glm)
ggplot(
data,
aes(
x = x1,
y = x2,
color = square_glm_probs
)) +
geom_point()
library(e1071)
##
## Attaching package: 'e1071'
## The following object is masked from 'package:ggplot2':
##
## element
set.seed(1)
svm_data <- svm(y ~ x1 + x2,
data = data,
kernel = "linear")
svm_data
##
## Call:
## svm(formula = y ~ x1 + x2, data = data, kernel = "linear")
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: linear
## cost: 1
##
## Number of Support Vectors: 479
svm_pred <- predict(svm_data, data)
data$svm_pred <- svm_pred
ggplot(data,
aes(
x = x1,
y = x2,
color = svm_pred
)) +
geom_point()
set.seed(1)
nonlinear_svm_data <- svm(y ~ x1 + x2,
data = data,
kernel = "radial")
nonlinear_svm_data
##
## Call:
## svm(formula = y ~ x1 + x2, data = data, kernel = "radial")
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 1
##
## Number of Support Vectors: 160
nonlinear_svm_pred <- predict(nonlinear_svm_data, data)
data$svm_pred <- nonlinear_svm_pred
ggplot(data,
aes(
x = x1,
y = x2,
color = nonlinear_svm_pred
)) +
geom_point()
When the support machince vector has a kernal of linear it creates a linear boundary like the logistic regression model. However, when manipulating the predictor variables (by squaring the value) or using radial as the kernel for the support machine vector has a boundary line more adapt to the data.
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(ISLR2)
attach(Auto)
## The following object is masked from package:ggplot2:
##
## mpg
median_mpg <- median(Auto$mpg)
Auto$target <- ifelse(Auto$mpg > median_mpg, 1, 0)
Auto$target <- as.factor(Auto$target)
set.seed(1)
updated_auto <- Auto[, -c(1,9)]
auto_svm <- svm(target ~ ., data = updated_auto, kernel = "linear")
summary(auto_svm)
##
## Call:
## svm(formula = target ~ ., data = updated_auto, kernel = "linear")
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: linear
## cost: 1
##
## Number of Support Vectors: 88
##
## ( 43 45 )
##
##
## Number of Classes: 2
##
## Levels:
## 0 1
set.seed(1)
tune.out <- tune(svm, target ~ ., data = updated_auto, kernel = "linear",
ranges = list(cost = c(0.001, 0.01, 0.1, 1, 5, 10)))
summary(tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 1
##
## - best performance: 0.08435897
##
## - Detailed performance results:
## cost error dispersion
## 1 1e-03 0.13525641 0.05661708
## 2 1e-02 0.08923077 0.04698309
## 3 1e-01 0.09185897 0.04393409
## 4 1e+00 0.08435897 0.03662670
## 5 5e+00 0.08948718 0.03898410
## 6 1e+01 0.08948718 0.03898410
From the tune out summary, it can be seen that when cost is equal to 1, it has the lowest cross-validation error of 0.08435897.
set.seed(1)
radial_auto_svm <- svm(target ~ ., data = updated_auto, kernel = "radial")
summary(radial_auto_svm)
##
## Call:
## svm(formula = target ~ ., data = updated_auto, kernel = "radial")
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 1
##
## Number of Support Vectors: 102
##
## ( 51 51 )
##
##
## Number of Classes: 2
##
## Levels:
## 0 1
set.seed(1)
radial_tune.out <- tune(svm, target ~ ., data = updated_auto, kernel = "radial",
ranges = list(cost = c(0.001, 0.01, 0.1, 1, 5, 10),
gamma = c(0.5, 1, 2, 3, 4)))
summary(radial_tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost gamma
## 1 1
##
## - best performance: 0.06634615
##
## - Detailed performance results:
## cost gamma error dispersion
## 1 1e-03 0.5 0.55115385 0.04366593
## 2 1e-02 0.5 0.55115385 0.04366593
## 3 1e-01 0.5 0.08666667 0.04687413
## 4 1e+00 0.5 0.06884615 0.02963114
## 5 5e+00 0.5 0.07903846 0.03051601
## 6 1e+01 0.5 0.08923077 0.02732003
## 7 1e-03 1.0 0.55115385 0.04366593
## 8 1e-02 1.0 0.55115385 0.04366593
## 9 1e-01 1.0 0.08673077 0.04535158
## 10 1e+00 1.0 0.06634615 0.03244101
## 11 5e+00 1.0 0.08916667 0.02708952
## 12 1e+01 1.0 0.08923077 0.02732003
## 13 1e-03 2.0 0.55115385 0.04366593
## 14 1e-02 2.0 0.55115385 0.04366593
## 15 1e-01 2.0 0.14282051 0.07578262
## 16 1e+00 2.0 0.08673077 0.04371113
## 17 5e+00 2.0 0.09942308 0.04881948
## 18 1e+01 2.0 0.09429487 0.05387705
## 19 1e-03 3.0 0.55115385 0.04366593
## 20 1e-02 3.0 0.55115385 0.04366593
## 21 1e-01 3.0 0.31878205 0.13973969
## 22 1e+00 3.0 0.08416667 0.04171436
## 23 5e+00 3.0 0.09179487 0.05416218
## 24 1e+01 3.0 0.09179487 0.05416218
## 25 1e-03 4.0 0.55115385 0.04366593
## 26 1e-02 4.0 0.55115385 0.04366593
## 27 1e-01 4.0 0.51788462 0.06842176
## 28 1e+00 4.0 0.08923077 0.03843042
## 29 5e+00 4.0 0.09173077 0.05268076
## 30 1e+01 4.0 0.09179487 0.05139393
From the radial tune out summary, it can be seen that when cost is equal to 1 and gamma is equal to 1, it has the lowest cross-validation error of 0.08435897.
set.seed(1)
poly_auto_svm <- svm(target ~ ., data = updated_auto, kernel = "polynomial")
summary(poly_auto_svm)
##
## Call:
## svm(formula = target ~ ., data = updated_auto, kernel = "polynomial")
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: polynomial
## cost: 1
## degree: 3
## coef.0: 0
##
## Number of Support Vectors: 151
##
## ( 74 77 )
##
##
## Number of Classes: 2
##
## Levels:
## 0 1
set.seed(1)
poly_tune.out <- tune(svm, target ~ ., data = updated_auto, kernel = "polynomial",
ranges = list(cost = c(0.001, 0.01, 0.1, 1, 5, 10),
gamma = c(0.5, 1, 2, 3, 4)))
summary(poly_tune.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost gamma
## 0.001 4
##
## - best performance: 0.07666667
##
## - Detailed performance results:
## cost gamma error dispersion
## 1 1e-03 0.5 0.25794872 0.09147506
## 2 1e-02 0.5 0.10217949 0.03617516
## 3 1e-01 0.5 0.08173077 0.03779968
## 4 1e+00 0.5 0.08685897 0.03861230
## 5 5e+00 0.5 0.08673077 0.03641774
## 6 1e+01 0.5 0.09698718 0.05063701
## 7 1e-03 1.0 0.09185897 0.03993389
## 8 1e-02 1.0 0.08679487 0.04533461
## 9 1e-01 1.0 0.08179487 0.03391477
## 10 1e+00 1.0 0.09442308 0.04647330
## 11 5e+00 1.0 0.11467949 0.04965956
## 12 1e+01 1.0 0.12487179 0.04202595
## 13 1e-03 2.0 0.08679487 0.04533461
## 14 1e-02 2.0 0.07923077 0.03084659
## 15 1e-01 2.0 0.08935897 0.04210128
## 16 1e+00 2.0 0.11467949 0.04965956
## 17 5e+00 2.0 0.11743590 0.03875223
## 18 1e+01 2.0 0.12512821 0.04455710
## 19 1e-03 3.0 0.08435897 0.04544023
## 20 1e-02 3.0 0.08429487 0.04194489
## 21 1e-01 3.0 0.10198718 0.05484627
## 22 1e+00 3.0 0.10974359 0.04369624
## 23 5e+00 3.0 0.13006410 0.03044171
## 24 1e+01 3.0 0.12506410 0.03318569
## 25 1e-03 4.0 0.07666667 0.03209996
## 26 1e-02 4.0 0.08673077 0.03641774
## 27 1e-01 4.0 0.11467949 0.04965956
## 28 1e+00 4.0 0.12512821 0.04455710
## 29 5e+00 4.0 0.12756410 0.03410915
## 30 1e+01 4.0 0.12756410 0.03410915
From the radial tune out summary, it can be seen that when cost is equal to 0.001 and gamma is equal to 4, it has the lowest cross-validation error of 0.08435897.
linear_svm <- svm(target ~ ., data = updated_auto, kernel = "linear", cost = tune.out$best.parameters$cost)
radial_svm <- svm(target ~ ., data = updated_auto, kernel = "radial", cost = radial_tune.out$best.parameters$cost, gamma = radial_tune.out$best.parameters$gamma)
poly_svm <- svm(target ~ ., data = updated_auto, kernel = "polynomial", cost = poly_tune.out$best.parameters$cost, gamma = poly_tune.out$best.parameters$gamma)
plot(linear_svm, updated_auto, displacement ~ weight)
plot(radial_svm, updated_auto, displacement ~ weight)
plot(poly_svm, updated_auto, displacement ~ weight)
This problem involves the OJ data set which is part of the ISLR2 package.
attach(OJ)
set.seed(1)
trainindex <- sample(1:nrow(OJ), 800)
train <- OJ[trainindex, ]
test<- OJ[-trainindex, ]
set.seed(1)
oj_svm <- svm(Purchase ~., data = train, kernel = "linear", cost = 0.01)
summary(oj_svm)
##
## Call:
## svm(formula = Purchase ~ ., data = 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
Based on the SVM summary, we can see that there are two classes and the binary response will be either CH or MM. Also 435 support vectors were used to create the model. Of those 435, 219 are in on class and 216 are in the other.
train_pred <- predict(oj_svm, train)
table(train_pred, train$Purchase)
##
## train_pred CH MM
## CH 420 75
## MM 65 240
((65 + 75) / 800)* 100
## [1] 17.5
test_pred <- predict(oj_svm, test)
table(test_pred, test$Purchase)
##
## test_pred CH MM
## CH 153 33
## MM 15 69
((15 + 33) / 270) * 100
## [1] 17.77778
oj_tune <- tune(svm, Purchase ~., data = train, kernel = "linear",
ranges = list(cost = c(0.01, 0.1, 1, 5, 10)))
summary(oj_tune)
##
## 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 0.01 0.17625 0.02853482
## 2 0.10 0.17250 0.03162278
## 3 1.00 0.17500 0.02946278
## 4 5.00 0.17250 0.03162278
## 5 10.00 0.17375 0.03197764
updated_oj_svm <- svm(Purchase ~., data = train, kernel = "linear", cost = oj_tune$best.parameters$cost)
updated_train_pred <- predict(updated_oj_svm, train)
table(updated_train_pred, train$Purchase)
##
## updated_train_pred CH MM
## CH 422 69
## MM 63 246
((62 + 69) / 800) * 100
## [1] 16.375
updated_train_pred <- predict(updated_oj_svm, test)
table(updated_train_pred, test$Purchase)
##
## updated_train_pred CH MM
## CH 155 31
## MM 13 71
((12 + 28) / 270) * 100
## [1] 14.81481
set.seed(1)
oj_svm_radial <- svm(Purchase ~., data = train, kernel = "radial", cost = 0.01)
summary(oj_svm_radial)
##
## Call:
## svm(formula = Purchase ~ ., data = train, 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
Based on the SVM summary, we can see that there are two classes and the binary response will be either CH or MM. Also 634 support vectors were used to create the model. Of those 435, 319 are in on class and 315 are in the other.
radial_train_pred <- predict(oj_svm_radial, train)
table(radial_train_pred, train$Purchase)
##
## radial_train_pred CH MM
## CH 485 315
## MM 0 0
((315 + 0) / 800)* 100
## [1] 39.375
radial_test_pred <- predict(oj_svm_radial, test)
table(radial_test_pred, test$Purchase)
##
## radial_test_pred CH MM
## CH 168 102
## MM 0 0
((102 + 0) / 270) * 100
## [1] 37.77778
radial_oj_tune <- tune(svm, Purchase ~., data = train, kernel = "radial",
ranges = list(cost = c(0.01, 0.1, 1, 5, 10)))
summary(radial_oj_tune)
##
## 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 0.01 0.39375 0.04007372
## 2 0.10 0.18625 0.02853482
## 3 1.00 0.17125 0.02128673
## 4 5.00 0.18000 0.02220485
## 5 10.00 0.18625 0.02853482
updated_radial_oj_svm <- svm(Purchase ~., data = train, kernel = "radial", cost = radial_oj_tune$best.parameters$cost)
updated_radial_train_pred <- predict(updated_radial_oj_svm, train)
table(updated_radial_train_pred, train$Purchase)
##
## updated_radial_train_pred CH MM
## CH 441 77
## MM 44 238
((44 + 77) / 800) * 100
## [1] 15.125
updated_radial_test_pred <- predict(updated_radial_oj_svm, test)
table(updated_radial_test_pred, test$Purchase)
##
## updated_radial_test_pred CH MM
## CH 151 33
## MM 17 69
((17 + 33) / 270) * 100
## [1] 18.51852
set.seed(1)
oj_svm_poly <- svm(Purchase ~., data = train, kernel = "polynomial", degree = 2, cost = 0.01)
summary(oj_svm_poly)
##
## Call:
## svm(formula = Purchase ~ ., data = train, kernel = "polynomial",
## degree = 2, cost = 0.01)
##
##
## 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
Based on the SVM summary, we can see that there are two classes and the binary response will be either CH or MM. Also 636 support vectors were used to create the model. Of those 435, 321 are in on class and 315 are in the other.
poly_train_pred <- predict(oj_svm_poly, train)
table(poly_train_pred, train$Purchase)
##
## poly_train_pred CH MM
## CH 484 297
## MM 1 18
((297 + 1) / 800)* 100
## [1] 37.25
poly_test_pred <- predict(oj_svm_poly, test)
table(poly_test_pred, test$Purchase)
##
## poly_test_pred CH MM
## CH 167 98
## MM 1 4
((98 + 1) / 270) * 100
## [1] 36.66667
poly_oj_tune <- tune(svm, Purchase ~., data = train, kernel = "polynomial", degree = 2,
ranges = list(cost = c(0.01, 0.1, 1, 5, 10)))
summary(poly_oj_tune)
##
## 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 0.01 0.39125 0.04210189
## 2 0.10 0.32125 0.05001736
## 3 1.00 0.20250 0.04116363
## 4 5.00 0.18250 0.03496029
## 5 10.00 0.18125 0.02779513
updated_poly_oj_svm <- svm(Purchase ~., data = train, kernel = "polynomial", cost = poly_oj_tune$best.parameters$cost)
updated_poly_train_pred <- predict(updated_poly_oj_svm, train)
table(updated_poly_train_pred, train$Purchase)
##
## updated_poly_train_pred CH MM
## CH 446 75
## MM 39 240
((75 + 39) / 800) * 100
## [1] 14.25
updated_poly_test_pred <- predict(updated_poly_oj_svm, test)
table(updated_poly_test_pred, test$Purchase)
##
## updated_poly_test_pred CH MM
## CH 155 42
## MM 13 60
((13 + 42) / 270) * 100
## [1] 20.37037
| Model | Error Rate (%) |
|---|---|
| Linear SVM Train | 16.375 |
| Linear SVM Test | 14.81481 |
| Radial SVM Train | 15.125 |
| Radial SVM Test | 18.51852 |
| Polynomial SVM Train | 14.25 |
| Polynomial SVM Test | 20.37037 |
Of all the models (after using tune), the model that performed the best was the Polynomial SVM.