Here are a couple of options, using the built-in iris
data frame for illustration:
library(e1071)
m1 <- svm(Species ~ ., data = iris)
Create confusion matrix using table
function:
table(predict(m1), iris$Species, dnn=c("Prediction", "Actual"))
Actual
Prediction setosa versicolor virginica
setosa 50 0 0
versicolor 0 48 2
virginica 0 2 48
Use the caret
package to generate the confusion matrix and other model diagnostics (you can also use caret
for the entire model development, tuning and validation process):
library(caret)
confusionMatrix(iris$Species, predict(m1))
Confusion Matrix and Statistics
Reference
Prediction setosa versicolor virginica
setosa 50 0 0
versicolor 0 48 2
virginica 0 2 48
Overall Statistics
Accuracy : 0.9733
95% CI : (0.9331, 0.9927)
No Information Rate : 0.3333
P-Value [Acc > NIR] : < 2.2e-16
Kappa : 0.96
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: setosa Class: versicolor Class: virginica
Sensitivity 1.0000 0.9600 0.9600
Specificity 1.0000 0.9800 0.9800
Pos Pred Value 1.0000 0.9600 0.9600
Neg Pred Value 1.0000 0.9800 0.9800
Prevalence 0.3333 0.3333 0.3333
Detection Rate 0.3333 0.3200 0.3200
Detection Prevalence 0.3333 0.3333 0.3333
Balanced Accuracy 1.0000 0.9700 0.9700