# How to obtain decision boundaries from linear SVM in R?

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? ## 1 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.

• Thanks, but my question is about how I get the w and b values. Also, about the decision value, I'm asking how e1071 decides which given label is positive and negative for the training data when you pass it factors. – reisner Apr 13 '12 at 17:25
• I'm not familiar with e1071, but I know SVM. Whether a class is considered positive or negative won't affect the outcome. You could take any dataset and swap the class labels and you should still get the same result in terms of classifying test points (for the same parameters). The positive and negative are used to determine if the instance falls on the right side of the decision boundary. – karenu May 9 '12 at 21:59