• Stars
    star
    114
  • Rank 297,454 (Top 7 %)
  • Language
    R
  • License
    GNU General Publi...
  • Created over 2 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tidy interface for spatial dependence

sfdep

CRAN status R-CMD-check Lifecycle: stable

sfdep builds on the great shoulders of spdep package for spatial dependence. sfdep creates an sf and tidyverse friendly interface to the package as well as introduces new functionality that is not present in spdep. sfdep utilizes list columns extensively to make this interface possible.

Installation

Install the released version from CRAN

install.packages("sfdep")

You can install the development version of sfdep like so:

remotes::install_github("josiahparry/sfdep")

Usage

There are three main categories of functionality relating to geometry neighbors, weights, and local indicators of spatial association (LISAs).

Neighbors

The most fundamental usage is to find contiguous neighbors from a polygon. This is done with st_contiguity() which, by default creates queen weights. If rook weights are desired, set queen = FALSE. Additional arguments can be passed to the underlying spdep::poly2nb() via .... st_contiguity() creates an object of class nb as used by spdep.

library(sf)
library(sfdep)
library(dplyr)

# grab geometry
geo <- st_geometry(guerry)

nb <- st_contiguity(geo)

nb
#> Neighbour list object:
#> Number of regions: 85 
#> Number of nonzero links: 420 
#> Percentage nonzero weights: 5.813149 
#> Average number of links: 4.941176

We can identify higher order neighbors with st_nb_lag() and the cumulative higher order neighbors with st_nb_lag_cumul().

st_nb_lag(nb, 2)
#> Neighbour list object:
#> Number of regions: 85 
#> Number of nonzero links: 756 
#> Percentage nonzero weights: 10.46367 
#> Average number of links: 8.894118
st_nb_lag_cumul(nb, 2)
#> Neighbour list object:
#> Number of regions: 85 
#> Number of nonzero links: 1176 
#> Percentage nonzero weights: 16.27682 
#> Average number of links: 13.83529

Other point geometry neighbor functions are st_knn(), st_dist_band(), st_nb_dists().

Weights

Polygon weights are created with st_weights() (which calls spdep::nb2listw). By default they are row standardized weights.

wt <- st_weights(nb)

wt[1:2]
#> [[1]]
#> [1] 0.25 0.25 0.25 0.25
#> 
#> [[2]]
#> [1] 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667

Other point based weights can be created with st_nb_dists(), st_kernel_weights() and st_inverse_weights().

Local Indicators of Spatial Association (LISAs)

LISAs are created from a combination of neighbors and weights and are intended to be used inside of a dplyr pipeline. The below is a worked example of calculating the spatial lag and the local moran.

g <- guerry %>% 
  mutate(
    nb = st_contiguity(geometry),
    wt = st_weights(nb)
    )

Then calculate the spatial lag with st_lag(). Given that we’ve only modified an sf object, we can visualize this with ggplot2.

library(ggplot2)

# create spatial lag
g %>% 
  mutate(crime_pers_lag = st_lag(crime_pers, nb, wt)) %>% 
  ggplot(aes(fill = crime_pers_lag)) + 
  geom_sf(lwd = 0.2, color = "black") +
  theme_void()

Most users will be interested in local indicators of spatial association (LISA). Utilize local_moran() to do this. local_moran() will create a data frame column which contains a number of informative variables. For example the cluster that a polygon falls into based on mean, median, or pysal calculations. This will need to be unnested or certain variables hoisted.

Create the Local Moran data frame column.

lisa <- g %>% 
  mutate(moran = local_moran(crime_pers, nb, wt))

pull(lisa, moran) %>% 
  glimpse()
#> Rows: 85
#> Columns: 12
#> $ ii           <dbl> 0.52226452, 0.82801651, 0.80353997, 0.74188966, 0.2311871…
#> $ eii          <dbl> -0.0436664933, 0.0169987175, -0.0106696690, -0.0015410148…
#> $ var_ii       <dbl> 0.3648295427, 0.1244317578, 0.1409560743, 0.2311181704, 0…
#> $ z_ii         <dbl> 0.9369545, 2.2991365, 2.1686743, 1.5464057, 1.1463544, 1.…
#> $ p_ii         <dbl> 0.348781971, 0.021497187, 0.030107416, 0.122006629, 0.251…
#> $ p_ii_sim     <dbl> 0.376, 0.016, 0.036, 0.092, 0.284, 0.124, 0.560, 0.108, 0…
#> $ p_folded_sim <dbl> 0.188, 0.008, 0.018, 0.046, 0.142, 0.062, 0.280, 0.054, 0…
#> $ skewness     <dbl> 0.186247324, -0.166050386, -0.065842084, -0.148874532, 0.…
#> $ kurtosis     <dbl> -0.256988635, -0.083615702, -0.115769407, -0.105166850, 0…
#> $ mean         <fct> High-High, High-High, High-High, Low-Low, Low-Low, Low-Lo…
#> $ median       <fct> High-High, High-High, High-High, Low-Low, Low-Low, Low-Lo…
#> $ pysal        <fct> High-High, High-High, High-High, Low-Low, Low-Low, Low-Lo…

Visualize this by converting insignificant values to NA. This uses a cutoff of 0.1.

lisa %>% 
  tidyr::unnest(moran) %>% 
  mutate(pysal = ifelse(p_folded_sim <= 0.1, as.character(pysal), NA)) |> 
  ggplot(aes(fill = pysal)) +
  geom_sf() +
  geom_sf(lwd = 0.2, color = "black") +
  theme_void() +
  scale_fill_manual(values = c("#1C4769", "#24975E", "#EACA97", "#B20016"))

Other topics:

More Repositories

1

genius

Easily access song lyrics from Genius in a tibble.
HTML
121
star
2

valve

Redirects your plumbing for you.
R
116
star
3

sfweight

Calculate spatial weights and lags with {sf} objects and the tidyverse.
R
59
star
4

rsgeo

R bindings to the geo Rust crate
Rust
43
star
5

cpcinema

Create color palettes and extract them from @colorpallete.cinema instagram photos.
R
28
star
6

sdf

A back-end agnostic spatial data frame inspired by rust trait implementations
R
26
star
7

intro-tidymodels

Intro to tidymodels. Productionalize w/ plumber, and deploy with RStudio Connect.
HTML
22
star
8

pyfns

Exporting python functions in R packages
R
18
star
9

arrow-extendr

Integration between arrow-rs and extendr
Rust
17
star
10

h3o

An R interface to Rust's h3o library
Rust
17
star
11

placekey

An R library to generate placekeys and interact with the placekey.io.
HTML
17
star
12

bbcharts

Access billboard charts. Not on CRAN quite yet.
R
14
star
13

extrachecks

Extra R package checks to save you from a headache. A package to accompany https://github.com/DavisVaughan/extrachecks
R
13
star
14

exploratory-spatial-data-analysis

Repository for talk on Exploratory Spatial Data Analysis in R using sfdep and the tidyverse
R
12
star
15

sfconversions

Convert {sf} geometry to Rust native primitives
Rust
11
star
16

youtube-tutorials

Code and slides used for youtube videos
JavaScript
10
star
17

trendyy

A tidy wrapper to gtrendsR
R
10
star
18

shiny-tauri

JavaScript
8
star
19

R-in-prod

Productionizing R models & code
R
8
star
20

vctrsrs

extendr vctrs
Rust
7
star
21

rsc-gsheets

Example of deploying an R Markdown using googlesheets4 to RStudio Connect.
R
7
star
22

plumber-excel

A small example of POSTing and reading an excel file with Plumber
R
6
star
23

opensecrets

An OpenSecrets API wrapper for R
R
6
star
24

renv-example

Example of `renv` prepared for the VA Dept. Environmental Quality.
R
6
star
25

sparklyr_exploration

An exploration of the new sparklyr package from @rstudio.
HTML
6
star
26

you-should-use-APIs

Jupyter Notebook
6
star
27

populartimes

A R translation of the python library populartimes. "The goal of this library is to provide an option to use Google Maps popular times data, until it is available via Google's API. For legal concerns please read [Issue #90](https://github.com/m-wrzr/populartimes/issues/90).
R
6
star
28

serde_esri

Esri JSON struct definitions and serde integration.
Rust
5
star
29

urban-informatics-toolkit

Master's capstone project
HTML
4
star
30

SLICED

Scripts used for SLICED! Season 1 episode 01. (I almost won)
R
3
star
31

manifest-to-renv

Convert a manifest.json to an renv.lock file
R
3
star
32

genius-api

Plumber API for genius, Docker image, and python code to interact with it.
Python
3
star
33

pathattr

Making R code 200-300x faster using Rust
R
2
star
34

geospatial-book

2
star
35

uitk

Associated package for the Urban Informatics Toolkit
HTML
2
star
36

russia-investigation

Transcript of James Comey's hearing from June 8th, and Jeff Sessions' from June 13th.
R
2
star
37

outReach

R
2
star
38

r-4-campaigns

Using R in progressive campaigns.
HTML
2
star
39

r-history

2
star
40

r-sysreqs

A simple api for fetching R package system dependencies
HTML
1
star
41

us_representation

Repository for US representation analysis
R
1
star
42

rspmapi

A package to interface with the RStudio Public Package Manager API.
R
1
star
43

deploying-r-models

Supplementary repository to the brief video series on deploying R Models with Plumber.
R
1
star
44

wkrsgeo

Rust
1
star
45

FEN

FEN parser in Rust
Rust
1
star
46

intro-to-r

Intro to R prepared for the Queensland Department of Treasury
R
1
star
47

stackoverflow

illustrate stack overflow problem with lists in extendr library
R
1
star
48

josiahparry.github.io

HTML
1
star
49

esri-uc-2023

R scripts for Esri UC 2023
R
1
star
50

uwu

(ꈍᴗꈍ)♡ create UUIDs and impute them into character vectors by reference.
Rust
1
star
51

optout

Simple website to help people opt-out of 23 and me updated terms and conditions.
HTML
1
star