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:
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
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?
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.
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.
The easiest way to plot a decision tree in R is to use the prp() function from the rpart. plot package.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With