Plot ctree using rpart.plot functionality

别说谁变了你拦得住时间么 提交于 2019-12-01 06:40:25

问题


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)

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?


回答1:


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



来源:https://stackoverflow.com/questions/48322213/plot-ctree-using-rpart-plot-functionality

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!