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

Description Usage Arguments Details Value See Also Examples

View source: R/parttree.R

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

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## 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 Dec. 24, 2021, 11:56 a.m.