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
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With