I have built a decision tree model in R using rpart
and ctree
.
I also have predicted a new dataset using the built model and got predicted probabilities and classes.
However, I would like to extract the rule/path, in a single string, for every observation (in predicted dataset) has followed. Storing this data in tabular format, I can explain prediction with reason in a automated manner without opening R.
Which means I want to got 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
Kind of code I'm looking for is
library(rpart)
fit <- rpart(Reliability~.,data=car.test.frame)
this is what needs to expanded into multiple lines possibly
predResults <- predict(fit, newdata = newcar, type= "GETPATTERNS")
The
partykit
package has a function.list.rules.party()
which is currently unexported but can be leveraged to do what you want to do. The main reason that we haven't exported it, yet, is that its type of output may change in future versions.To obtain the predictions you describe above you can do:
Illustration using the
iris
data andrpart()
:(Only the first observation of each species is shown for brevity here. This corresponds to indexes 1, 51, and 101.)
And with
ctree()
: