Warning to those who read this later ...
My background in tree and graph algorithms meant that I expected this to be a simple question with a simple answer. But, it is not.
How this could work in the context that I come from is, say, functions left and right that get the left and right node of the current node. So that root(fit) is the root node and left(root(fit)) is the left child of the root node. And that other functions such as split(node) would give the information about the decision in the node.
However, the real answer is - almost no one who uses rpart, including the developer, thinks of a decision tree as being anything to do with a tree in that sense. Most of the answers given involve merely printing the tree out in human readable form as a diagram or text.
Several people have asked this question and the best answer is to look at getAnywere(summary.rpart), which lists the code that produces the text version of the tree. Is is quite mucky.
The original question follows.
I have a decision tree obtained by ...
makeDecisionTree <- function(ndata, fp, fn) {
ctrl <- rpart.control( nobs = nrow(ndata), mincut = 2, minsize=20, maxdepth=20)
mytree <- rpart( formula = PredictedLabel ~ . ,
data=ndata,
minsplit=1,
method="class",
control = ctrl,
parms=list(split="information",
loss=matrix(c(0, fp, fn, 0),
byrow=TRUE, nrow=2)) )
return(mytree)
}
I can print it out and get a listing of the decisions such as x>6, and I can understand what the tree is doing and so on. But, what I cannot see is how to work directly with the tree structure - in the sense of a recursive descent of the tree from the root node, under program control.
I have got to the point that I am considering seriously to print the tree to a text file and parse the resulting file just to get the actual tree structure. As this seems somewhat absurd -- I am assuming that I am missing something.
I have looked at the structure of the types, and looked at the splits matrix and so on. But, it is not clear to me how any of this produces a tree structure.