You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
sfdep builds on the great shoulders of spdep package for spatialdependence. sfdep creates an sf and tidyverse friendly interface to thepackage as well as introduces new functionality that is not present inspdep. sfdep utilizes list columns extensively to make this interfacepossible.
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 geometryneighbors, weights, and local indicators of spatial association (LISAs).
Neighbors
The most fundamental usage is to find contiguous neighbors from apolygon. This is done withst_contiguity() which, by default createsqueen weights. If rook weights are desired, setqueen = FALSE.Additional arguments can be passed to the underlyingspdep::poly2nb()via....st_contiguity() creates an object of classnb as used byspdep.
library(sf)library(sfdep)library(dplyr)# grab geometrygeo<- 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 withst_nb_lag() and thecumulative higher order neighbors withst_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.894118st_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 arest_knn(),st_dist_band(),st_nb_dists().
Weights
Polygon weights are created withst_weights() (which callsspdep::nb2listw). By default they are row standardized weights.
Other point based weights can be created withst_nb_dists(),st_kernel_weights() andst_inverse_weights().
Local Indicators of Spatial Association (LISAs)
LISAs are created from a combination of neighbors and weights and areintended to be used inside of a dplyr pipeline. The below is a workedexample of calculating the spatial lag and the local moran.
Most users will be interested in local indicators of spatial association(LISA). Utilizelocal_moran() to do this.local_moran() will createa 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 certainvariables hoisted.