Description Usage Arguments Details Value Note Author(s) References See Also Examples
svm
is used to train a support vector machine. It can be used to carry
out general regression and classification (of nu and epsilontype), as
well as densityestimation. A formula interface is provided.
1 2 3 4 5 6 7 8 9 10  ## S3 method for class 'formula'
svm(formula, data = NULL, ..., subset, na.action =
na.omit, scale = TRUE)
## Default S3 method:
svm(x, y = NULL, scale = TRUE, type = NULL, kernel =
"radial", degree = 3, gamma = if (is.vector(x)) 1 else 1 / ncol(x),
coef0 = 0, cost = 1, nu = 0.5,
class.weights = NULL, cachesize = 40, tolerance = 0.001, epsilon = 0.1,
shrinking = TRUE, cross = 0, probability = FALSE, fitted = TRUE,
..., subset, na.action = na.omit)

formula 
a symbolic description of the model to be fit. 
data 
an optional data frame containing the variables in the model. By default the variables are taken from the environment which ‘svm’ is called from. 
x 
a data matrix, a vector, or a sparse matrix (object of class

y 
a response vector with one label for each row/component of

scale 
A logical vector indicating the variables to be
scaled. If 
type 

kernel 
the kernel used in training and predicting. You
might consider changing some of the following parameters, depending
on the kernel type.

degree 
parameter needed for kernel of type 
gamma 
parameter needed for all kernels except 
coef0 
parameter needed for kernels of type 
cost 
cost of constraints violation (default: 1)—it is the ‘C’constant of the regularization term in the Lagrange formulation. 
nu 
parameter needed for 
class.weights 
a named vector of weights for the different
classes, used for asymmetric class sizes. Not all factor levels have
to be supplied (default weight: 1). All components have to be
named. Specifying 
cachesize 
cache memory in MB (default 40) 
tolerance 
tolerance of termination criterion (default: 0.001) 
epsilon 
epsilon in the insensitiveloss function (default: 0.1) 
shrinking 
option whether to use the shrinkingheuristics
(default: 
cross 
if a integer value k>0 is specified, a kfold cross validation on the training data is performed to assess the quality of the model: the accuracy rate for classification and the Mean Squared Error for regression 
fitted 
logical indicating whether the fitted values should be computed
and included in the model or not (default: 
probability 
logical indicating whether the model should allow for probability predictions. 
... 
additional parameters for the low level fitting function

subset 
An index vector specifying the cases to be used in the training sample. (NOTE: If given, this argument must be named.) 
na.action 
A function to specify the action to be taken if 
For multiclassclassification with k levels, k>2, libsvm
uses the
‘oneagainstone’approach, in which k(k1)/2 binary classifiers are
trained; the appropriate class is found by a voting scheme.
libsvm
internally uses a sparse data representation, which is
also highlevel supported by the package SparseM.
If the predictor variables include factors, the formula interface must be used to get a correct model matrix.
plot.svm
allows a simple graphical
visualization of classification models.
The probability model for classification fits a logistic distribution using maximum likelihood to the decision values of all binary classifiers, and computes the aposteriori class probabilities for the multiclass problem using quadratic optimization. The probabilistic regression model assumes (zeromean) laplacedistributed errors for the predictions, and estimates the scale parameter using maximum likelihood.
For linear kernel, the coefficients of the regression/decision hyperplane
can be extracted using the coef
method (see examples).
An object of class "svm"
containing the fitted model, including:
SV 
The resulting support vectors (possibly scaled). 
index 
The index of the resulting support vectors in the data
matrix. Note that this index refers to the preprocessed data (after
the possible effect of 
coefs 
The corresponding coefficients times the training labels. 
rho 
The negative intercept. 
sigma 
In case of a probabilistic regression model, the scale parameter of the hypothesized (zeromean) laplace distribution estimated by maximum likelihood. 
probA, probB 
numeric vectors of length k(k1)/2, k number of classes, containing the parameters of the logistic distributions fitted to the decision values of the binary classifiers (1 / (1 + exp(a x + b))). 
Data are scaled internally, usually yielding better results.
Parameters of SVMmodels usually must be tuned to yield sensible results!
David Meyer (based on C/C++code by ChihChung Chang and ChihJen Lin)
David.Meyer@Rproject.org
Chang, ChihChung and Lin, ChihJen:
LIBSVM: a library for Support Vector Machines
https://www.csie.ntu.edu.tw/~cjlin/libsvm/
Exact formulations of models, algorithms, etc. can be found in the
document:
Chang, ChihChung and Lin, ChihJen:
LIBSVM: a library for Support Vector Machines
https://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.ps.gz
More implementation details and speed benchmarks can be found on:
RongEn Fan and PaiHsune Chen and ChihJen Lin:
Working Set Selection Using the Second Order Information for Training SVM
https://www.csie.ntu.edu.tw/~cjlin/papers/quadworkset.pdf
predict.svm
plot.svm
tune.svm
matrix.csr
(in package SparseM)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105  data(iris)
attach(iris)
## classification mode
# default with factor response:
model < svm(Species ~ ., data = iris)
# alternatively the traditional interface:
x < subset(iris, select = Species)
y < Species
model < svm(x, y)
print(model)
summary(model)
# test with train data
pred < predict(model, x)
# (same as:)
pred < fitted(model)
# Check accuracy:
table(pred, y)
# compute decision values and probabilities:
pred < predict(model, x, decision.values = TRUE)
attr(pred, "decision.values")[1:4,]
# visualize (classes by color, SV by crosses):
plot(cmdscale(dist(iris[,5])),
col = as.integer(iris[,5]),
pch = c("o","+")[1:150 %in% model$index + 1])
## try regression mode on two dimensions
# create data
x < seq(0.1, 5, by = 0.05)
y < log(x) + rnorm(x, sd = 0.2)
# estimate model and predict input values
m < svm(x, y)
new < predict(m, x)
# visualize
plot(x, y)
points(x, log(x), col = 2)
points(x, new, col = 4)
## densityestimation
# create 2dim. normal with rho=0:
X < data.frame(a = rnorm(1000), b = rnorm(1000))
attach(X)
# traditional way:
m < svm(X, gamma = 0.1)
# formula interface:
m < svm(~., data = X, gamma = 0.1)
# or:
m < svm(~ a + b, gamma = 0.1)
# test:
newdata < data.frame(a = c(0, 4), b = c(0, 4))
predict (m, newdata)
# visualize:
plot(X, col = 1:1000 %in% m$index + 1, xlim = c(5,5), ylim=c(5,5))
points(newdata, pch = "+", col = 2, cex = 5)
## weights: (example not particularly sensible)
i2 < iris
levels(i2$Species)[3] < "versicolor"
summary(i2$Species)
wts < 100 / table(i2$Species)
wts
m < svm(Species ~ ., data = i2, class.weights = wts)
## extract coefficients for linear kernel
# a. regression
x < 1:100
y < x + rnorm(100)
m < svm(y ~ x, scale = FALSE, kernel = "linear")
coef(m)
plot(y ~ x)
abline(m, col = "red")
# b. classification
# transform iris data to binary problem, and scale data
setosa < as.factor(iris$Species == "setosa")
iris2 = scale(iris[,5])
# fit binary Cclassification model
m < svm(setosa ~ Petal.Width + Petal.Length,
data = iris2, kernel = "linear")
# plot data and separating hyperplane
plot(Petal.Length ~ Petal.Width, data = iris2, col = setosa)
(cf < coef(m))
abline(cf[1]/cf[3], cf[2]/cf[3], col = "red")
# plot margin and mark support vectors
abline((cf[1] + 1)/cf[3], cf[2]/cf[3], col = "blue")
abline((cf[1]  1)/cf[3], cf[2]/cf[3], col = "blue")
points(m$SV, pch = 5, cex = 2)

Call:
svm.default(x = x, y = y)
Parameters:
SVMType: Cclassification
SVMKernel: radial
cost: 1
Number of Support Vectors: 51
Call:
svm.default(x = x, y = y)
Parameters:
SVMType: Cclassification
SVMKernel: radial
cost: 1
Number of Support Vectors: 51
( 8 22 21 )
Number of Classes: 3
Levels:
setosa versicolor virginica
y
pred setosa versicolor virginica
setosa 50 0 0
versicolor 0 48 2
virginica 0 2 48
setosa/versicolor setosa/virginica versicolor/virginica
1 1.196152 1.091757 0.6708810
2 1.064621 1.056185 0.8483518
3 1.180842 1.074542 0.6439798
4 1.110699 1.053012 0.6782041
1 2
TRUE FALSE
setosa versicolor
50 100
setosa versicolor
2 1
(Intercept) x
0.08357761 1.00155934
(Intercept) Petal.Width Petal.Length
1.658953 1.172523 1.358030
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.