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 makerangerreturn a matrix (predict.all = TRUE,type = "quantiles",type = "se"withpredict.all) are rejected, because this method's contract is one number per row ofnewdata. Callpredict(fit$engine, data = ...)directly for those. So is anythingranger's predict method itself refuses, such astype = "quantiles"on a forest grown withoutquantreg = TRUEortype = "se"withoutkeep.inbag = TRUE: the error names ranger's reason.seeddefaults to a constant: an unsetseedmakesrangerdraw one uniform from the global RNG stream per call, so the number ofpredict()calls a script happens to make (viapredict_surface'schunk_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.
See also
Other methods on a fitted model:
coef.bayesian_fit(),
coef.gwr_fit(),
coef.rf_fit(),
fitted.bayesian_fit(),
fitted.gwr_fit(),
fitted.rf_fit(),
model_metrics(),
predict.bayesian_fit(),
predict.gwr_fit(),
print.rf_fit(),
print.spatial_fit(),
residuals.bayesian_fit(),
residuals.gwr_fit(),
residuals.rf_fit(),
summary.spatial_fit()