Skip to contents

With newdata = NULL this returns out-of-bag predictions, not in-sample ones. See fit_rf_model.

Usage

# S3 method for class 'rf_fit'
predict(object, newdata = NULL, ...)

Arguments

object

An rf_fit.

newdata

Optional sf object carrying the same predictors. It is transformed to the CRS used at fitting time first, so a forest that includes the coordinates is not fed a different coordinate system. Categorical predictors must not carry a level the forest was never grown with, meaning a level with no training rows, not merely one absent from levels(): an ordinary subset, or a spatial-CV fold that holds out a whole class, keeps the unused level while the forest has no split for it. An unseen level is an error, not a guess. A predictor that was numeric or logical when the forest was grown must also arrive numeric or logical: ranger would otherwise factor-code a character column and apply the numeric split thresholds to the codes, predicting confidently from nonsense, so a character column is refused instead. (ranger sees a logical as 0/1, so either form is accepted for one.)

...

Passed to ranger's predict method. Arguments that make ranger return a matrix (predict.all = TRUE, type = "quantiles", type = "se" with predict.all) are rejected, because this method's contract is one number per row of newdata. Call predict(fit$engine, data = ...) directly for those. So is anything ranger's predict method itself refuses, such as type = "quantiles" on a forest grown without quantreg = TRUE or type = "se" without keep.inbag = TRUE: the error names ranger's reason. seed defaults to a constant: an unset seed makes ranger draw one uniform from the global RNG stream per call, so the number of predict() calls a script happens to make (via predict_surface's chunk_size, say) would otherwise shift every later random draw. It does not affect a regression forest's predictions; pass your own if you need one.

Value

Numeric vector, aligned to nrow(newdata) with NA for rows dropped as incomplete (so all NA, with a WARN line in the log, when every row is). A failure inside ranger's predict method is an error, not an all-NA vector.