Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201977

Decision Tree party package prediction error - Levels do not match

$
0
0

I am building a CART regression tree model in R using party package, but I got error message saying levels do not match when I try to apply the model with testing dataset.

I have spent the past week reading through the threads on the forum, but still couldn't find the right solution to my problem. So I am reposting this question here using fake examples I made up.. Can someone help explain the error message and provide a solution?

my training dataset has about 1000 records and testing dataset has about 150. There's no NA or blank fields in either dataset.

my CART model using ctree under the party package is:

mytree<- ctree(Rate~Bank+Product+Salary, data=data_train)

data_train example:

Rate  Bank  Product  Salary    
1.5    A     aaa     100000
0.6    B     abc      60000
3      C     bac      10000
2.1    D     cba      50000
1.1    E     cca      80000

data_test example:

Rate  Bank  Product   Salary
2.0    A     cba       80000
0.5    D     cca      250000
0.8    E     cba      120000
2.1    C     abc       65000

levels(data_train$Bank) : A, B, C, D, E

levels(data_test$Bank): A,D,E,C

I tried to set to the same level using the following codes:

>is.factor(data_test$Bank)

 TRUE 
(Made sure Bank and Products are factors in both datasets)
>levels(data_test$Bank) <-union(levels(data_test$Bank), levels(data_train$Bank))

> levels(data_test$product)<-union(levels(data_test$product),levels(data_train$product))

However, when I try to run prediction on the testing dataset, I get the following error:

> fit1<- predict(mytree,newdata=data_test)

Error in checkData(oldData, RET) : 
  Levels in factors of new data do not match original data

I have also tried the following method but it alters the fields of my testing dataset...:

levels(data_test$Bank) <-levels(data_train$Bank)

The data_test table is altered:

Rate  Bank(altered)  Bank (original)   
2.0    A              A      
0.5    B              D      
0.8    C              E      
2.1    D              C       

Viewing all articles
Browse latest Browse all 201977

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>