tidyrgeoda
provides
following methods for spatial weights by invoking
rgeoda
:
library(sf)
library(dplyr)
library(tidyrgeoda)
guerry = read_sf(system.file("extdata","Guerry.shp",package = "rgeoda"))
head(guerry)
## Simple feature collection with 6 features and 29 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 595532 ymin: 1858801 xmax: 975716 ymax: 2564568
## Projected CRS: NTF (Paris) / Lambert zone II
## # A tibble: 6 × 30
## CODE_DE COUNT AVE_ID_ dept Region Dprtmnt Crm_prs Crm_prp Litercy Donatns
## <chr> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 01 1 49 1 E Ain 28870 15890 37 5098
## 2 02 1 812 2 N Aisne 26226 5521 51 8901
## 3 03 1 1418 3 C Allier 26747 7925 13 10973
## 4 04 1 1603 4 E Basses-Alp… 12935 7289 46 2733
## 5 05 1 1802 5 E Hautes-Alp… 17488 8174 69 6962
## 6 07 1 2249 7 S Ardeche 9474 10263 27 3188
## # ℹ 20 more variables: Infants <dbl>, Suicids <dbl>, MainCty <dbl>,
## # Wealth <dbl>, Commerc <dbl>, Clergy <dbl>, Crm_prn <dbl>, Infntcd <dbl>,
## # Dntn_cl <dbl>, Lottery <dbl>, Desertn <dbl>, Instrct <dbl>, Prsttts <dbl>,
## # Distanc <dbl>, Area <dbl>, Pop1831 <dbl>, TopCrm <dbl>, TopLit <dbl>,
## # TopWealth <dbl>, geometry <MULTIPOLYGON [m]>
Contiguity means that two spatial units share a common border of non-zero length. Operationally, we can further distinguish between a rook and a queen criterion of contiguity, in analogy to the moves allowed for the such-named pieces on a chess board. The queen criterion is somewhat more encompassing and defines neighbors as spatial units sharing a common edge or a common vertex.The rook criterion defines neighbors by the existence of a common edge between two spatial units.
To create a Queen contiguity weights, one can call the function
## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " TRUE
## 3 "sparsity:" 0.0581314878892734
## 4 "min neighbors:" 2
## 5 "max neighbors:" 8
## 6 "mean neighbors:" 4.94117647058824
## 7 "median neighbors:" 5
## 8 "has isolates:" FALSE
If queen
is assigned to
FALSE
,tidyrgeoda
will invoke
rgeoda
to create a Rook contiguity weights.
## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " TRUE
## 3 "sparsity:" 0.0581314878892734
## 4 "min neighbors:" 2
## 5 "max neighbors:" 8
## 6 "mean neighbors:" 4.94117647058824
## 7 "median neighbors:" 5
## 8 "has isolates:" FALSE
You can also create high order of contiguity weights by changing
order
to more than 1.
## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " TRUE
## 3 "sparsity:" 0.104636678200692
## 4 "min neighbors:" 2
## 5 "max neighbors:" 14
## 6 "mean neighbors:" 8.89411764705882
## 7 "median neighbors:" 9
## 8 "has isolates:" FALSE
Create contiguity weights with 1-order and 2-order together:
## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " TRUE
## 3 "sparsity:" 0.162768166089965
## 4 "min neighbors:" 4
## 5 "max neighbors:" 21
## 6 "mean neighbors:" 13.8352941176471
## 7 "median neighbors:" 14
## 8 "has isolates:" FALSE
The most straightforward spatial weights matrix constructed from a
distance measure is obtained when i and j are considered neighbors
whenever j falls within a critical distance band from i.You can use
st_distance_weights()
to achieve the Distance Based
Weights.
## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " TRUE
## 3 "sparsity:" 0.0434602076124567
## 4 "min neighbors:" 1
## 5 "max neighbors:" 7
## 6 "mean neighbors:" 3.69411764705882
## 7 "median neighbors:" 4
## 8 "has isolates:" FALSE
The distance threshold default is generate use
rgeoda::min_distthreshold()
,but you can assign
dist_thres
argument by hand.
## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " TRUE
## 3 "sparsity:" 0.0968858131487889
## 4 "min neighbors:" 2
## 5 "max neighbors:" 13
## 6 "mean neighbors:" 8.23529411764706
## 7 "median neighbors:" 9
## 8 "has isolates:" FALSE
A special case of distance based weights is K-Nearest neighbor
weights, in which every observation will have exactly k neighbors. It
can be used to avoid the problem of isolate in distance-band weights
when a smaller cut-off distance is used. To create a KNN weights, we can
call the function st_knn_weights()
:
## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " FALSE
## 3 "sparsity:" 0.0705882352941176
## 4 "min neighbors:" 6
## 5 "max neighbors:" 6
## 6 "mean neighbors:" 6
## 7 "median neighbors:" 6
## 8 "has isolates:" FALSE
Kernel weights apply kernel function to determine the distance decay in the derived continuous weights kernel. The kernel weights are defined as a function Kz of the ratio between the distance dij from i to j, and the bandwidth hi, with $z = \frac{d_{ij}}{h_i}$.
The kernel functions include
triangular
uniform
quadratic
epanechnikov
quartic
gaussian
Two functions are provided in tidyrgeoda
to create
kernel weights.
st_kernel_weights()
for Kernel Weights with
afixedbandwidth## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " FALSE
## 3 "sparsity:" 0.0434602076124567
## 4 "min neighbors:" 1
## 5 "max neighbors:" 7
## 6 "mean neighbors:" 3.69411764705882
## 7 "median neighbors:" 4
## 8 "has isolates:" FALSE
st_kernel_knn_weights()
for Kernel Weights with
adaptive bandwidth## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " FALSE
## 3 "sparsity:" 0.0705882352941176
## 4 "min neighbors:" 6
## 5 "max neighbors:" 6
## 6 "mean neighbors:" 6
## 7 "median neighbors:" 6
## 8 "has isolates:" FALSE
st_weights()
st_weights()
is a wrapper function for all above
st_*_weights
,you can use it like:
## Reference class object of class "Weight"
## Field "gda_w":
## An object of class "p_GeoDaWeight"
## Slot "pointer":
## <pointer: 0x556d91a14f90>
##
## Field "is_symmetric":
## [1] TRUE
## Field "sparsity":
## [1] 0.05813149
## Field "min_neighbors":
## [1] 2
## Field "max_neighbors":
## [1] 8
## Field "num_obs":
## [1] 85
## Field "mean_neighbors":
## [1] 4.941176
## Field "median_neighbors":
## [1] 5
## Field "has_isolates":
## [1] FALSE
## # A tibble: 8 × 2
## name value
## <chr> <chr>
## 1 "number of observations:" 85
## 2 "is symmetric: " TRUE
## 3 "sparsity:" 0.0581314878892734
## 4 "min neighbors:" 2
## 5 "max neighbors:" 8
## 6 "mean neighbors:" 4.94117647058824
## 7 "median neighbors:" 5
## 8 "has isolates:" FALSE