predict.svm {e1071}R Documentation

Predict method for Support Vector Machines

Description

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

Usage

predict.svm(model, x, type="class")

Arguments

model object of class svm, created by svm.
x A matrix containing the input data.
type If raw is supplied, the estimated values are returned. With class, predict returns the first level of the training response for negative (non-zero) values, and the second level otherwise. If class is used on numerical data, a factor with labels "-1" and "1" is created.

Value

The predicted value.

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