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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.