
Spatial Machine Learning
The R package SpatialML implements a spatial extension of the random forest algorithm: the Geographically Weighted Random Forest (GRF), currently for regression. GRF fits a local random forest at each location, allowing the relationship between the dependent variable and its predictors to vary across space — a property known as spatial non-stationarity. Compared with linear local models such as Geographically Weighted Regression, GRF combines this local perspective with a flexible, non-linear learner that is hard to overfit thanks to its bootstrapping nature.
Available in CRAN @ https://CRAN.R-project.org/package=SpatialML
Install the latest version from R with:
install.packages("SpatialML")
Current release: version 1.8.2 (6 July 2026). Requires R ≥ 4.1. The package now depends on a single random-forest back-end, ranger.
Main functions and data
- grf: Fits a Geographically Weighted Random Forest regression and returns local models, local variable importance, and goodness-of-fit diagnostics (including AIC/AICc)
- grf.bw: Geographically Weighted Random Forest optimal bandwidth selection
- predict.grf: Predictions at new locations (called automatically via
predict()) - rf.mtry.optim: Selects the optimal
mtryfor a random forest using out-of-bag error or (repeated) cross-validation - random.test.data: Synthetic spatial data generator for testing
- Income: Mean household income at local authorities in Greece in 2011 (dataset)
Changelog
Version: 1.8.2 (6 July 2026)
Major update returning SpatialML to CRAN after its archival in November 2025.
Highlights:
Dependencies: caret and randomForest removed; ranger is now the single random-forest back-end. Requires R ≥ 4.1.
grf: More robust local fitting — a leave-one-out fallback for non-finite out-of-bag predictions (new oob.fallback argument), correct handling of duplicate coordinates and zero-distance kernels, lower memory use, and stricter input validation. The function now returns an object of class "grf", so predict() dispatches directly. Local model summaries report AIC and AICc.
grf.bw: Bandwidth-search diagnostics reimplemented in base R, safer mtry defaults, a verbose option, and an informative warning (instead of an empty result) when no bandwidth yields a finite local R-squared.
rf.mtry.optim: Redesigned. New arguments include cv.method (“oob”, “cv”, “repeatedcv”), num.trees, cv.repeats and num.threads; it now returns a plain list with best.mtry and the evaluation results rather than a caret object.
random.test.data: Bug fixes — the normal option now draws the dependent variable from a normal distribution, and predictor generation has been corrected.
Documentation and testing: New vignette with a complete workflow (mtry tuning, bandwidth search, GRF fit, local-importance plotting, prediction at new locations) and a full test suite. Reproducibility is now under user control via set.seed().
Version: 0.1.7 (2 April 2024)
Code: Maintenance release; updated package dependencies (R ≥ 4.3.0, ranger, caret, randomForest) to comply with CRAN policies.
Version: 0.1.6 (8 November 2023)
grf: a bug has been fixed.
Version: 0.1.5 (2 September 2022)
grf: The function now supports Geographically Weighted Random Forest regression.
grf.bw: Geographically Weighted Random Forest optimal bandwidth selection
grf.mtry.optim: This function calculates the optimal mtry for a given Random Forest (RF) model in a specified range of values. The optimal mtry value can then be used in the grf model.
Version: 0.1.3 (9 May 2019)
grf: This function refers to a geographical (local) version of the popular Random Forest algorithm
predict.grf: Predict Method for Geographical Random Forest
random.test.data: Random data generator
Datasets
Income: Mean household income at local authorities in Greece in 2011
Download SpatialML
SpatialML is available at the Comprehensive R Archive Network (CRAN): https://CRAN.R-project.org/package=SpatialML. A reference manual for SpatialML is available here…
References
Georganos S, Grippa T, Niang Gadiaga A, Linard C, Lennert M, Vanhuysse S, Mboga N, Wolff E, Kalogirou S. 2021. Geographical random forests: a spatial extension of the random forest algorithm to address spatial heterogeneity in remote sensing and population modelling. Geocarto Int. 36(2):121–136. doi: 10.1080/10106049.2019.1595177.
Georganos S, Kalogirou S. 2019. Forest of Forests: A Spatially Weighted and Computationally Efficient Formulation of Geographical Random Forests. ISPRS International Journal of Geo-Information. 11(9):471. https://doi.org/10.3390/ijgi11090471)