Contrast drop off from factor x in using predict in decision tree in R

milk

New Member
#1
Hi,
I have tried research about this error warning message for several days without any info, e.g. ": contrasts dropped from factor staffx.f" etc..
as I tried to use newly decision tree model with the predict function on test data within the rpaprt package.


Here are the scripts that I used to create decision tree model :

db$staff.f<- as.factor(db$staff)
...create same for other categorical variables.

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

tree<-rpart(L2 ~ .,method = "class", data = Train )

cpok<-db$cptable[which.min(db$cptable[,"xerror"]),"CP"]

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

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

Is there certain parts of the scripts that needs to amend? as it is strange to me that the contrast is drop off even though it is used to create dummy variables and also factor has been created to recognize the certain variables. Much grateful for all advice.

Thank you!
 

hlsmith

Omega Contributor
#2
I am not overly familiar with rpart, only used it a couple of times. Question, does all of code work up until predict?

What does contrast do, set a reference level?
 

rogojel

TS Contributor
#4
hi,
my guess would be that this has to do with the pruning. Maybe the contrasts are not defined in the pruned tree anymore?

regards