views:

1051

answers:

2

I have an svm in R and I would now like to plot the classificaiton 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

+7  A: 

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
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
Could somebody tell me which package I need to install to use svm in R?
sunqiang
The package is e1071. I've added the command to load the package to the code above
StompChicken
+5  A: 

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