RANDOM_FOREST

RANDOM_FOREST fits a bootstrap-aggregated CART regression forest. The first series in the input list is the dependent variable; all remaining series are predictors. RainbowStats aligns the series to a common date range before estimation.

Syntax

RANDOM_FOREST(LIST(Y,X1,X2,...),TREES:500,MAX_DEPTH:12,MIN_LEAF:5,MTRY:2,SEED:12345)

Arguments

ArgumentDescriptionDefault
LIST(Y,X1,...)Y is the dependent series. X1...Xp are predictor series.Required
TREESNumber of bootstrap regression trees.500
MAX_DEPTHMaximum depth of each CART tree.12
MIN_LEAFMinimum observations allowed in a terminal leaf.5
MTRYNumber of randomly selected predictors considered at each split.max(1,p/3)
SEEDRandom seed for reproducible results.12345

Method

Each tree is estimated from a bootstrap sample. At each node, a random subset of predictors is considered and the split producing the largest reduction in squared error is selected. Forest fitted values are averages across trees.

Observations not selected in a tree's bootstrap sample are out-of-bag (OOB) observations. RainbowStats uses these observations to calculate OOB predictions, RMSE and R-squared. Predictor importance is measured by the increase in OOB squared prediction error after permuting each predictor.

Extracts

Use EXTRACT(rf,extract_name) or its shorthand EX(rf,extract_name).

ExtractResult
summary / modelModel settings, training diagnostics and OOB diagnostics.
fitted / predictedForest conditional-mean fitted-value series.
residualsActual minus mean fitted series.
oob / oob_predictionOut-of-bag mean prediction series.
importanceRaw OOB permutation-importance array in original predictor order.
benchmarkComparison of forest and OLS fit and validation diagnostics.
ols_summary / ols_inferenceOLS benchmark coefficients and inference table.
ols_fittedOLS benchmark fitted-value series.
ols_oos / ols_loocvOLS leave-one-out prediction series.
regression / proposed_regressionProposed regression using the forest input data.
selected_dataAligned dependent and predictor series used by the forest.
equationHTML explanation of the exact forest mean equation: the average of tree predictions expressed as terminal-region indicators.
rulesOriginal table of up to 50 terminal leaf rules, ranked by influence; rules from different trees can overlap or repeat.
consolidate_rules / consolidated_rulesFull table grouping identical terminal regions across trees. Includes exact conditions, supporting trees, leaf predictions, combined weight, active contribution, matching aligned rows and bootstrap counts.
rules_charts / rule_chartsSlideshow containing the consolidated rule table and one contribution line chart per nonzero rule on the aligned observations.
rules_bar_chart / bar_chart / bar_chartsSingle horizontal bar chart of mean rule contributions. Shows the top 30 by absolute contribution, with remaining contributions grouped into Other positive rules and Other negative rules.
predictor_bar_chartsSlideshow with one horizontal rule-contribution chart per predictor, followed by its matching rule-detail table. Shows up to 30 nonzero rules per predictor, selected by absolute mean contribution.
regimesTable grouping nearby split thresholds for each predictor, with threshold ranges, tree support, split support and mean right-minus-left subtree prediction contrast.
regimes_bar_chart / regimes_bar_chartsSlideshow with one horizontal threshold-regime chart per predictor, followed by its matching detail table. Shows up to 30 nonzero clusters per predictor, selected by tree support and then split support.
actual_vs_fittedActual-versus-mean-fitted chart.
slideshowModel slideshow with diagnostics and the original rule/regime tables. Use the chart extracts separately for contribution and regime slideshows.

Interpreting rules and regime charts

consolidate_rules combines regions with identical full-precision bounds after simplifying repeated bounds on a predictor. Nearby but different thresholds remain separate. Consolidation summarizes the fitted forest without changing its predictions. The full table retains zero-contribution rules; contribution charts omit rules whose contribution is zero throughout the aligned sample.

For a consolidated rule, its active contribution is the sum of its member leaf predictions divided by the total number of trees. Its line chart shows that contribution when all rule conditions hold and zero otherwise. Contributions from all consolidated rules sum to the forest mean fitted value at each observation. Its bar height is the active contribution multiplied by the fraction of aligned observations matching the rule. All bars in rules_bar_chart, including the Other bars, sum to the mean fitted prediction.

Labels such as R1 identify rules in the full consolidated table. Predictor charts shorten the displayed bounds; the next slide gives full conditions and exact thresholds for the displayed rules. A multivariate rule appears on each predictor chart whose predictor it constrains. These charts show the full conditional rule contribution, so contributions must not be summed across predictor charts. Remaining contributions are reported in subtitles rather than Other bars.

regimes groups nearby split thresholds within 2.5% of a predictor's observed range around a running weighted center. Labels such as G1 identify clusters in the full regime list. Chart ranges describe clusters of split locations, not disjoint intervals of predictor values. Bars show the observation-weighted mean right-minus-left subtree prediction contrast. Positive means the higher side predicts a higher target within the tree's other conditions; negative means a lower target. Supporting-tree counts show how widely the split cluster appears in the forest; a tree may support several clusters.

Chart interpretation: rule bars decompose mean fitted predictions; their sign does not indicate the direction of a predictor's effect. Regime bars summarize conditional prediction contrasts and are not additive contributions. Neither chart establishes causal effects or out-of-sample performance. Matching-observation counts refer to unique aligned rows; bootstrap counts include repeated observations.

Example

u=DIFF(SERIES_SINCE(UNRATE,20000101))
p=LOGDIFF(SERIES_SINCE(PAYEMS,20000101))
c=LOGDIFF(SERIES_SINCE(CPIAUCSL,20000101))
i=LOGDIFF(SERIES_SINCE(INDPRO,20000101))
rf=RANDOM_FOREST(LIST(u,p,c,i),TREES:500,MAX_DEPTH:12,MIN_LEAF:5,MTRY:1,SEED:12345)
EXTRACT(rf,summary)
EXTRACT(rf,importance)
EXTRACT(rf,slideshow)
Interpretation: training fit describes in-sample performance. OOB statistics provide the more useful internal estimate of predictive performance. A positive permutation-importance value means that shuffling the predictor worsened OOB prediction.

Rule and regime examples

EX(rf,equation)
EX(rf,rules)
EX(rf,consolidate_rules)
EX(rf,rules_charts)
EX(rf,rules_bar_chart)
EX(rf,predictor_bar_charts)
EX(rf,regimes)
EX(rf,regimes_bar_chart)