Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 212
Spacen Jasset Avatar asked Jul 17 '09 09:07

Spacen Jasset


2 Answers

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)
like image 92
Stompchicken Avatar answered Nov 09 '22 00:11

Stompchicken


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)
like image 29
Paolo Avatar answered Nov 09 '22 00:11

Paolo