Contrasts dropped from factor x in decision tree?

r

#1

Hi,
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 )

printcp(tree)

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

cpok<-tree$cptable[which.min(tree$cptable[,“xerror”]),“CP”]

tree.pruned<-prune(tree,cp=bestcp)

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

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

Many thanks!


#2

Hi
The above have been solved. thanks!


#3

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