[R] Questions concerning function 'svm' in e1071 package

From: Van Kerckhoven, Johan <Johan.VanKerckhoven_at_econ.kuleuven.be>
Date: Mon 03 Jul 2006 - 16:52:44 EST

Greetings everyone,

I have the following problem (illustrating R-code at bottom of mail): Given a training sample with binary outcomes (-1/+1), I train a linear Support Vector Machine to separate them. Afterwards, I compute the weight vector w in the usual way, and obtain the fitted values as w'x + b > 0 ==> yfitted = 1, otherwise -1.

However, upon verifying with the 'predict' method, the outcomes do not match up as they should. I've already tried to find information concerning this issue on the R-help board, but to no avail. Can any of you point me in the right direction?


Johan Van Kerckhoven
ORSTAT and University Center of Statistics
Katholieke Universiteit Leuven

#initialization of the problem




n = 50
d = 4
p = 0.5

x = matrix(rnorm(n*d), ncol=d)

mushift = c(1, -1, rep(0, d-2))

y = runif(n) > p
y = factor(2*y - 1)

x = x - outer(rep(1, n), mushift)
x[y == 1, ] = x[y == 1] + 2*outer(rep(1, sum(y == 1)), mushift)

svclass = svm(x, y, scale=FALSE, kernel="linear")

#Computation of the weight vector

w = t(svclass$coefs) %*% svclass$SV
if (y[1] == -1) {

   w = -w

#Derivation of predicted class lavels

#Using method in documentation

yfit = (x %*% t(w) + svclass$rho) > 0
yfit = factor(2*yfit - 1)

#Extracting them directly from the model
yfit2 = svclass$fitted

#Display where predictions differ from each other
yfit != yfit2

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Jul 03 17:06:56 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 03 Jul 2006 - 18:15:01 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.