I am using rpart for building a decision tree classifier. I wish to use my own pruning function based on certain parameters of the leaf nodes corresponding to a subtree. For this, I think I need to traverse the tree. I explored the documentation of rpart, but could not come up with a way to do it. Please help.
Asked
Active
Viewed 946 times
3
-
1The function `dendrapply()` might be what you're looking for – Andrie Jan 15 '15 at 20:33
-
1You could use the `as.party()` coercion function from the `partykit` package. This transforms the `rpart` structure to a structure called `party` that comes with many convenience tools including a `nodeapply()` and a `nodeprune()` function. See `vignette("partykit", package = "partykit")` for an introduction and `vignette("constparty", package = "partykit")` for examples including coercion from `rpart`. – Achim Zeileis Jan 16 '15 at 01:42
-
@Andrie `dendrapply()` function applies a passed function to each node and returns the altered tree. This is not what I'm looking for. I wish to get every leaf node corresponding to a subtree. – Kundan Kumar Jan 16 '15 at 04:08
1 Answers
2
Take a look at the tree traversal code I had at extracting predictors from ctree object
Potentially, this will be useful.