Skip to contents

Predictions of a forest (class rpf, from rpf() with prune = FALSE) for new data: the mean of its tree families' predictions. For classification, type chooses probabilities, classes or the raw scores. A pruned model, the default of rpf(), has its own method, predict.rpf_pruned(), with the same arguments.

Usage

# S3 method for class 'rpf'
predict(
  object,
  new_data,
  type = ifelse(object$mode == "regression", "numeric", "prob"),
  nthreads = NULL,
  ...
)

Arguments

object

[rpf]: A fitted rpf model.

new_data

[data.frame | matrix]: New observations to predict.

type

[character(1)]: "numeric" for regression outcomes, "class" for class predictions or "prob" for probability predictions. Defaults to "numeric" for regression and "prob" for classification.

For classification and loss = "L1" or "L2", "numeric" yields raw predictions which are not guaranteed to be valid probabilities in [0, 1]. For type = "prob", these are truncated to ensure this property.

For classification, type = "link" is an alias for type = "numeric". If loss is "logit" or "exponential", the raw predictions are the scores on the link scale, like the linear predictor of a glm: with two classes one score, the log-odds of the second class; with more, one score per class ("exponential", summing to 0) or per class but the first ("logit", whose first class has score 0). The probabilities of type = "prob" are their softmax (see "Classification losses" in rpf()). If loss is "L1" or "L2", the link is the identity.

nthreads

[integer(1) | NULL: NULL]: Number of threads. NULL uses the nthreads the forest was fitted with, capped at the available cores.

...

Not currently used, but required for extensibility. Unknown arguments are an error.

Value

For regression: A tbl with column .pred with the same number of rows as new_data.

For classification: A tbl with one column .pred_<level> for each level in y containing class probabilities if type = "prob". For type = "class", one column .pred_class with class predictions is returned. For type = "numeric" or "link", raw predictions are returned: one column .pred for binary outcomes (the log-odds of the second level with loss = "logit" or "exponential"), and one column .pred_<level> per level for multiclass outcomes. With loss = "logit" or "exponential", these are one score per class that sum to 0 across the classes, and the probabilities are their softmax. (The multiclass logit loss fits K - 1 scores against the first level; they are reported with the first level's score 0 joined and centred across the K classes, which leaves the probabilities as they are.)

Details

new_data is prepared as the training data were: by the formula or recipe of the fit, and with factor levels coded as in the fit (see "Factor predictors" in rpf()). Missing values are an error.

See also

predict.rpf_pruned() for the pruned model, predict_components() for the prediction as a sum of components.

Examples

# Regression: a tibble with the column .pred
rpfit <- rpf(mpg ~ cyl + wt, data = mtcars, n_families = 20, prune = FALSE)
predict(rpfit, mtcars[1:3, ])
#> # A tibble: 3 × 1
#>   .pred
#>   <dbl>
#> 1  21.1
#> 2  20.2
#> 3  24.2

# Classification: probabilities by default, or classes, or the raw scores
rpfit <- rpf(Species ~ ., data = iris, n_families = 20, prune = FALSE)
predict(rpfit, iris[c(1, 51, 101), ])
#> # A tibble: 3 × 3
#>   .pred_setosa .pred_versicolor .pred_virginica
#>          <dbl>            <dbl>           <dbl>
#> 1     0.990             0.00654         0.00314
#> 2     0.00521           0.963           0.0315 
#> 3     0.000948          0.00452         0.995  
predict(rpfit, iris[c(1, 51, 101), ], type = "class")
#> # A tibble: 3 × 1
#>   .pred_class
#>   <fct>      
#> 1 setosa     
#> 2 versicolor 
#> 3 virginica  
predict(rpfit, iris[c(1, 51, 101), ], type = "link")
#> # A tibble: 3 × 3
#>   .pred_setosa .pred_versicolor .pred_virginica
#>          <dbl>            <dbl>           <dbl>
#> 1         3.59            -1.43          -2.16 
#> 2        -2.34             2.88          -0.541
#> 3        -2.84            -1.28           4.12