svm
is used to train a support vector machine.
It can be used to carry out general regression and classification
(of nu and epsilon-type), as well as density-estimation.
A formula interface is provided.
## 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=1/ncol(as.matrix(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)
x
.
Can be either a factor (for classification tasks)
or a numeric vector (for regression).
scale
is of length 1,
the value is recycled as many times as needed.
Per default, data are scaled internally
(both
x
and
y
variables) to zero mean and unit variance.
The center and scale values are returned and used for later predictions.
svm
can be used as a classification machine,
as a regresson machine, or for novelty detection.
Depending of whether
y
is a factor or not,
the default setting for
type
is
C-classification
or
eps-regression
, respectively,
but may be overwritten by setting an explicit value.
Valid options are:
C-classification
nu-classification
one-classification
(for novelty detection)
eps-regression
nu-regression
u'*v
polynomial
(default: 3)
linear
(default: 1/(data dimension))
polynomial
and
sigmoid
(default: 0)
nu-classification
,
nu-regression
,
and
one-classification
TRUE
)
TRUE
)
svm.default
NA
s are found.
The default action is
na.omit
,
which leads to rejection of cases with missing values on any required variable.
An alternative is
na.fail
,
which causes an error if
NA
cases are found.
NOTE: If given, this argument must be named.
"svm"
containing
the fitted model, including:
na.omit
and
subset
)
For multiclass-classification with k levels, k>2,
libsvm
uses the
'one-against-one'-approach, in which k(k-1)/2 binary classifiers are trained;
the appropriate class is found by a voting scheme.
libsvm
internally uses a sparse data
representation, which is also high-level 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 a-posteriori class probabilities for the multi-class problem using quadratic optimization. The probabilistic regression model assumes (zero-mean) laplace-distributed errors for the predictions, and estimates the scale parameter using maximum likelihood.
Data are scaled internally, usually yielding better results.
Parameters of SVM-models usually must be tuned to yield sensible results!
David Meyer (based on C/C++-code by Chih-Chung Chang and Chih-Jen Lin)
mailto:David.Meyer@R-project.org
Chang, Chih-Chung and Lin, Chih-Jen. LIBSVM: a library for Support Vector Machines http://www.csie.ntu.edu.tw/~cjlin/libsvm
Exact formulations of models, algorithms, etc. can be found in the
document:
Chang, Chih-Chung and Lin, Chih-Jen.
LIBSVM: a library for Support Vector Machines
http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.ps.gz
More implementation details and speed benchmarks can be found on: Rong-En Fan and Pai-Hsune Chen and Chih-Jen Linr. Working Set Selection Using the Second Order Information for Training SVM, http://www.csie.ntu.edu.tw/~cjlin/papers/quadworkset.pdf
## classification mode # default with factor response: model <- svm(Species ~ ., data = iris.df) # alternatively the traditional interface: x <- iris.df[,-5] y <- iris.df[,5] 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): plotdata <- cmdscale(dist(iris.df[,-5])) plotdata[,2] <- -1 * plotdata[,2] plot(plotdata, col = as.integer(iris.df[,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) ## density-estimation # create 2-dim. normal with rho=0: X <- data.frame(a = rnorm(1000), b = rnorm(1000)) # traditional way: m <- svm(X, gamma = 0.1) # formula interface: m <- svm(~., data = X, gamma = 0.1) # or: m <- svm(~ X$a + X$b, gamma = 0.1) # test: newdata <- data.frame(a = c(0, 4), b = c(0, 4)) predict (m, newdata) # visualize: plot(as.matrix(X), col = 1:1000 %in% m$index + 1, xlim = c(-5,5), ylim=c(-5,5)) points(as.matrix(newdata), pch = "+", col = 2, cex = 5) # weights: (example not particularly sensible) i2 <- iris.df levels(i2$Species)[3] <- "versicolor" summary(i2$Species) wts <- 100 / table(i2$Species) wts m <- svm(Species ~ ., data = i2, class.weights = wts)