I have used the decision tree to predict my test set. After running my code I get a table which has the results, but I want to use the confusionMatrix() command from the caret
library. I have tried several things, but none has worked. Please see my code:
library(rpart)
tree <- rpart(train$number ~ ., train, method = "class")
pred <- predict(tree,test, type ="class")
p <- predict(tree, type="class")
# Confusion Matrix
conf <- table(test$number, pred)
> conf
pred
Problem Reference
Problem 0 100
Reference 0 2782
I tried to do this:
p <- predict(tree, type="class")
confusionMatrix(p, entiredata$number)
Errors like data and reference should be the same type, so I changed it both to factors with as.factors()
, then the arguments were not the same length. I searched the web and found similiar questions but they all didn't help me. My final goal is to receive the statistics as the accuracy.
library(caret)
confusionMatrix(p, test$number)
Since you predict
only on the test data, you should compare predictions only on the test
data, not the whole dataset.