An example on Support Vector Machine.
For this we require package “e1071” and “MASS” for cats dataset.
In this example we will try to predict the sex of cat using body weight and heart weight variables.

require(e1071)
require(MASS)
data(cats)
head(cats)
##   Sex Bwt Hwt
## 1   F 2.0 7.0
## 2   F 2.0 7.4
## 3   F 2.0 9.5
## 4   F 2.1 7.2
## 5   F 2.1 7.3
## 6   F 2.1 7.6
dim(cats)
## [1] 144   3
input.data <- data.frame(cats[, c(2,3)], response = as.factor(cats$Sex))

We build the linear SVM model.

svm.fit <- svm(response ~., data = input.data, kernel = "linear", cost = 10, scale = FALSE)
summary(svm.fit)
## 
## Call:
## svm(formula = response ~ ., data = input.data, kernel = "linear", 
##     cost = 10, scale = FALSE)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  linear 
##        cost:  10 
##       gamma:  0.5 
## 
## Number of Support Vectors:  79
## 
##  ( 39 40 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  F M
plot(svm.fit, input.data)

table(input.data$response, predict(svm.fit))
##    
##      F  M
##   F 33 14
##   M 14 83
mean(input.data$response != predict(svm.fit))
## [1] 0.1944444

We will also build radial SVM model. When a radial kernel is used the resulting hyperplane need not to be a line anymore. A curved region of separation is usually defined to demarcate the separation between classes, often leading to higher accuracy with the data.

svmfit <- svm(response ~., data = input.data, kernel = "radial", cost = 10, scale = FALSE)
summary(svmfit)
## 
## Call:
## svm(formula = response ~ ., data = input.data, kernel = "radial", 
##     cost = 10, scale = FALSE)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  10 
##       gamma:  0.5 
## 
## Number of Support Vectors:  83
## 
##  ( 37 46 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  F M
plot(svmfit, input.data)

table(input.data$response, predict(svmfit))
##    
##      F  M
##   F 36 11
##   M 16 81
mean(input.data$response != predict(svmfit))
## [1] 0.1875

We can find optimal parameters using tune.svm() function. First we’ll separate the data into train and test dataset.

set.seed(300)
train <- sample(1:nrow(input.data), .8*nrow(input.data))
cats.train <- input.data[train, ]
cats.test <- input.data[-train, ]
dim(cats.train)
## [1] 115   3
dim(cats.test)
## [1] 29  3
tuned <- tune.svm(response ~., data = cats.train, cost = 10^(1:2),
                  gamma = 10^(-6:-1))
summary(tuned)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  gamma cost
##    0.1   10
## 
## - best performance: 0.2265152 
## 
## - Detailed performance results:
##    gamma cost     error dispersion
## 1  1e-06   10 0.3227273 0.15722410
## 2  1e-05   10 0.3227273 0.15722410
## 3  1e-04   10 0.3227273 0.15722410
## 4  1e-03   10 0.3227273 0.15722410
## 5  1e-02   10 0.2689394 0.09141623
## 6  1e-01   10 0.2265152 0.07426561
## 7  1e-06  100 0.3227273 0.15722410
## 8  1e-05  100 0.3227273 0.15722410
## 9  1e-04  100 0.3227273 0.15722410
## 10 1e-03  100 0.2689394 0.09141623
## 11 1e-02  100 0.2280303 0.08068567
## 12 1e-01  100 0.2439394 0.05756247

We will use the parameters that we got from tuned model.

new.svmfit <- svm(response ~., data = cats.train, kernel = "radial",
                  cost = 10, gamma = 0.1, scale = FALSE)
summary(new.svmfit)
## 
## Call:
## svm(formula = response ~ ., data = cats.train, kernel = "radial", 
##     cost = 10, gamma = 0.1, scale = FALSE)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  10 
##       gamma:  0.1 
## 
## Number of Support Vectors:  69
## 
##  ( 36 33 )
## 
## 
## Number of Classes:  2 
## 
## Levels: 
##  F M
plot(new.svmfit, cats.train)

table(cats.test$response, predict(new.svmfit, cats.test))
##    
##      F  M
##   F  7  3
##   M  1 18
mean(cats.test$response != predict(new.svmfit, cats.test))
## [1] 0.137931

We will also make grid plot. A 2-coloured grid plot, makes is visually clear which regions of the plot is designated to which class of response by the SVM classifier.

n_points_in_grid = 60
x_axis_range <- range(input.data[, 2])
y_axis_range <- range(input.data[, 1])
x_grid_points <- seq(from = x_axis_range[1], to = x_axis_range[2],
                     length = n_points_in_grid)
y_grid_points <- seq(from = y_axis_range[1], to = y_axis_range[2],
                     length = n_points_in_grid)
all_grid_points <- expand.grid(x_grid_points, y_grid_points)
names(all_grid_points) <- c("Hwt", "Bwt")
all_points_predicted <- predict(new.svmfit, all_grid_points)
colour_array <- c("red", "blue")[as.numeric(all_points_predicted)]
plot(all_grid_points, col = colour_array, pch = 20, cex = 0.50)
points(x = cats.train$Hwt, y = cats.train$Bwt, col = c("red", "blue")[as.numeric(cats.train$Sex)], pch = 20)
points(cats.train[new.svmfit$index, c(2, 1)], pch = 5, cex = 1)