2

Is it possible to extract the node assignment for a fitted rpart tree? What about when I apply the model to new data?

The idea is that I would like to use the nodes as a way to cluster my data. In other packages (e.g. SPSS), I can save the predicted class, probabilities, and node number for further analysis.

Given how powerful R can be, I imagine there is a simple solution to this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Btibert3
  • 38,798
  • 44
  • 129
  • 168
  • possible duplicate of [Search for corresponding node in a regression tree using rpart](http://stackoverflow.com/questions/5102754/search-for-corresponding-node-in-a-regression-tree-using-rpart) – joran Mar 21 '12 at 01:39
  • 1
    Also note that for the data used to fit the model (as opposed to predictions for new values), you can find this info in the `where` component of the object created by `summary.rpart`, or in the fitted rpart object itself. – joran Mar 21 '12 at 01:40

1 Answers1

12

Try using the partykit package:

library(rpart)
z.auto <- rpart(Mileage ~ Weight, car.test.frame)
library(partykit)
z.auto2 <- as.party(z.auto)
predict(z.auto2, car.test.frame[1:3,], type = "node")

# Eagle Summit 4 Ford Escort   4  Ford Festiva 4 
#              7               7               7 
rawr
  • 20,481
  • 4
  • 44
  • 78
topepo
  • 13,534
  • 3
  • 39
  • 52