Skip to content

Read a randomForest stump without getTree - #367

Merged
EmilHvitfeldt merged 1 commit into
mainfrom
rf-stump-getTree
Aug 16, 2026
Merged

Read a randomForest stump without getTree#367
EmilHvitfeldt merged 1 commit into
mainfrom
rf-stump-getTree

Conversation

@EmilHvitfeldt

Copy link
Copy Markdown
Member

Closes #362

A tree with a single root node and no split made tidypredict_fit() and parse_model() abort with "argument of length 0".

Root cause

The error comes from randomForest itself, before any tidypredict code runs. randomForest::getTree() assembles the node table with cbind() and subsets it to the tree's node count without drop = FALSE. For a stump that count is 1, so the matrix collapses to a plain vector, nrow() returns NULL, and the 1:nrow(tree) on the next line fails.

Fix

Both call sites (rf_tree_info_full() for the parse_model() path and build_nested_rf_tree() for the direct tidypredict_fit() path) now go through rf_get_tree(), which delegates to getTree() for a tree with more than one node and assembles the same six columns itself for a stump. The parser and the nested formula builder already handled a terminal root node, so nothing downstream changed.

Tests

Two tests in tests/testthat/test-model-rf.R, both comparing against randomForest::predict() on the direct and the parsed path, and both asserting the fit actually contains a stump. Verified they fail on the unpatched parser.

A tree with a single root node and no split made tidypredict_fit() and
parse_model() abort with "argument of length 0", a base-R error coming
out of randomForest itself.

randomForest::getTree() assembles the node table with cbind() and then
subsets it to the tree's node count without drop = FALSE. For a stump
that count is 1, so the matrix collapses to a plain vector, nrow()
returns NULL, and the 1:nrow(tree) on the next line fails before the
tree is ever returned. Nothing in tidypredict was reached.

Both call sites now go through rf_get_tree(), which delegates to
getTree() for a tree with more than one node and assembles the same
six columns itself for a stump. The rest of the parser and the nested
formula builder already handled a terminal root node.

A stump appears whenever the outcome is constant within a bootstrap
sample, which a constant outcome or a zero-variance predictor makes
routine, and predict() scores such a forest fine.

Closes #362
@EmilHvitfeldt
EmilHvitfeldt merged commit f949cd7 into main Aug 16, 2026
9 checks passed
@EmilHvitfeldt
EmilHvitfeldt deleted the rf-stump-getTree branch August 16, 2026 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

randomForest: a stump in the forest aborts with "argument of length 0"

1 participant