2
$\begingroup$

I am building a classification model using randomForest. When trying to predict I get the below error

Type of predictors in new data do not match that of the training data

I made sure that the testing and training data has same levels.

I also included

levels(test_var) <- levels(train_var)

to make sure that the levels are matching.

But still I end up getting this error, is there anything else that I should look for?

**EDITED ON 3rd Aug 2015**

Here is the structure of the training dataset

enter image description here

Structure of the test dataset

enter image description here

Sapply training data

enter image description here

Sapply testing data

enter image description here

In order to make sure that the levels are matching between the training and test datasets, I wrote this loop to see if there are any differences exist between the datasets

for(i in 1:28) 

 {
   if(is.factor(testing[,i]))
    {

    print(names((testing[i])))
    difference_in_test = setdiff(levels(testing[,i]), levels(training_data[,i]))
      print(difference_in_test)

    }
 }

Results of the above For loop which shows that there are no differences exist between the levels.

[1] "VAR1"
character(0)
[1] "VAR2"
character(0)
[1] "VAR3"
character(0)
[1] "VAR4"
character(0)
[1] "VAR5"
character(0)
[1] "VAR7"

I still continue to get the error as mentioned below:

enter image description here

Edit on 4th Aug

I started getting the above error only after including a filter in my dataset like this:

training_data <- subset(training_data, gender !="F")

$\endgroup$
12
  • $\begingroup$ It would be very useful if you would provide the R package that you are using and a sample of the data. My guess is that there are some sort of hidden differences in the features. Often R data comes in as factors or character strings instead of numbers, so even though they look like numbers, they aren't. There are several ways to convert to numeric data, but as.numeric() is the most popular. Check out this stack overflow post $\endgroup$
    – AN6U5
    Jul 31 '15 at 16:54
  • $\begingroup$ @AN6U5- Thanks for the reply. I am using randomForest package. The error i got during the predict statmenet. I will try to provide some sample data by tomorrow. However I could see that both the training data and the test data contains same factors for categorical data and the few numerical variables. $\endgroup$
    – Arun
    Jul 31 '15 at 18:42
  • $\begingroup$ I'm still skeptical that either the types are different, the number of features are different, or you are giving it the transpose of your data so the shape is different. Those are the things I would investigate with that error. $\endgroup$
    – AN6U5
    Jul 31 '15 at 21:40
  • $\begingroup$ @AN6U5 - Thank you again. So do you advice to try all the numeric fie lds to be declared as.numeric explicitly ( and the factor fields as as.factor() respectively) and give a try? $\endgroup$
    – Arun
    Jul 31 '15 at 21:52
  • $\begingroup$ I just want you to run sapply(data, mode) and sapply(data, class) and dim(data) on your training data and testing data to see if everything matches. $\endgroup$
    – AN6U5
    Jul 31 '15 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.