Repository navigation
Measure the spatial sorting bias of cross-validation folds - #25
Merged
Merged
Conversation
- New sdmApp_ssb(): for each fold, the mean distance from test presences to the nearest training presence divided by that of test absences (Hijmans 2012); well below 1, the AUC is inflated by distance alone. It agrees with dismo::ssb(). - Fits keep the coordinates of their points (cv$coords). - The per-fold evaluation table of the interface shows the sorting bias; the workflow vignette explains it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The workflow vignette showed that random folds give the most optimistic AUC. Hijmans (2012) identified a main reason: spatial sorting bias. When test presences lie closer to training presences than test absences do, a model can score well just by telling places near known presences from places far from them, without learning anything about the environment. This PR measures that bias, so users can see when an AUC is inflated.
sdmApp_ssb(fit)For each fold, it computes the distance from every test presence, and from every test absence, to the nearest training presence. It returns their means and their ratio
ssb, using great-circle distances for geographic coordinates and Euclidean distances otherwise:Fits now keep the coordinates of their points (
cv$coords, withcv$lonlat), next to their folds. Fits made with an older version give a clear "refit" error.App and documentation
Validation
dismo::ssb(), Hijmans' own implementation: ratio 0.70819 vs 0.70823. The individual distances differ by about 0.3% because terra measures on the WGS84 ellipsoid while dismo uses a sphere.Testing
test-sdmApp_ssb.R:dismo::ssb()within 1%;lintr::lint_package(): no lints.spelling: no errors.devtools::test(): 105 tests, 526 expectations, 0 failed, 0 skipped.knitr::knit()of the workflow vignette: no errors. Its new example shows a sorting bias of 0.71–1.12 with random folds and 0.91–0.94 with spatial blocks.Reference
Hijmans, R. J. (2012). Cross-validation of species distribution models: removing spatial sorting bias and calibration with a null model. Ecology 93: 679–688. doi:10.1890/11-0826.1
🤖 Generated with Claude Code