1
votes

I need to extract the path and probability of each leaf in a decision tree. Here's a quick sample to work with:

data(iris)

model<-rpart(Species~., data=iris)

summary(model)

enter image description here

I'd like to be able to extract all of that information in summary here such as "predicted class" , "class counts" and "probabilities" in a format that I can put into a table rather than just being able to read it on the console.

Much thanks!

1
unclass(model) has a lot of stuff that might be useful. It looks like if you pick out what you want, tableing it might not be too bad - Rich Scriven

1 Answers

1
votes

You need to access properties of "model" which is instance of rpart.object. Docs: https://stat.ethz.ch/R-manual/R-devel/library/rpart/html/rpart.object.html

For example slot "where" has following info: an integer vector of the same length as the number of observations in the root node, containing the row number of frame corresponding to the leaf node that each observation falls into.

Probabilities you will find in slot "frame".