Learn R Programming

SpatialML (version 1.8.2)

grf.bw: Optimal Bandwidth Selection for a Geographically Weighted Random Forest

Description

Searches an exhaustive grid of bandwidths and returns the one that maximises the out-of-bag (OOB) R-squared of the local model produced by grf.

Usage

grf.bw(formula, dataset, kernel = "adaptive", coords, bw.min = NULL,
       bw.max = NULL, step = 1, trees = 500, mtry = NULL,
       importance = "impurity", nthreads = 1, forests = FALSE,
       geo.weighted = TRUE, verbose = TRUE, ...)

Value

A list with two components:

tested.bandwidths

A data frame with one row per evaluated bandwidth and four columns: Bandwidth, Local, Mixed, Low.Local.

Best.BW

The bandwidth that yields the highest Local R-squared.

Arguments

formula

a formula (or character string coercible to one) of the local model, as accepted by ranger.

dataset

a data frame containing the dependent and independent variables that appear in formula.

kernel

the kernel used in the regression. Either "adaptive" (default) or "fixed".

coords

a numeric matrix or data frame with the X and Y coordinates of the observations in dataset.

bw.min

numeric; lower bound of the bandwidth search.

bw.max

numeric; upper bound of the bandwidth search.

step

numeric; the step size of the search. Default is 1.

trees

integer; the number of trees to grow for each local random forest.

mtry

the number of variables randomly sampled as candidates at each split. Default is max(floor(p / 3), 1) where p is the number of predictors in formula. Values above p are capped at p.

importance

variable importance measure passed to ranger. Default is "impurity".

nthreads

number of threads passed to ranger and predict.

forests

logical; if TRUE all local forests are kept in memory. Default is FALSE (recommended for bandwidth search).

geo.weighted

logical; if TRUE fits a Geographically Weighted Random Forest using ranger's case-weight option.

verbose

logical; if TRUE (default) prints one progress message per evaluated bandwidth and the selected bandwidth.

...

additional arguments passed to grf and ranger.

Author

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

Warning

Bandwidth selection on large data sets is computationally expensive because a full grf is fitted at every bandwidth in the grid.

Details

The function evaluates a sequence of candidate bandwidths from bw.min to bw.max in increments of step. For each bandwidth a Geographically Weighted Random Forest is fitted via grf and three goodness-of-fit values are recorded:

Local

R-squared of the local model alone.

Mixed

R-squared of the equally weighted average of local and global predictions.

Low.Local

R-squared of 0.25 * local + 0.75 * global.

The optimal bandwidth is selected as the one that maximises the Local R-squared.

If you require fully reproducible bandwidth selection, call set.seed() before grf.bw().

References

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").

See Also

grf, ranger

Examples

Run this code
  # \donttest{
    set.seed(1)
    RDF <- random.test.data(8, 8, 3)
    Coords <- RDF[, 4:5]
    bw.test <- grf.bw(dep ~ X1 + X2, dataset = RDF, kernel = "adaptive",
                      coords = Coords,
                      bw.min = 12, bw.max = 16, step = 2,
                      trees = 100, mtry = 1, nthreads = 1L,
                      forests = FALSE, geo.weighted = TRUE,
                      verbose = FALSE)
    bw.test$Best.BW
  # }
  if (FALSE) {
    ## Real-world demonstration on the Greek Income dataset.
    ## Not run by R CMD check because an exhaustive bandwidth search on
    ## 325 municipalities is intrinsically slow.
    data(Income)
    Coords <- Income[, 1:2]

    bwe <- grf.bw(Income01 ~ UnemrT01 + PrSect01, dataset = Income,
                  kernel = "adaptive", coords = Coords,
                  bw.min = 30, bw.max = 80, step = 5,
                  forests = FALSE, geo.weighted = TRUE)

    m <- grf(Income01 ~ UnemrT01 + PrSect01, dframe = Income,
             bw = bwe$Best.BW, kernel = "adaptive", coords = Coords)
  }

Run the code above in your browser using DataLab