Get a decision tree rule / path template for each row of the predicted dataset for the rpart / ctree packet in R

I built a decision tree model in R using rpart and ctree . I also predicted a new dataset using the constructed model and got the predicted probabilities and classes.

However, I would like to extract the rule / path in one row for each observation (in the predicted dataset). By storing this data in a tabular format, I can explain the prediction by the mind in an automatic way without opening R.

This means that I want the following.

 ObsID Probability PredictedClass PathFollowed 1 0.68 Safe CarAge < 10 & Country = Germany & Type = Compact & Price < 12822.5 2 0.76 Safe CarAge < 10 & Country = Korea & Type = Compact & Price > 12822.5 3 0.88 Unsafe CarAge > 10 & Type = Van & Country = USA & Price > 15988 

The type of code I'm looking for

 library(rpart) fit <- rpart(Reliability~.,data=car.test.frame) 

this is what needs to be expanded on several lines, perhaps

 predResults <- predict(fit, newdata = newcar, type= "GETPATTERNS") 
+5
source share
1 answer

partykit has a .list.rules.party() function, which is currently not supported, but can be used to accomplish what you want to do. The main reason that we have not exported it yet is that its output type may change in future versions.

To get the predictions described above, you can do:

 pathpred <- function(object, ...) { ## coerce to "party" object if necessary if(!inherits(object, "party")) object <- as.party(object) ## get standard predictions (response/prob) and collect in data frame rval <- data.frame(response = predict(object, type = "response", ...)) rval$prob <- predict(object, type = "prob", ...) ## get rules for each node rls <- partykit:::.list.rules.party(object) ## get predicted node and select corresponding rule rval$rule <- rls[as.character(predict(object, type = "node", ...))] return(rval) } 

Illustration using iris and rpart() data:

 library("rpart") library("partykit") rp <- rpart(Species ~ ., data = iris) rp_pred <- pathpred(rp) rp_pred[c(1, 51, 101), ] ## response prob.setosa prob.versicolor prob.virginica ## 1 setosa 1.00000000 0.00000000 0.00000000 ## 51 versicolor 0.00000000 0.90740741 0.09259259 ## 101 virginica 0.00000000 0.02173913 0.97826087 ## rule ## 1 Petal.Length < 2.45 ## 51 Petal.Length >= 2.45 & Petal.Width < 1.75 ## 101 Petal.Length >= 2.45 & Petal.Width >= 1.75 

(Here, for brevity, only the first observation of each species is shown, which corresponds to indices 1, 51, and 101).

And with ctree() :

 ct <- ctree(Species ~ ., data = iris) ct_pred <- pathpred(ct) ct_pred[c(1, 51, 101), ] ## response prob.setosa prob.versicolor prob.virginica ## 1 setosa 1.00000000 0.00000000 0.00000000 ## 51 versicolor 0.00000000 0.97826087 0.02173913 ## 101 virginica 0.00000000 0.02173913 0.97826087 ## rule ## 1 Petal.Length <= 1.9 ## 51 Petal.Length > 1.9 & Petal.Width <= 1.7 & Petal.Length <= 4.8 ## 101 Petal.Length > 1.9 & Petal.Width > 1.7 
+5
source

All Articles