Skip to contents

Mirrors the C# DecisionTree class: recursively splits the training data on the feature and threshold that most reduce variance (regression) or most increase information gain (classification), then predicts by walking a new observation down the tree.

Usage

ml_decision_tree(
  x,
  y,
  newdata,
  seed = NULL,
  regression = TRUE,
  features = NULL,
  minimum_split_size = 2,
  max_depth = 100
)

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 for the random feature subsets; NULL uses the computer clock.

regression

TRUE (the default) fits a regression tree; FALSE a classifier.

features

the number of random features to consider at each split. NULL (the default) uses the library's own max(1, ncol(x) - 1).

minimum_split_size

the smallest node the tree will split. Default 2.

max_depth

the recursion cap. Default 100.

Value

a numeric vector of predictions, one per row of newdata.

Details

At the library's defaults a REGRESSION tree recurses until every leaf holds a single training observation, so it memorizes the training data and generalizes poorly. That is upstream's behaviour, not a port artifact, and it is why ml_random_forest() exists. Set minimum_split_size or max_depth to regularize it.

See also

ml_random_forest(), which averages many bootstrapped trees.

Examples

x <- c(1, 2, 3, 4, 5, 6, 100, 101, 102, 103, 104, 105)
y <- c(10, 10, 10, 10, 10, 10, 100, 100, 100, 100, 100, 100)
ml_decision_tree(x, y, newdata = c(3, 104), seed = 7)
#> [1]  10 100