An open API service indexing awesome lists of open source software.

https://github.com/ropensci/waywiser

Ergonomic tooling to assess models of spatial data
https://github.com/ropensci/waywiser

cran r r-package spatial spatial-analysis tidymodels tidyverse

Last synced: 8 months ago
JSON representation

Ergonomic tooling to assess models of spatial data

Awesome Lists containing this project

README

          

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
library(ggplot2)
theme_set(theme_minimal())
```

# waywiser

[![arXiv preprint](https://img.shields.io/badge/arXiv-2303.11312-brightgreen)](https://arxiv.org/abs/2303.11312)
[![R-CMD-check](https://github.com/ropensci/waywiser/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/ropensci/waywiser/actions/workflows/R-CMD-check.yaml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green)](https://choosealicense.com/licenses/mit/)
[![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-blue.svg)](https://lifecycle.r-lib.org/articles/stages.html#maturing)
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![Codecov test coverage](https://codecov.io/gh/ropensci/waywiser/branch/main/graph/badge.svg)](https://app.codecov.io/gh/ropensci/waywiser?branch=main)
[![CRAN status](https://www.r-pkg.org/badges/version/waywiser)](https://CRAN.R-project.org/package=waywiser)
[![Status at rOpenSci Software Peer Review](https://badges.ropensci.org/571_status.svg)](https://github.com/ropensci/software-review/issues/571)

"Waywiser" is an old-timey name for a
[surveyor's wheel](https://en.wikipedia.org/wiki/Surveyor%27s_wheel), a device
that makes measuring long distances easier than with measurement tools like a
ruler or yardstick. The waywiser R package makes it easier to measure the
performance of models fit to 2D spatial data by implementing a number of
well-established assessment methods in a consistent, ergonomic toolbox; features
include new [yardstick](https://yardstick.tidymodels.org/) metrics
for measuring agreement and spatial autocorrelation, functions to assess model
predictions across multiple scales, and methods to calculate the area of
applicability of a model.

## Installation

You can install waywiser from CRAN via:

```r
install.packages("waywiser")
```

You can install the development version of waywiser from [GitHub](https://github.com/) with:

``` r
# install.packages("remotes")
remotes::install_github("ropensci/waywiser")

# or, equivalently:
install.packages("waywiser", repos = "https://ropensci.r-universe.dev")
```

## Example

Let's say that we fit a linear model predicting crimes against people as a
function of literacy, using the `guerry` data included in waywiser:

```{r}
library(waywiser)

set.seed(123)
split_idx <- sample(seq_len(nrow(guerry)), nrow(guerry) * 0.8)
guerry_train <- guerry[split_idx, ]
guerry_test <- guerry[-split_idx, ]

crime_model <- lm(Crm_prs ~ Litercy, guerry_train)
```

We want to assess this model, to better understand how well it predicts crime
rates across 1830s France. One method to do so is to evaluate our predictions at
multiple levels of aggregation, as suggested by Riemann et al. (2010)
(). This approach is focused on
aggregating point predictions, so we'll convert our data to points and then see
how well our predictions perform when aggregated to two different scales:

```{r}
guerry_points <- data.frame(
truth = guerry$Crm_prs,
estimate = predict(crime_model, guerry),
geometry = sf::st_centroid(sf::st_geometry(guerry))
)
guerry_points <- sf::st_as_sf(guerry_points)

guerry_multi_scale <- ww_multi_scale(
guerry_points,
truth,
estimate,
n = list(c(5, 5), c(2, 2))
)

guerry_multi_scale
```

More information about multi-scale assessment is included in `vignette("multi-scale-assessment", package = "waywiser")`.

We could also assess the spatial dependence of our model residuals, to identify
any potential "hot spots" where our model is consistently less accurate than
we'd expect by chance:

```{r}
guerry_predicted <- guerry
guerry_predicted$predictions <- predict(crime_model, guerry)

ww_local_moran_i(guerry_predicted, Crm_prs, predictions)
```

More information about multi-scale assessment is included in `vignette("residual-autocorrelation", package = "waywiser")`.

Lastly, we can also see if there's any areas in our data that are too different
from our training data for us to safely predict on, which fall outside the
"area of applicability" defined by Meyer and Pebesma (2021)
():

```{r}
crime_model_aoa <- ww_area_of_applicability(
Crm_prs ~ Litercy,
guerry_train,
guerry_test,
importance = vip::vi_model(crime_model)
)

guerry_aoa <- cbind(
guerry,
predict(crime_model_aoa, guerry)
)

plot(guerry_aoa["aoa"])
```

We can see that two areas are outside our model's area of applicability, meaning
that we probably can't trust our model when extrapolating into those regions!

For more information, check out [the documentation website!](https://docs.ropensci.org/waywiser/)

## Citing waywiser

To cite waywiser in publications please use:

Mahoney M. J. (2023). waywiser: Ergonomic Methods for Assessing Spatial Models. arXiv:2303.11312 [cs.MS]. https://doi.org/10.48550/arXiv.2303.11312

A BibTeX entry for LaTeX users is

```bibtex
@Misc{,
title = {waywiser: Ergonomic Methods for Assessing Spatial Models},
author = {Michael J Mahoney},
year = {2023},
eprint = {2303.11312},
archiveprefix = {arXiv},
primaryclass = {cs.MS},
doi = {10.48550/arXiv.2303.11312},
url = {https://arxiv.org/abs/2303.11312},
}
```

See `citation("waywiser")` for the most up-to-date citation information.

## Contributing

Please note that this package is released with a
[Contributor Code of Conduct](https://ropensci.org/code-of-conduct/).
By contributing to this project, you agree to abide by its terms.

- If you think you have encountered a bug, please [submit an issue](https://github.com/ropensci/waywiser).

- Please include a [reprex](https://reprex.tidyverse.org/articles/articles/learn-reprex.html) (a minimal, reproducible example) to clearly communicate about your code.

[![ropensci_footer](https://ropensci.org/public_images/github_footer.png)](https://ropensci.org)