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")