Alright, thus far, we have only used Queen neighborhood matrices with our data. Let’s use this exercise to try out different variations. First of all, run the below in order to compile the data that were also used in the lecture.
## Reading layer `OGRGeoJSON' from data source
## `https://geoportal.stadt-koeln.de/arcgis/rest/services/Basiskarten/kgg/MapServer/20/query?where=objectid+is+not+null&text=&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&distance=&units=esriSRUnit_Foot&relationParam=&outFields=*&returnGeometry=true&returnTrueCurves=false&maxAllowableOffset=&geometryPrecision=&outSR=4326&havingClause=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&historicMoment=&returnDistinctValues=false&resultOffset=&resultRecordCount=&returnExtentOnly=false&datumTransformation=¶meterValues=&rangeValues=&quantizationParameters=&featureEncoding=esriDefault&f=geojson'
## using driver `GeoJSON'
## Simple feature collection with 543 features and 20 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 6.77253 ymin: 50.83045 xmax: 7.162028 ymax: 51.08496
## Geodetic CRS: WGS 84
## ℹ Using "','" as decimal and "'.'" as grouping mark. Use `read_delim()` for more control.
## Rows: 949 Columns: 79
## ── Column specification ───────────────────────────────────────────────────────────────────────────────
## Delimiter: ";"
## chr (3): wahl, ags, gebiet-name
## dbl (71): gebiet-nr, max-schnellmeldungen, anz-schnellmeldungen, A1, A2, A3, A, B, B1, C, D, E, F, ...
## lgl (4): D30, F30, D31, F31
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
##
|
| | 0%
|
|= | 1%
|
|== | 2%
|
|== | 3%
|
|=== | 3%
|
|=== | 4%
|
|==== | 4%
|
|==== | 5%
|
|===== | 5%
|
|===== | 6%
|
|====== | 6%
|
|====== | 7%
|
|======= | 7%
|
|======= | 8%
|
|======== | 8%
|
|======== | 9%
|
|========= | 9%
|
|========= | 10%
|
|========== | 10%
|
|========== | 11%
|
|=========== | 11%
|
|=========== | 12%
|
|============ | 13%
|
|============= | 13%
|
|============= | 14%
|
|============== | 15%
|
|=============== | 16%
|
|=============== | 17%
|
|================ | 17%
|
|================ | 18%
|
|================= | 18%
|
|================= | 19%
|
|================== | 19%
|
|================== | 20%
|
|=================== | 20%
|
|=================== | 21%
|
|==================== | 21%
|
|==================== | 22%
|
|===================== | 22%
|
|===================== | 23%
|
|====================== | 23%
|
|====================== | 24%
|
|======================= | 24%
|
|======================= | 25%
|
|======================== | 25%
|
|======================== | 26%
|
|========================= | 27%
|
|========================== | 27%
|
|========================== | 28%
|
|=========================== | 29%
|
|============================ | 30%
|
|============================ | 31%
|
|============================= | 31%
|
|============================= | 32%
|
|============================== | 32%
|
|============================== | 33%
|
|=============================== | 33%
|
|=============================== | 34%
|
|================================ | 34%
|
|================================ | 35%
|
|================================= | 35%
|
|================================= | 36%
|
|================================== | 36%
|
|================================== | 37%
|
|=================================== | 37%
|
|=================================== | 38%
|
|==================================== | 38%
|
|==================================== | 39%
|
|===================================== | 39%
|
|===================================== | 40%
|
|====================================== | 40%
|
|====================================== | 41%
|
|======================================= | 41%
|
|======================================= | 42%
|
|======================================== | 43%
|
|========================================= | 44%
|
|========================================= | 45%
|
|========================================== | 45%
|
|========================================== | 46%
|
|=========================================== | 46%
|
|=========================================== | 47%
|
|============================================ | 47%
|
|============================================ | 48%
|
|============================================= | 48%
|
|============================================= | 49%
|
|============================================== | 49%
|
|============================================== | 50%
|
|=============================================== | 50%
|
|=============================================== | 51%
|
|================================================ | 51%
|
|================================================ | 52%
|
|================================================= | 52%
|
|================================================= | 53%
|
|================================================== | 53%
|
|================================================== | 54%
|
|=================================================== | 54%
|
|=================================================== | 55%
|
|==================================================== | 55%
|
|==================================================== | 56%
|
|===================================================== | 57%
|
|====================================================== | 58%
|
|====================================================== | 59%
|
|======================================================= | 59%
|
|======================================================= | 60%
|
|======================================================== | 60%
|
|======================================================== | 61%
|
|========================================================= | 61%
|
|========================================================= | 62%
|
|========================================================== | 62%
|
|========================================================== | 63%
|
|=========================================================== | 63%
|
|=========================================================== | 64%
|
|============================================================ | 64%
|
|============================================================ | 65%
|
|============================================================= | 65%
|
|============================================================= | 66%
|
|============================================================== | 66%
|
|============================================================== | 67%
|
|=============================================================== | 67%
|
|=============================================================== | 68%
|
|================================================================ | 68%
|
|================================================================ | 69%
|
|================================================================= | 69%
|
|================================================================= | 70%
|
|================================================================== | 71%
|
|=================================================================== | 72%
|
|=================================================================== | 73%
|
|==================================================================== | 73%
|
|===================================================================== | 74%
|
|===================================================================== | 75%
|
|====================================================================== | 75%
|
|====================================================================== | 76%
|
|======================================================================= | 76%
|
|======================================================================= | 77%
|
|======================================================================== | 77%
|
|======================================================================== | 78%
|
|========================================================================= | 78%
|
|========================================================================= | 79%
|
|========================================================================== | 79%
|
|========================================================================== | 80%
|
|=========================================================================== | 80%
|
|=========================================================================== | 81%
|
|============================================================================ | 81%
|
|============================================================================ | 82%
|
|============================================================================= | 82%
|
|============================================================================= | 83%
|
|============================================================================== | 83%
|
|============================================================================== | 84%
|
|=============================================================================== | 85%
|
|================================================================================ | 86%
|
|================================================================================ | 87%
|
|================================================================================= | 87%
|
|================================================================================== | 88%
|
|================================================================================== | 89%
|
|=================================================================================== | 89%
|
|=================================================================================== | 90%
|
|==================================================================================== | 90%
|
|==================================================================================== | 91%
|
|===================================================================================== | 91%
|
|===================================================================================== | 92%
|
|====================================================================================== | 92%
|
|====================================================================================== | 93%
|
|======================================================================================= | 93%
|
|======================================================================================= | 94%
|
|======================================================================================== | 94%
|
|======================================================================================== | 95%
|
|========================================================================================= | 95%
|
|========================================================================================= | 96%
|
|========================================================================================== | 96%
|
|========================================================================================== | 97%
|
|=========================================================================================== | 97%
|
|=========================================================================================== | 98%
|
|============================================================================================ | 99%
|
|=============================================================================================| 100%
##
|
| | 0%
|
|= | 1%
|
|== | 2%
|
|== | 3%
|
|=== | 3%
|
|=== | 4%
|
|==== | 4%
|
|==== | 5%
|
|===== | 5%
|
|===== | 6%
|
|====== | 6%
|
|====== | 7%
|
|======= | 7%
|
|======= | 8%
|
|======== | 8%
|
|======== | 9%
|
|========= | 9%
|
|========= | 10%
|
|========== | 10%
|
|========== | 11%
|
|=========== | 11%
|
|=========== | 12%
|
|============ | 13%
|
|============= | 13%
|
|============= | 14%
|
|============== | 15%
|
|=============== | 16%
|
|=============== | 17%
|
|================ | 17%
|
|================ | 18%
|
|================= | 18%
|
|================= | 19%
|
|================== | 19%
|
|================== | 20%
|
|=================== | 20%
|
|=================== | 21%
|
|==================== | 21%
|
|==================== | 22%
|
|===================== | 22%
|
|===================== | 23%
|
|====================== | 23%
|
|====================== | 24%
|
|======================= | 24%
|
|======================= | 25%
|
|======================== | 25%
|
|======================== | 26%
|
|========================= | 27%
|
|========================== | 27%
|
|========================== | 28%
|
|=========================== | 29%
|
|============================ | 30%
|
|============================ | 31%
|
|============================= | 31%
|
|============================= | 32%
|
|============================== | 32%
|
|============================== | 33%
|
|=============================== | 33%
|
|=============================== | 34%
|
|================================ | 34%
|
|================================ | 35%
|
|================================= | 35%
|
|================================= | 36%
|
|================================== | 36%
|
|================================== | 37%
|
|=================================== | 37%
|
|=================================== | 38%
|
|==================================== | 38%
|
|==================================== | 39%
|
|===================================== | 39%
|
|===================================== | 40%
|
|====================================== | 40%
|
|====================================== | 41%
|
|======================================= | 41%
|
|======================================= | 42%
|
|======================================== | 43%
|
|========================================= | 44%
|
|========================================= | 45%
|
|========================================== | 45%
|
|========================================== | 46%
|
|=========================================== | 46%
|
|=========================================== | 47%
|
|============================================ | 47%
|
|============================================ | 48%
|
|============================================= | 48%
|
|============================================= | 49%
|
|============================================== | 49%
|
|============================================== | 50%
|
|=============================================== | 50%
|
|=============================================== | 51%
|
|================================================ | 51%
|
|================================================ | 52%
|
|================================================= | 52%
|
|================================================= | 53%
|
|================================================== | 53%
|
|================================================== | 54%
|
|=================================================== | 54%
|
|=================================================== | 55%
|
|==================================================== | 55%
|
|==================================================== | 56%
|
|===================================================== | 57%
|
|====================================================== | 58%
|
|====================================================== | 59%
|
|======================================================= | 59%
|
|======================================================= | 60%
|
|======================================================== | 60%
|
|======================================================== | 61%
|
|========================================================= | 61%
|
|========================================================= | 62%
|
|========================================================== | 62%
|
|========================================================== | 63%
|
|=========================================================== | 63%
|
|=========================================================== | 64%
|
|============================================================ | 64%
|
|============================================================ | 65%
|
|============================================================= | 65%
|
|============================================================= | 66%
|
|============================================================== | 66%
|
|============================================================== | 67%
|
|=============================================================== | 67%
|
|=============================================================== | 68%
|
|================================================================ | 68%
|
|================================================================ | 69%
|
|================================================================= | 69%
|
|================================================================= | 70%
|
|================================================================== | 71%
|
|=================================================================== | 72%
|
|=================================================================== | 73%
|
|==================================================================== | 73%
|
|===================================================================== | 74%
|
|===================================================================== | 75%
|
|====================================================================== | 75%
|
|====================================================================== | 76%
|
|======================================================================= | 76%
|
|======================================================================= | 77%
|
|======================================================================== | 77%
|
|======================================================================== | 78%
|
|========================================================================= | 78%
|
|========================================================================= | 79%
|
|========================================================================== | 79%
|
|========================================================================== | 80%
|
|=========================================================================== | 80%
|
|=========================================================================== | 81%
|
|============================================================================ | 81%
|
|============================================================================ | 82%
|
|============================================================================= | 82%
|
|============================================================================= | 83%
|
|============================================================================== | 83%
|
|============================================================================== | 84%
|
|=============================================================================== | 85%
|
|================================================================================ | 86%
|
|================================================================================ | 87%
|
|================================================================================= | 87%
|
|================================================================================== | 88%
|
|================================================================================== | 89%
|
|=================================================================================== | 89%
|
|=================================================================================== | 90%
|
|==================================================================================== | 90%
|
|==================================================================================== | 91%
|
|===================================================================================== | 91%
|
|===================================================================================== | 92%
|
|====================================================================================== | 92%
|
|====================================================================================== | 93%
|
|======================================================================================= | 93%
|
|======================================================================================= | 94%
|
|======================================================================================== | 94%
|
|======================================================================================== | 95%
|
|========================================================================================= | 95%
|
|========================================================================================= | 96%
|
|========================================================================================== | 96%
|
|========================================================================================== | 97%
|
|=========================================================================================== | 97%
|
|=========================================================================================== | 98%
|
|============================================================================================ | 99%
|
|=============================================================================================| 100%
voting_districts <-
glue::glue(
"https://geoportal.stadt-koeln.de/arcgis/rest/services/Basiskarten/kgg/\\
MapServer/20/query?where=objectid+is+not+null&text=&objectIds=&time=&\\
geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=\\
esriSpatialRelIntersects&distance=&units=esriSRUnit_Foot&relationParam=&\\
outFields=*&returnGeometry=true&returnTrueCurves=false&maxAllowableOffset=\\
&geometryPrecision=&outSR=4326&havingClause=&returnIdsOnly=false&return\\
CountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=\\
&returnZ=false&returnM=false&gdbVersion=&historicMoment=&returnDistinct\\
Values=false&resultOffset=&resultRecordCount=&returnExtentOnly=false&datum\\
Transformation=¶meterValues=&rangeValues=&quantizationParameters=&\\
featureEncoding=esriDefault&f=geojson"
) %>%
sf::st_read(as_tibble = TRUE) %>%
sf::st_transform(3035) %>%
dplyr::transmute(Stimmbezirk = as.numeric(nummer))
afd_votes <-
glue::glue(
"https://www.stadt-koeln.de/wahlen/bundestagswahl/09-2021/praesentation/\\
Open-Data-Bundestagswahl476.csv"
) %>%
readr::read_csv2() %>%
dplyr::transmute(Stimmbezirk = `gebiet-nr`, afd_share = (F1 / F) * 100)
election_results <-
dplyr::left_join(
voting_districts,
afd_votes,
by = "Stimmbezirk"
)
immigrants_cologne <-
z11::z11_get_100m_attribute(STAATSANGE_KURZ_2) %>%
terra::crop(election_results) %>%
terra::mask(terra::vect(election_results))
inhabitants_cologne <-
z11::z11_get_100m_attribute(Einwohner) %>%
terra::crop(election_results) %>%
terra::mask(terra::vect(election_results))
immigrant_share_cologne <-
(immigrants_cologne / inhabitants_cologne) * 100
election_results <-
election_results %>%
dplyr::mutate(
immigrant_share =
exactextractr::exact_extract(immigrant_share_cologne, ., 'mean'),
inhabitants =
exactextractr::exact_extract(inhabitants_cologne, ., 'mean')
)
sdep
package with its function spdep::poly2nb()
or the more modern approach of the sfdep
package using the sfdep::st_contiguity()
. In both cases you have to set the option queen = FALSE
.
# spdep
queen_neighborhood <-
spdep::poly2nb(
election_results,
queen = TRUE
)
queen_W <- spdep::nb2listw(queen_neighborhood, style = "W")
rook_neighborhood <-
spdep::poly2nb(
election_results,
queen = FALSE
)
rook_W <- spdep::nb2listw(rook_neighborhood, style = "W")
# sfdep
election_results <-
election_results %>%
dplyr::mutate(
queen_neighborhood = sfdep::st_contiguity(., queen = TRUE),
queen_W = sfdep::st_weights(queen_neighborhood),
rook_neighborhood = sfdep::st_contiguity(., queen = FALSE),
rook_W = sfdep::st_weights(rook_neighborhood)
)
We have not used them, but you can also create distance-based weight matrices. Use again the package of your choice and create weights for a distance between 0 and 5000 meters. Use again row-normalization.
For the purpose of this exercise, you also have to convert the polygon data to point coordinates. I’d propose to use the centroids for this endevour:
election_results_centroids <- sf::st_centroid(election_results)
spdep
use the function spdep::dnearneigh()
and if you use sfdep
use the function sfdep::st_dist_band()
.
# convert to centroids
election_results_centroids <- sf::st_centroid(election_results)
tm_shape(election_results_centroids) +
tm_dots()
# spdep
distance_neighborhood_5000 <-
spdep::dnearneigh(election_results_centroids, 0, 5000)
distance_neighborhood_5000_W <-
spdep::nb2listw(distance_neighborhood_5000, style = "W")
# sfdep
election_results_centroids <-
election_results_centroids %>%
dplyr::mutate(
neighbors_5000 = sfdep::st_dist_band(., 0, 5000),
weights_5000 = sfdep::st_weights(neighbors_5000)
)
afd_share
.
spdep
and sfdep
– as it determines the way how you solve this exercise.
# spdep
spdep::moran.test(election_results$immigrant_share, listw = queen_W)
##
## Moran I test under randomisation
##
## data: election_results$immigrant_share
## weights: queen_W
##
## Moran I statistic standard deviate = 20.396, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.5407453087 -0.0018450185 0.0007077082
spdep::moran.test(election_results$immigrant_share, listw = rook_W)
##
## Moran I test under randomisation
##
## data: election_results$immigrant_share
## weights: rook_W
##
## Moran I statistic standard deviate = 19.84, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.547579338 -0.001845018 0.000766878
spdep::moran.test(
election_results_centroids$immigrant_share,
listw = distance_neighborhood_5000_W
)
##
## Moran I test under randomisation
##
## data: election_results_centroids$immigrant_share
## weights: distance_neighborhood_5000_W
##
## Moran I statistic standard deviate = 22.671, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 1.228036e-01 -1.845018e-03 3.023058e-05
spdep::geary.test(election_results$immigrant_share, listw = queen_W)
##
## Geary C test under randomisation
##
## data: election_results$immigrant_share
## weights: queen_W
##
## Geary C statistic standard deviate = 17.372, p-value < 2.2e-16
## alternative hypothesis: Expectation greater than statistic
## sample estimates:
## Geary C statistic Expectation Variance
## 0.443471727 1.000000000 0.001026338
spdep::geary.test(election_results$immigrant_share, listw = rook_W)
##
## Geary C test under randomisation
##
## data: election_results$immigrant_share
## weights: rook_W
##
## Geary C statistic standard deviate = 17.18, p-value < 2.2e-16
## alternative hypothesis: Expectation greater than statistic
## sample estimates:
## Geary C statistic Expectation Variance
## 0.434242217 1.000000000 0.001084442
spdep::geary.test(
election_results_centroids$immigrant_share,
listw = distance_neighborhood_5000_W
)
##
## Geary C test under randomisation
##
## data: election_results_centroids$immigrant_share
## weights: distance_neighborhood_5000_W
##
## Geary C statistic standard deviate = 11.532, p-value < 2.2e-16
## alternative hypothesis: Expectation greater than statistic
## sample estimates:
## Geary C statistic Expectation Variance
## 0.8814754355 1.0000000000 0.0001056289
# sfdep
library(magrittr)
election_results %$%
sfdep::global_moran_test(immigrant_share, queen_neighborhood, queen_W)
##
## Moran I test under randomisation
##
## data: x
## weights: listw
##
## Moran I statistic standard deviate = 20.396, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.5407453087 -0.0018450185 0.0007077082
election_results %$%
sfdep::global_moran_test(immigrant_share, rook_neighborhood, rook_W)
##
## Moran I test under randomisation
##
## data: x
## weights: listw
##
## Moran I statistic standard deviate = 19.84, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 0.547579338 -0.001845018 0.000766878
election_results_centroids %$%
sfdep::global_moran_test(immigrant_share, neighbors_5000, weights_5000)
##
## Moran I test under randomisation
##
## data: x
## weights: listw
##
## Moran I statistic standard deviate = 22.671, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic Expectation Variance
## 1.228036e-01 -1.845018e-03 3.023058e-05
election_results %$%
sfdep::global_c_test(immigrant_share, queen_neighborhood, queen_W)
##
## Geary C test under randomisation
##
## data: x
## weights: listw
##
## Geary C statistic standard deviate = 17.372, p-value < 2.2e-16
## alternative hypothesis: Expectation greater than statistic
## sample estimates:
## Geary C statistic Expectation Variance
## 0.443471727 1.000000000 0.001026338
election_results %$%
sfdep::global_c_test(immigrant_share, rook_neighborhood, rook_W)
##
## Geary C test under randomisation
##
## data: x
## weights: listw
##
## Geary C statistic standard deviate = 17.18, p-value < 2.2e-16
## alternative hypothesis: Expectation greater than statistic
## sample estimates:
## Geary C statistic Expectation Variance
## 0.434242217 1.000000000 0.001084442
election_results_centroids %$%
sfdep::global_c_test(immigrant_share, neighbors_5000, weights_5000)
##
## Geary C test under randomisation
##
## data: x
## weights: listw
##
## Geary C statistic standard deviate = 11.532, p-value < 2.2e-16
## alternative hypothesis: Expectation greater than statistic
## sample estimates:
## Geary C statistic Expectation Variance
## 0.8814754355 1.0000000000 0.0001056289