Skip to content

Measure the spatial sorting bias of cross-validation folds - #25

Merged
Abson-dev merged 1 commit into
masterfrom
feat/sorting-bias
Dec 1, 2026
Merged

Abson-dev merged 1 commit into
masterfrom
feat/sorting-bias

Conversation

@Abson-dev

@Abson-dev Abson-dev commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

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:

  • close to 1: the evaluation isn't biased by distance;
  • well below 1: test presences are much closer to the training data than test absences, and the AUC is inflated. Spatial blocks or environmental folds reduce this.

Fits now keep the coordinates of their points (cv$coords, with cv$lonlat), next to their folds. Fits made with an older version give a clear "refit" error.

App and documentation

  • Each model's per-fold evaluation table gains a "Sorting bias" column, with a short explanation of how to read it.
  • The workflow vignette compares the sorting bias of random folds and spatial blocks, and cites Hijmans (2012).

Validation

  • Agrees with 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.
  • On 600 Niakhar points with 4 folds, SSB is 0.71–0.92 with random folds and 0.85–1.05 with spatial blocks, and the test points lie about three times farther from the training data in spatial blocks. The bias is modest here because the Niakhar survey samples the area evenly.
  • With tightly clustered presences and spread-out absences (a synthetic case), random folds give SSB below 0.1, as expected.

Testing

  • New test-sdmApp_ssb.R:
    • the structure of the result;
    • ratio = presence distance / absence distance;
    • agreement with dismo::ssb() within 1%;
    • the clustered-presence case;
    • input checks.
  • 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

- 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>
@Abson-dev
Abson-dev merged commit b83ea6a into master Dec 1, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant