# SVM ILLUSTRATION
library(ggplot2);library(kernlab)
## Warning: package 'kernlab' was built under R version 3.1.2
n=30; x1=matrix(runif(2*n), n, 2); y1=rep(1,n); x2=1.3 + matrix(runif(2*n),n,2); y2=rep(-1,n);
data=data.frame(x=c(x1[,1], x2[,1]), y=c(x1[,2], x2[,2]), class=factor(c(y1,y2)))
head(data); tail(data)
## x y class
## 1 0.66328818 0.7314241 1
## 2 0.47605013 0.7824353 1
## 3 0.01454671 0.7698725 1
## 4 0.19860358 0.4793752 1
## 5 0.19975897 0.0328325 1
## 6 0.31415543 0.0209978 1
## x y class
## 55 1.859023 1.762381 -1
## 56 2.007304 1.854036 -1
## 57 1.439992 2.247808 -1
## 58 1.904903 2.053389 -1
## 59 2.090746 1.636583 -1
## 60 2.140494 1.921421 -1
ggplot(data, aes(x,y, color=class)) +
geom_point(size=3) +
geom_abline(intercept=2, slope=-0.5) +
geom_abline(intercept=2.3, slope=-0.9) + geom_abline(intercept=1.4, slope=-0.3) + geom_abline(intercept=2.3, slope=-0.8) + geom_abline(intercept=2.1, slope=-0.7)

svm_model= ksvm(class ~ x+ y, data=data, kernel='vanilladot')
## Setting default kernel parameters
alpha(svm_model)
## [[1]]
## [1] 1.0000000 0.7551954 0.7551954 1.0000000
coef(svm_model)
## [[1]]
## [1] 1.0000000 0.7551954 -0.7551954 -1.0000000
b(svm_model)
## [1] 0.08323978
w = colSums(coef(svm_model)[[1]] * data[alphaindex(svm_model)[[1]],c('x','y')])
b = b(svm_model)
data[, 1:2] = sapply(data[,1:2], scale)
ggplot(data,aes(x, y, color=class)) +
geom_point(size=3) + geom_abline(intercept=b/w[1], slope=-w[2]/w[1]) +
geom_abline(intercept=(b+1)/w[1], slope=-w[2]/w[1], linetype=2)

plot(svm_model, data=data)

##Redo with smaller cost factor C=0.1
svm_model= ksvm(class ~ x+ y, data=data, kernel='vanilladot')
## Setting default kernel parameters
alpha(svm_model); coef(svm_model); b(svm_model)
## [[1]]
## [1] 1.0000000 0.7551954 0.7551954 1.0000000
## [[1]]
## [1] 1.0000000 0.7551954 -0.7551954 -1.0000000
## [1] 0.08323978
w = colSums(coef(svm_model)[[1]] * data[alphaindex(svm_model)[[1]],c('x','y')]); b = b(svm_model)
data[, 1:2] = sapply(data[,1:2], scale)
plot(svm_model, data=data)

###### SVM example 2 - IRIS #######
library("kernlab")
data(iris)
mod <- ksvm(Species ~., data=iris, type= "C-bsvc",
kernel = "rbfdot", kpar=list(sigma=0.1), C=10,
prob.model=TRUE)
mod
## Support Vector Machine object of class "ksvm"
##
## SV type: C-bsvc (classification)
## parameter : cost C = 10
##
## Gaussian Radial Basis kernel function.
## Hyperparameter : sigma = 0.1
##
## Number of Support Vectors : 32
##
## Objective Function Value : -5.8442 -3.0652 -136.9786
## Training error : 0.02
## Probability model included.
predict(mod, iris[c(3, 10, 56, 68, 107, 120), -5]
,type="probabilities")
## setosa versicolor virginica
## [1,] 0.985375867 0.007939411 0.006684722
## [2,] 0.982069560 0.010878547 0.007051893
## [3,] 0.005640800 0.941928275 0.052430925
## [4,] 0.009779772 0.986603958 0.003616270
## [5,] 0.013153484 0.093614800 0.893231716
## [6,] 0.011864412 0.175161211 0.812974377
predict(mod, iris[c(3, 10, 56, 68, 107, 120), -5]
, type = "decision")
## [,1] [,2] [,3]
## [1,] -1.460398 -1.1910251 -3.8868836
## [2,] -1.357355 -1.1749491 -4.2107843
## [3,] 1.647272 0.7655001 -1.3205306
## [4,] 1.412721 0.4736201 -2.7521640
## [5,] 1.844763 1.0000000 1.0000019
## [6,] 1.848985 1.0069010 0.6742889
#2
k <- function(x, y) { (sum(x * y) + 1) * exp(0.001 * sum((x - y)^2))}
class(k) <- "kernel"
data("promotergene")
gene <- ksvm(Class ~ ., data = promotergene,
kernel = k, C = 1, cross = 5)
gene
## Support Vector Machine object of class "ksvm"
##
## SV type: C-svc (classification)
## parameter : cost C = 1
##
##
## Number of Support Vectors : 64
##
## Objective Function Value : -52.7603
## Training error : 0.04717
## Cross validation error : 0.141558
x <- rbind(matrix(rnorm(120), , 2), matrix(rnorm(120, mean = 3), , 2))