Giter Club home page Giter Club logo

spatialsample's Introduction

spatialsample

Lifecycle: experimental R-CMD-check Codecov test coverage

Introduction

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

The implementation of more spatial resampling approaches is planned.

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")  ## not yet released

And the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("tidymodels/spatialsample")

Example

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

library(spatialsample)
data("ames", package = "modeldata")

set.seed(1234)
folds <- spatial_clustering_cv(ames, v = 5, coords = c("Latitude", "Longitude"))

folds
#> # A tibble: 5 x 2
#>   splits             id   
#>   <list>             <chr>
#> 1 <split [2.3K/598]> Fold1
#> 2 <split [2.2K/743]> Fold2
#> 3 <split [2.6K/360]> Fold3
#> 4 <split [2.1K/812]> Fold4
#> 5 <split [2.5K/417]> Fold5

In this example, the ames data on houses in Ames, IA is resampled with v = 5; notice that the resulting partitions do not contain an equal number of observations.

We can create a helper plotting function to visualize the five folds.

library(ggplot2)
library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

plot_splits <- function(split) {
    p <- analysis(split) %>%
        mutate(analysis = "Analysis") %>%
        bind_rows(assessment(split) %>%
                      mutate(analysis = "Assessment")) %>%
        ggplot(aes(Longitude, Latitude, color = analysis)) + 
        geom_point(alpha = 0.5) +
        labs(color = NULL)
    print(p)
}

walk(folds$splits, plot_splits)

Contributing

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

spatialsample's People

Contributors

juliasilge avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.