Been trying to use the rpart.plot
package to plot a ctree
from the partykit
library. The reason for this being that the default plot m
To the best of my knowledge all the other packages for visualizing rpart
trees are really rpart
-specific and not based on the agnostic party
class for representing trees/recursive partitions. Also, we haven't tried to implement an as.rpart()
method for party
objects because the rpart
class is really not well-suited for this.
But you can try to tweak the partykit
visualizations which are customizable through panel functions for almost all aspects of the tree. One thing that might be helpful is to compute a simpleparty
object which has all sorts of simple summary information in the $info
of each node. This can then be used in the node_terminal()
panel function for printing information in the tree display. Consider the following simple example for predicting one of three school types in the German Socio-Economic Panel. To achieve the desired depth I switch significance testing essentiall off:
library("partykit")
data("GSOEP9402", package = "AER")
ct <- ctree(school ~ ., data = GSOEP9402, maxdepth = 5, alpha = 0.5)
The default plot(ct)
on a sufficiently big device gives you:
When turning the tree into a simpleparty
you get a textual summary by default:
st <- as.simpleparty(ct)
plot(st)
This has still overlapping labels so we could set up a small convenience function that extracts the interesting bits from the $info
of each node and puts them into a longer character vector with less wide entries:
myfun <- function(i) c(
as.character(i$prediction),
paste("n =", i$n),
format(round(i$distribution/i$n, digits = 3), nsmall = 3)
)
plot(st, tp_args = list(FUN = myfun), ep_args = list(justmin = 20))
In addition to the arguments of the terminal panel function (tp_args
) I have tweaked the arguments of the edge panel function (ep_args
) to avoid some of the overplotting in the edges.
Of course, you could also change the entire panel function and roll your own...