plot.rfsgt: Tree Size Diagnostics for Random Forest Super Greedy Trees

View source: R/plot.rfsgt.R

plot.rfsgtR Documentation

Tree Size Diagnostics for Random Forest Super Greedy Trees

Description

Two diagnostics for choosing the tree size of a random forest of super greedy trees: the out-of-bag (OOB) empirical risk of each tree as it grows, and the OOB error of whole forests grown with different tree sizes.

Usage

## S3 method for class 'rfsgt'
plot(x,
  which = c("risk", "forest"),
  treesize = c(1, 2, 3, 6, 12, 25),
  lag = 8,
  smooth = TRUE,
  verbose = TRUE,
  ...)

Arguments

x

An object of class (rfsgt, grow).

which

Which plots to draw: "risk" for the OOB empirical risk of each tree by tree size, "forest" for the forest OOB error by tree size, or both (the default).

treesize

Tree sizes (upper bounds on the number of terminal nodes) at which forests are grown for the "forest" plot.

lag

Window of the running average applied to each tree's OOB empirical risk. Use 1 for no smoothing.

smooth

Logical. If TRUE, the average risk curve is smoothed by loess and the lowest point is taken from the smooth.

verbose

Logical. If TRUE, report each forest as it is grown.

...

Further arguments passed to rfsgt when growing the forests for the "forest" plot, for example ntree or seed. These take precedence over the settings copied from x.

Details

OOB empirical risk by tree size. Each tree is grown best split first, one terminal node at a time, and its OOB empirical risk is recorded at each size (x$oob.empr.risk). Each tree's risk is shown as a running average over lag sizes (grey) together with the average over trees (thin dark line) and its smooth (thick line). The average at a given size is over the trees that reached it. It is drawn where at least a fifth of the trees with recorded risk contributed, with a minimum of three contributors when available. If only one or two trees have recorded risk, all of them must contribute at that size.

Forest OOB error by tree size. The per-tree risk does not account for the averaging of the forest: deeper trees are individually worse but vary more from tree to tree, and averaging removes much of that variation. The forest is therefore often best at a larger tree size than the per-tree curve suggests. The second plot grows a forest at each value of treesize and shows its OOB error against the average number of terminal nodes (log scale); the forest x is shown in red at its average tree size. Use this plot to choose the tree size. The forests are grown from common random numbers; hence the differences between tree sizes are not confounded with run-to-run variation. Supplying seed in ... turns this off. The OOB error of a forest still varies from run to run, more for small data sets; the overall level of the curve can therefore shift between runs, and a larger ntree gives a more stable curve.

The forest-size comparison requires a finite OOB error for the fitted forest and each comparison forest. It is unavailable with bootstrap="none", which provides no OOB observations.

The forests are grown exactly as x was grown, apart from the tree size: from the response and source predictors stored in x, including parents of retained polynomial terms, with the basis terms of x (rebuilt from x$term.map; hence any filtering applied to x, for example by tune.hcut, is retained), and with the number of trees, hcut, node size, node centering, node restriction, best split first setting and sampling scheme of x. Grown at its own tree size, this reproduces x. Settings that are not stored in x (pure.lasso, eps, maxit, fast) take their defaults; pass them in ... if x used other values.

Value

Invisibly, a list with components risk (the running averages for each tree, curves; the average and smoothed average by tree size, average; and the size with the lowest smoothed average, lowest) and forest (a data frame of tree size, average number of terminal nodes and OOB error for each forest, with treesize = NA for x).

Author(s)

Hemant Ishwaran and Udaya B. Kogalur

References

Ishwaran H. (2026). Super greedy trees. Artificial Intelligence Review, 59:142. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1007/s10462-026-11541-6")}

See Also

rfsgt, tune.hcut

Examples


## ------------------------------------------------------------
##
##  ozone
##
## ------------------------------------------------------------

## load the data, calendar variables as integers
data(Ozone, package = "mlbench")
o <- data.frame(data.matrix(na.omit(Ozone)))

## fit the forest and plot both diagnostics
fit <- rfsgt(V4 ~ ., o, hcut = 3)
plot(fit)

## per-tree risk only (no forests are grown)
plot(fit, which = "risk")

## the forest error at more tree sizes, keeping the results
res <- plot(fit, which = "forest", treesize = c(1, 2, 4, 8, 16, 32, 64))
print(res$forest)

## ------------------------------------------------------------
##
##  boston housing, grown using a tune.hcut filter
##
## ------------------------------------------------------------

data(BostonHousing, package = "mlbench")
f <- tune.hcut(medv ~ ., BostonHousing, hcut = 3)
fit <- rfsgt(medv ~ ., BostonHousing, filter = use.tune.hcut(f, hcut = 3))

## the filtered basis terms of fit are retained by the forests
plot(fit)


randomForestSGT documentation built on Nov. 16, 2026, 5:07 p.m.