How to do it...

To classify using SVM, follow these steps:

  1. Load the e1071 and caret packages:
> library(e1071) 
> library(caret)
  1. Read the data:
> bn <- read.csv("banknote-authentication.csv") 
  1. Convert the outcome variable class to a factor:
> bn$class <- factor(bn$class) 
  1. Partition the data:
> set.seed(1000) 
> t.idx <- createDataPartition(bn$class, p=0.7, list=FALSE) 
  1. Build the model:
> mod <- svm(class ~ ., data = bn[t.idx,]) 
  1. Check model performance on training data by generating an error/classification confusion matrix:
> table(bn[t.idx,"class"], fitted(mod), dnn = c("Actual", "Predicted")) 
      Predicted 
Actual   0   1 
     0 534   0 
     1   0 427 
  1. Check model performance on the validation partition:
> pred <- predict(mod, bn[-t.idx,]) > table(bn[-t.idx, ...

Get R Data Analysis Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.