svmr {rchemo} | R Documentation |
SVM Regression and Discrimination
Description
SVM models with Gaussian (RBF) kernel
svmr
: SVM regression (SVMR).
svmda
: SVM discrimination (SVMC).
The functions uses function svm
of package e1071
(Meyer et al. 2021) available on CRAN (e1071 uses the tool box LIVSIM; Chang & Lin, http://www.csie.ntu.edu.tw/~cjlin/libsvm).
The SVM models are fitted with parameterization 'C'
, not the 'nu'
parameterization.
The RBF kernel is defined by: exp(-gamma * |x - y|^2).
For tuning the model, usual preliminary ranges are for instance:
- cost = 10^(-5:15)
- epsilon = seq(.1, .3, by = .1)
- gamma = 10^(-6:3)
Usage
svmr(X, y, cost = 1, epsilon = .1, gamma = 1, scale = FALSE)
svmda(X, y, cost = 1, epsilon = .1, gamma = 1, scale = FALSE)
## S3 method for class 'Svm'
predict(object, X, ...)
## S3 method for class 'Svm'
summary(object, ...)
Arguments
X |
For the main functions: Training X-data ( |
y |
Training Y-data ( |
cost |
The cost of constraints violation |
epsilon |
The |
gamma |
The |
scale |
Logical. If |
object |
For the auxiliary functions: A fitted model, output of a call to the main function. |
... |
For the auxiliary functions: Optional arguments. |
Value
For svmr
and svmda
:
fm |
list of outputs such as:
|
For predict.Svm
:
pred |
predictions for each observation. |
For summary.Svm
:display of call, parameters, and number of support vectors
Note
The first example illustrates SVMR. The second one is the example of fitting the function sinc(x) described in Rosipal & Trejo 2001 p. 105-106. The third one illustrates SVMC.
References
Meyer, M. 2021 Support Vector Machines - The Interface to libsvm in package e1071. FH Technikum Wien, Austria, David.Meyer@R-Project.org. https://cran.r-project.org/web/packages/e1071/vignettes/svmdoc.pdf
Chang, cost.-cost. & Lin, cost.-J. (2001). LIBSVM: a library for support vector machines. Software available at http://www.csie.ntu.edu.tw/~cjlin/libsvm. Detailed documentation (algorithms, formulae, . . . ) can be found in http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.ps.gz
Examples
## EXAMPLE 1 (SVMR)
n <- 50 ; p <- 4
Xtrain <- matrix(rnorm(n * p), ncol = p)
ytrain <- rnorm(n)
m <- 3
Xtest <- Xtrain[1:m, , drop = FALSE]
ytest <- ytrain[1:m]
fm <- svmr(Xtrain, ytrain)
predict(fm, Xtest)
pred <- predict(fm, Xtest)$pred
msep(pred, ytest)
summary(fm)
## EXAMPLE 2
x <- seq(-10, 10, by = .2)
x[x == 0] <- 1e-5
n <- length(x)
zy <- sin(abs(x)) / abs(x)
y <- zy + rnorm(n, 0, .2)
plot(x, y, type = "p")
lines(x, zy, lty = 2)
X <- matrix(x, ncol = 1)
fm <- svmr(X, y, gamma = .5)
pred <- predict(fm, X)$pred
plot(X, y, type = "p")
lines(X, zy, lty = 2)
lines(X, pred, col = "red")
## EXAMPLE 3 (SVMC)
n <- 50 ; p <- 8
Xtrain <- matrix(rnorm(n * p), ncol = p)
ytrain <- sample(c("a", "10", "d"), size = n, replace = TRUE)
m <- 5
Xtest <- Xtrain[1:m, ] ; ytest <- ytrain[1:m]
cost <- 100 ; epsilon <- .1 ; gamma <- 1
fm <- svmda(Xtrain, ytrain,
cost = cost, epsilon = epsilon, gamma = gamma)
predict(fm, Xtest)
pred <- predict(fm, Xtest)$pred
err(pred, ytest)
summary(fm)