Learn R Programming

spatialsample

Introduction

The goal of spatialsample is to provide functions and classes for spatial resampling to use with rsample, including:

Like rsample, spatialsample provides building blocks for creating and analyzing resamples of a spatial data set but does not include code for modeling or computing statistics. The resampled data sets created by spatialsample are efficient and do not have much memory overhead.

Installation

You can install the released version of spatialsample from CRAN with:

install.packages("spatialsample")

And the development version from GitHub with:

# install.packages("pak")
pak::pak("tidymodels/spatialsample")

Example

The most straightforward spatial resampling strategy is spatial_clustering_cv(), which uses k-means clustering to identify cross-validation folds:

library(spatialsample)

set.seed(1234)
folds <- spatial_clustering_cv(boston_canopy, v = 5)

folds
#> #  5-fold spatial cross-validation 
#> # A tibble: 5 × 2
#>   splits            id   
#>   <list>            <chr>
#> 1 <split [604/78]>  Fold1
#> 2 <split [595/87]>  Fold2
#> 3 <split [524/158]> Fold3
#> 4 <split [490/192]> Fold4
#> 5 <split [515/167]> Fold5

In this example, the boston_canopy data on tree cover in Boston, MA is resampled with v = 5; notice that the resulting partitions do not contain an equal number of observations.

In addition to resampling algorithms, spatialsample provides methods to visualize resamples using ggplot2 through the autoplot() function:

autoplot(folds)

We can use the same function to visualize each fold separately:

library(purrr)

walk(folds$splits, function(x) print(autoplot(x)))

So far, we’ve only scratched the surface of the functionality spatialsample provides. For more information, check out the Getting Started documentation!

Contributing

This project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('spatialsample')

Monthly Downloads

737

Version

0.6.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Michael Mahoney

Last Published

October 2nd, 2024

Functions in spatialsample (0.6.0)

spatial_buffer_vfold_cv

V-Fold Cross-Validation with Buffering
reexports

Objects exported from other packages
spatialsample-package

spatialsample: Spatial Resampling Infrastructure
spatial_clustering_cv

Spatial Clustering Cross-Validation
spatial_block_cv

Spatial block cross-validation
boston_canopy

Boston tree canopy and heat index data.
check_v

Check that "v" is sensible
spatial_nndm_cv

Nearest neighbor distance matching (NNDM) cross-validation
buffer_indices

Apply an inclusion radius and exclusion buffer to indices
autoplot.spatial_rset

Create a ggplot for spatial resamples.