Random forest model in R - predictors and training data types mismatch I tried the random forest model in my research topic, but I met a problem during the validation phase.

When, I used the final model of random forest to predict on an independent dataset, I received this message:


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


So, to detect the different categories in my factors/variables, I used:

levels(Train$Aquifer.media)           levels(Test$Aquifer.media)


For this factor "Aquifer.media", I have:

Train dataset: "Carbonates rocks"  "Crystalline rocks"  "Siliciclastic sedimentary rocks"  "Unconsolisated sediments rocks"  "Volcanic rocks"

Test Dataset: "Crystalline rocks"  "Siliciclastic sedimentary rocks"  "Unconsolisated sediments rocks"  "Volcanic rocks"


I detected that predictors  were of different categories, I would like to know, how I can solve this problem?

Is it possible  to delete some categories in the factors?
Your training set should be true representation of the entire population which is not true in your case. The levels in your train data set's media column has 4 factor levels which is 1 level less than the test data set's media column factor levels. Assuming you are using R, you can fix it with below code

levels(TrainAquifier.media) &lt;- levels(TestAquifier.media)


You can find answer to similar question in stackoverflow here