I want to add some information to my tree. Let's say for instance I have a database like this :
library(rpart)
library(rpart.plot)
set.seed(1)
mydb<-data.frame(results=rnorm(1000,0,1),expo=runif(1000),var1=sample(LETTERS[1:4],1000,replace=T),
var2=sample(LETTERS[5:6],1000,replace=T),var3=sample(LETTERS[20:25], 1000,replace=T))
I can run a tree :
mytree<-rpart(results~var1+var2+var3,data=mydb,cp=0)
pfit<- prune(mytree, cp=mytree$cptable[4,"CP"])
prp(pfit,type=1,extra=100,fallen.leaves=F,shadow.col="darkgray",box.col=rgb(0.8,0.9,0.8))
And it's ok for me, but let's imagine I want to know the average exposure for each leaf.
I know i can add some informations to prp, for instance the weight of each leaf with a function :
node.fun1 <- function(x, labs, digits, varlen)
{
paste("Weight \n",x$frame$wt)
}
prp(pfit,type=1,extra=100,fallen.leaves=F,shadow.col="darkgray",box.col=rgb(0.8,0.9,0.8),node.fun = node.fun1)
But it works only if it's calculated in frame, the results of the rpart function.
My question :
How can I add custom informations to the plot, like the average exposure, or any other function that calculates custom indicators and add it to the table frame ?


This is really nice, I didn't know this was an option.
All the work seems to be getting the subset of the original data used on each node. This is easy for terminal nodes, but I didn't find a straight-forward way of identifying rows of data that were used in every node, not just the leaves. If someone knows an easier way, I would love to hear it.
Define your new function which takes
x, the result of fittingrpart(I didn't look into the other arguments, but the vignette should be helpful).For every line of
x$framewe need to get the data used to calculate summary statistics. Unfortunately,x$whereonly tells us the terminal node in which each observation lies. So for each node number, we usesubset.rpartto get the underlying data, and do whatever you want with itThe work was done by
subset.rpartwhich takes a node number and returns the subset ofdataused on the node.Tests