library(rpart)
library(imptree)
data(carEvaluation)
table(carEvaluation$acceptance)
> table(carEvaluation$acceptance)
acc good unacc vgood
384 69 1210 65
Note that unacc
is just one of the categories within the acceptance
attribute.
So you can do something like this:
{set.seed(3456)
train <- caret::createDataPartition(carEvaluation$acceptance, p = .8, # partition 80%~20%
list = FALSE)
carTrain <- carEvaluation[train,]
carTest <- carEvaluation[-train,]
fit <- rpart::rpart(acceptance~., data = carTrain, method = 'class')
}
df <- data.frame(obs = carTest$acceptance,
predict(fit, newdata = carTest, type = "class"))
cfm <- caret::confusionMatrix(df$predict.fit..newdata...carTest..type....class.., df$obs)
cfm
> cfm
Confusion Matrix and Statistics
Reference
Prediction acc good unacc vgood
acc 70 0 10 2
good 5 12 1 0
unacc 1 0 231 0
vgood 0 1 0 11
Overall Statistics
Accuracy : 0.9419
95% CI : (0.9116, 0.9641)
No Information Rate : 0.7035
P-Value [Acc > NIR] : < 2.2e-16
Kappa : 0.8762
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: acc Class: good Class: unacc Class: vgood
Sensitivity 0.9211 0.92308 0.9545 0.84615
Specificity 0.9552 0.98187 0.9902 0.99698
Pos Pred Value 0.8537 0.66667 0.9957 0.91667
Neg Pred Value 0.9771 0.99693 0.9018 0.99398
Prevalence 0.2209 0.03779 0.7035 0.03779
Detection Rate 0.2035 0.03488 0.6715 0.03198
Detection Prevalence 0.2384 0.05233 0.6744 0.03488
Balanced Accuracy 0.9381 0.95248 0.9724 0.92157
You do not necessarily need to make your code exactly as it is exemplified here. I suggest looking at the documentation of the caret
package and the rpart
for code enhancement. Or you can provide a fully reproducible example.