2

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

Error: data and reference should be factors with the same levels.

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

partymod<-ctree(share~., data=train)

partypred<-predict(partymod, test)
confusionMatrix(partypred, test$share)

I cant figure out what the problem is. Any help would be greatly appreciated as its making me cracked!!

고사랑
  • 21
  • 1
  • I think you need to set both the `levels` of the 'share' column same in both datasets. i.e. `lvs <- union(levels(train$share), test$share)); test$share <- factor(test$share, levels = lvs); train$share <- factor(train$share, levels = lvs)` a reproducible example would have been helpful though – akrun Mar 14 '19 at 05:33
  • 1
    Possible duplicate of [Error in Confusion Matrix : the data and reference factors must have the same number of levels](https://stackoverflow.com/questions/30002013/error-in-confusion-matrix-the-data-and-reference-factors-must-have-the-same-nu) – phiver Mar 14 '19 at 08:59

0 Answers0