The predict()
function can be used to predict the class label on a set of
test observations, at any given value of the cost parameter. We begin by
generating a test data set.
xtest <- matrix(rnorm(20 * 2), ncol = 2)
ytest <- sample(c(-1, 1), 20, rep = TRUE)
xtest[ytest == 1,] <- xtest[ytest == 1,] + 1
testdat <- data.frame(x = xtest, y = as.factor(ytest))
Now we predict the class labels of these test observations. Here we use the best model obtained through cross-validation in order to make predictions.
ypred <- predict(bestmod, testdat)
table(predict = ypred, truth = testdat$y)
truth
predict -1 1
-1 9 1
1 2 8
Thus, with this value of cost
, 17 of the test observations are correctly
classified.
cost
of 0.01.
Store this model in svmfit
ypred
.cf.test
table()
function, and the ground truth as the second argument).Assume that:
e1071
library has been loaded