I'am trying to use svm
from e1071
and before going too far with heavy data I intended to play with toy examples.
Here's what I am doing, and I don't understand why it obviously doesn't work.
# generate some silly 2D data
X = data.frame(x1 = runif(10), x2 = runif(10))
# attach a label according to position above/below diagonal x+y=1
X$y <- rep(1, 10)
X$y[(X$x1 + X$x2)<1] = -1
X$y <- factor(X$y)
# train svm model
require(e1071)
meta <- svm(y~., data = X, kernel = "linear", scale = FALSE)
# visualize the result
plot(meta, X)
So from this point on the graph error is already visible because there are some misclassified points and classifier is not the one I'm expecting (all vectors are supports especially).
If I want to predict then, it's wrong too:
predict(meta, newdata = X[,-3])==X$y
[1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
If I want to do manual prediction I can't get it working too:
omega <- t(meta$coefs)%*%meta$SV
pred <- c(-sign(omega%*%t(X[,-3]) - meta$rho))
pred==X$y
[1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
I'm sure there is something I am missing, but can't figure out what !