使用rpart.plot功能绘制ctree [英] Plot ctree using rpart.plot functionality

查看:1012
本文介绍了使用rpart.plot功能绘制ctree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使用rpart.plot包从partykit库中绘制ctree.原因是当树很深时,默认的绘图方法很糟糕.就我而言,是我的max_depth = 5.

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.

我非常喜欢rpart.plot的输出,因为它允许深树更好地视觉显示.输出如何寻找一个简单的示例:

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:

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)

我想使用ctree从partykit模型中获取此输出

I would like to get this output from the partykit model using 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

是否可以通过某种方式将ctree对象强制为rpart以便运行?

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

推荐答案

据我所知,用于可视化rpart树的所有其他软件包实际上都是rpart特定的,而不是基于不可知的party类用于表示树/递归分区.另外,我们还没有尝试为party对象实现as.rpart()方法,因为rpart类实际上并不适合于此.

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.

但是您可以尝试调整partykit可视化效果,这些可视化效果可以通过面板功能针对树的几乎所有方面进行自定义.可能有用的一件事是计算一个simpleparty对象,该对象在每个节点的$info中具有各种简单的摘要信息.然后可以在node_terminal()面板功能中使用它在树状显示中打印信息.考虑下面的简单示例,以预测德国社会经济专家组中三种学校类型之一.为了达到所需的深度,我关闭了重要性测试:

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)

足够大的设备上的默认plot(ct)可为您提供:

The default plot(ct) on a sufficiently big device gives you:

将树变成simpleparty时,默认情况下会显示文本摘要:

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

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

这仍然具有重叠的标签,因此我们可以设置一个小的便利函数,该函数从每个节点的$info中提取有趣的位,并将它们放入更长的字符向量中,且输入宽度较小:

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

除了终端面板功能(tp_args)的参数外,我还对边缘面板功能(ep_args)的参数进行了调整,以避免边缘的过度绘制.

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

这篇关于使用rpart.plot功能绘制ctree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆