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.
You can install the development version of sfdep like so:
There are three main categories of functionality relating to geometry neighbors, weights, and local indicators of spatial association (LISAs).
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()
.
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_kernel_weights()
and st_inverse_weights()
.
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.
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> -1.351653e-02, -8.570710e-03, -1.380643e-02, -1.310390e-0…
#> $ var_ii <dbl> 0.3601895318, 0.1183545193, 0.1488396612, 0.2125733254, 0…
#> $ z_ii <dbl> 0.8927334, 2.4317493, 2.1185895, 1.6375285, 1.0419403, 1.…
#> $ p_ii <dbl> 0.371999935, 0.015026103, 0.034125174, 0.101520093, 0.297…
#> $ p_ii_sim <dbl> 0.360, 0.012, 0.044, 0.116, 0.300, 0.128, 0.612, 0.108, 0…
#> $ p_folded_sim <dbl> 0.180, 0.006, 0.022, 0.058, 0.150, 0.064, 0.306, 0.054, 0…
#> $ skewness <dbl> -0.019288715, 0.140263529, -0.131077971, 0.101128975, -0.…
#> $ kurtosis <dbl> -0.08426765, 0.12078476, 0.07679298, -0.18648472, -0.3583…
#> $ 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.