Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 211971

Error with confusion matrix: Error: `data` and `reference` should be factors with the same levels

$
0
0

I have to evaluate the performance of a classification tree implemented by the function tree::tree(), and to do this I rely on confusionMatrix().

My code is

train_idx <- caret::createDataPartition(Cancer_data$Prognosis,`p = 0.43)[[1]]
train_set <- Cancer_data[train_idx, ]
test_set <- Cancer_data[-train_idx,]
train_set$Prognosis <- as.factor(train_set$Prognosis)
test_set$Prognosis <- as.factor(test_set$Prognosis)
tree_model <- tree::tree(Prognosis ~.,data= train_set)
pred_tree_test <- predict(tree_model,newdata = test_set)
C.2 <- confusionMatrix(test_set$Prognosis,pred_tree_test)

Further Information: Prognosis is a factor with two levels 4 for malignant cancer and 2 for benign one.The data set contains also other 9 variables to use as predictors. The output is Prognosis. I have no prolem in doing the model and I plot it with no efforts but when I have to use confusionMatrix() some problems rise up. If you desire the data set ask me in private.


Viewing all articles
Browse latest Browse all 211971

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>