Contrasts dropped from factor x in decision tree?



I have faced this block for a few days even with much research online, and much grateful if there are some insights in dealing this kind of warnings: " Contrasts dropped from factor x" . This occurred when I tried to use newly created decision tree model to predict on the test data using rpart package.

Following are the scripts that I have used :
db$staffx.f<- as.factor(db$staffx)

contrasts(db$staffx.f) = contr.treatment(8) … create same for other categorical variables.

tree<-rpart(L2 ~ staffx+…other variables, method = “class”, data = db )


rpart.plot(tree,type = 3,tweak =1.2)



pred1 <- predict(tree,Testdata,type=“class”)

In which areas that I need to adjust ? Does this mean the model has failed?

Many thanks!


The above have been solved. thanks!


Hi @cupcake, can you share how you solved the problem?