Skip to content

Fix environmental folds when points lack predictor values - #27

Open
Abson-dev wants to merge 1 commit into
masterfrom
fix/cluster-folds-missing-values
Open

Abson-dev wants to merge 1 commit into
masterfrom
fix/cluster-folds-missing-values

Conversation

@Abson-dev

Copy link
Copy Markdown
Owner

Bug

Building environmental cross-validation folds (Spatial Analysis > Cross-validation folds > Environmental clusters) failed when any occurrence point fell on a cell without predictor values, for example on a coastline or at the edge of the rasters:

Error in blockCV::cv_cluster(...): The input raster layer does not cover all the column points.

Such points are common with GBIF data at continental scale. I found the bug while modelling Faidherbia albida across Africa for the sdmApp manuscript's case study. Spatial blocks weren't affected, and sdmApp_fit() already silently drops points without values, so only environmental folds were broken.

Fix

A new exported function, sdmApp_cluster_folds(occ, env, column, k = 5, seed = 1994):

  • It runs blockCV::cv_cluster() on the points that have predictor values only.
  • It maps the result back to all rows of occ. folds_ids has one entry per point, NA for points without values, which sdmApp_fit() and sdmApp_tune() ignore. folds_list refers to rows of occ, so cv_plot(), the fold explorer and fold summaries keep working.
  • It seeds the clustering with set.seed(), because cv_cluster() doesn't use its seed argument for point data (see Cross-validate on environmental clusters #18). It returns the number of points left out in $dropped.

Callers:

  • App: the folds page uses it. When points are left out, a notification says how many and why.
  • R-Code tab: it writes blocks <- sdmApp_cluster_folds(occ, env, "...", k = ...) instead of set.seed() plus blockCV::cv_cluster().
  • Workflow vignette: it uses the new function.

With complete data the folds are identical to before, so existing results and scripts don't change.

Testing

  • New test-sdmApp_cluster_folds.R:
    • with complete data, folds_ids and folds_list are identical to set.seed(1994); blockCV::cv_cluster(...), and reproducible;
    • with a presence placed outside the rasters, there is no error. The point gets NA, dropped is 1, and folds_list refers to rows of the occurrences without that point, consistent with folds_ids. A GLM fits on the folds (3 folds evaluated) and the fold explorer draws them;
    • input checks.
  • The end-to-end script test (environmental folds in the app, then the generated script) now expects sdmApp_cluster_folds(). It still reproduces the same folds_ids and fold metrics.
  • Workflow vignette knit: no errors, and the same results as before (AUC with random / spatial / environmental folds: 0.575 / 0.552 / 0.501).
  • devtools::test(): 109 tests, 552 expectations, 0 failed, 0 skipped. lintr: no lints. spelling: no errors.

🤖 Generated with Claude Code

blockCV::cv_cluster() fails ("The input raster layer does not cover all
the column points") when an occurrence point falls on a cell without
predictor values, for example on a coast. New sdmApp_cluster_folds()
clusters the points that have values and gives the others no fold
(NA), which models ignore; folds_list still refers to rows of the
occurrences. It also seeds the clustering, as cv_cluster() ignores its
seed for point data. The interface, the R-Code tab and the workflow
vignette use it; with complete data the folds are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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