svm {e1071}R Documentation

Support Vector Machines

Description

svm is used to train a support vector machine. It can be used to carry out general regression and 3 types of classification. At the moment, no more than 2 classes can be classified.

Usage

svm(x, y, svm.type=NULL, kernel.type="radial", degree=3, gamma=1/dim(x)[2],
coef0=0, cost=1, nu=0.5, cachesize=40, tolerance=0.001, epsilon=0.5,
shrinking=TRUE)
summary (svm.obj)

Arguments

x A data matrix.
y A response vector with one label for each row of x. Can be either a factor or a numeric vector.
svm.type svm can either be used as a classification machine or as a regresson machine. Depending of whether y is a factor or not, the default setting for svm.type is C-classification or regression, respectively, but may be overwritten by setting an explicit value.
Valid options are:
  • C-classification
  • nu-classification
  • one-classification
  • regression
kernel.type The kernel used in training and predicting. You might consider changing some of the following parameters, depending on the kernel type.
linear:
u'*v
polynomial:
(gamma*u'*v + coef0)^degree
radial basis:
exp(-gamma*|u-v|^2)
sigmoid:
tanh(gamma*u'*v + coef0)
degree parameter needed for kernel of type polynomial (default: 3)
gamma parameter needed for all kernels except linear (default: 1/(data dimension))
coef0 parameter needed for kernels of type polynomial and sigmoid (default: 0)
cost cost of constraints violation. (default: 1)
nu parameter needed for nu-classification and one-classification
cachesize cache memory in MB. (default 40)
tolerance tolerance of termination criterion (default: 0.001)
epsilon epsilon in the insensitive-loss function (default: 0.5)
shrinking option whether to use the shrinking-heuristics (default: TRUE)
svm.obj An object of type svm as returned by svm

Value

An object of class svm containing the fitted model, especially:

sv the resulting support vectors
index the index of the resulting support vectors in the data matrix
coefs the corresponding coefficiants

(Use summary and print to get some output).

Author(s)

David Meyer (based on C++-code by Chih-Chung Chang and Chih-Jen Lin)
david.meyer@ci.tuwien.ac.at

References

See Also

predict.svm

Examples

data(iris)
# amputate data to two factors
iris.sub <- subset(iris, Species != "virginica")

# get independent vars
x <- subset (iris.sub, select = -Species)

# get responses
y <- iris.sub[,"Species"]

# coercion needed for correct factor levels
y <- as.factor(as.character(y))

# default with factor response: classification mode
model <- svm (x, y)
print (model)
summary (model)

# test with train data
pred <- predict (model, x)

# should be TRUE:
all.equal (pred, y)

# try regression mode on two dimensions in linear mode
model <- svm (x[,"Petal.Length"], x[,"Petal.Width"],
svm.type="regression", kernel.type="linear")
print (model)

pred <- predict (model,x[,"Petal.Length"])

par (mfcol=c(1,2))
plot(x[,"Petal.Length"],x[,"Petal.Width"])
plot(x[,"Petal.Length"],pred)