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.
2 Answers
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("" ) 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) 1I 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.