I would like to inspect all the observations that reached some node in an rpart decision tree. For example, in the following code:
fit <- rpart(Kyphosis ~ Age + Start, data = kyphosis)
fit
n= 81
node), split, n, loss, yval, (yprob)
* denotes terminal node
1) root 81 17 absent (0.79012346 0.20987654)
2) Start>=8.5 62 6 absent (0.90322581 0.09677419)
4) Start>=14.5 29 0 absent (1.00000000 0.00000000) *
5) Start< 14.5 33 6 absent (0.81818182 0.18181818)
10) Age< 55 12 0 absent (1.00000000 0.00000000) *
11) Age>=55 21 6 absent (0.71428571 0.28571429)
22) Age>=111 14 2 absent (0.85714286 0.14285714) *
23) Age< 111 7 3 present (0.42857143 0.57142857) *
3) Start< 8.5 19 8 present (0.42105263 0.57894737) *
I would like to see all the observations in node (5) (i.e.: the 33 observations for which Start>=8.5 & Start< 14.5). Obviously I could manually get to them. But I would like to have some function like (say) "get_node_date". For which I could just run get_node_date(5) - and get the relevant observations.
Any suggestions on how to go about this?
rpart returns rpart.object element which contains the information you need:
Two years after original post, but may be of use to others. Node assignments for training observations in rpart can be obtained from
$where
:As a function:
This works for training observations only though, not for new observations.
There seems to be no such function which enables an extraction of the observations from a specific node. I would solve it as follows: first determine which rule/s is/are used for the node you are insterested in. You can use
path.rpart
for it. Then you could apply the rule/s one after the other to extract the observations.This approach as a function:
For node 5 you get:
The
partykit
package also provides a canned solution for this. You just need to convert therpart
object to theparty
class in order to use its unified interface for dealing with trees. And then you can use thedata_party()
function.Using the
fit
from the question and having loadedlibrary("partykit")
you can first coerce therpart
tree toparty
:There are only two small nuisances for extracting the data in the way you want: (1) The
model.frame()
from the original fit is always dropped in the coercion and needs to be reattached manually. (2) A different numbering scheme is used for the nodes. You want node 4 (rather than 5) now.Another route is to subset the subtree starting from node 4 and then taking the data from that:
Then
data_party(pfit4)
gives you the same asdata4
above. Andpfit4$data
gives you the data without the(fitted)
node and the predicted(response)
.Yet another way, this works by finding all of the terminal nodes of any particular node and returning the subset of data used in the call.