predict.svm {e1071}R Documentation

Predict method for Support Vector Machines

Description

This function predicts values based upon a model trained by svm.

Usage

## S3 method for class 'svm':
predict(object, newdata, decision.values = FALSE, ..., na.action = na.omit)

Arguments

object Object of class "svm", created by svm.
newdata A matrix containing the new input data. A vector will be transformed to a n x 1 matrix.
decision.values Logical controlling whether the decision values of all binary classifiers computed in multiclass classification shall be computed and returned.
na.action A function to specify the action to be taken if `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.)
... Currently not used.

Value

A vector of predicted values (for classification: a vector of labels, for density estimation: a logical vector). If decision.value is TRUE, the vector gets a "decision.values" attribute containing a n x k matrix (n number of predicted values, k number of classes) of all k binary classifiers' decision values. The colnames of the matrix indicate the labels of the two classes.

Note

If the training set was scaled by svm (done by default), the new data is scaled accordingly using scale and center of the training data.

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

svm

Examples

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:
pred <- predict(model, x, decision.values = TRUE)
attr(pred, "decision.values")[1:4,]

## 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))
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)

[Package Contents]