Fits a local version of the Random Forest algorithm. The function fits one sub-model for every observation in the data set, where each local model is trained on the geographically nearest neighbours of the focal observation (adaptive kernel) or on every observation within a fixed distance (fixed kernel). Local observations may further be weighted with a bi-square kernel to reproduce a Geographically Weighted Random Forest.
grf(formula, dframe, bw, kernel = c("adaptive", "fixed"), coords,
ntree = 500, mtry = NULL, importance = "impurity", nthreads = NULL,
forests = TRUE, geo.weighted = TRUE, print.results = TRUE,
progress = print.results, oob.fallback = c("loo", "inbag"), ...)A list of class "grf" with the following components:
A ranger object containing the global random
forest fitted on dframe.
The coords object passed to the function.
A data frame with one row per observation and one column per predictor giving the local feature importance.
A data frame with the observed values and local
goodness-of-fit statistics: OOB and in-sample predicted values,
residuals, mean squared error, R-squared and the number of attempts
needed to obtain the OOB or fallback prediction (LPerm).
When forests = TRUE, a list of length
nrow(dframe) containing all local ranger forests.
A list with the average local variable importance, mean squared error (OOB and predicted), R-squared (OOB and predicted), and AIC / AICc statistics.
a formula (or character string coercible to one) specifying
the local model to be fitted, using the syntax of
ranger.
a data frame containing the dependent and independent
variables that appear in formula.
a positive number representing either the number of nearest
neighbours (when kernel = "adaptive") or the bandwidth in the
units of coords (when kernel = "fixed").
the type of kernel used to define the neighbourhood of each
focal observation. One of "adaptive" (default) or
"fixed".
a numeric matrix or data frame with two columns giving the
X and Y coordinates of the observations in dframe. The number of
rows must match nrow(dframe).
integer; the number of trees to grow for each (global and
local) random forest. Default is 500.
the number of variables randomly sampled as candidates at
each split. The default is max(floor(p / 3), 1) where p
is the number of predictors in formula.
variable importance measure. Default is
"impurity" (see ranger).
number of threads used by ranger
and by predict. The default (NULL) lets
ranger pick a sensible value.
logical; if TRUE (default) all local forests are
stored in the returned object. Required if you intend to call
predict.grf on new data.
logical; if TRUE (default) the local random
forests are fitted using bi-square geographical case weights. If
FALSE, local forests are fitted on the same neighbourhood but
without weighting (i.e. local but unweighted).
logical; if TRUE (default) the function
prints global and local model summaries to the console.
logical; if TRUE a text progress bar is shown
while the local random forests are fitted (only in interactive
sessions). Defaults to print.results.
character; fallback used when ranger cannot
produce an out-of-bag prediction for the focal observation. The
default "loo" fits one additional local forest after removing
the focal observation and predicts it out-of-sample. "inbag"
uses the in-sample local prediction.
additional arguments passed to ranger.
Stamatis Kalogirou stamatis.science@gmail.com, Stefanos Georganos stefanos.georganos@kau.se
Large data sets may take a long time to calibrate. A
high number of observations combined with forests = TRUE can
produce a very large output object.
Geographically Weighted Random Forest (GRF) is a spatial analysis method that fits a local version of the Random Forest algorithm to investigate spatial non-stationarity in the relationship between a dependent variable and a set of predictors. A sub-model is fitted for every observation in space, considering only its neighbouring observations. The technique is inspired by Geographically Weighted Regression (Kalogirou, 2003) but replaces the linear local model by a non-linear ensemble that is robust in high dimensions and less prone to over-fitting thanks to its bootstrapping nature, which relaxes the distributional assumptions of classical Gaussian statistics.
For each focal observation \(i\) the function:
identifies the local neighbourhood of \(i\) according to
kernel and bw;
computes bi-square geographical weights \(w_{ij} = (1 - (d_{ij} / h)^2)^2\) where \(d_{ij}\) is the distance between observations \(i\) and \(j\) and \(h\) is the local bandwidth;
fits a ranger random forest on the local sub-sample (with
case weights when geo.weighted = TRUE);
stores the local variable importance, the OOB and in-sample predictions for \(i\), and (optionally) the entire local forest.
For very small neighbourhoods or highly concentrated case weights,
ranger can occasionally have no out-of-bag trees for the focal
observation. In that case the OOB prediction is undefined. The default
oob.fallback = "loo" avoids noisy retry warnings by fitting one
extra local forest with the focal row removed and using that
out-of-sample prediction for the focal row.
The procedure is described in Georganos et al. (2019) and Georganos and Kalogirou (2022).
Georganos, S., Grippa, T., Niang Gadiaga, A., Linard, C., Lennert, M., Vanhuysse, S., Mboga, N., Wolff, E., Kalogirou, S. (2019) Geographical Random Forests: A Spatial Extension of the Random Forest Algorithm to Address Spatial Heterogeneity in Remote Sensing and Population Modelling. Geocarto International, tools:::Rd_expr_doi("10.1080/10106049.2019.1595177").
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").
Kalogirou, S. (2003) The Statistical Analysis and Modelling of Internal Migration Flows within England and Wales. PhD Thesis, School of Geography, Politics and Sociology, University of Newcastle upon Tyne.
predict.grf, grf.bw,
ranger
# \donttest{
set.seed(1)
RDF <- random.test.data(8, 8, 3)
Coords <- RDF[, 4:5]
m <- grf(dep ~ X1 + X2, dframe = RDF, bw = 12,
kernel = "adaptive", coords = Coords,
ntree = 100, mtry = 1, nthreads = 1,
print.results = FALSE, progress = FALSE)
# }
if (FALSE) {
## Real-world demonstration on the Greek Income dataset.
## Not run by R CMD check because a full GRF on 325 municipalities
## with 500 trees per local fit takes longer than a CRAN example
## should.
data(Income)
Coords <- Income[, 1:2]
m <- grf(Income01 ~ UnemrT01 + PrSect01, dframe = Income, bw = 60,
kernel = "adaptive", coords = Coords)
}
Run the code above in your browser using DataLab