-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.Rmd
151 lines (104 loc) · 5.15 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# sfdep
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/sfdep)](https://CRAN.R-project.org/package=sfdep)
[![R-CMD-check](https://github.com/JosiahParry/sfdep/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/JosiahParry/sfdep/actions/workflows/R-CMD-check.yaml)
[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)
<!-- badges: end -->
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
```r
install.packages("sfdep")
```
You can install the development version of sfdep like so:
``` r
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`.
```{r message=FALSE}
library(sf)
library(sfdep)
library(dplyr)
# grab geometry
geo <- st_geometry(guerry)
nb <- st_contiguity(geo)
nb
```
We can identify higher order neighbors with `st_nb_lag()` and the cumulative higher order neighbors with `st_nb_lag_cumul()`.
```{r}
st_nb_lag(nb, 2)
st_nb_lag_cumul(nb, 2)
```
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.
```{r}
wt <- st_weights(nb)
wt[1:2]
```
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.
```{r, message = FALSE, warn = FALSE}
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.
```{r}
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.
```{r}
lisa <- g %>%
mutate(moran = local_moran(crime_pers, nb, wt))
pull(lisa, moran) %>%
glimpse()
```
Visualize this by converting insignificant values to NA. This uses a cutoff of 0.1.
```{r}
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:
* [Emerging Hot Spot Analysis](https://sfdep.josiahparry.com/articles/understanding-emerging-hotspots.html#using-emerging_hotspot_analysis)
* [Spatio-Temporal Data](https://sfdep.josiahparry.com/articles/spacetime-s3.html)
* [`{sfnetworks}` integration](https://sfdep.josiahparry.com/reference/index.html#networks)
<!-- ## Neighbor apply function -->
<!-- In the case that you want to create custom local level metrics, you can do so with the function `st_nb_apply()`. This provides a purrr like interface to do calculation on an observation $x_{i}$ using it's neighbors, $x_{ij}$, and their respective weights $w_{ij}$. In the formula syntax utilize `.xij`, `.nb` and `.wt` for these values respectively. We can specify the type of output with the suffix argument. By default this will be `"dbl"`. These can be any purrr sufix. To get the default list output use `"list"`. -->
<!-- For example -->
<!-- ```{r} -->
<!-- g %>% -->
<!-- transmute(x = st_nb_apply(crime_pers, nb, wt, -->
<!-- .f = function(.xij, .wt, ...) sum((.xij - crime_pers)^2) / (length(.wt) + 1), suffix = "dbl")) -->
<!-- ``` -->