parttree: Convert a decision tree into a data frame of partition...

View source: R/parttree.R

parttreeR Documentation

Convert a decision tree into a data frame of partition coordinates

Description

Extracts the terminal leaf nodes of a decision tree with one or two numeric predictor variables. These leaf nodes are then converted into a data frame, where each row represents a partition (or leaf or terminal node) that can easily be plotted in coordinate space.

Usage

parttree(tree, keep_as_dt = FALSE, flipaxes = FALSE)

Arguments

tree

A tree object. Supported classes include rpart::rpart.object, or the compatible classes from from the parsnip, workflows, or mlr3 front-ends, or the constparty class inheriting from partykit::party().

keep_as_dt

Logical. The function relies on data.table for internal data manipulation. But it will coerce the final return object into a regular data frame (default behavior) unless the user specifies TRUE.

flipaxes

Logical. The function will automatically set the y-axis variable as the first split variable in the tree provided unless the user specifies TRUE.

Details

This function can be used with a regression or classification tree containing one or (at most) two numeric predictors.

Value

A data frame comprising seven columns: the leaf node, its path, a set of coordinates understandable to ggplot2 (i.e., xmin, xmax, ymin, ymax), and a final column corresponding to the predicted value for that leaf.

See Also

geom_parttree(), rpart::rpart(), partykit::ctree().

Examples

## rpart trees
library("rpart")
rp = rpart(Species ~ Petal.Length + Petal.Width, data = iris)
parttree(rp)

## conditional inference trees
library("partykit")
ct = ctree(Species ~ Petal.Length + Petal.Width, data = iris)
parttree(ct)

## rpart via partykit
rp2 = as.party(rp)
parttree(rp2)

grantmcdermott/parttree documentation built on Jan. 4, 2023, 6:40 p.m.