. Advertisement .
..3..
. Advertisement .
..4..
I get the error message:
Error in confusionMatrix.default(pred, testing$Final) : the data and reference factors must have the same number of levels
Has anyone ever faced this problem? How to troubleshoot the “ error: `data` and `reference` should be factors with the same levels.”
But the problem is appearing when I try to operate the following program
EnglishMarks <- read.csv("E:/Subject Wise Data/EnglishMarks.csv",
header=TRUE)
inTrain<-createDataPartition(y=EnglishMarks$Final,p=0.7,list=FALSE)
training<-EnglishMarks[inTrain,]
testing<-EnglishMarks[-inTrain,]
predictionsTree <- predict(treeFit, testdata)
confusionMatrix(predictionsTree, testdata$catgeory)
modFit<-train(Final~UT1+UT2+HalfYearly+UT3+UT4,method="lm",data=training)
pred<-format(round(predict(modFit,testing)))
confusionMatrix(pred,testing$Final)
> 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)
NULL
> levels(testing$Final)
NULL
The cause: This error occured due to data argument was not casted as factor.
Solution: You can try this my following suggestion:
table(pred)
andtable(testing$Final)
table(pred)
andtable(testing$Final)
will show you that at least one number is not predicted in the testing set. Never present inpred
This is why there are “different levels”.This trick worked for me.
It should provide exactly the same confusion matrix that you get from the function.