Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Machine Learning by (19k points)

I've trained a Linear Regression model with R caret. I'm now trying to generate a confusion matrix and keep getting the following error:

Error in confusionMatrix.default(pred, testing$Final) : the data and reference factors must have the same number of levels

EnglishMarks <- read.csv("E:/Subject Wise Data/EnglishMarks.csv", 





predictionsTree <- predict(treeFit, testdata)

confusionMatrix(predictionsTree, testdata$catgeory)




The error occurs when generating the confusion matrix. The levels are the same on both objects. I cant figure out what the problem is. Their structure and levels are given below. They should be the same. Any help would be greatly appreciated as its making me cracked!!

> str(pred)

chr [1:148] "85" "84" "87" "65" "88" "84" "82" "84" "65" "78" "78" "88" "85"  

"86" "77" ...

> str(testing$Final)

int [1:148] 88 85 86 70 85 85 79 85 62 77 ...

> levels(pred)


> levels(testing$Final)


2 Answers

0 votes
by (33.1k points)

Simply use table(pred) and table(testing$Final). You will see that there is at least one number in the testing set that is never predicted. This is what is meant why "different number of levels". There is an example of a custom made function to get around this problem here.

However, I found that this trick works fine:

table(factor(pred, levels=min(test):max(test)), 

      factor(test, levels=min(test):max(test)))

It would give you exactly the same confusion matrix as with the function. Study Logistic Regression is one of the best topics to study for a better grasp of this course.

Hope this answer helps you!

0 votes
by (6.8k points)

The model is not predicting a certain factor. Use the table() function instead of confusionMatrix() to see if that is the problem.

confusionMatrix(table(Argument 1, Argument 2))

Browse Categories