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.
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, ...)A list with the following components:
The mtry value with the lowest average RMSE.
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".
The evaluation strategy used.
Number of trees used in each fit.
The matched call.
a model formula (or a character string coercible to one).
a data frame containing the variables of formula.
integer; lower bound of the mtry grid. Default
1.
integer; upper bound of the mtry grid. Default
is the number of predictors in formula.
integer; step of the mtry grid. Default
1.
integer; number of trees in each random forest fit.
Default 500.
character; evaluation strategy. One of "oob"
(default), "repeatedcv" or "cv".
integer; number of folds when
cv.method != "oob". Default 10.
integer; number of repeats when
cv.method == "repeatedcv". Default 5.
number of threads passed to
ranger. The default (NULL) lets
ranger pick a sensible value.
logical; if TRUE (default) the mtry vs.
RMSE curve is plotted.
logical; if TRUE (default) progress messages are
printed for every evaluated mtry.
additional arguments forwarded to
ranger (e.g. importance = "impurity").
Stamatis Kalogirou stamatis.science@gmail.com, Stefanos Georganos stefanos.georganos@kau.se
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.
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").
grf, ranger
# \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