r - Get id/name of rpart model nodes -


how can id (or name) of terminal node of rpart model every row? predict.rpart can return predicted class (number or factor) or class probability or combination (using type="matrix") classification tree.

i like:

fit <- rpart(kyphosis ~ age + number + start, data = kyphosis) plot(fit) # there 5 terminal nodes predict(fit, type = "node_id")   # should return ids of terminal nodes (e.g. 1-5) (does not work) 

for model there 4 splits, yielding 5 "terminal nodes" or in terminology used in rpart: <leaf>s. not see why there should 5 predictions anything. predictions particular cases , leaves result of variable number of splits used make predictions. numbers of rows in original dataset ended in leaves may want, in case these ways of getting numbers:

# row-wise predicted class fit$where  # counts of cases in leaves of prediction rules table(fit$where)  3  5  7  8  9  29 12 14  7 19  

in order assemble labels(fit) apply particular leaf, need traverse rule-tree , accumulate labels splits applied produce particular leaf. want at:

?print.rpart     ?rpart.object ?text.rpart ?labels.rpart 

Comments