Plot ctree using rpart.plot functionality
Asked Answered
M

1

6

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 method is terrible when the tree is deep. In my case, my max_depth = 5.

I really enjoy rpart.plot's output as it allows for deep trees to visually display better. How the output looks for a simple example:

rpart

library(partykit)
library(rpart)
library(rpart.plot)

df_test <- cu.summary[complete.cases(cu.summary),]

multi.class.model <- rpart(Reliability~., data = df_test)

rpart.plot(multi.class.model)

enter image description here

I would like to get this output from the partykit model using ctree

ctree

multi.class.model <- ctree(Reliability~., data = df_test)

rpart.plot(multi.class.model)
>Error: the object passed to prp is not an rpart object

Is there some way one could coerce the ctree object to rpart so this would run?

Mimas answered 18/1, 2018 at 13:11 Comment(0)
L
7

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:

ctree-default

When turning the tree into a simpleparty you get a textual summary by default:

st <- as.simpleparty(ct)
plot(st)

simpleparty

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))

simpleparty2

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...

Loree answered 19/1, 2018 at 11:52 Comment(2)
thanks @achim-zeileis, appreciate you taking the time to help. Ill play around with the function. What is the official git repo for project, then if I integrate some color schemes to decision nodes and leaves Ill pull request the functionMimas
I would recommend that you start out from node_terminal (note that this has the class grapcon_generator) and create your own node_prp_like (or something like that). The package is not on GitHub but on R-Forge (old-style svn): R-Forge.R-project.org/projects/partykit Please send suggestions simply via e-mail and then we'll take it from there. If you have further questions, please feel free to ask here, on R-Forge or directly via e-mail if you feel it is too specific for a public forum.Loree

© 2022 - 2024 — McMap. All rights reserved.