Learn R Programming

SpatialML (version 1.8.2)

rf.mtry.optim: Optimal mtry for a Random Forest via OOB or Cross-Validation

Description

Searches for the value of mtry that minimises the predictive error of a Random Forest on a user-supplied grid. Three evaluation strategies are available:

  • "oob" (default): one ranger fit per mtry; OOB error is the criterion (fast).

  • "cv": a single cv.folds-fold cross-validation is performed for each mtry.

  • "repeatedcv": cv.repeats repeats of cv.folds-fold CV are performed for each mtry.

The selected value can then be passed to the mtry argument of grf.

Usage

rf.mtry.optim(formula, dataset, min.mtry = NULL, max.mtry = NULL,
              mtry.step = 1, num.trees = 500,
              cv.method = c("oob", "repeatedcv", "cv"),
              cv.folds = 10, cv.repeats = 5, num.threads = NULL,
              plot.it = TRUE, verbose = TRUE, ...)

Value

A list with the following components:

best.mtry

The mtry value with the lowest average RMSE.

results

A data frame with one row per evaluated mtry and the columns mtry, RMSE, Rsquared, SDRMSE and SDRsq. Standard deviations are NA for cv.method == "oob".

cv.method

The evaluation strategy used.

num.trees

Number of trees used in each fit.

call

The matched call.

Arguments

formula

a model formula (or a character string coercible to one).

dataset

a data frame containing the variables of formula.

min.mtry

integer; lower bound of the mtry grid. Default 1.

max.mtry

integer; upper bound of the mtry grid. Default is the number of predictors in formula.

mtry.step

integer; step of the mtry grid. Default 1.

num.trees

integer; number of trees in each random forest fit. Default 500.

cv.method

character; evaluation strategy. One of "oob" (default), "repeatedcv" or "cv".

cv.folds

integer; number of folds when cv.method != "oob". Default 10.

cv.repeats

integer; number of repeats when cv.method == "repeatedcv". Default 5.

num.threads

number of threads passed to ranger. The default (NULL) lets ranger pick a sensible value.

plot.it

logical; if TRUE (default) the mtry vs. RMSE curve is plotted.

verbose

logical; if TRUE (default) progress messages are printed for every evaluated mtry.

...

additional arguments forwarded to ranger (e.g. importance = "impurity").

Author

Stamatis Kalogirou stamatis.science@gmail.com, Stefanos Georganos stefanos.georganos@kau.se

Details

The criterion to minimise is the average RMSE across folds (or the OOB RMSE in the "oob" case). The average squared correlation between predictions and observations is also recorded as the Rsquared column.

For reproducible results call set.seed() before rf.mtry.optim().

Compatibility note: the function no longer returns a caret train object. The dependency on caret (and indirectly on randomForest) has been removed and the cross-validation is now implemented directly on top of ranger. Code that read $bestTune$mtry from the previous return value should now read $best.mtry.

References

Wright, M. N. and Ziegler, A. (2017) ranger: A Fast Implementation of Random Forests for High Dimensional Data in C++ and R. Journal of Statistical Software, 77(1), 1-17. tools:::Rd_expr_doi("10.18637/jss.v077.i01").

Georganos, S. and Kalogirou, S. (2022) A Forest of Forests: A Spatially Weighted and Computationally Efficient Formulation of Geographical Random Forests. ISPRS International Journal of Geo-Information, 11(9), 471. tools:::Rd_expr_doi("10.3390/ijgi11090471").

See Also

grf, ranger

Examples

Run this code
  # \donttest{
    data(Income)
    set.seed(123)
    res <- rf.mtry.optim(Income01 ~ UnemrT01 + PrSect01, dataset = Income,
                         num.trees = 200, num.threads = 1L,
                         cv.method = "oob", plot.it = FALSE,
                         verbose = FALSE)
    res$best.mtry
    res$results
  # }

Run the code above in your browser using DataLab