Solved – How to obtain decision boundaries from linear SVM in R

e1071rsvm

I am in need of a package that can give me the equation for a linear SVM model. Currently I'm using e1071 like so:

library(e1071)
m = svm(data, labels, type='C', kernel='linear', cost=cost, probability=FALSE, scale=scale)
w = t(m$coefs) %*% data[m$index,]  #Weight vector
b = -model$rho #Offset

However, I'm not sure how e1071::svm() selects positive and negative classes, so I think this might screw up with different data sets. Can anyone confirm how this function decides which class is positive and which one is negative?

Also, is there a better package for this?

Best Answer

For data point $x$ your SVM calculates decision value $d$ in the following way:

d <- sum(w * x) + b

If $d > 0$ then label of $x$ is $+1$, else it's $-1$. You can also get labels or decision values for data matrix newdata by saying

predict(m, newdata)

or

predict(m, newdata, decision.values = TRUE)

Be cautious when using SVM from package e1071, see Problem with e1071 libsvm? question. Several other SVM packages for R are kernlab, klaR and svmpath, see this overview: Support Vector Machines in R by A. Karatzoglou and D. Meyer.