tidymodels / tidymodels/rsample
Nested cross-validation with spatial block CV and cluster CV
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 343
- Forks
- 67
- Avg merge
- 1h 9m
- Merged PRs (30d)
- 2
Description
I'm setting up a nested cross-validation using spatial block cross-validation for outer folds and cluster cross-validation for inner folds. Interaction between these two types of cross-validations gives me the following error:
library(spatialsample)
library(rsample)
folds <- spatialsample::spatial_block_cv(boston_canopy, v = 3)
rsample::nested_cv(boston_canopy, outside = folds, inside = rsample::clustering_cv(v = 3, repeats = 1, vars = c('mean_temp','mean_heat_index_morning')))
#> Error in `map()`:
#> ℹ In index: 1.
#> Caused by error in `dist()`:
#> ! 'list' object cannot be coerced to type 'double'
The behavior that I'm trying to achieve is shown here:
library(spatialsample)
library(rsample)
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
folds <- spatialsample::spatial_block_cv(boston_canopy, v = 3)
tmp<-list()
for(j in 1:nrow(folds)){
x<-rsample::analysis(folds$splits[[j]]) %>% sf::st_drop_geometry()
tmp[[j]] <- rsample::clustering_cv(data=x, v = 3, repeats = 1, vars = c('mean_temp','mean_heat_index_morning'))
}
folds$inner_resamples <- tmp
folds
#> # 3-fold spatial block cross-validation
#> # A tibble: 3 × 3
#> splits id inner_resamples
#> <list> <chr> <list>
#> 1 <split [458/224]> Fold1 <clt_fold [3 × 2]>
#> 2 <split [469/213]> Fold2 <clt_fold [3 × 2]>
#> 3 <split [437/245]> Fold3 <clt_fold [3 × 2]>
This example uses data from the spatialsample package. I've opened the issue in rsample because the error message seems to be related to how rsample::nested_cv interacts with the output of spatial_block_cv.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the example with spatialsample::spatial_block_cv, rsample::nested_cv, and rsample::clustering_cv using boston_canopy. Compare the failing nested call with the manually constructed folds$inner_resamples result, focusing on how nested_cv handles the spatial resampling output. Done means nested_cv supports this combination without the dist() coercion error and preserves the expected inner_resamples structure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100