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

Van Kerckhoven, Johan Johan.VanKerckhoven at econ.kuleuven.be
Mon Jul 3 08:52:44 CEST 2006


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?

Signed,

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

----------------------------------------------------------------------

#initialization of the problem

rm(list=ls())

library(e1071)

set.seed(2)

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



More information about the R-help mailing list