12

I have an SVM in R and I would now like to plot the classification space for this machine. I have found some examples on the Internet, but I can't seem to make sense of them.

My R script is as follows:

library(e1071)
day_of_week <- c(0,1,2,3,4,5,6)
holiday <- factor( c(T, F, F, F, F, F, T) )
model <- svm(day_of_week, holiday)
plot(model, day_of_week, holiday)

I cannot get the plot command to work. I would like a graph something like this http://bm2.genes.nig.ac.jp/RGM2/R_current/library/e1071/man/images/plot.svm_001.png

Iterator
  • 20,250
  • 12
  • 75
  • 111
Spacen Jasset
  • 938
  • 2
  • 11
  • 21

2 Answers2

18

First of all, the plot.svm function assumes that the data varies across two dimensions. The data you have used in your example is only one-dimensional and so the decision boundary would have to be plotted on a line, which isn't supported. Secondly, the function seems to need a data frame as input and you are working with vectors.

This should work...

library(e1071)

day = c(0,1,2,3,4,5,6)
weather = c(1,0,0,0,0,0,0)
happy = factor(c(T,F,F,F,F,F,F))

d = data.frame(day=day, weather=weather, happy=happy)
model = svm(happy ~ day + weather, data = d)
plot(model, d)
Stompchicken
  • 15,833
  • 1
  • 33
  • 38
  • Thanks, it seems I need to be familiar with the ~ operator which relates to formula. I had assumed that given a svm object it would be able to render it's classification spaces without further direction. – Spacen Jasset Jul 17 '09 at 13:50
  • Could somebody tell me which package I need to install to use svm in R? – sunqiang Jul 17 '09 at 22:32
  • 1
    The package is e1071. I've added the command to load the package to the code above – Stompchicken Jul 18 '09 at 10:54
13

Alternatively, you can use the kernlab package:

library(kernlab)

model.ksvm = ksvm(happy ~ day + weather, data = d, type="C-svc")
plot(model.ksvm, data=d)
Paolo
  • 2,795
  • 1
  • 20
  • 23