library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.4.3
## Warning: package 'ggplot2' was built under R version 4.4.3
## Warning: package 'tidyr' was built under R version 4.4.3
## Warning: package 'dplyr' was built under R version 4.4.3
## Warning: package 'lubridate' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.4.3
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
library(e1071) # svm()
## Warning: package 'e1071' was built under R version 4.4.3
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
library(ISLR2) # Auto dataset
## Warning: package 'ISLR2' was built under R version 4.4.2
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.4.3
##
## Attaching package: 'gridExtra'
##
## The following object is masked from 'package:dplyr':
##
## combine
RNGkind(kind = "Mersenne-Twister",
normal.kind = "Inversion",
sample.kind = "Rejection") # diff versions of R/RMD using different sampling types was annoying me
theme_set(theme_light())
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.
set.seed(2025)
x1=runif (500) -0.5
x2=runif (500) -0.5
y=1*( x1^2-x2^2 > 0)
df <- data.frame(x1, x2, y = factor(y))
plot(x1[y == 0], x2[y == 0], col = "darkblue", xlab = "X1", ylab = "X2", pch = 1)
points(x1[y == 1], x2[y == 1], col = "red", pch = 8)
(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")
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.06947 0.08980 0.774 0.439
## x1 0.04296 0.30613 0.140 0.888
## x2 -0.48914 0.31747 -1.541 0.123
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 692.64 on 499 degrees of freedom
## Residual deviance: 690.25 on 497 degrees of freedom
## AIC: 696.25
##
## Number of Fisher Scoring iterations: 3
data = data.frame(x1 = x1, x2 = x2, y = y)
lm.prob = predict(glm.fit, data, type = "response")
lm.pred = ifelse(lm.prob > 0.52, 1, 0)
data.pos = data[lm.pred == 1, ]
data.neg = data[lm.pred == 0, ]
plot(data.pos$x1, data.pos$x2, col = "darkblue", xlab = "X1", ylab = "X2", pch = 1)
points(data.neg$x1, data.neg$x2, col = "red", pch = 8)
library(ggplot2)
# Assemble your data
data <- data.frame(
x1 = x1,
x2 = x2,
y = factor(y) # make sure y is a factor for plotting
)
# Get predicted probabilities and classes
data$prob <- predict(glm.fit, data, type = "response")
data$pred <- factor(ifelse(data$prob > 0.52, 1, 0),
levels = c(0,1),
labels = c("Class 0","Class 1"))
# Plot
ggplot(data, aes(x = x1, y = x2)) +
geom_point(aes(color = pred, shape = y), size = 3) +
scale_color_manual(
values = c("Class 0" = "darkred", "Class 1" = "darkblue"),
name = "Predicted"
) +
scale_shape_manual(
values = c(8, 1),
name = "Actual"
) +
labs(
x = "X1",
y = "X2"
) +
theme_minimal(base_size = 14)
(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).
glm.fit.1 <- glm(y ~ x1 + x2 + I(x1^2) + I(x2^2), family = "binomial")
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(glm.fit.1)
##
## Call:
## glm(formula = y ~ x1 + x2 + I(x1^2) + I(x2^2), family = "binomial")
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 10.47 306.81 0.034 0.973
## x1 -144.33 6836.74 -0.021 0.983
## x2 -77.69 8566.29 -0.009 0.993
## I(x1^2) 35274.25 641636.19 0.055 0.956
## I(x2^2) -35474.51 636573.63 -0.056 0.956
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 6.9264e+02 on 499 degrees of freedom
## Residual deviance: 1.3831e-05 on 495 degrees of freedom
## AIC: 10
##
## Number of Fisher Scoring iterations: 25
# Predict probabilities and classes
data$prob <- predict(glm.fit.1, data, type = "response")
data$pred <- factor(ifelse(data$prob > 0.5, 1, 0),
levels = c(0, 1),
labels = c("Class 0", "Class 1"))
ggplot(data, aes(x = x1, y = x2)) +
geom_point(aes(color = pred, shape = y), size = 3) +
scale_color_manual(
values = c("Class 0" = "darkred", "Class 1" = "darkblue"),
name = "Predicted"
) +
scale_shape_manual(
values = c(8, 1),
name = "Actual"
) +
labs(
x = "X1",
y = "X2"
) +
theme_minimal(base_size = 14)
(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(as.factor(y) ~ x1 + x2, data = data, kernel = "linear", cost = 0.1)
data$svm_pred <- predict(svm.fit, data)
data$svm_pred <- factor(data$svm_pred, levels = c(0, 1), labels = c("Class 0", "Class 1"))
ggplot(data, aes(x = x1, y = x2)) +
geom_point(aes(color = svm_pred, shape = y), size = 3) +
scale_color_manual(
values = c("Class 0" = "darkred", "Class 1" = "darkblue"),
name = "Predicted"
) +
scale_shape_manual(
values = c(8, 1),
name = "Actual"
) +
labs(
x = "X1",
y = "X2"
) +
theme_minimal(base_size = 14)
svm.fit <- svm(as.factor(y) ~ x1 + x2, data = data, kernel = "radial", gamma = 1)
# Make predictions
data$svm_pred <- predict(svm.fit, data)
data$svm_pred <- factor(data$svm_pred, levels = c(0, 1), labels = c("Class 0", "Class 1"))
ggplot(data, aes(x = x1, y = x2)) +
geom_point(aes(color = svm_pred, shape = y), size = 3) +
scale_color_manual(
values = c("Class 0" = "darkred", "Class 1" = "darkblue"),
name = "Predicted"
) +
scale_shape_manual(
values = c(8, 1),
name = "Actual"
) +
labs(
x = "X1",
y = "X2"
) +
theme_minimal(base_size = 14)
This exercise shows that SVMs using non-linear kernel are very powerful in finding non-linear boundary. Logistic regression and SVMs with linear kernel could not achieve this. The trick with SVMs is tuning the hyperparameters correctly. This can get difficult when there are many number of features. It was possible here due to the simpler size of 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.
attach(Auto)
## The following object is masked from package:lubridate:
##
## origin
## The following object is masked from package:ggplot2:
##
## mpg
str(Auto)
## 'data.frame': 392 obs. of 9 variables:
## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : int 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : int 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : int 3504 3693 3436 3433 3449 4341 4354 4312 4425 3850 ...
## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : int 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : int 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 ...
## - attr(*, "na.action")= 'omit' Named int [1:5] 33 127 331 337 355
## ..- attr(*, "names")= chr [1:5] "33" "127" "331" "337" ...
Auto$mpg.new <- ifelse(mpg > median(mpg), 1, 0)
Auto$mpg <- NULL
Auto$mpg.new <- as.factor(Auto$mpg.new)
set.seed(1)
form = mpg.new ~ .
tuned.svm = tune.svm(form, data = Auto, kernel = "linear", gamma = seq(.01, .1, by = .01), cost = seq(.1, 1, by =.1))
summary(tuned.svm)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## gamma cost
## 0.01 0.4
##
## - best performance: 0.08173077
##
## - Detailed performance results:
## gamma cost error dispersion
## 1 0.01 0.1 0.08673077 0.04040897
## 2 0.02 0.1 0.08673077 0.04040897
## 3 0.03 0.1 0.08673077 0.04040897
## 4 0.04 0.1 0.08673077 0.04040897
## 5 0.05 0.1 0.08673077 0.04040897
## 6 0.06 0.1 0.08673077 0.04040897
## 7 0.07 0.1 0.08673077 0.04040897
## 8 0.08 0.1 0.08673077 0.04040897
## 9 0.09 0.1 0.08673077 0.04040897
## 10 0.10 0.1 0.08673077 0.04040897
## 11 0.01 0.2 0.08673077 0.04040897
## 12 0.02 0.2 0.08673077 0.04040897
## 13 0.03 0.2 0.08673077 0.04040897
## 14 0.04 0.2 0.08673077 0.04040897
## 15 0.05 0.2 0.08673077 0.04040897
## 16 0.06 0.2 0.08673077 0.04040897
## 17 0.07 0.2 0.08673077 0.04040897
## 18 0.08 0.2 0.08673077 0.04040897
## 19 0.09 0.2 0.08673077 0.04040897
## 20 0.10 0.2 0.08673077 0.04040897
## 21 0.01 0.3 0.08429487 0.04381671
## 22 0.02 0.3 0.08429487 0.04381671
## 23 0.03 0.3 0.08429487 0.04381671
## 24 0.04 0.3 0.08429487 0.04381671
## 25 0.05 0.3 0.08429487 0.04381671
## 26 0.06 0.3 0.08429487 0.04381671
## 27 0.07 0.3 0.08429487 0.04381671
## 28 0.08 0.3 0.08429487 0.04381671
## 29 0.09 0.3 0.08429487 0.04381671
## 30 0.10 0.3 0.08429487 0.04381671
## 31 0.01 0.4 0.08173077 0.03799005
## 32 0.02 0.4 0.08173077 0.03799005
## 33 0.03 0.4 0.08173077 0.03799005
## 34 0.04 0.4 0.08173077 0.03799005
## 35 0.05 0.4 0.08173077 0.03799005
## 36 0.06 0.4 0.08173077 0.03799005
## 37 0.07 0.4 0.08173077 0.03799005
## 38 0.08 0.4 0.08173077 0.03799005
## 39 0.09 0.4 0.08173077 0.03799005
## 40 0.10 0.4 0.08173077 0.03799005
## 41 0.01 0.5 0.08948718 0.05042300
## 42 0.02 0.5 0.08948718 0.05042300
## 43 0.03 0.5 0.08948718 0.05042300
## 44 0.04 0.5 0.08948718 0.05042300
## 45 0.05 0.5 0.08948718 0.05042300
## 46 0.06 0.5 0.08948718 0.05042300
## 47 0.07 0.5 0.08948718 0.05042300
## 48 0.08 0.5 0.08948718 0.05042300
## 49 0.09 0.5 0.08948718 0.05042300
## 50 0.10 0.5 0.08948718 0.05042300
## 51 0.01 0.6 0.08948718 0.05042300
## 52 0.02 0.6 0.08948718 0.05042300
## 53 0.03 0.6 0.08948718 0.05042300
## 54 0.04 0.6 0.08948718 0.05042300
## 55 0.05 0.6 0.08948718 0.05042300
## 56 0.06 0.6 0.08948718 0.05042300
## 57 0.07 0.6 0.08948718 0.05042300
## 58 0.08 0.6 0.08948718 0.05042300
## 59 0.09 0.6 0.08948718 0.05042300
## 60 0.10 0.6 0.08948718 0.05042300
## 61 0.01 0.7 0.09205128 0.05318685
## 62 0.02 0.7 0.09205128 0.05318685
## 63 0.03 0.7 0.09205128 0.05318685
## 64 0.04 0.7 0.09205128 0.05318685
## 65 0.05 0.7 0.09205128 0.05318685
## 66 0.06 0.7 0.09205128 0.05318685
## 67 0.07 0.7 0.09205128 0.05318685
## 68 0.08 0.7 0.09205128 0.05318685
## 69 0.09 0.7 0.09205128 0.05318685
## 70 0.10 0.7 0.09205128 0.05318685
## 71 0.01 0.8 0.09461538 0.05299421
## 72 0.02 0.8 0.09461538 0.05299421
## 73 0.03 0.8 0.09461538 0.05299421
## 74 0.04 0.8 0.09461538 0.05299421
## 75 0.05 0.8 0.09461538 0.05299421
## 76 0.06 0.8 0.09461538 0.05299421
## 77 0.07 0.8 0.09461538 0.05299421
## 78 0.08 0.8 0.09461538 0.05299421
## 79 0.09 0.8 0.09461538 0.05299421
## 80 0.10 0.8 0.09461538 0.05299421
## 81 0.01 0.9 0.09711538 0.05121546
## 82 0.02 0.9 0.09711538 0.05121546
## 83 0.03 0.9 0.09711538 0.05121546
## 84 0.04 0.9 0.09711538 0.05121546
## 85 0.05 0.9 0.09711538 0.05121546
## 86 0.06 0.9 0.09711538 0.05121546
## 87 0.07 0.9 0.09711538 0.05121546
## 88 0.08 0.9 0.09711538 0.05121546
## 89 0.09 0.9 0.09711538 0.05121546
## 90 0.10 0.9 0.09711538 0.05121546
## 91 0.01 1.0 0.09961538 0.04923181
## 92 0.02 1.0 0.09961538 0.04923181
## 93 0.03 1.0 0.09961538 0.04923181
## 94 0.04 1.0 0.09961538 0.04923181
## 95 0.05 1.0 0.09961538 0.04923181
## 96 0.06 1.0 0.09961538 0.04923181
## 97 0.07 1.0 0.09961538 0.04923181
## 98 0.08 1.0 0.09961538 0.04923181
## 99 0.09 1.0 0.09961538 0.04923181
## 100 0.10 1.0 0.09961538 0.04923181
tuned.svm$best.parameters
## gamma cost
## 31 0.01 0.4
The best parameters from the SVM model are gamma = 0.01 and cost = 0.4. For these values, the cross validation error obtained if 0.0817.
set.seed(1)
form = mpg.new ~ .
tuned.svm.radial = tune.svm(form, data = Auto, kernel = "radial", gamma = seq(.01, .1, by = .01),
cost = seq(.1, 1, by =.1), degree = c(2,3,4))
summary(tuned.svm.radial)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## degree gamma cost
## 2 0.04 0.1
##
## - best performance: 0.08410256
##
## - Detailed performance results:
## degree gamma cost error dispersion
## 1 2 0.01 0.1 0.11224359 0.03836937
## 2 3 0.01 0.1 0.11224359 0.03836937
## 3 4 0.01 0.1 0.11224359 0.03836937
## 4 2 0.02 0.1 0.09698718 0.04315610
## 5 3 0.02 0.1 0.09698718 0.04315610
## 6 4 0.02 0.1 0.09698718 0.04315610
## 7 2 0.03 0.1 0.09179487 0.04693642
## 8 3 0.03 0.1 0.09179487 0.04693642
## 9 4 0.03 0.1 0.09179487 0.04693642
## 10 2 0.04 0.1 0.08410256 0.04164179
## 11 3 0.04 0.1 0.08410256 0.04164179
## 12 4 0.04 0.1 0.08410256 0.04164179
## 13 2 0.05 0.1 0.08666667 0.04193895
## 14 3 0.05 0.1 0.08666667 0.04193895
## 15 4 0.05 0.1 0.08666667 0.04193895
## 16 2 0.06 0.1 0.08666667 0.04193895
## 17 3 0.06 0.1 0.08666667 0.04193895
## 18 4 0.06 0.1 0.08666667 0.04193895
## 19 2 0.07 0.1 0.08666667 0.04193895
## 20 3 0.07 0.1 0.08666667 0.04193895
## 21 4 0.07 0.1 0.08666667 0.04193895
## 22 2 0.08 0.1 0.08666667 0.04193895
## 23 3 0.08 0.1 0.08666667 0.04193895
## 24 4 0.08 0.1 0.08666667 0.04193895
## 25 2 0.09 0.1 0.08666667 0.04193895
## 26 3 0.09 0.1 0.08666667 0.04193895
## 27 4 0.09 0.1 0.08666667 0.04193895
## 28 2 0.10 0.1 0.08666667 0.04193895
## 29 3 0.10 0.1 0.08666667 0.04193895
## 30 4 0.10 0.1 0.08666667 0.04193895
## 31 2 0.01 0.2 0.09955128 0.04427158
## 32 3 0.01 0.2 0.09955128 0.04427158
## 33 4 0.01 0.2 0.09955128 0.04427158
## 34 2 0.02 0.2 0.08410256 0.04164179
## 35 3 0.02 0.2 0.08410256 0.04164179
## 36 4 0.02 0.2 0.08410256 0.04164179
## 37 2 0.03 0.2 0.08923077 0.04698309
## 38 3 0.03 0.2 0.08923077 0.04698309
## 39 4 0.03 0.2 0.08923077 0.04698309
## 40 2 0.04 0.2 0.08923077 0.04698309
## 41 3 0.04 0.2 0.08923077 0.04698309
## 42 4 0.04 0.2 0.08923077 0.04698309
## 43 2 0.05 0.2 0.08923077 0.04698309
## 44 3 0.05 0.2 0.08923077 0.04698309
## 45 4 0.05 0.2 0.08923077 0.04698309
## 46 2 0.06 0.2 0.08923077 0.04698309
## 47 3 0.06 0.2 0.08923077 0.04698309
## 48 4 0.06 0.2 0.08923077 0.04698309
## 49 2 0.07 0.2 0.08923077 0.04698309
## 50 3 0.07 0.2 0.08923077 0.04698309
## 51 4 0.07 0.2 0.08923077 0.04698309
## 52 2 0.08 0.2 0.08923077 0.04698309
## 53 3 0.08 0.2 0.08923077 0.04698309
## 54 4 0.08 0.2 0.08923077 0.04698309
## 55 2 0.09 0.2 0.08923077 0.04698309
## 56 3 0.09 0.2 0.08923077 0.04698309
## 57 4 0.09 0.2 0.08923077 0.04698309
## 58 2 0.10 0.2 0.08923077 0.04698309
## 59 3 0.10 0.2 0.08923077 0.04698309
## 60 4 0.10 0.2 0.08923077 0.04698309
## 61 2 0.01 0.3 0.09435897 0.04349516
## 62 3 0.01 0.3 0.09435897 0.04349516
## 63 4 0.01 0.3 0.09435897 0.04349516
## 64 2 0.02 0.3 0.08923077 0.04698309
## 65 3 0.02 0.3 0.08923077 0.04698309
## 66 4 0.02 0.3 0.08923077 0.04698309
## 67 2 0.03 0.3 0.08923077 0.04698309
## 68 3 0.03 0.3 0.08923077 0.04698309
## 69 4 0.03 0.3 0.08923077 0.04698309
## 70 2 0.04 0.3 0.08923077 0.04698309
## 71 3 0.04 0.3 0.08923077 0.04698309
## 72 4 0.04 0.3 0.08923077 0.04698309
## 73 2 0.05 0.3 0.08923077 0.04698309
## 74 3 0.05 0.3 0.08923077 0.04698309
## 75 4 0.05 0.3 0.08923077 0.04698309
## 76 2 0.06 0.3 0.08923077 0.04698309
## 77 3 0.06 0.3 0.08923077 0.04698309
## 78 4 0.06 0.3 0.08923077 0.04698309
## 79 2 0.07 0.3 0.08923077 0.04698309
## 80 3 0.07 0.3 0.08923077 0.04698309
## 81 4 0.07 0.3 0.08923077 0.04698309
## 82 2 0.08 0.3 0.08923077 0.04698309
## 83 3 0.08 0.3 0.08923077 0.04698309
## 84 4 0.08 0.3 0.08923077 0.04698309
## 85 2 0.09 0.3 0.08923077 0.04698309
## 86 3 0.09 0.3 0.08923077 0.04698309
## 87 4 0.09 0.3 0.08923077 0.04698309
## 88 2 0.10 0.3 0.08923077 0.04698309
## 89 3 0.10 0.3 0.08923077 0.04698309
## 90 4 0.10 0.3 0.08923077 0.04698309
## 91 2 0.01 0.4 0.08410256 0.04164179
## 92 3 0.01 0.4 0.08410256 0.04164179
## 93 4 0.01 0.4 0.08410256 0.04164179
## 94 2 0.02 0.4 0.08923077 0.04698309
## 95 3 0.02 0.4 0.08923077 0.04698309
## 96 4 0.02 0.4 0.08923077 0.04698309
## 97 2 0.03 0.4 0.08923077 0.04698309
## 98 3 0.03 0.4 0.08923077 0.04698309
## 99 4 0.03 0.4 0.08923077 0.04698309
## 100 2 0.04 0.4 0.08923077 0.04698309
## 101 3 0.04 0.4 0.08923077 0.04698309
## 102 4 0.04 0.4 0.08923077 0.04698309
## 103 2 0.05 0.4 0.08923077 0.04698309
## 104 3 0.05 0.4 0.08923077 0.04698309
## 105 4 0.05 0.4 0.08923077 0.04698309
## 106 2 0.06 0.4 0.08666667 0.04193895
## 107 3 0.06 0.4 0.08666667 0.04193895
## 108 4 0.06 0.4 0.08666667 0.04193895
## 109 2 0.07 0.4 0.08666667 0.04193895
## 110 3 0.07 0.4 0.08666667 0.04193895
## 111 4 0.07 0.4 0.08666667 0.04193895
## 112 2 0.08 0.4 0.08666667 0.04193895
## 113 3 0.08 0.4 0.08666667 0.04193895
## 114 4 0.08 0.4 0.08666667 0.04193895
## 115 2 0.09 0.4 0.08666667 0.04193895
## 116 3 0.09 0.4 0.08666667 0.04193895
## 117 4 0.09 0.4 0.08666667 0.04193895
## 118 2 0.10 0.4 0.08666667 0.04193895
## 119 3 0.10 0.4 0.08666667 0.04193895
## 120 4 0.10 0.4 0.08666667 0.04193895
## 121 2 0.01 0.5 0.08923077 0.04698309
## 122 3 0.01 0.5 0.08923077 0.04698309
## 123 4 0.01 0.5 0.08923077 0.04698309
## 124 2 0.02 0.5 0.08673077 0.04551036
## 125 3 0.02 0.5 0.08673077 0.04551036
## 126 4 0.02 0.5 0.08673077 0.04551036
## 127 2 0.03 0.5 0.08673077 0.04551036
## 128 3 0.03 0.5 0.08673077 0.04551036
## 129 4 0.03 0.5 0.08673077 0.04551036
## 130 2 0.04 0.5 0.08416667 0.04010502
## 131 3 0.04 0.5 0.08416667 0.04010502
## 132 4 0.04 0.5 0.08416667 0.04010502
## 133 2 0.05 0.5 0.08416667 0.04010502
## 134 3 0.05 0.5 0.08416667 0.04010502
## 135 4 0.05 0.5 0.08416667 0.04010502
## 136 2 0.06 0.5 0.08666667 0.04193895
## 137 3 0.06 0.5 0.08666667 0.04193895
## 138 4 0.06 0.5 0.08666667 0.04193895
## 139 2 0.07 0.5 0.08666667 0.04193895
## 140 3 0.07 0.5 0.08666667 0.04193895
## 141 4 0.07 0.5 0.08666667 0.04193895
## 142 2 0.08 0.5 0.08666667 0.04193895
## 143 3 0.08 0.5 0.08666667 0.04193895
## 144 4 0.08 0.5 0.08666667 0.04193895
## 145 2 0.09 0.5 0.08666667 0.04193895
## 146 3 0.09 0.5 0.08666667 0.04193895
## 147 4 0.09 0.5 0.08666667 0.04193895
## 148 2 0.10 0.5 0.08666667 0.04193895
## 149 3 0.10 0.5 0.08666667 0.04193895
## 150 4 0.10 0.5 0.08666667 0.04193895
## 151 2 0.01 0.6 0.08923077 0.04698309
## 152 3 0.01 0.6 0.08923077 0.04698309
## 153 4 0.01 0.6 0.08923077 0.04698309
## 154 2 0.02 0.6 0.08673077 0.04551036
## 155 3 0.02 0.6 0.08673077 0.04551036
## 156 4 0.02 0.6 0.08673077 0.04551036
## 157 2 0.03 0.6 0.08416667 0.04010502
## 158 3 0.03 0.6 0.08416667 0.04010502
## 159 4 0.03 0.6 0.08416667 0.04010502
## 160 2 0.04 0.6 0.08416667 0.04010502
## 161 3 0.04 0.6 0.08416667 0.04010502
## 162 4 0.04 0.6 0.08416667 0.04010502
## 163 2 0.05 0.6 0.08416667 0.04010502
## 164 3 0.05 0.6 0.08416667 0.04010502
## 165 4 0.05 0.6 0.08416667 0.04010502
## 166 2 0.06 0.6 0.08416667 0.04010502
## 167 3 0.06 0.6 0.08416667 0.04010502
## 168 4 0.06 0.6 0.08416667 0.04010502
## 169 2 0.07 0.6 0.08666667 0.04193895
## 170 3 0.07 0.6 0.08666667 0.04193895
## 171 4 0.07 0.6 0.08666667 0.04193895
## 172 2 0.08 0.6 0.08666667 0.04193895
## 173 3 0.08 0.6 0.08666667 0.04193895
## 174 4 0.08 0.6 0.08666667 0.04193895
## 175 2 0.09 0.6 0.08666667 0.04193895
## 176 3 0.09 0.6 0.08666667 0.04193895
## 177 4 0.09 0.6 0.08666667 0.04193895
## 178 2 0.10 0.6 0.08666667 0.04193895
## 179 3 0.10 0.6 0.08666667 0.04193895
## 180 4 0.10 0.6 0.08666667 0.04193895
## 181 2 0.01 0.7 0.08673077 0.04551036
## 182 3 0.01 0.7 0.08673077 0.04551036
## 183 4 0.01 0.7 0.08673077 0.04551036
## 184 2 0.02 0.7 0.08416667 0.04010502
## 185 3 0.02 0.7 0.08416667 0.04010502
## 186 4 0.02 0.7 0.08416667 0.04010502
## 187 2 0.03 0.7 0.08416667 0.04010502
## 188 3 0.03 0.7 0.08416667 0.04010502
## 189 4 0.03 0.7 0.08416667 0.04010502
## 190 2 0.04 0.7 0.08416667 0.04010502
## 191 3 0.04 0.7 0.08416667 0.04010502
## 192 4 0.04 0.7 0.08416667 0.04010502
## 193 2 0.05 0.7 0.08416667 0.04010502
## 194 3 0.05 0.7 0.08416667 0.04010502
## 195 4 0.05 0.7 0.08416667 0.04010502
## 196 2 0.06 0.7 0.08416667 0.04010502
## 197 3 0.06 0.7 0.08416667 0.04010502
## 198 4 0.06 0.7 0.08416667 0.04010502
## 199 2 0.07 0.7 0.08416667 0.04010502
## 200 3 0.07 0.7 0.08416667 0.04010502
## 201 4 0.07 0.7 0.08416667 0.04010502
## 202 2 0.08 0.7 0.08666667 0.04193895
## 203 3 0.08 0.7 0.08666667 0.04193895
## 204 4 0.08 0.7 0.08666667 0.04193895
## 205 2 0.09 0.7 0.08666667 0.04193895
## 206 3 0.09 0.7 0.08666667 0.04193895
## 207 4 0.09 0.7 0.08666667 0.04193895
## 208 2 0.10 0.7 0.08666667 0.04193895
## 209 3 0.10 0.7 0.08666667 0.04193895
## 210 4 0.10 0.7 0.08666667 0.04193895
## 211 2 0.01 0.8 0.08673077 0.04551036
## 212 3 0.01 0.8 0.08673077 0.04551036
## 213 4 0.01 0.8 0.08673077 0.04551036
## 214 2 0.02 0.8 0.08416667 0.04010502
## 215 3 0.02 0.8 0.08416667 0.04010502
## 216 4 0.02 0.8 0.08416667 0.04010502
## 217 2 0.03 0.8 0.08416667 0.04010502
## 218 3 0.03 0.8 0.08416667 0.04010502
## 219 4 0.03 0.8 0.08416667 0.04010502
## 220 2 0.04 0.8 0.08416667 0.04010502
## 221 3 0.04 0.8 0.08416667 0.04010502
## 222 4 0.04 0.8 0.08416667 0.04010502
## 223 2 0.05 0.8 0.08673077 0.04040897
## 224 3 0.05 0.8 0.08673077 0.04040897
## 225 4 0.05 0.8 0.08673077 0.04040897
## 226 2 0.06 0.8 0.08673077 0.04217805
## 227 3 0.06 0.8 0.08673077 0.04217805
## 228 4 0.06 0.8 0.08673077 0.04217805
## 229 2 0.07 0.8 0.08673077 0.04217805
## 230 3 0.07 0.8 0.08673077 0.04217805
## 231 4 0.07 0.8 0.08673077 0.04217805
## 232 2 0.08 0.8 0.08923077 0.04376306
## 233 3 0.08 0.8 0.08923077 0.04376306
## 234 4 0.08 0.8 0.08923077 0.04376306
## 235 2 0.09 0.8 0.08666667 0.04193895
## 236 3 0.09 0.8 0.08666667 0.04193895
## 237 4 0.09 0.8 0.08666667 0.04193895
## 238 2 0.10 0.8 0.08666667 0.04193895
## 239 3 0.10 0.8 0.08666667 0.04193895
## 240 4 0.10 0.8 0.08666667 0.04193895
## 241 2 0.01 0.9 0.08673077 0.04551036
## 242 3 0.01 0.9 0.08673077 0.04551036
## 243 4 0.01 0.9 0.08673077 0.04551036
## 244 2 0.02 0.9 0.08416667 0.04010502
## 245 3 0.02 0.9 0.08416667 0.04010502
## 246 4 0.02 0.9 0.08416667 0.04010502
## 247 2 0.03 0.9 0.08416667 0.04010502
## 248 3 0.03 0.9 0.08416667 0.04010502
## 249 4 0.03 0.9 0.08416667 0.04010502
## 250 2 0.04 0.9 0.08416667 0.04010502
## 251 3 0.04 0.9 0.08416667 0.04010502
## 252 4 0.04 0.9 0.08416667 0.04010502
## 253 2 0.05 0.9 0.08929487 0.04229479
## 254 3 0.05 0.9 0.08929487 0.04229479
## 255 4 0.05 0.9 0.08929487 0.04229479
## 256 2 0.06 0.9 0.08929487 0.04229479
## 257 3 0.06 0.9 0.08929487 0.04229479
## 258 4 0.06 0.9 0.08929487 0.04229479
## 259 2 0.07 0.9 0.08673077 0.04217805
## 260 3 0.07 0.9 0.08673077 0.04217805
## 261 4 0.07 0.9 0.08673077 0.04217805
## 262 2 0.08 0.9 0.08673077 0.04217805
## 263 3 0.08 0.9 0.08673077 0.04217805
## 264 4 0.08 0.9 0.08673077 0.04217805
## 265 2 0.09 0.9 0.08923077 0.04376306
## 266 3 0.09 0.9 0.08923077 0.04376306
## 267 4 0.09 0.9 0.08923077 0.04376306
## 268 2 0.10 0.9 0.08923077 0.04376306
## 269 3 0.10 0.9 0.08923077 0.04376306
## 270 4 0.10 0.9 0.08923077 0.04376306
## 271 2 0.01 1.0 0.08673077 0.04551036
## 272 3 0.01 1.0 0.08673077 0.04551036
## 273 4 0.01 1.0 0.08673077 0.04551036
## 274 2 0.02 1.0 0.08416667 0.04010502
## 275 3 0.02 1.0 0.08416667 0.04010502
## 276 4 0.02 1.0 0.08416667 0.04010502
## 277 2 0.03 1.0 0.08416667 0.04010502
## 278 3 0.03 1.0 0.08416667 0.04010502
## 279 4 0.03 1.0 0.08416667 0.04010502
## 280 2 0.04 1.0 0.08416667 0.04010502
## 281 3 0.04 1.0 0.08416667 0.04010502
## 282 4 0.04 1.0 0.08416667 0.04010502
## 283 2 0.05 1.0 0.08929487 0.04229479
## 284 3 0.05 1.0 0.08929487 0.04229479
## 285 4 0.05 1.0 0.08929487 0.04229479
## 286 2 0.06 1.0 0.08673077 0.04040897
## 287 3 0.06 1.0 0.08673077 0.04040897
## 288 4 0.06 1.0 0.08673077 0.04040897
## 289 2 0.07 1.0 0.08673077 0.04217805
## 290 3 0.07 1.0 0.08673077 0.04217805
## 291 4 0.07 1.0 0.08673077 0.04217805
## 292 2 0.08 1.0 0.08673077 0.04217805
## 293 3 0.08 1.0 0.08673077 0.04217805
## 294 4 0.08 1.0 0.08673077 0.04217805
## 295 2 0.09 1.0 0.08923077 0.04376306
## 296 3 0.09 1.0 0.08923077 0.04376306
## 297 4 0.09 1.0 0.08923077 0.04376306
## 298 2 0.10 1.0 0.08923077 0.04376306
## 299 3 0.10 1.0 0.08923077 0.04376306
## 300 4 0.10 1.0 0.08923077 0.04376306
tuned.svm.radial$best.parameters
## degree gamma cost
## 10 2 0.04 0.1
The best parameters for the SVM using radial basis kernel are degree=2, gamma = 0.04 and cost = 0.1. For these values, the corresponding cross validation error obtained is 0.0841.
set.seed(1)
form = mpg.new ~ .
tuned.svm.poly = tune.svm(form, data = Auto, kernel = "polynomial", gamma = seq(.01, .1, by = .01),
cost = seq(.1, 1, by =.1), degree = c(2,3,4))
summary(tuned.svm.poly)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## degree gamma cost
## 3 0.09 0.9
##
## - best performance: 0.08929487
##
## - Detailed performance results:
## degree gamma cost error dispersion
## 1 2 0.01 0.1 0.55115385 0.04366593
## 2 3 0.01 0.1 0.55115385 0.04366593
## 3 4 0.01 0.1 0.55115385 0.04366593
## 4 2 0.02 0.1 0.55115385 0.04366593
## 5 3 0.02 0.1 0.55115385 0.04366593
## 6 4 0.02 0.1 0.55115385 0.04366593
## 7 2 0.03 0.1 0.54878205 0.05575773
## 8 3 0.03 0.1 0.45948718 0.09179871
## 9 4 0.03 0.1 0.55115385 0.04366593
## 10 2 0.04 0.1 0.44923077 0.12300486
## 11 3 0.04 0.1 0.35237179 0.09810836
## 12 4 0.04 0.1 0.49243590 0.09768898
## 13 2 0.05 0.1 0.44429487 0.09385887
## 14 3 0.05 0.1 0.28083333 0.08610472
## 15 4 0.05 0.1 0.48512821 0.09163942
## 16 2 0.06 0.1 0.40096154 0.07627412
## 17 3 0.06 0.1 0.27057692 0.08611321
## 18 4 0.06 0.1 0.42121795 0.08170693
## 19 2 0.07 0.1 0.36782051 0.09962527
## 20 3 0.07 0.1 0.25794872 0.09305854
## 21 4 0.07 0.1 0.39570513 0.07118050
## 22 2 0.08 0.1 0.33974359 0.09722621
## 23 3 0.08 0.1 0.26051282 0.09336419
## 24 4 0.08 0.1 0.38288462 0.08294675
## 25 2 0.09 0.1 0.32442308 0.09790171
## 26 3 0.09 0.1 0.25544872 0.09429216
## 27 4 0.09 0.1 0.36769231 0.08590870
## 28 2 0.10 0.1 0.31673077 0.09410274
## 29 3 0.10 0.1 0.25794872 0.09147506
## 30 4 0.10 0.1 0.33967949 0.08586578
## 31 2 0.01 0.2 0.55115385 0.04366593
## 32 3 0.01 0.2 0.55115385 0.04366593
## 33 4 0.01 0.2 0.55115385 0.04366593
## 34 2 0.02 0.2 0.54615385 0.04947031
## 35 3 0.02 0.2 0.48987179 0.11528834
## 36 4 0.02 0.2 0.55115385 0.04366593
## 37 2 0.03 0.2 0.44923077 0.10982731
## 38 3 0.03 0.2 0.36769231 0.09324822
## 39 4 0.03 0.2 0.53339744 0.06383881
## 40 2 0.04 0.2 0.42897436 0.08000349
## 41 3 0.04 0.2 0.28083333 0.08610472
## 42 4 0.04 0.2 0.48512821 0.09163942
## 43 2 0.05 0.2 0.36525641 0.09799510
## 44 3 0.05 0.2 0.26301282 0.08951030
## 45 4 0.05 0.2 0.42121795 0.08170693
## 46 2 0.06 0.2 0.32698718 0.09471403
## 47 3 0.06 0.2 0.26051282 0.09336419
## 48 4 0.06 0.2 0.39570513 0.07118050
## 49 2 0.07 0.2 0.31929487 0.09021673
## 50 3 0.07 0.2 0.25544872 0.09429216
## 51 4 0.07 0.2 0.37282051 0.09015737
## 52 2 0.08 0.2 0.29371795 0.10142926
## 53 3 0.08 0.2 0.25794872 0.09147506
## 54 4 0.08 0.2 0.35243590 0.07958465
## 55 2 0.09 0.2 0.27589744 0.09117910
## 56 3 0.09 0.2 0.25794872 0.09147506
## 57 4 0.09 0.2 0.32435897 0.08495625
## 58 2 0.10 0.2 0.27589744 0.09276763
## 59 3 0.10 0.2 0.25288462 0.09305414
## 60 4 0.10 0.2 0.29878205 0.09294476
## 61 2 0.01 0.3 0.55115385 0.04366593
## 62 3 0.01 0.3 0.55115385 0.04366593
## 63 4 0.01 0.3 0.55115385 0.04366593
## 64 2 0.02 0.3 0.49775641 0.09287734
## 65 3 0.02 0.3 0.45179487 0.13701291
## 66 4 0.02 0.3 0.55115385 0.04366593
## 67 2 0.03 0.3 0.43923077 0.08889598
## 68 3 0.03 0.3 0.31153846 0.08874135
## 69 4 0.03 0.3 0.48474359 0.11529468
## 70 2 0.04 0.3 0.36782051 0.09962527
## 71 3 0.04 0.3 0.27570513 0.08670078
## 72 4 0.04 0.3 0.43641026 0.10565750
## 73 2 0.05 0.3 0.32698718 0.09471403
## 74 3 0.05 0.3 0.26051282 0.09336419
## 75 4 0.05 0.3 0.40846154 0.06332708
## 76 2 0.06 0.3 0.31160256 0.09134932
## 77 3 0.06 0.3 0.25544872 0.09429216
## 78 4 0.06 0.3 0.38544872 0.08252072
## 79 2 0.07 0.3 0.28102564 0.09683623
## 80 3 0.07 0.3 0.25794872 0.09147506
## 81 4 0.07 0.3 0.35750000 0.08053489
## 82 2 0.08 0.3 0.27846154 0.09252319
## 83 3 0.08 0.3 0.25794872 0.09147506
## 84 4 0.08 0.3 0.32692308 0.08564139
## 85 2 0.09 0.3 0.27339744 0.09058783
## 86 3 0.09 0.3 0.24269231 0.10047744
## 87 4 0.09 0.3 0.29878205 0.08810285
## 88 2 0.10 0.3 0.26320513 0.08884943
## 89 3 0.10 0.3 0.18660256 0.09936538
## 90 4 0.10 0.3 0.28083333 0.09185174
## 91 2 0.01 0.4 0.55115385 0.04366593
## 92 3 0.01 0.4 0.55115385 0.04366593
## 93 4 0.01 0.4 0.55115385 0.04366593
## 94 2 0.02 0.4 0.44923077 0.12300486
## 95 3 0.02 0.4 0.42634615 0.08934714
## 96 4 0.02 0.4 0.55115385 0.04366593
## 97 2 0.03 0.4 0.40096154 0.07627412
## 98 3 0.03 0.4 0.28852564 0.09512769
## 99 4 0.03 0.4 0.49262821 0.08771999
## 100 2 0.04 0.4 0.33974359 0.09722621
## 101 3 0.04 0.4 0.26301282 0.08951030
## 102 4 0.04 0.4 0.43391026 0.08042393
## 103 2 0.05 0.4 0.31673077 0.09410274
## 104 3 0.05 0.4 0.26051282 0.09336419
## 105 4 0.05 0.4 0.39570513 0.07118050
## 106 2 0.06 0.4 0.28102564 0.09683623
## 107 3 0.06 0.4 0.25544872 0.09429216
## 108 4 0.06 0.4 0.37282051 0.09015737
## 109 2 0.07 0.4 0.27589744 0.09276763
## 110 3 0.07 0.4 0.25794872 0.09147506
## 111 4 0.07 0.4 0.33967949 0.08586578
## 112 2 0.08 0.4 0.26570513 0.08899117
## 113 3 0.08 0.4 0.25032051 0.09251285
## 114 4 0.08 0.4 0.30391026 0.08851234
## 115 2 0.09 0.4 0.26064103 0.09024848
## 116 3 0.09 0.4 0.19429487 0.11454046
## 117 4 0.09 0.4 0.28083333 0.09185174
## 118 2 0.10 0.4 0.26833333 0.09229345
## 119 3 0.10 0.4 0.15583333 0.08858349
## 120 4 0.10 0.4 0.26564103 0.09977887
## 121 2 0.01 0.5 0.55115385 0.04366593
## 122 3 0.01 0.5 0.55115385 0.04366593
## 123 4 0.01 0.5 0.55115385 0.04366593
## 124 2 0.02 0.5 0.44685897 0.10214947
## 125 3 0.02 0.5 0.38532051 0.10529070
## 126 4 0.02 0.5 0.55115385 0.04366593
## 127 2 0.03 0.5 0.37544872 0.09718631
## 128 3 0.03 0.5 0.27826923 0.08894458
## 129 4 0.03 0.5 0.49019231 0.08644849
## 130 2 0.04 0.5 0.32442308 0.09790171
## 131 3 0.04 0.5 0.25794872 0.09305854
## 132 4 0.04 0.5 0.42121795 0.08170693
## 133 2 0.05 0.5 0.29884615 0.09842466
## 134 3 0.05 0.5 0.25544872 0.09429216
## 135 4 0.05 0.5 0.39057692 0.07286923
## 136 2 0.06 0.5 0.27589744 0.09117910
## 137 3 0.06 0.5 0.25794872 0.09147506
## 138 4 0.06 0.5 0.36769231 0.08590870
## 139 2 0.07 0.5 0.27083333 0.08905272
## 140 3 0.07 0.5 0.25544872 0.09429216
## 141 4 0.07 0.5 0.32692308 0.08564139
## 142 2 0.08 0.5 0.26064103 0.09024848
## 143 3 0.08 0.5 0.21974359 0.11590489
## 144 4 0.08 0.5 0.29621795 0.08735551
## 145 2 0.09 0.5 0.26833333 0.09229345
## 146 3 0.09 0.5 0.16615385 0.10008434
## 147 4 0.09 0.5 0.27826923 0.09137530
## 148 2 0.10 0.5 0.26833333 0.09149851
## 149 3 0.10 0.5 0.11487179 0.05447151
## 150 4 0.10 0.5 0.26051282 0.09719766
## 151 2 0.01 0.6 0.55115385 0.04366593
## 152 3 0.01 0.6 0.55115385 0.04366593
## 153 4 0.01 0.6 0.55115385 0.04366593
## 154 2 0.02 0.6 0.44173077 0.09750637
## 155 3 0.02 0.6 0.37769231 0.09237780
## 156 4 0.02 0.6 0.55115385 0.04366593
## 157 2 0.03 0.6 0.35762821 0.09267222
## 158 3 0.03 0.6 0.27570513 0.09241074
## 159 4 0.03 0.6 0.48762821 0.08859153
## 160 2 0.04 0.6 0.32185897 0.09261210
## 161 3 0.04 0.6 0.26051282 0.09336419
## 162 4 0.04 0.6 0.41615385 0.07712095
## 163 2 0.05 0.6 0.28102564 0.09683623
## 164 3 0.05 0.6 0.25544872 0.09429216
## 165 4 0.05 0.6 0.38801282 0.07742126
## 166 2 0.06 0.6 0.27333333 0.08973352
## 167 3 0.06 0.6 0.25794872 0.09147506
## 168 4 0.06 0.6 0.35493590 0.07813390
## 169 2 0.07 0.6 0.26320513 0.08884943
## 170 3 0.07 0.6 0.24775641 0.09578145
## 171 4 0.07 0.6 0.31666667 0.08996767
## 172 2 0.08 0.6 0.27089744 0.09228157
## 173 3 0.08 0.6 0.18147436 0.10463300
## 174 4 0.08 0.6 0.28339744 0.08902257
## 175 2 0.09 0.6 0.26576923 0.09222614
## 176 3 0.09 0.6 0.15070513 0.08518245
## 177 4 0.09 0.6 0.26814103 0.09754364
## 178 2 0.10 0.6 0.26833333 0.09989548
## 179 3 0.10 0.6 0.09435897 0.04008538
## 180 4 0.10 0.6 0.26051282 0.09719766
## 181 2 0.01 0.7 0.55115385 0.04366593
## 182 3 0.01 0.7 0.55115385 0.04366593
## 183 4 0.01 0.7 0.55115385 0.04366593
## 184 2 0.02 0.7 0.43153846 0.08506625
## 185 3 0.02 0.7 0.36512821 0.08988958
## 186 4 0.02 0.7 0.55115385 0.04366593
## 187 2 0.03 0.7 0.33974359 0.09722621
## 188 3 0.03 0.7 0.27314103 0.09017218
## 189 4 0.03 0.7 0.48512821 0.09163942
## 190 2 0.04 0.7 0.31160256 0.09134932
## 191 3 0.04 0.7 0.26051282 0.09336419
## 192 4 0.04 0.7 0.40846154 0.06332708
## 193 2 0.05 0.7 0.27589744 0.09117910
## 194 3 0.05 0.7 0.25544872 0.09429216
## 195 4 0.05 0.7 0.37782051 0.08374771
## 196 2 0.06 0.7 0.26570513 0.08899117
## 197 3 0.06 0.7 0.25794872 0.09147506
## 198 4 0.06 0.7 0.34737179 0.08177619
## 199 2 0.07 0.7 0.26576923 0.08981845
## 200 3 0.07 0.7 0.23506410 0.10305175
## 201 4 0.07 0.7 0.30134615 0.08876165
## 202 2 0.08 0.7 0.26320513 0.09207949
## 203 3 0.08 0.7 0.16615385 0.10008434
## 204 4 0.08 0.7 0.28083333 0.09185174
## 205 2 0.09 0.7 0.26833333 0.09616961
## 206 3 0.09 0.7 0.10717949 0.04490007
## 207 4 0.09 0.7 0.26307692 0.10037014
## 208 2 0.10 0.7 0.27339744 0.10052607
## 209 3 0.10 0.7 0.09185897 0.04206749
## 210 4 0.10 0.7 0.25794872 0.09840025
## 211 2 0.01 0.8 0.54615385 0.04947031
## 212 3 0.01 0.8 0.55115385 0.04366593
## 213 4 0.01 0.8 0.55115385 0.04366593
## 214 2 0.02 0.8 0.42897436 0.08000349
## 215 3 0.02 0.8 0.35237179 0.09810836
## 216 4 0.02 0.8 0.54358974 0.05056569
## 217 2 0.03 0.8 0.32698718 0.09471403
## 218 3 0.03 0.8 0.27057692 0.08611321
## 219 4 0.03 0.8 0.47487179 0.07959154
## 220 2 0.04 0.8 0.29371795 0.10142926
## 221 3 0.04 0.8 0.26051282 0.09336419
## 222 4 0.04 0.8 0.39820513 0.06982297
## 223 2 0.05 0.8 0.27589744 0.09276763
## 224 3 0.05 0.8 0.25794872 0.09147506
## 225 4 0.05 0.8 0.37282051 0.09015737
## 226 2 0.06 0.8 0.26320513 0.08884943
## 227 3 0.06 0.8 0.25544872 0.09429216
## 228 4 0.06 0.8 0.33711538 0.08475042
## 229 2 0.07 0.8 0.27089744 0.09228157
## 230 3 0.07 0.8 0.20961538 0.11310707
## 231 4 0.07 0.8 0.30134615 0.08876165
## 232 2 0.08 0.8 0.26833333 0.09149851
## 233 3 0.08 0.8 0.15583333 0.08858349
## 234 4 0.08 0.8 0.27826923 0.09137530
## 235 2 0.09 0.8 0.27089744 0.10205500
## 236 3 0.09 0.8 0.09698718 0.04332293
## 237 4 0.09 0.8 0.26051282 0.09719766
## 238 2 0.10 0.8 0.27083333 0.10487394
## 239 3 0.10 0.8 0.09442308 0.03969077
## 240 4 0.10 0.8 0.25794872 0.09840025
## 241 2 0.01 0.9 0.54878205 0.05575773
## 242 3 0.01 0.9 0.55115385 0.04366593
## 243 4 0.01 0.9 0.55115385 0.04366593
## 244 2 0.02 0.9 0.40096154 0.07627412
## 245 3 0.02 0.9 0.33698718 0.09634646
## 246 4 0.02 0.9 0.52820513 0.07730260
## 247 2 0.03 0.9 0.32442308 0.09790171
## 248 3 0.03 0.9 0.26301282 0.08951030
## 249 4 0.03 0.9 0.44153846 0.09375926
## 250 2 0.04 0.9 0.28102564 0.09683623
## 251 3 0.04 0.9 0.25801282 0.09620001
## 252 4 0.04 0.9 0.39820513 0.06982297
## 253 2 0.05 0.9 0.27083333 0.09306396
## 254 3 0.05 0.9 0.25794872 0.09147506
## 255 4 0.05 0.9 0.37025641 0.08933525
## 256 2 0.06 0.9 0.26064103 0.09024848
## 257 3 0.06 0.9 0.25288462 0.09305414
## 258 4 0.06 0.9 0.32692308 0.08564139
## 259 2 0.07 0.9 0.26320513 0.09207949
## 260 3 0.07 0.9 0.18147436 0.10463300
## 261 4 0.07 0.9 0.29108974 0.08472779
## 262 2 0.08 0.9 0.26833333 0.09616961
## 263 3 0.08 0.9 0.14044872 0.08231265
## 264 4 0.08 0.9 0.27320513 0.09582984
## 265 2 0.09 0.9 0.27083333 0.09766021
## 266 3 0.09 0.9 0.08929487 0.04546017
## 267 4 0.09 0.9 0.26051282 0.09719766
## 268 2 0.10 0.9 0.27583333 0.10091446
## 269 3 0.10 0.9 0.09179487 0.03809033
## 270 4 0.10 0.9 0.25794872 0.09840025
## 271 2 0.01 1.0 0.52064103 0.07403209
## 272 3 0.01 1.0 0.55115385 0.04366593
## 273 4 0.01 1.0 0.55115385 0.04366593
## 274 2 0.02 1.0 0.38307692 0.08937613
## 275 3 0.02 1.0 0.31153846 0.08874135
## 276 4 0.02 1.0 0.53339744 0.06383881
## 277 2 0.03 1.0 0.31929487 0.09571717
## 278 3 0.03 1.0 0.26051282 0.09178598
## 279 4 0.03 1.0 0.43897436 0.09955605
## 280 2 0.04 1.0 0.27333333 0.09449191
## 281 3 0.04 1.0 0.25544872 0.09429216
## 282 4 0.04 1.0 0.39570513 0.07118050
## 283 2 0.05 1.0 0.26826923 0.08823894
## 284 3 0.05 1.0 0.25794872 0.09147506
## 285 4 0.05 1.0 0.36769231 0.08590870
## 286 2 0.06 1.0 0.27089744 0.09228157
## 287 3 0.06 1.0 0.24269231 0.10047744
## 288 4 0.06 1.0 0.32435897 0.08495625
## 289 2 0.07 1.0 0.26576923 0.09222614
## 290 3 0.07 1.0 0.16615385 0.10008434
## 291 4 0.07 1.0 0.28852564 0.08455733
## 292 2 0.08 1.0 0.27089744 0.10205500
## 293 3 0.08 1.0 0.10974359 0.04021388
## 294 4 0.08 1.0 0.26564103 0.09977887
## 295 2 0.09 1.0 0.27083333 0.10487394
## 296 3 0.09 1.0 0.09442308 0.04005262
## 297 4 0.09 1.0 0.25794872 0.09840025
## 298 2 0.10 1.0 0.27846154 0.10298534
## 299 3 0.10 1.0 0.09692308 0.03929858
## 300 4 0.10 1.0 0.26301282 0.09350192
tuned.svm.poly$best.parameters
## degree gamma cost
## 266 3 0.09 0.9
The best parameters for the SVM using polynomial kernel are degree=3, gamma = 0.09 and cost = 0.9. For these values, the corresponding cross validation error obtained is 0.089
We will now build all 3 SVM models with their corresponding best parameters and plot weight and horsepower for each of them.
form = mpg.new~.
svm.linear = svm(form, data = Auto, kernel = "linear", cost = 0.4, gamma = 0.01)
svm.radial = svm(form, data = Auto, kernel = "radial", cost = 0.1, gamma = 0.04, degree = 2 )
svm.poly = svm(form, data = Auto, kernel = "polynomial", cost = 0.9, gamma = 0.09, degree = 3)
plot(svm.linear, Auto, weight ~ horsepower)
plot(svm.radial, Auto, weight ~ horsepower)
plot(svm.poly, Auto, weight ~ horsepower)
This problem involves the OJ data set which is part of the ISLR package.
detach(Auto)
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 ...
set.seed(1)
train.Index <- sample(nrow(OJ), 800)
train.OJ <- OJ[train.Index,]
test.OJ <- OJ[-train.Index,]
set.seed(1)
svm.linear.OJ = svm(Purchase~., kernel = 'linear', data = train.OJ, cost = 0.01)
summary(svm.linear.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
The number of support vectors created are 435 out of 800. Out of these, 219 belong to CH and 216 belong to MM.
train.pred.OJ = predict(svm.linear.OJ, train.OJ)
table(train.OJ$Purchase, train.pred.OJ)
## train.pred.OJ
## CH MM
## CH 420 65
## MM 75 240
train.error.rate.OJ = (75+65)/(420+75+65+240)
train.error.rate.OJ
## [1] 0.175
test.pred.OJ = predict(svm.linear.OJ, test.OJ)
table(test.OJ$Purchase, test.pred.OJ)
## test.pred.OJ
## CH MM
## CH 153 15
## MM 33 69
test.error.rate.OJ = (33+15)/(153+33+15+69)
test.error.rate.OJ
## [1] 0.1777778
The training error rate is 0.175 and the test error rate is 0.177
set.seed(1)
tuned.svm.OJ = tune.svm(Purchase~., data = train.OJ, kernel = "linear", cost = seq(.01, 10, by =.1))
summary(tuned.svm.OJ)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.51
##
## - best performance: 0.16875
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01 0.17625 0.02853482
## 2 0.11 0.17125 0.02889757
## 3 0.21 0.17125 0.02829041
## 4 0.31 0.17125 0.02889757
## 5 0.41 0.17000 0.02713137
## 6 0.51 0.16875 0.02651650
## 7 0.61 0.17125 0.02703521
## 8 0.71 0.16875 0.02779513
## 9 0.81 0.17000 0.02648375
## 10 0.91 0.17250 0.02687419
## 11 1.01 0.17500 0.02946278
## 12 1.11 0.17500 0.02946278
## 13 1.21 0.17500 0.02763854
## 14 1.31 0.17500 0.02763854
## 15 1.41 0.17375 0.02853482
## 16 1.51 0.17375 0.02853482
## 17 1.61 0.17250 0.02813657
## 18 1.71 0.17250 0.02813657
## 19 1.81 0.17500 0.02763854
## 20 1.91 0.17250 0.02874698
## 21 2.01 0.17250 0.02874698
## 22 2.11 0.17250 0.02874698
## 23 2.21 0.17125 0.03064696
## 24 2.31 0.17250 0.03216710
## 25 2.41 0.17000 0.03395258
## 26 2.51 0.17125 0.03283481
## 27 2.61 0.16875 0.03397814
## 28 2.71 0.17000 0.03291403
## 29 2.81 0.17000 0.03291403
## 30 2.91 0.17000 0.03291403
## 31 3.01 0.16875 0.03019037
## 32 3.11 0.16875 0.03019037
## 33 3.21 0.16875 0.03019037
## 34 3.31 0.16875 0.02960973
## 35 3.41 0.16875 0.02960973
## 36 3.51 0.16875 0.02960973
## 37 3.61 0.17000 0.02958040
## 38 3.71 0.17000 0.02958040
## 39 3.81 0.17000 0.02958040
## 40 3.91 0.17000 0.02958040
## 41 4.01 0.17000 0.02958040
## 42 4.11 0.17000 0.02958040
## 43 4.21 0.17000 0.02958040
## 44 4.31 0.17125 0.03064696
## 45 4.41 0.17125 0.03064696
## 46 4.51 0.17000 0.03073181
## 47 4.61 0.17125 0.03175973
## 48 4.71 0.17125 0.03175973
## 49 4.81 0.17125 0.03175973
## 50 4.91 0.17125 0.03175973
## 51 5.01 0.17250 0.03162278
## 52 5.11 0.17250 0.03162278
## 53 5.21 0.17250 0.03162278
## 54 5.31 0.17250 0.03162278
## 55 5.41 0.17250 0.03162278
## 56 5.51 0.17375 0.03304563
## 57 5.61 0.17250 0.03425801
## 58 5.71 0.17250 0.03425801
## 59 5.81 0.17250 0.03425801
## 60 5.91 0.17375 0.03304563
## 61 6.01 0.17500 0.03333333
## 62 6.11 0.17500 0.03333333
## 63 6.21 0.17500 0.03333333
## 64 6.31 0.17375 0.03197764
## 65 6.41 0.17375 0.03197764
## 66 6.51 0.17375 0.03197764
## 67 6.61 0.17375 0.03197764
## 68 6.71 0.17375 0.03197764
## 69 6.81 0.17375 0.03197764
## 70 6.91 0.17375 0.03197764
## 71 7.01 0.17500 0.03333333
## 72 7.11 0.17375 0.03197764
## 73 7.21 0.17375 0.03197764
## 74 7.31 0.17375 0.03197764
## 75 7.41 0.17375 0.03197764
## 76 7.51 0.17375 0.03197764
## 77 7.61 0.17375 0.03197764
## 78 7.71 0.17375 0.03197764
## 79 7.81 0.17375 0.03197764
## 80 7.91 0.17375 0.03197764
## 81 8.01 0.17375 0.03197764
## 82 8.11 0.17375 0.03197764
## 83 8.21 0.17375 0.03197764
## 84 8.31 0.17375 0.03197764
## 85 8.41 0.17375 0.03197764
## 86 8.51 0.17375 0.03197764
## 87 8.61 0.17375 0.03197764
## 88 8.71 0.17375 0.03197764
## 89 8.81 0.17375 0.03197764
## 90 8.91 0.17375 0.03197764
## 91 9.01 0.17375 0.03197764
## 92 9.11 0.17375 0.03197764
## 93 9.21 0.17375 0.03197764
## 94 9.31 0.17375 0.03197764
## 95 9.41 0.17375 0.03197764
## 96 9.51 0.17375 0.03197764
## 97 9.61 0.17375 0.03197764
## 98 9.71 0.17375 0.03197764
## 99 9.81 0.17375 0.03197764
## 100 9.91 0.17375 0.03197764
tuned.svm.OJ$best.parameters
## cost
## 6 0.51
The best parameter is cost = 0.51 with the lowest error of 0.168.
svm.linear.new <- svm(Purchase~., data = train.OJ, kernel = "linear", cost = 0.51)
train.pred.OJ = predict(svm.linear.new, train.OJ)
table(train.OJ$Purchase, train.pred.OJ)
## train.pred.OJ
## CH MM
## CH 424 61
## MM 71 244
train.error.rate.OJ = (71+61)/(424+71+61+244)
train.error.rate.OJ
## [1] 0.165
test.pred.OJ = predict(svm.linear.new, test.OJ)
table(test.OJ$Purchase, test.pred.OJ)
## test.pred.OJ
## CH MM
## CH 155 13
## MM 29 73
test.error.rate.OJ = (29+13)/(155+29+13+73)
test.error.rate.OJ
## [1] 0.1555556
The training error has reduced from 0.175 to 0.165 and the test error has reduced from 0.177 to 0.155
set.seed(1)
svm.radial.OJ = svm(Purchase~., kernel = 'radial', data = train.OJ)
summary(svm.radial.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
This model has 373 support vectors, of which, 188 belong to class CH and 185 belong to class MM.
train.pred.OJ = predict(svm.radial.OJ, train.OJ)
table(train.OJ$Purchase, train.pred.OJ)
## train.pred.OJ
## CH MM
## CH 441 44
## MM 77 238
train.error.rate.OJ = (77+44)/(441+77+44+238)
train.error.rate.OJ
## [1] 0.15125
test.pred.OJ = predict(svm.radial.OJ, test.OJ)
table(test.OJ$Purchase, test.pred.OJ)
## test.pred.OJ
## CH MM
## CH 151 17
## MM 33 69
test.error.rate.OJ = (33+17)/(151+33+17+69)
test.error.rate.OJ
## [1] 0.1851852
set.seed(1)
tuned.svm.OJ = tune.svm(Purchase~., data = train.OJ, kernel = "radial", cost = seq(.01, 10, by =.1))
summary(tuned.svm.OJ)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.51
##
## - best performance: 0.16625
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01 0.39375 0.04007372
## 2 0.11 0.18625 0.02853482
## 3 0.21 0.18250 0.03238227
## 4 0.31 0.17875 0.03230175
## 5 0.41 0.17625 0.02531057
## 6 0.51 0.16625 0.02433134
## 7 0.61 0.16875 0.02301117
## 8 0.71 0.16750 0.02776389
## 9 0.81 0.17000 0.02513851
## 10 0.91 0.16750 0.02220485
## 11 1.01 0.17125 0.02128673
## 12 1.11 0.17125 0.01958777
## 13 1.21 0.17250 0.02108185
## 14 1.31 0.17375 0.02161050
## 15 1.41 0.17375 0.02389938
## 16 1.51 0.17625 0.02161050
## 17 1.61 0.17625 0.02161050
## 18 1.71 0.17750 0.02188988
## 19 1.81 0.17625 0.02079162
## 20 1.91 0.17625 0.02079162
## 21 2.01 0.17750 0.02188988
## 22 2.11 0.17875 0.02128673
## 23 2.21 0.17875 0.02128673
## 24 2.31 0.17750 0.02266912
## 25 2.41 0.17750 0.02266912
## 26 2.51 0.17625 0.02239947
## 27 2.61 0.17625 0.02239947
## 28 2.71 0.17625 0.02239947
## 29 2.81 0.17625 0.02239947
## 30 2.91 0.17625 0.02239947
## 31 3.01 0.17625 0.02239947
## 32 3.11 0.17625 0.02239947
## 33 3.21 0.17750 0.02266912
## 34 3.31 0.17750 0.02266912
## 35 3.41 0.17875 0.02360703
## 36 3.51 0.17875 0.02360703
## 37 3.61 0.17875 0.02360703
## 38 3.71 0.17875 0.02360703
## 39 3.81 0.18000 0.02371708
## 40 3.91 0.18000 0.02371708
## 41 4.01 0.18125 0.02301117
## 42 4.11 0.18125 0.02301117
## 43 4.21 0.18125 0.02301117
## 44 4.31 0.18125 0.02301117
## 45 4.41 0.18250 0.02297341
## 46 4.51 0.18125 0.02144923
## 47 4.61 0.18125 0.02144923
## 48 4.71 0.18125 0.02144923
## 49 4.81 0.18125 0.02144923
## 50 4.91 0.18000 0.02220485
## 51 5.01 0.18000 0.02220485
## 52 5.11 0.18000 0.02220485
## 53 5.21 0.18000 0.02220485
## 54 5.31 0.18000 0.02220485
## 55 5.41 0.18000 0.02220485
## 56 5.51 0.18000 0.02220485
## 57 5.61 0.18000 0.02220485
## 58 5.71 0.18000 0.02220485
## 59 5.81 0.18000 0.02220485
## 60 5.91 0.18000 0.02220485
## 61 6.01 0.18000 0.02220485
## 62 6.11 0.18000 0.02220485
## 63 6.21 0.18000 0.02220485
## 64 6.31 0.18125 0.02301117
## 65 6.41 0.18125 0.02301117
## 66 6.51 0.18125 0.02301117
## 67 6.61 0.18125 0.02301117
## 68 6.71 0.18125 0.02301117
## 69 6.81 0.18125 0.02301117
## 70 6.91 0.18250 0.02371708
## 71 7.01 0.18375 0.02503470
## 72 7.11 0.18250 0.02443813
## 73 7.21 0.18250 0.02443813
## 74 7.31 0.18250 0.02443813
## 75 7.41 0.18375 0.02638523
## 76 7.51 0.18375 0.02638523
## 77 7.61 0.18375 0.02638523
## 78 7.71 0.18375 0.02638523
## 79 7.81 0.18375 0.02638523
## 80 7.91 0.18375 0.02638523
## 81 8.01 0.18250 0.02648375
## 82 8.11 0.18250 0.02648375
## 83 8.21 0.18125 0.02447363
## 84 8.31 0.18000 0.02443813
## 85 8.41 0.18000 0.02443813
## 86 8.51 0.18000 0.02443813
## 87 8.61 0.18000 0.02443813
## 88 8.71 0.18000 0.02443813
## 89 8.81 0.18375 0.02703521
## 90 8.91 0.18375 0.02703521
## 91 9.01 0.18375 0.02703521
## 92 9.11 0.18375 0.02703521
## 93 9.21 0.18375 0.02703521
## 94 9.31 0.18625 0.02853482
## 95 9.41 0.18625 0.02853482
## 96 9.51 0.18625 0.02853482
## 97 9.61 0.18625 0.02853482
## 98 9.71 0.18625 0.02853482
## 99 9.81 0.18625 0.02853482
## 100 9.91 0.18625 0.02853482
tuned.svm.OJ$best.parameters
## cost
## 6 0.51
svm.linear.new <- svm(Purchase~., data = train.OJ, kernel = "radial", cost = 0.51)
train.pred.OJ = predict(svm.linear.new, train.OJ)
table(train.OJ$Purchase, train.pred.OJ)
## train.pred.OJ
## CH MM
## CH 438 47
## MM 72 243
train.error.rate.OJ = (72+47)/(438+72+47+243)
train.error.rate.OJ
## [1] 0.14875
test.pred.OJ = predict(svm.linear.new, test.OJ)
table(test.OJ$Purchase, test.pred.OJ)
## test.pred.OJ
## CH MM
## CH 150 18
## MM 30 72
test.error.rate.OJ = (30+18)/(150+30+18+72)
test.error.rate.OJ
## [1] 0.1777778
The training error decreased from 0.151 to 0.148 and the testing error decreased from 0.185 to 0.177
set.seed(1)
svm.poly.OJ = svm(Purchase~., kernel = 'polynomial', degree=2, data = train.OJ)
summary(svm.poly.OJ)
##
## Call:
## svm(formula = Purchase ~ ., data = train.OJ, 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
This model has 447 support vectors, of which, 225 belong to class CH and 222 belong to class MM.
train.pred.OJ = predict(svm.poly.OJ, train.OJ)
table(train.OJ$Purchase, train.pred.OJ)
## train.pred.OJ
## CH MM
## CH 449 36
## MM 110 205
train.error.rate.OJ = (110+36)/(449+110+36+205)
train.error.rate.OJ
## [1] 0.1825
test.pred.OJ = predict(svm.poly.OJ, test.OJ)
table(test.OJ$Purchase, test.pred.OJ)
## test.pred.OJ
## CH MM
## CH 153 15
## MM 45 57
test.error.rate.OJ = (45+15)/(153+45+15+57)
test.error.rate.OJ
## [1] 0.2222222
set.seed(1)
tuned.svm.OJ = tune.svm(Purchase~., data = train.OJ, kernel = "polynomial",
degree=2, cost = seq(.01, 10, by =.1))
summary(tuned.svm.OJ)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## degree cost
## 2 2.41
##
## - best performance: 0.17125
##
## - Detailed performance results:
## degree cost error dispersion
## 1 2 0.01 0.39125 0.04210189
## 2 2 0.11 0.32000 0.04866267
## 3 2 0.21 0.22625 0.03839216
## 4 2 0.31 0.20000 0.04208127
## 5 2 0.41 0.20125 0.04185375
## 6 2 0.51 0.20625 0.04497299
## 7 2 0.61 0.20500 0.03961621
## 8 2 0.71 0.20250 0.04031129
## 9 2 0.81 0.20375 0.04251225
## 10 2 0.91 0.20250 0.04479893
## 11 2 1.01 0.20125 0.03928617
## 12 2 1.11 0.20125 0.04016027
## 13 2 1.21 0.19625 0.04411554
## 14 2 1.31 0.19250 0.04495368
## 15 2 1.41 0.19125 0.04411554
## 16 2 1.51 0.19000 0.04322101
## 17 2 1.61 0.18750 0.04330127
## 18 2 1.71 0.18500 0.04199868
## 19 2 1.81 0.18500 0.04199868
## 20 2 1.91 0.18125 0.04177070
## 21 2 2.01 0.18125 0.04177070
## 22 2 2.11 0.17875 0.04041881
## 23 2 2.21 0.17625 0.04016027
## 24 2 2.31 0.17375 0.03793727
## 25 2 2.41 0.17125 0.03729108
## 26 2 2.51 0.17250 0.03809710
## 27 2 2.61 0.17375 0.03884174
## 28 2 2.71 0.17500 0.03818813
## 29 2 2.81 0.17500 0.03818813
## 30 2 2.91 0.17500 0.03818813
## 31 2 3.01 0.17625 0.03793727
## 32 2 3.11 0.17750 0.03670453
## 33 2 3.21 0.18000 0.03545341
## 34 2 3.31 0.18000 0.03545341
## 35 2 3.41 0.17875 0.03586723
## 36 2 3.51 0.17875 0.03586723
## 37 2 3.61 0.18000 0.03545341
## 38 2 3.71 0.17875 0.03537988
## 39 2 3.81 0.18000 0.03395258
## 40 2 3.91 0.18125 0.03498512
## 41 2 4.01 0.18250 0.03395258
## 42 2 4.11 0.18375 0.03438447
## 43 2 4.21 0.18500 0.03425801
## 44 2 4.31 0.18500 0.03425801
## 45 2 4.41 0.18500 0.03425801
## 46 2 4.51 0.18375 0.03387579
## 47 2 4.61 0.18375 0.03387579
## 48 2 4.71 0.18250 0.03496029
## 49 2 4.81 0.18250 0.03496029
## 50 2 4.91 0.18250 0.03496029
## 51 2 5.01 0.18250 0.03496029
## 52 2 5.11 0.18250 0.03496029
## 53 2 5.21 0.18250 0.03496029
## 54 2 5.31 0.18375 0.03537988
## 55 2 5.41 0.18625 0.03143004
## 56 2 5.51 0.18625 0.03143004
## 57 2 5.61 0.18375 0.03064696
## 58 2 5.71 0.18500 0.03162278
## 59 2 5.81 0.18625 0.03304563
## 60 2 5.91 0.18625 0.03304563
## 61 2 6.01 0.18625 0.03304563
## 62 2 6.11 0.18500 0.03162278
## 63 2 6.21 0.18500 0.03162278
## 64 2 6.31 0.18500 0.03162278
## 65 2 6.41 0.18500 0.03162278
## 66 2 6.51 0.18500 0.03162278
## 67 2 6.61 0.18500 0.03162278
## 68 2 6.71 0.18625 0.03356689
## 69 2 6.81 0.18500 0.03162278
## 70 2 6.91 0.18500 0.03162278
## 71 2 7.01 0.18500 0.03162278
## 72 2 7.11 0.18625 0.03251602
## 73 2 7.21 0.18500 0.03525699
## 74 2 7.31 0.18375 0.03387579
## 75 2 7.41 0.18375 0.03387579
## 76 2 7.51 0.18375 0.03387579
## 77 2 7.61 0.18250 0.03291403
## 78 2 7.71 0.18250 0.03291403
## 79 2 7.81 0.18250 0.03291403
## 80 2 7.91 0.18125 0.03448530
## 81 2 8.01 0.18000 0.03395258
## 82 2 8.11 0.18125 0.03240906
## 83 2 8.21 0.18000 0.03129164
## 84 2 8.31 0.18125 0.02841288
## 85 2 8.41 0.18250 0.02898755
## 86 2 8.51 0.18250 0.02898755
## 87 2 8.61 0.18000 0.02838231
## 88 2 8.71 0.17875 0.02766993
## 89 2 8.81 0.17875 0.02766993
## 90 2 8.91 0.17875 0.02766993
## 91 2 9.01 0.17750 0.02751262
## 92 2 9.11 0.17750 0.02751262
## 93 2 9.21 0.17750 0.02751262
## 94 2 9.31 0.17750 0.02751262
## 95 2 9.41 0.17750 0.02751262
## 96 2 9.51 0.17750 0.02751262
## 97 2 9.61 0.17875 0.02766993
## 98 2 9.71 0.17875 0.02766993
## 99 2 9.81 0.17875 0.02766993
## 100 2 9.91 0.18000 0.02838231
tuned.svm.OJ$best.parameters
## degree cost
## 25 2 2.41
svm.poly.new <- svm(Purchase~., data = train.OJ, kernel = "polynomial", degree=2, cost = 2.41)
train.pred.OJ = predict(svm.poly.new, train.OJ)
table(train.OJ$Purchase, train.pred.OJ)
## train.pred.OJ
## CH MM
## CH 452 33
## MM 92 223
train.error.rate.OJ = (92+33)/(452+92+33+223)
train.error.rate.OJ
## [1] 0.15625
test.pred.OJ = predict(svm.poly.new, test.OJ)
table(test.OJ$Purchase, test.pred.OJ)
## test.pred.OJ
## CH MM
## CH 154 14
## MM 42 60
test.error.rate.OJ = (42+14)/(154+42+14+60)
test.error.rate.OJ
## [1] 0.2074074
The training error rate decreased from 0.182 to 0.156 and the testing erorr rate decreased from 0.222 to 0.207.
The model that gives the lowest test error rate is the SVMs using linear kernel. The radial and polynomial kernels gives a higher testing error rate compared to the linear kernel. Hence, we can say that the linear kernel seems to give the best results on this data.