Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot ctree using rpart.plot functionality

Tags:

r

rpart

party

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?

like image 663
Hanjo Odendaal Avatar asked Jan 18 '18 13:01

Hanjo Odendaal


People also ask

What does a rpart plot do?

It automatically scales and adjusts the displayed tree for best fit. It combines and extends the plot. rpart and text. rpart functions in the rpart package.

What does rpart plot do in R?

This function combines and extends plot. rpart and text. rpart in the rpart package. It automatically scales and adjusts the displayed tree for best fit.

How do you plot a tree in R?

The easiest way to plot a decision tree in R is to use the prp() function from the rpart. plot package.

What is the rpart function in R?

Rpart is a powerful machine learning library in R that is used for building classification and regression trees. This library implements recursive partitioning and is very easy to use.


1 Answers

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

like image 122
Achim Zeileis Avatar answered Sep 30 '22 01:09

Achim Zeileis