How do I plot a classification graph of a SVM in R


Question

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

1
11
11/5/2011 9:04:07 PM

Accepted Answer

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)
17
7/18/2009 10:52:43 AM

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon