Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

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?

share|improve this question
1  
I provided some information regarding that on a related thread: Computing the decision boundary of a linear SVM model. – chl May 6 '12 at 20:49

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.

share|improve this answer
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.