> x1=runif(500)-0.5
> x2=runif(500)-0.5
> y=1*(x1^2-x2^2 > 0)
set.seed(1)
x1 <- runif(500) - 0.5
x2 <- runif(500) - 0.5
y <- 1 * (x1^2 - x2^2 > 0)
#Construct the data frame with deviance and size
df.svm.x12 <- data.frame(x1 = x1,
x2 = x2,
y = y)
##Use the ggplot
ggplot(df.svm.x12, aes(x = x1, y = x2, color = y)) +
geom_point(size = 2) +
theme(panel.background = element_rect(fill = 'white', colour = 'red'), legend.position = "none") +
labs(title = "Non-linear Decision Boundary",
x = "x1",
y = "x2")
glm.svm.x12 <- glm(y ~ x1 + x2, family = "binomial")
summary(glm.svm.x12)
##
## Call:
## glm(formula = y ~ x1 + x2, family = "binomial")
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.179 -1.139 -1.112 1.206 1.257
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -0.087260 0.089579 -0.974 0.330
## x1 0.196199 0.316864 0.619 0.536
## x2 -0.002854 0.305712 -0.009 0.993
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 692.18 on 499 degrees of freedom
## Residual deviance: 691.79 on 497 degrees of freedom
## AIC: 697.79
##
## Number of Fisher Scoring iterations: 3
From the above summary, we can identify that none of the predictors are significant against y.
svm.probs1 <- predict(glm.svm.x12, df.svm.x12, type = "response")
svm.preds1 <- rep(0, 500)
svm.preds1[svm.probs1 > 0.47] <- 1
plot(df.svm.x12[svm.preds1 == 1, ]$x1, df.svm.x12[svm.preds1 == 1, ]$x2, col = "red", pch = "+", xlab = "X1", ylab = "X2")
points(df.svm.x12[svm.preds1 == 0, ]$x1, df.svm.x12[svm.preds1 == 0, ]$x2, col = "blue", pch = 4)
Using the few transformations on the x1 and x2 and use them as predictors in the logistic regression model.
log.x12.fit <- glm(y ~ poly(x1, 2) + poly(x2, 2) + I(x1 * x2), family = "binomial")
summary(log.x12.fit)
##
## Call:
## glm(formula = y ~ poly(x1, 2) + poly(x2, 2) + I(x1 * x2), family = "binomial")
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -8.240e-04 -2.000e-08 -2.000e-08 2.000e-08 1.163e-03
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -102.2 4302.0 -0.024 0.981
## poly(x1, 2)1 2715.3 141109.5 0.019 0.985
## poly(x1, 2)2 27218.5 842987.2 0.032 0.974
## poly(x2, 2)1 -279.7 97160.4 -0.003 0.998
## poly(x2, 2)2 -28693.0 875451.3 -0.033 0.974
## I(x1 * x2) -206.4 41802.8 -0.005 0.996
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 6.9218e+02 on 499 degrees of freedom
## Residual deviance: 3.5810e-06 on 494 degrees of freedom
## AIC: 12
##
## Number of Fisher Scoring iterations: 25
Again none of the predictors are statistically significant with y.
svm.probs2 <- predict(log.x12.fit, df.svm.x12, type = "response")
svm.preds2 <- rep(0, 500)
svm.preds2[svm.probs2 > 0.47] <- 1
plot(df.svm.x12[svm.preds2 == 1, ]$x1, df.svm.x12[svm.preds2 == 1, ]$x2, col = "red", pch = "+", xlab = "X1", ylab = "X2")
points(df.svm.x12[svm.preds2 == 0, ]$x1, df.svm.x12[svm.preds2 == 0, ]$x2, col = "blue", pch = 4)
The above plot shows that the decision boundary is non-linear.
df.svm.x12$y <- as.factor(df.svm.x12$y)
svm.x12.fit1 <- svm(y ~ x1 + x2, df.svm.x12, kernel = "linear", cost = 0.01)
svm.x12.preds1 <- predict(svm.x12.fit1, df.svm.x12)
plot(df.svm.x12[svm.x12.preds1 == 0, ]$x1, df.svm.x12[svm.x12.preds1 == 0, ]$x2, col = "red", pch = "+", xlab = "X1", ylab = "X2")
points(df.svm.x12[svm.x12.preds1 == 1, ]$x1, df.svm.x12[svm.x12.preds1 == 1, ]$x2, col = "blue", pch = 4)
The above plot shows that even with the low cost, the linear kernel fails to find non-linear decision boundary and it classifies all points to a single class.
svm.x12.fit2 <- svm(as.factor(y) ~ x1 + x2, df.svm.x12, gamma = 1)
svm.x12.preds2 <- predict(svm.x12.fit2, df.svm.x12)
df.svm.x12.pos <- df.svm.x12[svm.x12.preds2 == 1, ]
df.svm.x12.neg <- df.svm.x12[svm.x12.preds2 == 0, ]
plot(df.svm.x12.pos$x1, df.svm.x12.pos$x2, col = "red", xlab = "X1", ylab = "X2", pch = "+")
points(df.svm.x12.neg$x1, df.svm.x12.neg$x2, col = "blue", pch = 4)
The above plot shows that the non-linear decision boundary closely resembles the true decision boundary.
From all the above regression models, SVM with non-linear kernel and logistic regression with interaction terms are very powerful in finding the non-linear boundary, and the SVM with linear kernel are NOT good in finding non-linear decision boundaries. In order to make the logistic regression SVM with right interaction terms to find the non-linear decision boundaries, we need to fine tune gamma.
Auto data set.myAuto <- Auto
myAuto <- mutate(myAuto, mpg.median.level = as.factor(ifelse(Auto$mpg > median(Auto$mpg), 1, 0)))
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.set.seed(1)
auto.cost <- c(0.01, 0.1, 1, 10, 100, 1000)
tune.auto.linear <- tune(svm, mpg.median.level ~ ., data = myAuto, kernel = "linear", ranges = list(cost = auto.cost))
summary(tune.auto.linear)
##
## 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 1e+01 0.02051282 0.02648194
## 5 1e+02 0.03076923 0.03151981
## 6 1e+03 0.03076923 0.03151981
The above result shows the cross-validation errors of different cost, i.e., for the costs 0.01, 0.1, 1, 10, 100, 1000 their corresponding cross-validation errors are 0.07653846, 0.04596154, 0.01025641, 0.02051282, 0.03076923, 0.03076923 respectively. The lowest cross-validation error is 0.01025641, which is for the cost = 1 (perform the best) for the linear kernel.
gamma and degree and cost. Comment on your results.Running the SVM with radial kernel with different gamma and cost.
set.seed(1)
auto.gamma <- c(0.01, 0.1, 1, 5, 10, 100)
tune.auto.radial <- tune(svm, mpg.median.level ~ ., data = myAuto, kernel = "radial", ranges = list(cost = auto.cost, gamma = auto.gamma))
summary(tune.auto.radial)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost gamma
## 100 0.01
##
## - best performance: 0.01282051
##
## - Detailed performance results:
## cost gamma error dispersion
## 1 1e-02 1e-02 0.55115385 0.04366593
## 2 1e-01 1e-02 0.08929487 0.04382379
## 3 1e+00 1e-02 0.07403846 0.03522110
## 4 1e+01 1e-02 0.02557692 0.02093679
## 5 1e+02 1e-02 0.01282051 0.01813094
## 6 1e+03 1e-02 0.02820513 0.02549818
## 7 1e-02 1e-01 0.21711538 0.09865227
## 8 1e-01 1e-01 0.07903846 0.03874545
## 9 1e+00 1e-01 0.05371795 0.03525162
## 10 1e+01 1e-01 0.03076923 0.03375798
## 11 1e+02 1e-01 0.03583333 0.02759051
## 12 1e+03 1e-01 0.03583333 0.02759051
## 13 1e-02 1e+00 0.55115385 0.04366593
## 14 1e-01 1e+00 0.55115385 0.04366593
## 15 1e+00 1e+00 0.06384615 0.04375618
## 16 1e+01 1e+00 0.05884615 0.04020934
## 17 1e+02 1e+00 0.05884615 0.04020934
## 18 1e+03 1e+00 0.05884615 0.04020934
## 19 1e-02 5e+00 0.55115385 0.04366593
## 20 1e-01 5e+00 0.55115385 0.04366593
## 21 1e+00 5e+00 0.49493590 0.04724924
## 22 1e+01 5e+00 0.48217949 0.05470903
## 23 1e+02 5e+00 0.48217949 0.05470903
## 24 1e+03 5e+00 0.48217949 0.05470903
## 25 1e-02 1e+01 0.55115385 0.04366593
## 26 1e-01 1e+01 0.55115385 0.04366593
## 27 1e+00 1e+01 0.51794872 0.05063697
## 28 1e+01 1e+01 0.51794872 0.04917316
## 29 1e+02 1e+01 0.51794872 0.04917316
## 30 1e+03 1e+01 0.51794872 0.04917316
## 31 1e-02 1e+02 0.55115385 0.04366593
## 32 1e-01 1e+02 0.55115385 0.04366593
## 33 1e+00 1e+02 0.55115385 0.04366593
## 34 1e+01 1e+02 0.55115385 0.04366593
## 35 1e+02 1e+02 0.55115385 0.04366593
## 36 1e+03 1e+02 0.55115385 0.04366593
The lowest cross-validation error (0.01282051) for the radial kernel is for cost = 100 and gamma = 0.01.
set.seed(1)
auto.degree <- c(2, 3, 4)
tune.auto.poly <- tune(svm, mpg.median.level ~ ., data = myAuto, kernel = "polynomial", ranges = list(cost = auto.cost, degree = auto.degree))
summary(tune.auto.poly)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost degree
## 1000 2
##
## - best performance: 0.2454487
##
## - Detailed performance results:
## cost degree error dispersion
## 1 1e-02 2 0.5511538 0.04366593
## 2 1e-01 2 0.5511538 0.04366593
## 3 1e+00 2 0.5511538 0.04366593
## 4 1e+01 2 0.5130128 0.08963366
## 5 1e+02 2 0.3013462 0.09961961
## 6 1e+03 2 0.2454487 0.11551451
## 7 1e-02 3 0.5511538 0.04366593
## 8 1e-01 3 0.5511538 0.04366593
## 9 1e+00 3 0.5511538 0.04366593
## 10 1e+01 3 0.5511538 0.04366593
## 11 1e+02 3 0.3446154 0.09821588
## 12 1e+03 3 0.2528846 0.09383590
## 13 1e-02 4 0.5511538 0.04366593
## 14 1e-01 4 0.5511538 0.04366593
## 15 1e+00 4 0.5511538 0.04366593
## 16 1e+01 4 0.5511538 0.04366593
## 17 1e+02 4 0.5511538 0.04366593
## 18 1e+03 4 0.5435897 0.05056569
The lowest cross-validation error (0.2454487) for the polynomial kernel is for cost = 1000 and degree = 2.
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.
Fit the models with the kernels (linear, radial and polynomial) with those cost, gamma and degree for which we got the lowest cross-validation errors in (b) and (c).
svm.auto.best.linear <- svm(mpg.median.level ~ ., data = myAuto, kernel = "linear", cost = 1)
svm.auto.best.radial <- svm(mpg.median.level ~ ., data = myAuto, kernel = "radial", cost = 100, gamma = 0.01)
svm.auto.best.poly <- svm(mpg.median.level ~ ., data = myAuto, kernel = "polynomial", cost = 1000, degree = 2)
Writing a function to plot the pair of predictors with the mpg.median.level so that we can apply for all the three models (models with 3 different kernels).
plotpairedpredictors = function(model.fit) {
for (name in names(myAuto)[!(names(Auto) %in% c("mpg", "mpg.median.level", "name"))]) {
plot(model.fit, myAuto, as.formula(paste("mpg~", name, sep = "")))
}
}
Plot the paired predictors for linear kernel model.
plotpairedpredictors(svm.auto.best.linear)
Plot the paired predictors for `radial`` kernel model.
plotpairedpredictors(svm.auto.best.radial)
Plot the paired predictors for polynomial kernel model.
plotpairedpredictors(svm.auto.best.poly)
OJ data set which is part of the ISLR package.set.seed(1)
train <- sample(nrow(OJ), 800)
oj.train <- OJ[train, ]
oj.test <- OJ[-train, ]
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.oj.linear <- svm(Purchase ~ ., data = oj.train, kernel = "linear", cost = 0.01)
summary(svm.oj.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: 435
##
## ( 219 216 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
The support vector classifier svm using the training data and with cost 0.01 created 435 support vectors with 2 classes, that’s CH level with 219 support vectors and MM level with 216 support vectors.
train.oj.pred <- predict(svm.oj.linear, oj.train)
table(oj.train$Purchase, train.oj.pred)
## train.oj.pred
## CH MM
## CH 420 65
## MM 75 240
The training error rate is (75+65)/(420+240+75+65) = 0.175, 17.5%.
test.oj.pred <- predict(svm.oj.linear, oj.test)
table(oj.test$Purchase, test.oj.pred)
## test.oj.pred
## CH MM
## CH 153 15
## MM 33 69
The test error rate is (33+15)/(153+69+33+15) = 0.178, 17.8%.
tune() function to select an optimal cost. Consider values in the range 0.01 to 10.set.seed(1)
oj.cost <- c(0.01, 0.05, 0.1, 0.5, 1, 5, 10)
tune.oj.linear <- tune(svm, Purchase ~ ., data = oj.train, kernel = "linear", ranges = list(cost = oj.cost))
summary(tune.oj.linear)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.5
##
## - best performance: 0.16875
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01 0.17625 0.02853482
## 2 0.05 0.17625 0.02853482
## 3 0.10 0.17250 0.03162278
## 4 0.50 0.16875 0.02651650
## 5 1.00 0.17500 0.02946278
## 6 5.00 0.17250 0.03162278
## 7 10.00 0.17375 0.03197764
From the above tune() function summary, we see that the optimal cost is 0.5 as it has the lowest error rate 0.16875.
cost.svm.oj.cost.linear <- svm(Purchase ~ ., kernel = "linear", data = oj.train,
cost = tune.oj.linear$best.parameter$cost)
train.oj.cost.pred <- predict(svm.oj.cost.linear, oj.train)
table(oj.train$Purchase, train.oj.cost.pred)
## train.oj.cost.pred
## CH MM
## CH 424 61
## MM 71 244
The training error rate is (71+61)/(424+244+71+61) = 0.165, 16.5%.
test.oj.cost.pred <- predict(svm.oj.cost.linear, oj.test)
table(oj.test$Purchase, test.oj.cost.pred)
## test.oj.cost.pred
## CH MM
## CH 155 13
## MM 29 73
The test error rate is (29+13)/(155+73+29+13) = 0.156, 15.6%.
gamma.set.seed(1)
svm.oj.radial <- svm(Purchase ~ ., kernel = "radial", data = oj.train)
summary(svm.oj.radial)
##
## 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
The SVM radial kernel with default gamma model creates 373 support vectors, with 188 support vectors for the level CH and remaining 185 support vectors for the level MM.
train.radial.pred <- predict(svm.oj.radial, oj.train)
table(oj.train$Purchase, train.radial.pred)
## train.radial.pred
## CH MM
## CH 441 44
## MM 77 238
The training error rate is (77+44)/(441+238+77+44) = 0.151, 15.1%.
test.radial.pred <- predict(svm.oj.radial, oj.test)
table(oj.test$Purchase, test.radial.pred)
## test.radial.pred
## CH MM
## CH 151 17
## MM 33 69
The test error rate is (33+17)/(151+69+33+17) = 0.185, 18.5%.
The training error rate of the svm with radial and with default gamma is good improvement from the linear kernel, but the test error rate is bigger than the linear.
Lets try to find the optimal cost using the cross-validation for the radial kernel.
set.seed(1)
tune.radial.out <- tune(svm, Purchase ~ ., data = oj.train, kernel = "radial",
ranges = list(cost = oj.cost))
summary(tune.radial.out)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost
## 0.5
##
## - best performance: 0.1675
##
## - Detailed performance results:
## cost error dispersion
## 1 0.01 0.39375 0.04007372
## 2 0.05 0.20250 0.03374743
## 3 0.10 0.18625 0.02853482
## 4 0.50 0.16750 0.02443813
## 5 1.00 0.17125 0.02128673
## 6 5.00 0.18000 0.02220485
## 7 10.00 0.18625 0.02853482
The optimal cost is 0.5 from the above tune() function for the radial kernel.
set.seed(1)
svm.cost.radial <- svm(Purchase ~ ., kernel = "radial", data = oj.train,
cost = tune.radial.out$best.parameter$cost)
summary(svm.cost.radial)
##
## Call:
## svm(formula = Purchase ~ ., data = oj.train, kernel = "radial", cost = tune.radial.out$best.parameter$cost)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 0.5
##
## Number of Support Vectors: 407
##
## ( 205 202 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
train.radial.cost.pred <- predict(svm.cost.radial, oj.train)
table(oj.train$Purchase, train.radial.cost.pred)
## train.radial.cost.pred
## CH MM
## CH 438 47
## MM 71 244
The training error rate is (71+47)/(438+244+71+47) = 0.148, 14.8%.
test.radial.cost.pred <- predict(svm.cost.radial, oj.test)
table(oj.test$Purchase, test.radial.cost.pred)
## test.radial.cost.pred
## CH MM
## CH 150 18
## MM 30 72
The test error rate is (30+18)/(150+72+30+18) = 0.178, 17.8%.
Both training and test error rates reduced from the previous models with the radial kernel with tuning (that’s with optimal cost of 0.5).
degree=2.set.seed(1)
svm.oj.poly <- svm(Purchase ~ ., kernel = "polynomial", data = oj.train, degree = 2)
summary(svm.oj.poly)
##
## 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
The SVM polynomial kernel with degree=2 model creates 447 support vectors, with 225 support vectors for the level CH and remaining 222 support vectors for the level MM.
train.poly.pred <- predict(svm.oj.poly, oj.train)
table(oj.train$Purchase, train.poly.pred)
## train.poly.pred
## CH MM
## CH 449 36
## MM 110 205
The training error rate is (110+36)/(449+205+110+36) = 0.183, 18.3%.
test.poly.pred <- predict(svm.oj.poly, oj.test)
table(oj.test$Purchase, test.poly.pred)
## test.poly.pred
## CH MM
## CH 153 15
## MM 45 57
The test error rate is (45+15)/(153+57+45+15) = 0.222, 22.2%.
The training and test error rates of the svm with polynomial and with degree = 2 is increased considerably compare to the previous models.
Lets try to find the optimal cost using the cross-validation for the polynomial kernel.
set.seed(1)
tune.poly.out <- tune(svm, Purchase ~ ., data = oj.train, kernel = "polynomial",
degree = 2, ranges = list(cost = oj.cost))
summary(tune.poly.out)
##
## 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.05 0.34875 0.04348132
## 3 0.10 0.32125 0.05001736
## 4 0.50 0.20625 0.04050463
## 5 1.00 0.20250 0.04116363
## 6 5.00 0.18250 0.03496029
## 7 10.00 0.18125 0.02779513
The optimal cost is 10 from the above tune() function for the polynomial kernel.
set.seed(1)
svm.cost.poly <- svm(Purchase ~ ., kernel = "polynomial", data = oj.train,
degree = 2,
cost = tune.poly.out$best.parameter$cost)
summary(svm.cost.poly)
##
## Call:
## svm(formula = Purchase ~ ., data = oj.train, kernel = "polynomial",
## degree = 2, cost = tune.poly.out$best.parameter$cost)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: polynomial
## cost: 10
## degree: 2
## coef.0: 0
##
## Number of Support Vectors: 340
##
## ( 171 169 )
##
##
## Number of Classes: 2
##
## Levels:
## CH MM
train.poly.cost.pred <- predict(svm.cost.poly, oj.train)
table(oj.train$Purchase, train.poly.cost.pred)
## train.poly.cost.pred
## CH MM
## CH 447 38
## MM 82 233
The training error rate is (82+38)/(447+233+82+38) = 0.15, 15.0%.
test.poly.cost.pred <- predict(svm.cost.poly, oj.test)
table(oj.test$Purchase, test.poly.cost.pred)
## test.poly.cost.pred
## CH MM
## CH 154 14
## MM 37 65
The test error rate is (37+14)/(154+65+37+14) = 0.189, 18.9%.
Both training and test error rates reduced a lot from the previous model with the polynomial kernel with tuning (that’s with optimal cost of 10).
The lowest training rate is 14.8% from the tuned radial model with cost 0.5 and the lowest test rate is 15.6% from the tuned ‘linear’ model with cost 0.5.
The tuned radial basis kernel with cost=0.5 has the lowest training error rate of 14.8% and a decent test error rate of 17.8% as compare to all other kernels. So, I would recommend this one seems to give the best results on this data.