Extracts the decision rules for terminal nodes in a specified tree from an aorsf ObliqueForest model. Each rule represents the path from the root node to a terminal node using oblique (linear combination) splits.
Usage
# S3 method for class 'ObliqueForest'
extract_rules(x, tree = 1L, ...)Value
A tibble with columns:
tree: integer, the tree number (1-based).rules: list of R expressions, one per terminal node.id: integer, the terminal node ID (1-based for user convenience).
Details
Tree and Node Indexing
Both the tree parameter and the id column use 1-based indexing for
user convenience, matching R's standard indexing convention:
tree = 1extracts rules from the first treeid = 1refers to the first terminal node
Internally, aorsf uses 0-based indexing (where node 0 is the root), but this is automatically converted to 1-based indexing in the output for consistency with R conventions.
Factor Variables and Reference Coding
The aorsf package internally converts unordered factor variables using reference coding (also called dummy coding). For a factor with k levels, aorsf creates k-1 binary indicator variables, with the first level serving as the reference category:
A factor
colorwith levels["red", "blue", "green"]creates indicators forblueandgreenonly. When both indicators are 0, it representsred.The extracted rules automatically convert these back to factor comparisons:
2.1 * color_bluebecomes2.1 * (color == "blue").Rules can be evaluated directly on data with the original factor columns (no need to manually create indicator variables).
Ordered factors are converted to a single integer variable representing the ordinal level, not to multiple indicators.
Reference coding prevents collinearity in the internal regression computations used to find optimal splits.
Predictor Scaling
The aorsf package always scales data during prediction, regardless of
the scale_x parameter setting. The coefficients stored in trees are for
scaled data: (x - mean) / sd for numeric predictors.
To make rules work with unscaled input data, the extracted rules automatically include the scaling transformation in the expressions themselves. For example, instead of showing a pre-computed unscaled coefficient, rules show:
This approach:
Allows rules to be evaluated directly on original (unscaled) data
Preserves full floating-point precision (avoids errors from pre-computing unscaled coefficients)
Makes the scaling transformation explicit and transparent
Factor indicator variables are not scaled since they are binary 0/1 values.
Examples
if (rlang::is_installed(c("aorsf", "palmerpenguins"))) {
# Classification example
penguins <- palmerpenguins::penguins[complete.cases(palmerpenguins::penguins), ]
set.seed(2847)
forest <- aorsf::orsf(species ~ ., data = penguins, n_tree = 3)
# Extract rules from first tree (default)
rules <- extract_rules(forest)
# View rules as text
rules$rules[[1]] |> rule_text(bullets = TRUE) |> cat("\n")
# Extract rules from different tree
rules3 <- extract_rules(forest, tree = 3L)
# Regression example
data(mtcars)
set.seed(5193)
forest_reg <- aorsf::orsf(mpg ~ ., data = mtcars, n_tree = 3)
rules_reg <- extract_rules(forest_reg, tree = 1L)
}
#> * 1.276 * (sex == "male") + 0.00232 * ((year - 2008)/0.8129) - 1.884 * ((bill_length_mm - 43.99)/5.469) > 0.9291