Skip to contents

Mirrors the C# RandomForest class: fits number_of_trees decision trees on bootstrap resamples of the training data and reports the spread of their predictions as an interval.

Usage

ml_random_forest(
  x,
  y,
  newdata,
  seed = NULL,
  regression = TRUE,
  features = NULL,
  minimum_split_size = 2,
  max_depth = 100,
  number_of_trees = 1000,
  alpha = 0.1
)

Arguments

x

a numeric matrix or data frame of training predictors, one row per observation.

y

the training response, one value per row of x.

newdata

predictors to predict for, with the same number of columns as x.

seed

integer PRNG seed; NULL uses the computer clock.

regression

TRUE (the default) fits regression trees; FALSE classifiers.

features

the number of random features per split. NULL uses max(1, ncol(x) - 1).

minimum_split_size

the smallest node a tree will split. Default 2.

max_depth

the recursion cap. Default 100.

number_of_trees

how many trees to grow. Default 1000.

alpha

the interval level: 0.1 (the default) gives a 90% interval.

Value

a matrix with one row per row of newdata and columns lower, median, upper, mean.

Details

Training cost is linear in number_of_trees, and the library's default of 1000 is the knob to turn if a call is slow – a few dozen trees is usually enough to see the shape of the answer. A seeded run is bit-identical between R and Python, because the whole computation lives in the shared compiled core.

For a classifier every column is floored to an integer class label, including mean.

Examples

x <- c(1, 2, 3, 4, 5, 6, 100, 101, 102, 103, 104, 105)
y <- c(10, 11, 10, 11, 10, 11, 100, 101, 100, 101, 100, 101)
ml_random_forest(x, y, newdata = c(3, 104), seed = 42, number_of_trees = 25)
#>      lower median upper  mean
#> [1,]    10     10    11  10.4
#> [2,]   100    100   101 100.4