Young Jae  Seo
Young Jae Seo

Reputation: 67

How to solve "The data cannot have more levels than the reference" error when using confusioMatrix?

I'm using R programming. I divided the data as train & test for predicting accuracy.

This is my code:

library("tree")
credit<-read.csv("C:/Users/Administrator/Desktop/german_credit (2).csv")

library("caret")
set.seed(1000)

intrain<-createDataPartition(y=credit$Creditability,p=0.7,list=FALSE)
train<-credit[intrain, ]
test<-credit[-intrain, ]

treemod<-tree(Creditability~. , data=train)
plot(treemod)
text(treemod)

cv.trees<-cv.tree(treemod,FUN=prune.tree)
plot(cv.trees)

prune.trees<-prune.tree(treemod,best=3)
plot(prune.trees)
text(prune.trees,pretty=0)

install.packages("e1071")
library("e1071")
treepred<-predict(prune.trees, newdata=test)

confusionMatrix(treepred, test$Creditability)

The following error message happens in confusionMatrix:

Error in confusionMatrix.default(rpartpred, test$Creditability) : the data cannot have more levels than the reference

The credit data can download at this site.
http://freakonometrics.free.fr/german_credit.csv

Upvotes: 4

Views: 21997

Answers (3)

Anthony Basooma
Anthony Basooma

Reputation: 21

This also happens if the model correctly predicts the classes into '1' and '0s' or if the model fails to converge. So the confusion matrix cannot be created.

library(caret)

pred <- factor(c(0, 1, 1, 0))
actual <- factor(c(1,1,1,1))

Run a confusion matrix

cx = caret::confusionMatrix(data = pred, reference = actual)
Error in confusionMatrix.default(data = pred, reference = actual) : the data cannot have more levels than the reference

Upvotes: 0

pockeystar
pockeystar

Reputation: 31

I had the same issue in classification. It turns out that there is ZERO observation in a specific group therefore I got the error "the data cannot have more levels than the reference”.

Make sure there all groups in your test set appears in your training set.

Upvotes: 2

phiver
phiver

Reputation: 23608

If you look carefully at your plots, you will see that you are training a regression tree and not a classication tree.

If you run credit$Creditability <- as.factor(credit$Creditability) after reading in the data and use type = "class" in the predict function, your code should work.

code:

credit <- read.csv("http://freakonometrics.free.fr/german_credit.csv" )

credit$Creditability <- as.factor(credit$Creditability)

library(caret)
library(tree)
library(e1071)

set.seed(1000)
intrain <- createDataPartition(y = credit$Creditability, p = 0.7, list = FALSE)
train <- credit[intrain, ]
test <- credit[-intrain, ]

treemod <- tree(Creditability ~ ., data = train, )

cv.trees <- cv.tree(treemod, FUN = prune.tree)
plot(cv.trees)

prune.trees <- prune.tree(treemod, best = 3)
plot(prune.trees)
text(prune.trees, pretty = 0)

treepred <- predict(prune.trees, newdata = test, type = "class")
confusionMatrix(treepred, test$Creditability)

Upvotes: 2

Related Questions