I"ve trained a Linear Regression design with R caret. I"m now trying to generate a confusion matrix and also keep obtaining the complying with error:

Error in confusionMatrix.default(pred, testing\$Final) : the data and reference determinants must have actually the exact same variety of levels

EnglishMarks The error occurs once generating the confusion matrix. The levels are the exact same on both objects. I cant figure out what the trouble is. Their framework and levels are offered listed below. They must be the same. Any assist would certainly be significantly 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)NULL> levels(testing\$Final)NULL
r machine-discovering artificial-intelligence classification linear-regression
Share
Improve this question
Follow
edited May 2 "15 at 12:26 Has QUIT--Anony-Mousse
asked May 2 "15 at 11:57 abcdabcd
2

11
Do table(pred) and also table(testing\$Final). You will see that there is at least one number in the trial and error set that is never predicted (i.e. never existing in pred). This is what is meant why "various number of levels". Tright here is an example of a practice made attribute to obtain about this difficulty right here.

You are watching: Error: `data` and `reference` should be factors with the same levels.

However, I uncovered that this trick functions fine:

table(factor(pred, levels=min(test):max(test)), factor(test, levels=min(test):max(test)))It must give you specifically the same confusion matrix as through the feature.

Share
Follow
edited Jul 16 "18 at 5:41 Kim
answered May 10 "15 at 4:25 nayriznayriz
11
confusionMatrix(pred,testing\$Final)Whenever you try to construct a confusion matrix, make certain that both the true values and prediction values are of element datatype.

Here both pred and testing\$Final need to be of type variable. Instead of inspect for levels, check the form of both the variables and convert them to aspect if they are not.

Here testing\$last is of form int. conver it to variable and then develop the confusion matrix.

Share
Follow
edited Jul 31 "18 at 9:58 MLavoie
answered Jul 31 "18 at 9:36
sandeep patilsandeep patil
8
I had the very same issue. I guess it taken place bereason information argument was not casted as factor as I supposed.Try:

confusionMatrix(pred,as.factor(testing\$Final))hope it helps

Share
Follow
answered Apr 8 "19 at 1:08
Cenk ŞİMŞEKCenk ŞİMŞEK
2
4
Something like the adheres to seem to occupational for me. The idea is comparable to that of
nayriz:

confusionMatrix( factor(pred, levels = 1:148), factor(testing\$Final, levels = 1:148))The key is to make certain the element levels match.

Share
Follow
edited Jul 15 "18 at 21:37
Kim
answered Apr 30 "18 at 20:57
David C.David C.
2
On a similar error, I compelled the GLM predictions to have actually the very same class as the dependent variable.

See more: We Are All The Same On The Inside Paperback, We Are All The Same Inside

For instance, a GLM will predict a "numeric" course. But with the targain variable being a "factor" class, I ran right into an error.

erroneous code:

#Predicting making use of logistic modelglm.probs = predict(model_glm, newdata = test, kind = "response")test\$pred_glm = ifelse(glm.probs > 0.5, "1", "0")#Checking the accuracy of the logistic design confusionMatrix(test\$default,test\$pred_glm)Result:

Error: `data` and also `reference` must be determinants via the same levels.corrected code:

#Predicting making use of logistic version glm.probs = predict(model_glm, newdata = test, form = "response") test\$pred_glm = ifelse(glm.probs > 0.5, "1", "0") test\$pred_glm = as.factor(test\$pred_glm) #Checking the accuracy of the logistic modelconfusionMatrix(test\$default,test\$pred_glm)Result:

confusion Matrix and also Statistics ReferencePrediction 0 1 0 182 1317 1 122 22335 Accuracy : 0.9399 95% CI : (0.9368, 0.9429) No Information Rate : 0.9873 P-Value NIR> : 1