Hands-on Exercise 4: Spatial Weights and Applications

Published

August 13, 2024

Modified

August 16, 2024

1 Overview

In this hands-on exercise, you will learn how to compute spatial weights using R.

2 The Packages

Four R packages will be used, they are:

Package Description
spdep To create spatial weights matrix objects and related calculations (e.g. spatially lag attributes).
sf For importing, managing, and processing geospatial data
tidyverse A collection of functions for performing data science task such as importing, tidying, wrangling data and visualising data.
tmap For plotting thematic maps, such as choropleth and bubble maps
DTknitr and kableExtra For building tables
pacman::p_load(spdep, sf, tmap, tidyverse, knitr, DT, kableExtra)

3 The Data

Two data sets will be used in this hands-on exercise, they are:

Type Name Details
Geospatial Hunan
  • County boundary layer

  • Format: SHP (ESRI Shapefile)

Aspatial Hunan_2012
  • Contains selected Hunan’s local development indicators in 2012

  • Format: CSV

3.1 Loading Data Into R Environment

The code chunk below uses st_read() of sf package to import Hunan shapefile into R. The imported shapefile will be simple features Object of sf.

hunan <- st_read(dsn = "data/geospatial", 
                 layer = "Hunan")
Reading layer `Hunan' from data source 
  `C:\kytjy\ISSS626-GAA\Hands-on_Ex\Hands-on_Ex04\data\geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 88 features and 7 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 108.7831 ymin: 24.6342 xmax: 114.2544 ymax: 30.12812
Geodetic CRS:  WGS 84
Rows: 88
Columns: 8
$ NAME_2     <chr> "Changde", "Changde", "Changde", "Changde", "Changde", "Cha…
$ ID_3       <int> 21098, 21100, 21101, 21102, 21103, 21104, 21109, 21110, 211…
$ NAME_3     <chr> "Anxiang", "Hanshou", "Jinshi", "Li", "Linli", "Shimen", "L…
$ ENGTYPE_3  <chr> "County", "County", "County City", "County", "County", "Cou…
$ Shape_Leng <dbl> 1.869074, 2.360691, 1.425620, 3.474325, 2.289506, 4.171918,…
$ Shape_Area <dbl> 0.10056190, 0.19978745, 0.05302413, 0.18908121, 0.11450357,…
$ County     <chr> "Anxiang", "Hanshou", "Jinshi", "Li", "Linli", "Shimen", "L…
$ geometry   <POLYGON [°]> POLYGON ((112.0625 29.75523..., POLYGON ((112.2288 …
NAME_2 ID_3 NAME_3 ENGTYPE_3 Shape_Leng Shape_Area County geometry
Changde 21098 Anxiang County 1.8690742 0.1005619 Anxiang POLYGON ((112.0625 29.75523...
Changde 21100 Hanshou County 2.3606914 0.1997875 Hanshou POLYGON ((112.2288 29.11684...
Changde 21101 Jinshi County City 1.4256199 0.0530241 Jinshi POLYGON ((111.8927 29.6013,...
Changde 21102 Li County 3.4743245 0.1890812 Li POLYGON ((111.3731 29.94649...
Changde 21103 Linli County 2.2895061 0.1145036 Linli POLYGON ((111.6324 29.76288...
Changde 21104 Shimen County 4.1719181 0.3719471 Shimen POLYGON ((110.8825 30.11675...
Changsha 21109 Liuyang County City 4.0605788 0.4601679 Liuyang POLYGON ((113.9905 28.5682,...
Changsha 21110 Ningxiang County 3.3237542 0.2661420 Ningxiang POLYGON ((112.7181 28.38299...
Changsha 21111 Wangcheng County 2.2920930 0.1304916 Wangcheng POLYGON ((112.7914 28.52688...
Chenzhou 21112 Anren County 2.2407387 0.1334394 Anren POLYGON ((113.1757 26.82734...
Chenzhou 21115 Guidong County 2.0467289 0.1285299 Guidong POLYGON ((114.1799 26.20117...
Chenzhou 21117 Jiahe County 1.5038242 0.0631237 Jiahe POLYGON ((112.4425 25.74358...
Chenzhou 21118 Linwu County 2.0512481 0.1244964 Linwu POLYGON ((112.5914 25.55143...
Chenzhou 21119 Rucheng County 2.7155640 0.2176296 Rucheng POLYGON ((113.6759 25.87578...
Chenzhou 21120 Yizhang County 3.2765386 0.1935418 Yizhang POLYGON ((113.2621 25.68394...
Chenzhou 21121 Yongxing County 2.9211528 0.1767181 Yongxing POLYGON ((113.3169 26.41843...
Chenzhou 21122 Zixing County City 2.6995368 0.2476280 Zixing POLYGON ((113.7311 26.16259...
Hengyang 21123 Changning County City 2.4013569 0.1863642 Changning POLYGON ((112.6144 26.60198...
Hengyang 21124 Hengdong County 2.7098301 0.1756985 Hengdong POLYGON ((113.1056 27.21007...
Hengyang 21125 Hengnan County 3.7804926 0.2408050 Hengnan POLYGON ((112.7599 26.98149...
Hengyang 21126 Hengshan County 2.3513543 0.0899128 Hengshan POLYGON ((112.607 27.4689, ...
Hengyang 21129 Leiyang County 2.7427498 0.2426073 Leiyang POLYGON ((112.9996 26.69276...
Hengyang 21130 Qidong County 3.0714217 0.1733332 Qidong POLYGON ((111.7818 27.0383,...
Huaihua 21131 Chenxi County 3.0228314 0.1821929 Chenxi POLYGON ((110.2624 28.21778...
Huaihua 21134 Zhongfang County 2.9023763 0.1994978 Zhongfang POLYGON ((109.9431 27.72858...
Huaihua 21135 Huitong County 3.1096821 0.2053454 Huitong POLYGON ((109.9419 27.10512...
Huaihua 21136 Jingzhou County 2.8584583 0.1988991 Jingzhou POLYGON ((109.8186 26.75842...
Huaihua 21137 Mayang Autonomous County 2.3274351 0.1462606 Mayang POLYGON ((109.795 27.98008,...
Huaihua 21138 Tongdao Autonomous County 2.5711292 0.2021613 Tongdao POLYGON ((109.9294 26.46561...
Huaihua 21139 Xinhuang Autonomous County 2.1443973 0.1377764 Xinhuang POLYGON ((109.227 27.43733,...
Huaihua 21140 Xupu County 4.0350213 0.3136471 Xupu POLYGON ((110.7189 28.30485...
Huaihua 21141 Yuanling County 4.1642914 0.5345233 Yuanling POLYGON ((110.9652 28.99895...
Huaihua 21142 Zhijiang Autonomous County 2.4452310 0.1906027 Zhijiang POLYGON ((109.8818 27.60661...
Loudi 21143 Lengshuijiang County City 0.9753557 0.0372343 Lengshuijiang POLYGON ((111.5307 27.81472...
Loudi 21146 Shuangfeng County 2.4012680 0.1565776 Shuangfeng POLYGON ((112.263 27.70421,...
Loudi 21147 Xinhua County 3.3284533 0.3361624 Xinhua POLYGON ((111.3345 28.19642...
Shaoyang 21148 Chengbu Autonomous County 2.9374722 0.2362023 Chengbu POLYGON ((110.4455 26.69317...
Yongzhou 21149 Dongan District 3.2132100 0.1983914 Dongan POLYGON ((111.4531 26.86812...
Shaoyang 21150 Dongkou County 2.9425568 0.1971258 Dongkou POLYGON ((110.6622 27.37305...
Shaoyang 21151 Longhui County 2.7208650 0.2578820 Longhui POLYGON ((110.985 27.65983,...
Shaoyang 21152 Shaodong County 2.3029031 0.1701016 Shaodong POLYGON ((111.9054 27.40254...
Shaoyang 21155 Suining County 3.3043615 0.2659378 Suining POLYGON ((110.389 27.10006,...
Shaoyang 21156 Wugang County City 2.2565865 0.1400498 Wugang POLYGON ((110.9878 27.03345...
Shaoyang 21157 Xinning County 3.3414094 0.2487593 Xinning POLYGON ((111.0736 26.84627...
Shaoyang 21158 Xinshao County 2.2955891 0.1658312 Xinshao POLYGON ((111.6013 27.58275...
Xiangtan 21159 Shaoshan County City 0.7722034 0.0212792 Shaoshan POLYGON ((112.5391 27.97742...
Xiangtan 21162 Xiangxiang County City 3.0755761 0.1840808 Xiangxiang POLYGON ((112.4549 28.05783...
Xiangxi Tujia and Miao 21163 Baojing County 2.5569785 0.1606901 Baojing POLYGON ((109.7015 28.82844...
Xiangxi Tujia and Miao 21164 Fenghuang County 2.2928893 0.1596618 Fenghuang POLYGON ((109.5239 28.19206...
Xiangxi Tujia and Miao 21165 Guzhang County 1.7973808 0.1202073 Guzhang POLYGON ((109.8968 28.74034...
Xiangxi Tujia and Miao 21166 Huayuan County 1.7921938 0.1025162 Huayuan POLYGON ((109.5647 28.61712...
Xiangxi Tujia and Miao 21167 Jishou County City 1.8826069 0.0973363 Jishou POLYGON ((109.8375 28.4696,...
Xiangxi Tujia and Miao 21168 Longshan County 2.9782522 0.2919091 Longshan POLYGON ((109.6337 29.62521...
Xiangxi Tujia and Miao 21169 Luxi County 2.2051733 0.1434018 Luxi POLYGON ((110.1067 28.41835...
Xiangxi Tujia and Miao 21170 Yongshun County 3.0959707 0.3551324 Yongshun POLYGON ((110.0003 29.29499...
Yiyang 21171 Anhua County 4.5835050 0.4510648 Anhua POLYGON ((111.6034 28.63716...
Yiyang 21172 Nan County 2.3011103 0.1247939 Nan POLYGON ((112.3232 29.46074...
Yiyang 21176 Yuanjiang County City 2.3268236 0.1886048 Yuanjiang POLYGON ((112.4391 29.1791,...
Yongzhou 21178 Jianghua Autonomous County 3.3360379 0.2927690 Jianghua POLYGON ((111.6461 25.29661...
Yongzhou 21180 Lanshan County 2.3556792 0.1603531 Lanshan POLYGON ((112.2286 25.61123...
Yongzhou 21183 Ningyuan County 3.3322291 0.2266737 Ningyuan POLYGON ((112.0715 26.09892...
Yongzhou 21185 Shuangpai County 2.3877440 0.1540255 Shuangpai POLYGON ((111.8864 26.11957...
Yongzhou 21186 Xintian County 1.7274574 0.0894019 Xintian POLYGON ((112.2578 26.0796,...
Yueyang 21187 Huarong County 2.8178435 0.1678359 Huarong POLYGON ((112.9242 29.69134...
Yueyang 21188 Linxiang County City 2.5975997 0.1568525 Linxiang POLYGON ((113.5502 29.67418...
Yueyang 21189 Miluo County City 2.4474057 0.1497881 Miluo POLYGON ((112.9902 29.02139...
Yueyang 21190 Pingjiang County 3.2177944 0.3786800 Pingjiang POLYGON ((113.8436 29.06152...
Yueyang 21191 Xiangyin County 2.3515063 0.1491429 Xiangyin POLYGON ((112.9173 28.98264...
Zhangjiajie 21194 Cili County 2.8940385 0.3232206 Cili POLYGON ((110.8822 29.69017...
Zhuzhou 21197 Chaling County 2.2375615 0.2278921 Chaling POLYGON ((113.7666 27.10573...
Zhuzhou 21198 Liling County City 2.2435440 0.1960655 Liling POLYGON ((113.5673 27.94346...
Zhuzhou 21199 Yanling County 2.1078954 0.1849090 Yanling POLYGON ((113.9292 26.6154,...
Zhuzhou 21200 You County 2.8904505 0.2436366 You POLYGON ((113.5879 27.41324...
Zhuzhou 21201 Zhuzhou District 0.9331877 0.0373488 Zhuzhou POLYGON ((113.2493 28.02411...
Zhangjiajie 21196 Sangzhi County 3.3475449 0.3216362 Sangzhi POLYGON ((110.556 29.40543,...
Yueyang 21192 Yueyang District 2.5710437 0.1047594 Yueyang POLYGON ((113.343 29.61064,...
Yongzhou 21184 Qiyang County 3.1835301 0.2275458 Qiyang POLYGON ((111.5563 26.81318...
Yiyang 21173 Taojiang County 2.6080229 0.1905982 Taojiang POLYGON ((112.0508 28.67265...
Shaoyang 21153 Shaoyang County City 0.9765399 0.0316779 Shaoyang POLYGON ((111.5013 27.30207...
Loudi 21144 Lianyuan County City 2.8007253 0.2057341 Lianyuan POLYGON ((111.6789 28.02946...
Huaihua 21132 Hongjiang District 3.3031522 0.1994795 Hongjiang POLYGON ((110.1441 27.47513...
Hengyang 21127 Hengyang County City 0.9035944 0.0349171 Hengyang POLYGON ((112.7144 26.98613...
Chenzhou 21116 Guiyang County 3.6939698 0.2668106 Guiyang POLYGON ((113.0811 26.04963...
Changsha 21107 Changsha District 0.9536480 0.0320942 Changsha POLYGON ((112.9421 28.03722...
Changde 21105 Taoyuan County 4.1225866 0.4126555 Taoyuan POLYGON ((112.0612 29.32855...
Xiangtan 21160 Xiangtan County City 0.8480602 0.0253653 Xiangtan POLYGON ((113.0426 27.8942,...
Yongzhou 21177 Dao County 2.7674949 0.2206642 Dao POLYGON ((111.498 25.81679,...
Yongzhou 21179 Jiangyong County 2.2995970 0.1473782 Jiangyong POLYGON ((111.3659 25.39472...

Next, we will import Hunan_2012.csv into R by using read_csv() of readr package. The output is R dataframe class.

hunan2012 <- read_csv("data/aspatial/Hunan_2012.csv")
Rows: 88
Columns: 29
$ County      <chr> "Anhua", "Anren", "Anxiang", "Baojing", "Chaling", "Changn…
$ City        <chr> "Yiyang", "Chenzhou", "Changde", "Hunan West", "Zhuzhou", …
$ avg_wage    <dbl> 30544, 28058, 31935, 30843, 31251, 28518, 54540, 28597, 33…
$ deposite    <dbl> 10967.0, 4598.9, 5517.2, 2250.0, 8241.4, 10860.0, 24332.0,…
$ FAI         <dbl> 6831.7, 6386.1, 3541.0, 1005.4, 6508.4, 7920.0, 33624.0, 1…
$ Gov_Rev     <dbl> 456.72, 220.57, 243.64, 192.59, 620.19, 769.86, 5350.00, 1…
$ Gov_Exp     <dbl> 2703.0, 1454.7, 1779.5, 1379.1, 1947.0, 2631.6, 7885.5, 11…
$ GDP         <dbl> 13225.0, 4941.2, 12482.0, 4087.9, 11585.0, 19886.0, 88009.…
$ GDPPC       <dbl> 14567, 12761, 23667, 14563, 20078, 24418, 88656, 10132, 17…
$ GIO         <dbl> 9276.90, 4189.20, 5108.90, 3623.50, 9157.70, 37392.00, 513…
$ Loan        <dbl> 3954.90, 2555.30, 2806.90, 1253.70, 4287.40, 4242.80, 4053…
$ NIPCR       <dbl> 3528.3, 3271.8, 7693.7, 4191.3, 3887.7, 9528.0, 17070.0, 3…
$ Bed         <dbl> 2718, 970, 1931, 927, 1449, 3605, 3310, 582, 2170, 2179, 1…
$ Emp         <dbl> 494.310, 290.820, 336.390, 195.170, 330.290, 548.610, 670.…
$ EmpR        <dbl> 441.4, 255.4, 270.5, 145.6, 299.0, 415.1, 452.0, 127.6, 21…
$ EmpRT       <dbl> 338.0, 99.4, 205.9, 116.4, 154.0, 273.7, 219.4, 94.4, 174.…
$ Pri_Stu     <dbl> 54.175, 33.171, 19.584, 19.249, 33.906, 81.831, 59.151, 18…
$ Sec_Stu     <dbl> 32.830, 17.505, 17.819, 11.831, 20.548, 44.485, 39.685, 7.…
$ Household   <dbl> 290.4, 104.6, 148.1, 73.2, 148.7, 211.2, 300.3, 76.1, 139.…
$ Household_R <dbl> 234.5, 121.9, 135.4, 69.9, 139.4, 211.7, 248.4, 59.6, 110.…
$ NOIP        <dbl> 101, 34, 53, 18, 106, 115, 214, 17, 55, 70, 44, 84, 74, 17…
$ Pop_R       <dbl> 670.3, 243.2, 346.0, 184.1, 301.6, 448.2, 475.1, 189.6, 31…
$ RSCG        <dbl> 5760.60, 2386.40, 3957.90, 768.04, 4009.50, 5220.40, 22604…
$ Pop_T       <dbl> 910.8, 388.7, 528.3, 281.3, 578.4, 816.3, 998.6, 256.7, 45…
$ Agri        <dbl> 4942.253, 2357.764, 4524.410, 1118.561, 3793.550, 6430.782…
$ Service     <dbl> 5414.5, 3814.1, 14100.0, 541.8, 5444.0, 13074.6, 17726.6, …
$ Disp_Inc    <dbl> 12373, 16072, 16610, 13455, 20461, 20868, 183252, 12379, 1…
$ RORP        <dbl> 0.7359464, 0.6256753, 0.6549309, 0.6544614, 0.5214385, 0.5…
$ ROREmp      <dbl> 0.8929619, 0.8782065, 0.8041262, 0.7460163, 0.9052651, 0.7…
County City avg_wage deposite FAI Gov_Rev Gov_Exp GDP GDPPC GIO Loan NIPCR Bed Emp EmpR EmpRT Pri_Stu Sec_Stu Household Household_R NOIP Pop_R RSCG Pop_T Agri Service Disp_Inc RORP ROREmp
Anhua Yiyang 30544 10967.0 6831.7 456.72 2703.00 13225.0 14567 9276.90 3954.90 3528.3 2718 494.310 441.4 338.0 54.175 32.830 290.400 234.5 101 670.3 5760.60 910.8 4942.253 5414.5 12373 0.7359464 0.8929619
Anren Chenzhou 28058 4598.9 6386.1 220.57 1454.70 4941.2 12761 4189.20 2555.30 3271.8 970 290.820 255.4 99.4 33.171 17.505 104.600 121.9 34 243.2 2386.40 388.7 2357.764 3814.1 16072 0.6256753 0.8782065
Anxiang Changde 31935 5517.2 3541.0 243.64 1779.50 12482.0 23667 5108.90 2806.90 7693.7 1931 336.390 270.5 205.9 19.584 17.819 148.100 135.4 53 346.0 3957.90 528.3 4524.410 14100.0 16610 0.6549309 0.8041262
Baojing Hunan West 30843 2250.0 1005.4 192.59 1379.10 4087.9 14563 3623.50 1253.70 4191.3 927 195.170 145.6 116.4 19.249 11.831 73.200 69.9 18 184.1 768.04 281.3 1118.561 541.8 13455 0.6544614 0.7460163
Chaling Zhuzhou 31251 8241.4 6508.4 620.19 1947.00 11585.0 20078 9157.70 4287.40 3887.7 1449 330.290 299.0 154.0 33.906 20.548 148.700 139.4 106 301.6 4009.50 578.4 3793.550 5444.0 20461 0.5214385 0.9052651
Changning Hengyang 28518 10860.0 7920.0 769.86 2631.60 19886.0 24418 37392.00 4242.80 9528.0 3605 548.610 415.1 273.7 81.831 44.485 211.200 211.7 115 448.2 5220.40 816.3 6430.782 13074.6 20868 0.5490628 0.7566395
Changsha Changsha 54540 24332.0 33624.0 5350.00 7885.50 88009.0 88656 51361.00 40534.00 17070.0 3310 670.820 452.0 219.4 59.151 39.685 300.300 248.4 214 475.1 22604.00 998.6 13107.148 17726.6 183252 0.4757661 0.6738022
Chengbu Shaoyang 28597 2580.6 1922.3 160.73 1191.60 2569.5 10132 1681.40 1232.00 3271.8 582 162.480 127.6 94.4 18.751 7.869 76.100 59.6 17 189.6 1173.80 256.7 1356.950 1215.1 12379 0.7386054 0.7853274
Chenxi Huaihua 33580 4990.0 5818.4 460.49 1724.20 7755.2 17026 6644.50 3220.00 4777.0 2170 308.430 214.4 174.8 26.706 14.591 139.500 110.5 55 311.7 2570.60 456.7 2257.520 1306.3 14595 0.6825049 0.6951334
Cili Zhangjiajie 33099 8116.9 4498.1 499.77 2306.20 11378.0 18714 5843.30 5503.40 5031.8 2179 381.200 334.8 264.3 34.918 27.020 211.400 174.5 70 379.4 3116.90 610.4 3112.731 5005.6 15603 0.6215596 0.8782791
Dao Yongzhou 32541 7245.0 7922.0 461.66 2013.50 11034.0 18059 2393.80 2873.50 9000.0 1588 381.530 344.3 178.3 65.790 31.430 158.700 157.9 44 388.2 2898.80 613.6 5602.035 8411.1 16305 0.6326597 0.9024192
Dongan Yongzhou 36713 6549.3 9158.0 434.07 1720.60 11495.0 20901 11597.00 3320.60 9116.7 1305 322.250 278.1 163.6 39.419 20.661 166.500 163.0 84 366.0 3106.30 551.2 4866.481 6784.4 20265 0.6640058 0.8629946
Dongkou Shaoyang 31483 9489.6 8703.2 374.96 2209.50 10286.0 13240 10568.00 4120.30 3792.5 1620 583.560 499.2 263.2 69.845 43.194 262.400 220.4 74 523.1 3229.10 780.9 6025.184 9116.8 15375 0.6698681 0.8554390
Fenghuang Hunan West 31608 4008.7 2550.0 406.51 1763.60 4681.2 13382 848.99 2863.00 4681.2 1212 250.730 195.4 151.8 33.379 17.690 90.100 80.5 17 255.5 2605.40 351.3 1206.261 651.1 14281 0.7272986 0.7793244
Guidong Chenzhou 30149 2200.1 2512.9 111.19 880.00 1955.9 8497 1202.60 1437.20 3130.4 567 138.530 115.2 56.9 13.437 7.091 64.800 65.4 14 150.1 486.10 231.8 630.016 911.1 12290 0.6475410 0.8315888
Guiyang Chenzhou 41394 9664.8 15712.0 1309.20 2727.50 23023.0 32853 36820.00 4860.10 10271.0 2237 517.180 430.9 242.0 69.344 33.568 231.800 217.5 127 420.4 6281.80 704.1 8804.639 14791.5 64517 0.5970743 0.8331722
Guzhang Hunan West 28610 1563.8 1021.7 108.40 959.60 1490.4 11580 513.95 849.56 3595.9 392 98.421 74.6 56.7 8.718 6.288 35.200 31.4 10 85.2 354.48 129.0 527.230 504.0 11954 0.6604651 0.7579683
Hanshou Changde 32265 7979.0 8665.0 386.13 2062.40 15788.0 20981 13491.00 4550.00 8269.9 2560 456.780 388.8 246.7 42.097 33.029 240.200 208.7 95 553.2 4460.50 804.6 6545.350 17727.0 18925 0.6875466 0.8511756
Hengdong Hengyang 30992 8661.0 6665.0 542.25 1937.40 17409.0 27485 17305.00 4118.00 10829.0 1859 388.340 284.5 201.0 48.983 36.120 164.400 167.4 96 426.4 5127.00 634.9 5972.961 11278.5 19800 0.6716018 0.7326054
Hengnan Hengyang 28031 3288.9 9531.4 804.91 2894.40 21019.0 21911 20953.00 4013.00 9872.9 2173 601.570 503.6 354.0 76.933 57.519 256.000 274.3 118 629.1 5752.60 961.4 8624.680 15914.0 18683 0.6543582 0.8371428
Hengshan Hengyang 28688 5944.6 4871.1 451.46 1393.70 9718.9 25172 8571.50 2221.50 10619.0 966 291.120 261.9 158.8 28.997 20.909 103.700 101.0 89 256.4 1713.70 387.0 3505.269 6233.5 17732 0.6625323 0.8996290
Hengyang Hengyang 27760 14680.0 11145.0 597.66 2778.80 21495.0 19382 18288.00 5549.60 10313.0 2632 686.520 500.2 313.6 88.917 64.674 299.200 247.7 120 733.4 5386.30 1111.6 12451.938 23139.2 124392 0.6597697 0.7286022
Hongjiang Huaihua 37686 5966.0 6515.5 623.95 2067.80 7355.5 17733 13750.00 3272.20 6553.5 1161 229.400 199.9 132.0 23.614 18.837 136.100 124.7 76 260.9 1744.20 415.8 2588.250 202.8 27334 0.6274651 0.8714037
Huarong Yueyang 26832 8116.4 14292.0 365.84 1933.90 21654.0 30413 40193.00 4564.80 10367.0 1500 442.970 359.2 252.9 31.601 26.933 202.300 170.2 151 430.3 6348.90 714.1 7690.520 10050.6 21501 0.6025767 0.8108901
Huayuan Hunan West 31708 3669.8 2200.0 331.78 1503.70 5905.9 20337 9257.60 2463.60 4353.8 1240 214.790 149.8 94.9 23.831 14.031 76.900 59.9 74 188.1 1024.70 291.1 883.722 533.2 15467 0.6461697 0.6974254
Huitong Huaihua 33693 4615.0 2617.4 295.00 1306.70 4588.3 14334 2095.60 1369.30 4631.9 1194 246.150 193.5 143.7 20.603 13.337 96.700 114.4 25 234.5 1061.70 320.9 1580.090 5.2 13989 0.7307572 0.7861060
Jiahe Chenzhou 36023 5493.1 4864.3 453.68 1331.50 9608.2 32091 14882.00 1906.50 7903.3 719 242.800 198.9 83.0 36.054 19.497 99.000 108.5 100 179.6 1365.90 301.3 2276.272 3230.2 17744 0.5960836 0.8191928
Jianghua Yongzhou 34378 4586.4 4631.5 309.70 1651.40 6555.9 15801 3357.10 2976.00 3225.3 1663 306.310 272.7 188.9 37.494 20.644 109.900 116.1 33 285.1 2384.80 416.7 3272.511 10533.8 16274 0.6841853 0.8902746
Jiangyong Yongzhou 30250 3198.2 3942.5 187.23 1009.10 4012.2 17168 2022.70 1201.20 3253.9 720 153.580 132.8 94.3 21.936 11.093 68.300 61.2 25 163.5 1238.20 234.3 2862.901 9773.3 16634 0.6978233 0.8646959
Jingzhou Huaihua 33870 3592.8 2031.4 237.51 1129.00 5007.7 20348 4534.10 1566.50 5397.3 1054 163.910 112.6 85.8 16.978 11.446 69.100 56.1 32 140.6 1611.50 246.7 1769.328 337.5 12859 0.5699230 0.6869624
Jinshi Changde 28692 4581.7 4777.0 373.31 1148.40 8706.9 34592 10935.00 2242.00 8169.9 848 122.780 82.1 61.7 8.723 7.592 81.900 43.7 77 92.4 3683.00 251.8 2562.460 7525.0 19498 0.3669579 0.6686757
Jishou Hunan West 39816 9586.2 5235.0 470.22 1738.20 9631.5 31537 6345.70 8282.90 4823.0 3790 213.420 97.0 62.4 25.454 21.941 99.200 46.5 49 85.2 5651.00 306.6 849.010 611.6 17980 0.2778865 0.4545029
Lanshan Yongzhou 33756 4236.0 3986.7 296.68 1194.60 6627.2 20088 5392.10 1599.40 8672.1 1144 233.390 201.5 131.6 32.448 16.299 83.300 90.7 52 198.2 2234.20 330.9 2262.021 10341.9 19161 0.5989725 0.8633618
Leiyang Hengyang 32814 15393.0 18687.0 1535.20 3819.00 30213.0 26105 42707.00 7409.20 10611.0 2531 750.120 553.7 405.8 112.200 56.350 338.100 281.2 155 629.3 7268.10 1160.2 8349.199 19981.6 19634 0.5424065 0.7381486
Lengshuijiang Loudi 35647 6805.3 9746.8 1054.20 2105.30 21243.0 64257 36926.00 8936.60 10441.0 1413 191.320 101.8 61.8 29.639 20.113 112.400 56.9 118 78.4 5750.60 332.6 1277.869 991.8 23120 0.2357186 0.5320928
Li Changde 32541 13487.0 16066.0 709.61 2459.50 20322.0 24473 18402.00 6748.00 8377.0 2038 513.440 426.8 227.1 38.975 33.938 268.500 256.0 96 539.7 7110.20 832.5 7562.340 53160.0 18985 0.6482883 0.8312558
Lianyuan Loudi 30320 8059.7 9255.3 707.45 3298.60 18340.0 18346 20515.00 7243.20 4307.7 2260 706.840 612.9 344.1 71.232 53.597 316.200 298.7 154 712.6 6889.00 1001.5 7642.158 5965.4 38131 0.7115327 0.8670986
Liling Zhuzhou 42896 12865.0 19125.0 2602.60 4221.40 39553.0 41491 57146.00 9398.10 13347.0 2797 639.830 513.1 212.1 53.992 33.705 248.400 229.7 482 473.2 11213.00 958.0 5373.990 17378.0 24185 0.4939457 0.8019318
Linli Changde 32667 564.1 7781.2 336.86 1538.70 10355.0 25554 8214.00 358.00 8143.1 1440 307.360 272.2 100.8 23.286 18.943 129.100 157.2 99 246.6 3604.90 409.3 3583.910 7031.0 18604 0.6024921 0.8856065
Linwu Chenzhou 32031 5984.0 5734.3 478.92 1435.20 8191.1 23986 6250.70 2152.70 7102.2 959 215.300 184.1 90.2 41.913 13.238 101.500 88.0 67 227.5 2023.30 342.8 1606.947 2632.2 17966 0.6636523 0.8550859
Linxiang Yueyang 31669 5850.5 10852.0 319.20 1609.00 15968.0 31897 29988.00 3788.20 9739.0 1289 293.500 212.5 98.8 36.202 23.831 138.600 136.7 123 286.1 4598.80 502.3 3357.477 2994.6 20628 0.5695799 0.7240204
Liuyang Changsha 40446 21415.0 43599.0 2473.10 4605.50 81113.0 63118 99254.00 23408.00 15719.0 6225 919.620 721.4 300.1 90.978 58.819 374.800 369.8 733 642.7 16233.00 1285.5 10844.470 26617.8 27345 0.4999611 0.7844544
Longhui Shaoyang 33615 11925.0 8723.9 425.06 2741.60 10511.0 9572 9317.90 3985.50 3177.6 2726 709.990 604.4 436.7 95.163 54.211 344.600 298.8 85 834.1 2819.60 1098.2 4320.396 3264.6 12049 0.7595156 0.8512796
Longshan Hunan West 34203 5557.0 2810.0 218.53 2052.40 4933.6 9754 893.45 2128.20 4164.0 1505 334.420 266.7 167.5 42.189 30.946 148.200 126.1 18 345.8 2293.90 508.3 2248.709 921.5 13138 0.6803069 0.7975001
Luxi Hunan West 32680 2946.8 1772.0 184.25 1337.60 4878.3 17472 7085.60 1626.60 4089.1 664 197.490 149.7 80.9 21.683 17.160 82.400 71.1 42 174.3 917.41 280.3 1036.088 540.8 13247 0.6218337 0.7580131
Mayang Huaihua 32772 3456.0 2703.1 215.86 1461.00 4738.8 13744 3876.90 2242.00 4299.8 975 216.850 160.0 148.1 24.173 15.207 90.300 87.8 33 245.0 1581.70 345.6 1865.938 5.2 14487 0.7089120 0.7378372
Miluo Yueyang 36113 4749.3 15828.0 1068.60 2446.50 29548.0 42497 66342.00 3330.60 9421.3 965 451.270 303.3 162.8 40.221 30.662 204.900 193.6 269 351.4 5817.70 697.0 5688.717 8978.5 23379 0.5041607 0.6721032
Nan Yiyang 35272 8921.7 3367.5 300.00 1987.10 15568.0 21311 11804.00 4928.10 8369.8 1877 366.100 325.6 226.1 35.487 29.444 259.200 201.3 91 455.9 5266.20 732.8 7783.359 6781.7 17691 0.6221343 0.8893745
Ningxiang Changsha 40744 18662.0 49234.0 2448.90 4812.20 73250.0 62202 114145.00 18435.00 13763.0 4351 852.960 757.6 318.3 80.715 68.853 391.700 369.6 552 655.5 15623.00 1186.5 12804.480 18447.7 24020 0.5524652 0.8882011
Ningyuan Yongzhou 34190 7351.4 7716.5 514.24 2234.40 8984.3 12697 5627.80 4249.40 4151.5 1753 444.920 401.3 157.8 68.336 32.471 185.500 157.8 64 460.9 3097.50 710.2 4283.431 9958.4 19742 0.6489721 0.9019599
Pingjiang Yueyang 30017 8718.4 10627.0 461.62 2827.00 16444.0 17252 25647.00 5201.20 3780.8 2205 601.380 512.8 278.3 73.943 43.548 260.900 269.9 136 612.5 3448.30 957.5 5060.948 8789.3 14883 0.6396867 0.8527054
Qidong Hengyang 30990 13633.0 6875.7 463.91 2413.20 17718.0 18001 26260.00 4246.10 9020.6 2055 607.640 529.2 311.0 80.694 51.474 289.200 287.3 115 627.8 6076.00 986.4 7380.180 15451.7 18256 0.6364558 0.8709104
Qiyang Yongzhou 32059 14432.0 14439.0 499.51 2710.50 17705.0 20638 11567.00 6388.30 8816.0 2799 594.100 495.2 257.9 75.936 45.318 241.900 255.1 107 519.9 3832.70 860.7 6273.597 13568.8 19776 0.6040432 0.8335297
Rucheng Chenzhou 35575 4777.6 3816.6 343.00 1492.70 3756.0 11286 4492.20 2426.20 2993.7 1130 232.280 197.3 127.8 33.101 14.478 111.300 101.6 43 235.7 751.80 333.2 2079.963 1744.7 13755 0.7073830 0.8494059
Sangzhi Zhangjiajie 33916 3920.0 3081.3 248.06 1856.10 5615.4 14624 1694.80 2981.20 3405.9 1999 267.150 234.5 139.8 31.278 18.993 127.900 122.8 28 256.9 1803.30 385.5 3607.897 17585.8 45167 0.6664073 0.8777840
Shaodong Shaoyang 31507 16184.0 12214.0 771.60 2757.00 22898.0 25246 29175.00 7855.30 10089.0 3080 724.310 547.7 310.0 99.082 59.009 363.200 297.8 148 524.9 8576.30 912.1 5704.602 4679.3 18296 0.5754851 0.7561679
Shaoshan Xiangtan 33314 2626.4 4500.0 258.64 683.65 4956.8 55570 9717.60 1649.00 14916.0 488 73.652 62.0 33.6 5.910 3.201 27.147 30.4 48 57.7 1200.80 92.3 807.714 1630.3 24991 0.6251354 0.8417966
Shaoyang Shaoyang 31783 8345.7 7237.3 376.42 2780.80 9031.2 9653 7887.80 2425.30 3499.3 2294 655.130 569.2 385.0 71.417 43.525 265.400 247.9 65 657.6 3406.30 942.3 5518.299 21656.5 65431 0.6978669 0.8688352
Shimen Changde 33261 8334.4 10531.0 548.33 2178.80 16293.0 27137 17795.00 6026.50 6156.0 2502 392.050 329.6 193.8 29.245 26.104 190.600 184.7 122 399.2 6490.70 600.5 5266.510 6981.0 19275 0.6647794 0.8407091
Shuangfeng Loudi 33684 11455.0 7904.9 470.59 2657.50 15225.0 17755 16665.00 3914.00 5470.3 1862 559.470 488.5 309.6 57.966 41.575 244.300 245.6 129 644.6 4596.00 859.4 7943.893 2613.1 14994 0.7500582 0.8731478
Shuangpai Yongzhou 33302 1918.0 3572.0 226.37 881.96 3728.0 21942 2646.30 2056.40 4568.1 408 86.264 68.0 46.2 11.339 6.890 57.600 39.5 34 111.6 567.21 174.1 1866.171 9092.1 18479 0.6410109 0.7882778
Suining Shaoyang 31750 4307.1 3605.6 222.40 1442.50 5658.1 16069 11150.00 1566.40 4990.4 802 264.080 208.9 145.2 26.200 14.261 108.800 86.3 54 268.4 1761.50 352.9 2064.499 1413.5 15004 0.7605554 0.7910482
Taojiang Yiyang 35184 8657.4 8743.0 417.37 2276.80 15162.0 19509 18691.00 4656.80 8687.9 2504 472.800 411.4 194.0 48.047 33.234 260.900 208.8 153 504.5 5355.90 779.7 13878.842 13439.3 79214 0.6470437 0.8701354
Taoyuan Changde 31877 11948.0 8656.0 710.08 2625.10 19603.0 22879 16529.00 4735.00 7682.6 2070 558.770 479.9 300.5 44.429 36.630 274.500 272.0 60 607.4 8926.00 857.1 18328.460 50619.0 60461 0.7086688 0.8588507
Tongdao Huaihua 35400 2578.0 1525.4 165.59 1046.50 2653.2 12781 2514.50 1021.00 3696.4 882 144.490 105.5 82.2 15.913 7.776 57.900 49.7 27 154.2 749.14 208.1 981.418 644.2 12424 0.7409899 0.7301543
Wangcheng Changsha 45171 12122.0 48829.0 2285.50 3802.30 37488.0 70666 148976.00 10330.00 16495.0 1678 361.480 268.6 131.2 28.838 24.815 161.300 154.8 314 266.6 5623.30 533.4 5222.356 6648.6 27690 0.4998125 0.7430563
Wugang Shaoyang 30573 8618.0 7373.3 486.85 2346.00 9046.7 12112 5346.00 3852.00 4864.4 2250 483.620 402.5 271.4 70.758 43.683 243.900 194.0 52 502.9 3476.00 752.3 4613.704 1734.7 15126 0.6684833 0.8322650
Xiangtan Xiangtan 35274 15516.0 10121.0 1236.60 3050.10 22728.0 27060 30730.00 12217.00 10135.0 2022 610.490 551.9 347.1 46.556 54.439 248.590 239.1 187 574.0 4889.10 845.2 10688.618 40758.0 69261 0.6791292 0.9040279
Xiangxiang Xiangtan 33040 13298.0 9819.0 975.59 2942.80 23175.0 29361 39630.00 7900.30 9264.3 2451 586.400 492.0 324.5 47.743 39.612 231.710 242.0 189 535.7 5891.80 787.8 6852.943 17531.9 22297 0.6799949 0.8390177
Xiangyin Yueyang 33169 5466.1 14714.0 520.53 2318.80 23265.0 33983 49507.00 5222.90 8824.5 1550 466.110 357.4 169.4 44.878 30.752 193.800 190.1 152 408.4 4273.20 686.9 6000.857 8175.0 19945 0.5945552 0.7667718
Xinhua Loudi 32496 11075.0 6725.3 576.47 3599.50 14940.0 13398 12152.00 6121.20 3342.4 1542 755.140 677.9 451.6 96.224 57.670 316.600 313.8 112 817.4 5743.50 1116.7 6919.849 3145.3 13834 0.7319782 0.8977143
Xinhuang Huaihua 31891 2373.1 2075.8 170.11 1069.30 3782.1 15412 3405.70 1265.20 3935.6 1350 149.710 105.6 104.0 15.938 8.781 72.000 72.5 36 178.6 873.45 246.0 1130.322 785.8 12732 0.7260163 0.7053637
Xinning Shaoyang 32067 6126.3 5978.8 390.67 1830.60 6064.6 10732 3857.70 2899.90 3287.7 1565 429.840 347.8 227.4 41.633 22.005 171.400 146.5 38 383.6 1445.90 566.7 2415.712 2364.9 13031 0.6769014 0.8091383
Xinshao Shaoyang 34199 7344.2 8313.8 384.19 2229.20 8590.9 11514 10829.00 5042.50 3986.4 2319 577.690 477.4 265.8 68.131 39.034 234.100 213.0 79 533.6 2941.60 749.0 3003.553 5679.2 13778 0.7124166 0.8263948
Xintian Yongzhou 33609 3851.6 3401.9 250.31 1280.20 4776.4 14426 3357.40 1334.90 2894.9 1264 236.520 217.1 116.9 32.138 15.970 93.800 107.1 42 226.6 1329.80 332.1 2530.482 3134.3 16925 0.6823246 0.9178928
Xupu Huaihua 29873 8675.5 6112.2 457.86 2452.00 10314.0 13863 6720.40 4588.00 5453.1 2106 512.610 400.9 292.3 53.793 29.166 226.100 211.2 71 516.0 3427.30 745.9 4308.431 242.6 16558 0.6917817 0.7820760
Yanling Zhuzhou 34156 3274.4 5835.0 402.90 1038.20 4250.5 21021 5855.80 2379.60 3608.4 545 116.180 99.0 54.0 10.838 6.682 52.200 46.0 75 112.2 1241.20 202.2 971.500 6851.0 18652 0.5548961 0.8521260
Yizhang Chenzhou 33508 7938.8 9992.5 750.40 2142.60 10455.0 17814 19120.00 3697.00 3788.6 2280 348.830 279.9 128.8 63.564 25.843 161.300 142.5 169 363.4 5037.50 588.9 2521.831 3584.6 19318 0.6170827 0.8023966
Yongshun Hunan West 31234 4516.9 2464.1 220.66 2021.70 4142.1 9590 1061.00 2839.90 3963.1 1801 312.990 259.4 195.7 38.686 24.854 115.700 113.9 23 292.5 2247.30 433.3 2018.756 845.8 12650 0.6750519 0.8287805
Yongxing Chenzhou 33278 7097.6 13456.0 1294.50 2476.00 21382.0 37651 43700.00 3053.70 9856.1 1969 375.500 304.9 133.5 52.440 19.765 172.500 145.7 175 333.5 4807.10 562.6 3174.984 4540.5 19960 0.5927835 0.8119840
You Zhuzhou 36791 9541.6 15611.0 1533.30 2803.00 25153.0 36264 32738.00 6316.20 13021.0 1992 472.320 418.0 254.3 42.124 31.908 186.400 196.9 248 344.4 7654.80 693.7 6071.620 27443.0 22732 0.4964682 0.8849932
Yuanjiang Yiyang 33302 7763.4 10486.0 509.45 2381.20 17603.0 26258 21082.00 6464.20 10060.0 1856 401.220 335.2 203.3 34.220 27.932 260.300 161.0 105 366.7 5125.10 672.5 6644.520 5899.6 20552 0.5452788 0.8354519
Yuanling Huaihua 37762 5627.0 5466.8 677.40 2202.60 14156.0 24194 14835.00 2485.30 4265.4 2501 382.530 283.9 210.2 33.935 21.278 166.500 155.9 56 397.0 3092.50 586.5 2508.387 637.8 15165 0.6768968 0.7421640
Yueyang Yueyang 31470 4946.6 15084.0 330.43 1971.60 18974.0 26360 36800.00 3668.70 10377.0 1616 411.260 293.3 173.0 40.218 30.051 201.500 150.9 158 435.3 5836.60 721.7 11279.478 15381.2 88494 0.6031592 0.7131741
Zhijiang Huaihua 33582 4306.1 2802.2 356.96 1470.00 6992.5 20518 4491.80 2477.30 4535.1 935 223.750 175.5 116.1 23.693 14.726 101.800 94.3 56 247.6 2133.30 341.6 2576.470 90.8 13589 0.7248244 0.7843575
Zhongfang Huaihua 34941 1296.3 7594.6 348.83 1048.00 7332.1 30846 8608.50 968.44 6082.5 662 170.250 103.1 89.3 12.709 8.563 71.100 70.9 56 173.8 1064.90 238.3 2917.158 1938.6 37231 0.7293328 0.6055800
Zhuzhou Zhuzhou 37191 5395.6 4777.7 507.04 1408.80 7962.1 27589 6768.90 2970.20 11407.0 1032 210.380 177.0 106.0 12.436 11.824 92.200 85.2 82 210.6 2538.40 289.6 4236.370 10883.0 103228 0.7272099 0.8413347
Zixing Chenzhou 34163 7873.2 14891.0 1556.00 2679.00 22261.0 65706 51921.00 5014.70 10838.0 1159 261.980 177.9 76.0 22.316 14.760 114.000 86.3 155 140.2 4835.80 340.2 2761.164 3636.3 21600 0.4121105 0.6790595

The code chunk below will be used to update the attribute table of hunan’s SpatialPolygonsDataFrame with the attribute fields of hunan2012 dataframe. This is performed by using left_join() of dplyr package.

colnames(hunan)
[1] "NAME_2"     "ID_3"       "NAME_3"     "ENGTYPE_3"  "Shape_Leng"
[6] "Shape_Area" "County"     "geometry"  
colnames(hunan2012)
 [1] "County"      "City"        "avg_wage"    "deposite"    "FAI"        
 [6] "Gov_Rev"     "Gov_Exp"     "GDP"         "GDPPC"       "GIO"        
[11] "Loan"        "NIPCR"       "Bed"         "Emp"         "EmpR"       
[16] "EmpRT"       "Pri_Stu"     "Sec_Stu"     "Household"   "Household_R"
[21] "NOIP"        "Pop_R"       "RSCG"        "Pop_T"       "Agri"       
[26] "Service"     "Disp_Inc"    "RORP"        "ROREmp"     
hunan <- left_join(hunan,hunan2012)%>%
  select(1:4, 7, 15)

3.2 Visualising Data

4 Visualising Regional Development Indicator

Prepare a basemap and a choropleth map to show the distribution of GDPPC 2012 (GDP per capita) by using qtm() of tmap package.

basemap <- tm_shape(hunan) +
  tm_polygons() +
  tm_text("NAME_3", 
          size=0.3)+
  tm_layout(bg.color = "#E4D5C9",
            frame = F)

gdppc <- qtm(hunan, "GDPPC") +
  tm_layout(bg.color = "#E4D5C9",
            frame = F,
            legend.title.size = 0.8,
            legend.text.size = 0.5)

tmap_arrange(basemap, gdppc, asp=1, ncol=2)

5 Computing Contiguity/Adjacency Spatial Weights

Contiguity-based

  • Share common boundary (rooks, bishops, queen’s/king’s case)
  • Measured using poly2nb() of spdep package
  • Constructs neighbours list from polygon list
  • This function builds a neighbours list based on regions with contiguous boundaries.
  • What this function does is to build a neighbours list based on regions with contiguous boundaries - which means they’re in ‘contact’ and share one or more boundary points. For example, on the Singapore map, the North-East region is contiguous with the North region and the East region.
  • One thing to note is the queen argument:
    • TRUE (default): a single shared boundary point meets the contiguity condition
    • FALSE: more than one shared point is required
    • note that more than one shared boundary point does not necessarily mean a shared boundary line

Distance-based - Within threshold distance to the nucleus - Measured using knn2nb()

5.1 Computing contiguity based neighbours

We’ll be computing based on two types of contiguity: Queen and Rook. If you’re a chess buff (or even a chess amateur), these terms will sound familiar to you: and as you might’ve guessed, they’re related to the positions the queen and rook can move on a chess board.

::: panel-tabset ## Queen

The code chunk below is used to compute Queen contiguity weight matrix.

wm_q <- poly2nb(hunan, queen=TRUE)
summary(wm_q)
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 448 
Percentage nonzero weights: 5.785124 
Average number of links: 5.090909 
Link number distribution:

 1  2  3  4  5  6  7  8  9 11 
 2  2 12 16 24 14 11  4  2  1 
2 least connected regions:
30 65 with 1 link
1 most connected region:
85 with 11 links

Rook

The code chunk below is used to compute Rook contiguity weight matrix.

wm_r <- poly2nb(hunan, queen=FALSE)
summary(wm_r)
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 440 
Percentage nonzero weights: 5.681818 
Average number of links: 5 
Link number distribution:

 1  2  3  4  5  6  7  8  9 10 
 2  2 12 20 21 14 11  3  2  1 
2 least connected regions:
30 65 with 1 link
1 most connected region:
85 with 10 links

5.1.1 Display Neighbours for Specific County

For each polygon in our polygon object, wm_q() lists all neighboring polygons. For example, to see the neighbors for the first polygon in the object, type:

wm_q[[1]]
[1]  2  3  4 57 85
wm_r[[1]]
[1]  3  4 57 85

Polygon 1 has 5 neighbors. The numbers represent the polygon IDs as stored in Hunan SpatialPolygonsDataFrame class.

5.1.2 Retrieve County Name of Polygon ID=1

hunan$County[1]
[1] "Anxiang"

The output reveals that Polygon ID=1 is Anxiang county.

5.1.3 Reveal county names of neighboring polygons

hunan$NAME_3[c(2,3,4,57,85)]
[1] "Hanshou" "Jinshi"  "Li"      "Nan"     "Taoyuan"

5.1.4 Retrieve GDPCC

We can retrieve the GDPPC of these five counties by using the code chunk below.

nb1 <- wm_q[[1]]
nb1 <- hunan$GDPPC[nb1]
nb1
[1] 20981 34592 24473 21311 22879
nb2 <- wm_r[[1]]
nb2 <- hunan$GDPPC[nb2]
nb2
[1] 34592 24473 21311 22879

5.1.5 Display the complete weight matrix

The printed output above shows that the GDPPC of the five nearest neighbours based on Queen’s method are 20981, 34592, 24473, 21311 and 22879 respectively.

You can display the complete weight matrix by using str().

str(wm_q)
List of 88
 $ : int [1:5] 2 3 4 57 85
 $ : int [1:5] 1 57 58 78 85
 $ : int [1:4] 1 4 5 85
 $ : int [1:4] 1 3 5 6
 $ : int [1:4] 3 4 6 85
 $ : int [1:5] 4 5 69 75 85
 $ : int [1:4] 67 71 74 84
 $ : int [1:7] 9 46 47 56 78 80 86
 $ : int [1:6] 8 66 68 78 84 86
 $ : int [1:8] 16 17 19 20 22 70 72 73
 $ : int [1:3] 14 17 72
 $ : int [1:5] 13 60 61 63 83
 $ : int [1:4] 12 15 60 83
 $ : int [1:3] 11 15 17
 $ : int [1:4] 13 14 17 83
 $ : int [1:5] 10 17 22 72 83
 $ : int [1:7] 10 11 14 15 16 72 83
 $ : int [1:5] 20 22 23 77 83
 $ : int [1:6] 10 20 21 73 74 86
 $ : int [1:7] 10 18 19 21 22 23 82
 $ : int [1:5] 19 20 35 82 86
 $ : int [1:5] 10 16 18 20 83
 $ : int [1:7] 18 20 38 41 77 79 82
 $ : int [1:5] 25 28 31 32 54
 $ : int [1:5] 24 28 31 33 81
 $ : int [1:4] 27 33 42 81
 $ : int [1:3] 26 29 42
 $ : int [1:5] 24 25 33 49 54
 $ : int [1:3] 27 37 42
 $ : int 33
 $ : int [1:8] 24 25 32 36 39 40 56 81
 $ : int [1:8] 24 31 50 54 55 56 75 85
 $ : int [1:5] 25 26 28 30 81
 $ : int [1:3] 36 45 80
 $ : int [1:6] 21 41 47 80 82 86
 $ : int [1:6] 31 34 40 45 56 80
 $ : int [1:4] 29 42 43 44
 $ : int [1:4] 23 44 77 79
 $ : int [1:5] 31 40 42 43 81
 $ : int [1:6] 31 36 39 43 45 79
 $ : int [1:6] 23 35 45 79 80 82
 $ : int [1:7] 26 27 29 37 39 43 81
 $ : int [1:6] 37 39 40 42 44 79
 $ : int [1:4] 37 38 43 79
 $ : int [1:6] 34 36 40 41 79 80
 $ : int [1:3] 8 47 86
 $ : int [1:5] 8 35 46 80 86
 $ : int [1:5] 50 51 52 53 55
 $ : int [1:4] 28 51 52 54
 $ : int [1:5] 32 48 52 54 55
 $ : int [1:3] 48 49 52
 $ : int [1:5] 48 49 50 51 54
 $ : int [1:3] 48 55 75
 $ : int [1:6] 24 28 32 49 50 52
 $ : int [1:5] 32 48 50 53 75
 $ : int [1:7] 8 31 32 36 78 80 85
 $ : int [1:6] 1 2 58 64 76 85
 $ : int [1:5] 2 57 68 76 78
 $ : int [1:4] 60 61 87 88
 $ : int [1:4] 12 13 59 61
 $ : int [1:7] 12 59 60 62 63 77 87
 $ : int [1:3] 61 77 87
 $ : int [1:4] 12 61 77 83
 $ : int [1:2] 57 76
 $ : int 76
 $ : int [1:5] 9 67 68 76 84
 $ : int [1:4] 7 66 76 84
 $ : int [1:5] 9 58 66 76 78
 $ : int [1:3] 6 75 85
 $ : int [1:3] 10 72 73
 $ : int [1:3] 7 73 74
 $ : int [1:5] 10 11 16 17 70
 $ : int [1:5] 10 19 70 71 74
 $ : int [1:6] 7 19 71 73 84 86
 $ : int [1:6] 6 32 53 55 69 85
 $ : int [1:7] 57 58 64 65 66 67 68
 $ : int [1:7] 18 23 38 61 62 63 83
 $ : int [1:7] 2 8 9 56 58 68 85
 $ : int [1:7] 23 38 40 41 43 44 45
 $ : int [1:8] 8 34 35 36 41 45 47 56
 $ : int [1:6] 25 26 31 33 39 42
 $ : int [1:5] 20 21 23 35 41
 $ : int [1:9] 12 13 15 16 17 18 22 63 77
 $ : int [1:6] 7 9 66 67 74 86
 $ : int [1:11] 1 2 3 5 6 32 56 57 69 75 ...
 $ : int [1:9] 8 9 19 21 35 46 47 74 84
 $ : int [1:4] 59 61 62 88
 $ : int [1:2] 59 87
 - attr(*, "class")= chr "nb"
 - attr(*, "region.id")= chr [1:88] "1" "2" "3" "4" ...
 - attr(*, "call")= language poly2nb(pl = hunan, queen = TRUE)
 - attr(*, "type")= chr "queen"
 - attr(*, "sym")= logi TRUE

5.2 Visualising contiguity weights

  • A connectivity graph takes a point and displays a line to each neighboring point.
  • As we are working with polygons, we need to get points in order to make the connectivity graphs.
  • Most typical method: polygon centroids
    • Calculate these in the sf package before moving onto the graphs
    • Getting Latitude and Longitude of Polygon Centroids
    • It is a little more complicated than just running st_centroid on the sf object as we need to get the coordinates in a separate data frame..
  • Mapping function: applies a given function to each element of a vector and returns a vector of the same length.
  • input vector: will be the geometry column of us.bound.
  • function will be st_centroid.
  • Use map_dbl variation of map from the purrr package

5.2.1 Get Long & Lat

  • To get our longitude values, we map the st_centroid function over the geometry column of us.bound and access the longitude value through double bracket notation [[]] and 1.
  • Allows us to get only the longitude, which is the first value in each centroid.
longitude <- map_dbl(hunan$geometry, ~st_centroid(.x)[[1]])
  • Do the same for latitude with one key difference.
  • We access the second value per each centroid with [[2]]
latitude <- map_dbl(hunan$geometry, ~st_centroid(.x)[[2]])

cbind() to put longitude and latitude into the same object.

coords <- cbind(longitude, latitude)

We check the first few observations to see if things are formatted correctly.

head(coords)
     longitude latitude
[1,]  112.1531 29.44362
[2,]  112.0372 28.86489
[3,]  111.8917 29.47107
[4,]  111.7031 29.74499
[5,]  111.6138 29.49258
[6,]  111.0341 29.79863

5.2.2 Plotting contiguity-based neighbours map

Show the code
par(bg = '#E4D5C9')

par(mfrow=c(1,2))

plot(hunan$geometry, 
     border="#b89c8c", 
     main="Queen Contiguity")
plot(wm_q, 
     coords, 
     pch = 19, 
     cex = 0.6, 
     add = TRUE, 
     col= "red")

plot(hunan$geometry, 
     border="#b89c8c", 
     main="Rook Contiguity")
plot(wm_r, 
     coords, 
     pch = 19, 
     cex = 0.6, 
     add = TRUE, 
     col = "red")

6 Computing distance-based neighbours

  • Derive distance-based weight matrices by using dnearneigh() of spdep package
  • The function identifies neighbours of region points by Euclidean distance with a distance band with lower d1= and upper d2= bounds controlled by the bounds= argument.
  • If unprojected coordinates are used and either specified in the coordinates object x or with x as a two column matrix and longlat=TRUE, great circle distances in km will be calculated assuming the WGS84 reference ellipsoid.

6.2.1 Determine the cut-off distance / upper limit

To determine the upper limit for distance band:

Step Task Function
1 Return a matrix with the indices of points belonging to the set of the k nearest neighbours of each other. knearneigh() of spdep
2 Convert the knn object into a neighbours list of class nb + list of integer vectors containing neighbour region number ids. knn2nb()
3

Return the length of neighbour relationship edges.

The function returns in the units of the coordinates if the coordinates are projected, in km otherwise.

nbdists() of spdep
4 Remove the list structure of the returned object unlist()
  • knearneigh() of spdep: returns a matrix with the indices of points belonging to the set of the k nearest neighbours of each other.
  • knn2nb(): converts the knn object returned by knearneigh() into a neighbours list (class nb) with a list of integer vectors containing neighbour region number ids.
knearneigh(coords)
$nn
      [,1]
 [1,]    3
 [2,]   78
 [3,]    1
 [4,]    5
 [5,]    4
 [6,]   69
 [7,]   67
 [8,]   46
 [9,]   84
[10,]   70
[11,]   72
[12,]   63
[13,]   12
[14,]   17
[15,]   13
[16,]   22
[17,]   16
[18,]   20
[19,]   21
[20,]   82
[21,]   19
[22,]   16
[23,]   41
[24,]   54
[25,]   81
[26,]   81
[27,]   29
[28,]   49
[29,]   27
[30,]   33
[31,]   24
[32,]   50
[33,]   28
[34,]   45
[35,]   47
[36,]   34
[37,]   42
[38,]   44
[39,]   43
[40,]   39
[41,]   23
[42,]   37
[43,]   44
[44,]   43
[45,]   34
[46,]   47
[47,]   46
[48,]   51
[49,]   28
[50,]   52
[51,]   48
[52,]   54
[53,]   55
[54,]   52
[55,]   50
[56,]   36
[57,]   58
[58,]   57
[59,]   87
[60,]   13
[61,]   63
[62,]   61
[63,]   12
[64,]   57
[65,]   76
[66,]   68
[67,]    7
[68,]   66
[69,]    6
[70,]   10
[71,]   74
[72,]   11
[73,]   70
[74,]   71
[75,]   55
[76,]   65
[77,]   38
[78,]    2
[79,]   45
[80,]   34
[81,]   25
[82,]   21
[83,]   12
[84,]    9
[85,]    5
[86,]   74
[87,]   61
[88,]   87

$np
[1] 88

$k
[1] 1

$dimension
[1] 2

$x
      longitude latitude
 [1,]  112.1531 29.44362
 [2,]  112.0372 28.86489
 [3,]  111.8917 29.47107
 [4,]  111.7031 29.74499
 [5,]  111.6138 29.49258
 [6,]  111.0341 29.79863
 [7,]  113.7065 28.23215
 [8,]  112.3460 28.13081
 [9,]  112.8169 28.28918
[10,]  113.3534 26.57906
[11,]  113.8942 25.98122
[12,]  112.4006 25.63215
[13,]  112.5542 25.33880
[14,]  113.6636 25.54967
[15,]  112.9206 25.26722
[16,]  113.1883 26.21248
[17,]  113.4521 25.93480
[18,]  112.4209 26.36132
[19,]  113.0152 27.08120
[20,]  112.6350 26.75969
[21,]  112.7087 27.27930
[22,]  112.9095 26.42079
[23,]  111.9522 26.80117
[24,]  110.2606 27.89384
[25,]  110.0921 27.54115
[26,]  109.7985 26.91321
[27,]  109.5765 26.54507
[28,]  109.7211 27.78801
[29,]  109.7339 26.21157
[30,]  109.1537 27.22941
[31,]  110.6442 27.83407
[32,]  110.5916 28.57282
[33,]  109.5984 27.39828
[34,]  111.4783 27.67997
[35,]  112.1745 27.46256
[36,]  111.2315 27.86930
[37,]  110.3149 26.32113
[38,]  111.3248 26.48991
[39,]  110.5859 27.10164
[40,]  110.9593 27.34884
[41,]  111.8296 27.18765
[42,]  110.1926 26.70972
[43,]  110.7334 26.78494
[44,]  110.9123 26.54354
[45,]  111.4599 27.42910
[46,]  112.5268 27.92456
[47,]  112.3406 27.77407
[48,]  109.5602 28.66808
[49,]  109.5071 28.01142
[50,]  109.9954 28.60033
[51,]  109.4273 28.42749
[52,]  109.7587 28.31518
[53,]  109.5044 29.21940
[54,]  109.9899 28.16053
[55,]  109.9664 29.01206
[56,]  111.3785 28.28449
[57,]  112.4350 29.23817
[58,]  112.5558 28.97135
[59,]  111.7379 24.97087
[60,]  112.1831 25.31559
[61,]  111.9743 25.65101
[62,]  111.7009 25.91101
[63,]  112.2196 25.88615
[64,]  112.6472 29.48614
[65,]  113.5102 29.49285
[66,]  113.1172 28.79707
[67,]  113.7089 28.76024
[68,]  112.7963 28.71653
[69,]  110.9276 29.39439
[70,]  113.6420 26.80361
[71,]  113.4577 27.66123
[72,]  113.8404 26.37989
[73,]  113.4758 27.17064
[74,]  113.1428 27.62875
[75,]  110.3017 29.39053
[76,]  113.1957 29.25343
[77,]  111.7410 26.36035
[78,]  112.1831 28.49854
[79,]  111.3390 27.01465
[80,]  111.8208 27.75124
[81,]  110.0753 27.23539
[82,]  112.3965 27.08323
[83,]  112.7683 25.82828
[84,]  113.1679 28.30074
[85,]  111.4495 28.95406
[86,]  112.7956 27.68910
[87,]  111.5896 25.49530
[88,]  111.2393 25.19355

attr(,"class")
[1] "knn"
attr(,"call")
knearneigh(x = coords)
k1 <- knn2nb(knearneigh(coords))
k1
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 88 
Percentage nonzero weights: 1.136364 
Average number of links: 1 
25 disjoint connected subgraphs
Non-symmetric neighbours list
  • nbdists() of spdep: calculates the length of neighbour relationship edges (ie distance between neighboring regions). Returns units of the coordinates if the coordinates are projected, in km otherwise.
  • unlist(): removes the list structure of the returned object by using .
nbdists(k1, coords, longlat = TRUE)
[[1]]
[1] 25.53398

[[2]]
[1] 43.03114

[[3]]
[1] 25.53398

[[4]]
[1] 29.2848

[[5]]
[1] 29.2848

[[6]]
[1] 45.98097

[[7]]
[1] 58.52704

[[8]]
[1] 28.95985

[[9]]
[1] 34.45062

[[10]]
[1] 37.99885

[[11]]
[1] 44.49442

[[12]]
[1] 33.48816

[[13]]
[1] 35.98123

[[14]]
[1] 47.65184

[[15]]
[1] 37.73556

[[16]]
[1] 36.16613

[[17]]
[1] 40.53569

[[18]]
[1] 49.02492

[[19]]
[1] 37.47543

[[20]]
[1] 42.97316

[[21]]
[1] 37.47543

[[22]]
[1] 36.16613

[[23]]
[1] 44.51898

[[24]]
[1] 39.7744

[[25]]
[1] 33.9218

[[26]]
[1] 45.03425

[[27]]
[1] 40.15056

[[28]]
[1] 32.50795

[[29]]
[1] 40.15056

[[30]]
[1] 47.83345

[[31]]
[1] 38.35439

[[32]]
[1] 58.39365

[[33]]
[1] 44.85211

[[34]]
[1] 27.85864

[[35]]
[1] 38.2151

[[36]]
[1] 32.12293

[[37]]
[1] 44.74688

[[38]]
[1] 41.53815

[[39]]
[1] 38.02669

[[40]]
[1] 46.029

[[41]]
[1] 44.51898

[[42]]
[1] 44.74688

[[43]]
[1] 32.1334

[[44]]
[1] 32.1334

[[45]]
[1] 27.85864

[[46]]
[1] 24.79082

[[47]]
[1] 24.79082

[[48]]
[1] 29.66852

[[49]]
[1] 32.50795

[[50]]
[1] 39.19375

[[51]]
[1] 29.66852

[[52]]
[1] 28.43598

[[53]]
[1] 50.50645

[[54]]
[1] 28.43598

[[55]]
[1] 45.721

[[56]]
[1] 48.22649

[[57]]
[1] 31.82332

[[58]]
[1] 31.82332

[[59]]
[1] 59.98421

[[60]]
[1] 37.44866

[[61]]
[1] 35.83248

[[62]]
[1] 39.77577

[[63]]
[1] 33.48816

[[64]]
[1] 34.34758

[[65]]
[1] 40.45791

[[66]]
[1] 32.58547

[[67]]
[1] 58.52704

[[68]]
[1] 32.58547

[[69]]
[1] 45.98097

[[70]]
[1] 37.99885

[[71]]
[1] 31.27538

[[72]]
[1] 44.49442

[[73]]
[1] 43.88878

[[74]]
[1] 31.27538

[[75]]
[1] 53.12656

[[76]]
[1] 40.45791

[[77]]
[1] 43.93382

[[78]]
[1] 43.03114

[[79]]
[1] 47.45858

[[80]]
[1] 34.68711

[[81]]
[1] 33.9218

[[82]]
[1] 37.80739

[[83]]
[1] 42.81869

[[84]]
[1] 34.45062

[[85]]
[1] 61.79116

[[86]]
[1] 34.90929

[[87]]
[1] 42.32891

[[88]]
[1] 48.59005

attr(,"class")
[1] "nbdist"
attr(,"call")
nbdists(nb = k1, coords = coords, longlat = TRUE)
k1dists <- unlist(nbdists(k1, coords, longlat = TRUE))
k1dists
 [1] 25.53398 43.03114 25.53398 29.28480 29.28480 45.98097 58.52704 28.95985
 [9] 34.45062 37.99885 44.49442 33.48816 35.98123 47.65184 37.73556 36.16613
[17] 40.53569 49.02492 37.47543 42.97316 37.47543 36.16613 44.51898 39.77440
[25] 33.92180 45.03425 40.15056 32.50795 40.15056 47.83345 38.35439 58.39365
[33] 44.85211 27.85864 38.21510 32.12293 44.74688 41.53815 38.02669 46.02900
[41] 44.51898 44.74688 32.13340 32.13340 27.85864 24.79082 24.79082 29.66852
[49] 32.50795 39.19375 29.66852 28.43598 50.50645 28.43598 45.72100 48.22649
[57] 31.82332 31.82332 59.98421 37.44866 35.83248 39.77577 33.48816 34.34758
[65] 40.45791 32.58547 58.52704 32.58547 45.98097 37.99885 31.27538 44.49442
[73] 43.88878 31.27538 53.12656 40.45791 43.93382 43.03114 47.45858 34.68711
[81] 33.92180 37.80739 42.81869 34.45062 61.79116 34.90929 42.32891 48.59005
summary(k1dists)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  24.79   32.57   38.01   39.07   44.52   61.79 

6.2.2 Computing fixed distance weight matrix

dnearneigh() is used to compute the distance weight matrix.

wm_d62 <- dnearneigh(coords, 0, 62, longlat = TRUE)
wm_d62
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 324 
Percentage nonzero weights: 4.183884 
Average number of links: 3.681818 

Interpretation

88 regions in Hunan, 324 links amongst regions of distance up to 62km, Avg of 3-4 neighbours within this distance

:::

To display the structure of wm_d62 weight matrix:

str(wm_d62)
List of 88
 $ : int [1:5] 3 4 5 57 64
 $ : int [1:4] 57 58 78 85
 $ : int [1:4] 1 4 5 57
 $ : int [1:3] 1 3 5
 $ : int [1:4] 1 3 4 85
 $ : int 69
 $ : int [1:2] 67 84
 $ : int [1:4] 9 46 47 78
 $ : int [1:4] 8 46 68 84
 $ : int [1:4] 16 22 70 72
 $ : int [1:3] 14 17 72
 $ : int [1:5] 13 60 61 63 83
 $ : int [1:4] 12 15 60 83
 $ : int [1:2] 11 17
 $ : int 13
 $ : int [1:4] 10 17 22 83
 $ : int [1:3] 11 14 16
 $ : int [1:3] 20 22 63
 $ : int [1:5] 20 21 73 74 82
 $ : int [1:5] 18 19 21 22 82
 $ : int [1:6] 19 20 35 74 82 86
 $ : int [1:4] 10 16 18 20
 $ : int [1:3] 41 77 82
 $ : int [1:4] 25 28 31 54
 $ : int [1:4] 24 28 33 81
 $ : int [1:4] 27 33 42 81
 $ : int [1:2] 26 29
 $ : int [1:6] 24 25 33 49 52 54
 $ : int [1:2] 27 37
 $ : int 33
 $ : int [1:2] 24 36
 $ : int 50
 $ : int [1:5] 25 26 28 30 81
 $ : int [1:3] 36 45 80
 $ : int [1:6] 21 41 46 47 80 82
 $ : int [1:5] 31 34 45 56 80
 $ : int [1:2] 29 42
 $ : int [1:3] 44 77 79
 $ : int [1:4] 40 42 43 81
 $ : int [1:3] 39 45 79
 $ : int [1:5] 23 35 45 79 82
 $ : int [1:5] 26 37 39 43 81
 $ : int [1:3] 39 42 44
 $ : int [1:2] 38 43
 $ : int [1:6] 34 36 40 41 79 80
 $ : int [1:5] 8 9 35 47 86
 $ : int [1:5] 8 35 46 80 86
 $ : int [1:5] 50 51 52 53 55
 $ : int [1:4] 28 51 52 54
 $ : int [1:6] 32 48 51 52 54 55
 $ : int [1:4] 48 49 50 52
 $ : int [1:6] 28 48 49 50 51 54
 $ : int [1:2] 48 55
 $ : int [1:5] 24 28 49 50 52
 $ : int [1:4] 48 50 53 75
 $ : int 36
 $ : int [1:5] 1 2 3 58 64
 $ : int [1:5] 2 57 64 66 68
 $ : int [1:3] 60 87 88
 $ : int [1:4] 12 13 59 61
 $ : int [1:5] 12 60 62 63 87
 $ : int [1:4] 61 63 77 87
 $ : int [1:5] 12 18 61 62 83
 $ : int [1:4] 1 57 58 76
 $ : int 76
 $ : int [1:5] 58 67 68 76 84
 $ : int [1:2] 7 66
 $ : int [1:4] 9 58 66 84
 $ : int [1:2] 6 75
 $ : int [1:3] 10 72 73
 $ : int [1:2] 73 74
 $ : int [1:3] 10 11 70
 $ : int [1:4] 19 70 71 74
 $ : int [1:5] 19 21 71 73 86
 $ : int [1:2] 55 69
 $ : int [1:3] 64 65 66
 $ : int [1:3] 23 38 62
 $ : int [1:2] 2 8
 $ : int [1:4] 38 40 41 45
 $ : int [1:5] 34 35 36 45 47
 $ : int [1:5] 25 26 33 39 42
 $ : int [1:6] 19 20 21 23 35 41
 $ : int [1:4] 12 13 16 63
 $ : int [1:4] 7 9 66 68
 $ : int [1:2] 2 5
 $ : int [1:4] 21 46 47 74
 $ : int [1:4] 59 61 62 88
 $ : int [1:2] 59 87
 - attr(*, "class")= chr "nb"
 - attr(*, "region.id")= chr [1:88] "1" "2" "3" "4" ...
 - attr(*, "call")= language dnearneigh(x = coords, d1 = 0, d2 = 62, longlat = TRUE)
 - attr(*, "dnn")= num [1:2] 0 62
 - attr(*, "bounds")= chr [1:2] "GE" "LE"
 - attr(*, "nbtype")= chr "distance"
 - attr(*, "sym")= logi TRUE

Another way to display the structure of the weight matrix is to combine table() and card() of spdep.

table(hunan$County, 
       # list number of neighbours for each area
      card(wm_d62))
               
                1 2 3 4 5 6
  Anhua         1 0 0 0 0 0
  Anren         0 0 0 1 0 0
  Anxiang       0 0 0 0 1 0
  Baojing       0 0 0 0 1 0
  Chaling       0 0 1 0 0 0
  Changning     0 0 1 0 0 0
  Changsha      0 0 0 1 0 0
  Chengbu       0 1 0 0 0 0
  Chenxi        0 0 0 1 0 0
  Cili          0 1 0 0 0 0
  Dao           0 0 0 1 0 0
  Dongan        0 0 1 0 0 0
  Dongkou       0 0 0 1 0 0
  Fenghuang     0 0 0 1 0 0
  Guidong       0 0 1 0 0 0
  Guiyang       0 0 0 1 0 0
  Guzhang       0 0 0 0 0 1
  Hanshou       0 0 0 1 0 0
  Hengdong      0 0 0 0 1 0
  Hengnan       0 0 0 0 1 0
  Hengshan      0 0 0 0 0 1
  Hengyang      0 0 0 0 0 1
  Hongjiang     0 0 0 0 1 0
  Huarong       0 0 0 1 0 0
  Huayuan       0 0 0 1 0 0
  Huitong       0 0 0 1 0 0
  Jiahe         0 0 0 0 1 0
  Jianghua      0 0 1 0 0 0
  Jiangyong     0 1 0 0 0 0
  Jingzhou      0 1 0 0 0 0
  Jinshi        0 0 0 1 0 0
  Jishou        0 0 0 0 0 1
  Lanshan       0 0 0 1 0 0
  Leiyang       0 0 0 1 0 0
  Lengshuijiang 0 0 1 0 0 0
  Li            0 0 1 0 0 0
  Lianyuan      0 0 0 0 1 0
  Liling        0 1 0 0 0 0
  Linli         0 0 0 1 0 0
  Linwu         0 0 0 1 0 0
  Linxiang      1 0 0 0 0 0
  Liuyang       0 1 0 0 0 0
  Longhui       0 0 1 0 0 0
  Longshan      0 1 0 0 0 0
  Luxi          0 0 0 0 1 0
  Mayang        0 0 0 0 0 1
  Miluo         0 0 0 0 1 0
  Nan           0 0 0 0 1 0
  Ningxiang     0 0 0 1 0 0
  Ningyuan      0 0 0 0 1 0
  Pingjiang     0 1 0 0 0 0
  Qidong        0 0 1 0 0 0
  Qiyang        0 0 1 0 0 0
  Rucheng       0 1 0 0 0 0
  Sangzhi       0 1 0 0 0 0
  Shaodong      0 0 0 0 1 0
  Shaoshan      0 0 0 0 1 0
  Shaoyang      0 0 0 1 0 0
  Shimen        1 0 0 0 0 0
  Shuangfeng    0 0 0 0 0 1
  Shuangpai     0 0 0 1 0 0
  Suining       0 0 0 0 1 0
  Taojiang      0 1 0 0 0 0
  Taoyuan       0 1 0 0 0 0
  Tongdao       0 1 0 0 0 0
  Wangcheng     0 0 0 1 0 0
  Wugang        0 0 1 0 0 0
  Xiangtan      0 0 0 1 0 0
  Xiangxiang    0 0 0 0 1 0
  Xiangyin      0 0 0 1 0 0
  Xinhua        0 0 0 0 1 0
  Xinhuang      1 0 0 0 0 0
  Xinning       0 1 0 0 0 0
  Xinshao       0 0 0 0 0 1
  Xintian       0 0 0 0 1 0
  Xupu          0 1 0 0 0 0
  Yanling       0 0 1 0 0 0
  Yizhang       1 0 0 0 0 0
  Yongshun      0 0 0 1 0 0
  Yongxing      0 0 0 1 0 0
  You           0 0 0 1 0 0
  Yuanjiang     0 0 0 0 1 0
  Yuanling      1 0 0 0 0 0
  Yueyang       0 0 1 0 0 0
  Zhijiang      0 0 0 0 1 0
  Zhongfang     0 0 0 1 0 0
  Zhuzhou       0 0 0 0 1 0
  Zixing        0 0 1 0 0 0

n.comp.nb() finds the number of disjoint connected subgraphs in the graph depicted by nb.obj() - a spatial neighbours list object.

n_comp <- n.comp.nb(wm_d62)
n_comp$nc
[1] 1
table(n_comp$comp.id)

 1 
88 

6.2.3 Plotting fixed distance weight matrix

The red lines show the links of 1st nearest neighbours and the black lines show the links of neighbours within the cut-off distance of 62km.

par(bg = '#E4D5C9')
par(mfrow=c(1,2))

plot(hunan$geometry, 
     border="#b89c8c", 
     main="1st nearest neighbours")
plot(k1, 
     coords, 
     add=TRUE, 
     col="#909690",
     pch = 19,
     length=0.6)

plot(hunan$geometry, 
     border="#b89c8c", 
     main="Distance link")
plot(wm_d62, 
     coords, 
     add=TRUE, 
     col="#bd5f5a", 
     pch = 19, 
     cex = 0.6)

6.2.4 Computing adaptive distance weight matrix

  • For fixed distance weight matrix, more densely settled areas (usually urban areas) tend to have more neighbours and the less densely settled areas (usually rural counties) tend to have lesser neighbours.

  • Having many neighbours smoothes the neighbour relationship across more neighbours.

  • It is possible to control the numbers of neighbours directly using k-nearest neighbours (where k=n as specified), either accepting asymmetric neighbours or imposing symmetry.
knn6 <- knn2nb(knearneigh(coords, k=6))
knn6
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 528 
Percentage nonzero weights: 6.818182 
Average number of links: 6 
Non-symmetric neighbours list

Similarly, we can display the content of the matrix by using str().

str(knn6)
List of 88
 $ : int [1:6] 2 3 4 5 57 64
 $ : int [1:6] 1 3 57 58 78 85
 $ : int [1:6] 1 2 4 5 57 85
 $ : int [1:6] 1 3 5 6 69 85
 $ : int [1:6] 1 3 4 6 69 85
 $ : int [1:6] 3 4 5 69 75 85
 $ : int [1:6] 9 66 67 71 74 84
 $ : int [1:6] 9 46 47 78 80 86
 $ : int [1:6] 8 46 66 68 84 86
 $ : int [1:6] 16 19 22 70 72 73
 $ : int [1:6] 10 14 16 17 70 72
 $ : int [1:6] 13 15 60 61 63 83
 $ : int [1:6] 12 15 60 61 63 83
 $ : int [1:6] 11 15 16 17 72 83
 $ : int [1:6] 12 13 14 17 60 83
 $ : int [1:6] 10 11 17 22 72 83
 $ : int [1:6] 10 11 14 16 72 83
 $ : int [1:6] 20 22 23 63 77 83
 $ : int [1:6] 10 20 21 73 74 82
 $ : int [1:6] 18 19 21 22 23 82
 $ : int [1:6] 19 20 35 74 82 86
 $ : int [1:6] 10 16 18 19 20 83
 $ : int [1:6] 18 20 41 77 79 82
 $ : int [1:6] 25 28 31 52 54 81
 $ : int [1:6] 24 28 31 33 54 81
 $ : int [1:6] 25 27 29 33 42 81
 $ : int [1:6] 26 29 30 37 42 81
 $ : int [1:6] 24 25 33 49 52 54
 $ : int [1:6] 26 27 37 42 43 81
 $ : int [1:6] 26 27 28 33 49 81
 $ : int [1:6] 24 25 36 39 40 54
 $ : int [1:6] 24 31 50 54 55 56
 $ : int [1:6] 25 26 28 30 49 81
 $ : int [1:6] 36 40 41 45 56 80
 $ : int [1:6] 21 41 46 47 80 82
 $ : int [1:6] 31 34 40 45 56 80
 $ : int [1:6] 26 27 29 42 43 44
 $ : int [1:6] 23 43 44 62 77 79
 $ : int [1:6] 25 40 42 43 44 81
 $ : int [1:6] 31 36 39 43 45 79
 $ : int [1:6] 23 35 45 79 80 82
 $ : int [1:6] 26 27 37 39 43 81
 $ : int [1:6] 37 39 40 42 44 79
 $ : int [1:6] 37 38 39 42 43 79
 $ : int [1:6] 34 36 40 41 79 80
 $ : int [1:6] 8 9 35 47 78 86
 $ : int [1:6] 8 21 35 46 80 86
 $ : int [1:6] 49 50 51 52 53 55
 $ : int [1:6] 28 33 48 51 52 54
 $ : int [1:6] 32 48 51 52 54 55
 $ : int [1:6] 28 48 49 50 52 54
 $ : int [1:6] 28 48 49 50 51 54
 $ : int [1:6] 48 50 51 52 55 75
 $ : int [1:6] 24 28 49 50 51 52
 $ : int [1:6] 32 48 50 52 53 75
 $ : int [1:6] 32 34 36 78 80 85
 $ : int [1:6] 1 2 3 58 64 68
 $ : int [1:6] 2 57 64 66 68 78
 $ : int [1:6] 12 13 60 61 87 88
 $ : int [1:6] 12 13 59 61 63 87
 $ : int [1:6] 12 13 60 62 63 87
 $ : int [1:6] 12 38 61 63 77 87
 $ : int [1:6] 12 18 60 61 62 83
 $ : int [1:6] 1 3 57 58 68 76
 $ : int [1:6] 58 64 66 67 68 76
 $ : int [1:6] 9 58 67 68 76 84
 $ : int [1:6] 7 65 66 68 76 84
 $ : int [1:6] 9 57 58 66 78 84
 $ : int [1:6] 4 5 6 32 75 85
 $ : int [1:6] 10 16 19 22 72 73
 $ : int [1:6] 7 19 73 74 84 86
 $ : int [1:6] 10 11 14 16 17 70
 $ : int [1:6] 10 19 21 70 71 74
 $ : int [1:6] 19 21 71 73 84 86
 $ : int [1:6] 6 32 50 53 55 69
 $ : int [1:6] 58 64 65 66 67 68
 $ : int [1:6] 18 23 38 61 62 63
 $ : int [1:6] 2 8 9 46 58 68
 $ : int [1:6] 38 40 41 43 44 45
 $ : int [1:6] 34 35 36 41 45 47
 $ : int [1:6] 25 26 28 33 39 42
 $ : int [1:6] 19 20 21 23 35 41
 $ : int [1:6] 12 13 15 16 22 63
 $ : int [1:6] 7 9 66 68 71 74
 $ : int [1:6] 2 3 4 5 56 69
 $ : int [1:6] 8 9 21 46 47 74
 $ : int [1:6] 59 60 61 62 63 88
 $ : int [1:6] 59 60 61 62 63 87
 - attr(*, "region.id")= chr [1:88] "1" "2" "3" "4" ...
 - attr(*, "call")= language knearneigh(x = coords, k = 6)
 - attr(*, "sym")= logi FALSE
 - attr(*, "type")= chr "knn"
 - attr(*, "knn-k")= num 6
 - attr(*, "class")= chr "nb"

Each county will have exactly six neighbours as specified.

We can plot the weight matrix using the code chunk below.

par(bg = '#E4D5C9')

plot(hunan$geometry, 
     border="#b89c8c")
plot(knn6, 
     coords, 
     pch = 18, 
     cex = 0.6, 
     add = TRUE, 
     col = "#A70D04")

7 Inversed Distance Weights

Step 1: Compute Distance between Areas using nbdists() of spdep

dist <- nbdists(wm_q, 
                coords, 
                longlat = TRUE)

ids <- lapply(dist, function(x) 1/(x))
ids
[[1]]
[1] 0.01535405 0.03916350 0.01820896 0.02807922 0.01145113

[[2]]
[1] 0.01535405 0.01764308 0.01925924 0.02323898 0.01719350

[[3]]
[1] 0.03916350 0.02822040 0.03695795 0.01395765

[[4]]
[1] 0.01820896 0.02822040 0.03414741 0.01539065

[[5]]
[1] 0.03695795 0.03414741 0.01524598 0.01618354

[[6]]
[1] 0.015390649 0.015245977 0.021748129 0.011883901 0.009810297

[[7]]
[1] 0.01708612 0.01473997 0.01150924 0.01872915

[[8]]
[1] 0.02022144 0.03453056 0.02529256 0.01036340 0.02284457 0.01500600 0.01515314

[[9]]
[1] 0.02022144 0.01574888 0.02109502 0.01508028 0.02902705 0.01502980

[[10]]
[1] 0.02281552 0.01387777 0.01538326 0.01346650 0.02100510 0.02631658 0.01874863
[8] 0.01500046

[[11]]
[1] 0.01882869 0.02243492 0.02247473

[[12]]
[1] 0.02779227 0.02419652 0.02333385 0.02986130 0.02335429

[[13]]
[1] 0.02779227 0.02650020 0.02670323 0.01714243

[[14]]
[1] 0.01882869 0.01233868 0.02098555

[[15]]
[1] 0.02650020 0.01233868 0.01096284 0.01562226

[[16]]
[1] 0.02281552 0.02466962 0.02765018 0.01476814 0.01671430

[[17]]
[1] 0.01387777 0.02243492 0.02098555 0.01096284 0.02466962 0.01593341 0.01437996

[[18]]
[1] 0.02039779 0.02032767 0.01481665 0.01473691 0.01459380

[[19]]
[1] 0.01538326 0.01926323 0.02668415 0.02140253 0.01613589 0.01412874

[[20]]
[1] 0.01346650 0.02039779 0.01926323 0.01723025 0.02153130 0.01469240 0.02327034

[[21]]
[1] 0.02668415 0.01723025 0.01766299 0.02644986 0.02163800

[[22]]
[1] 0.02100510 0.02765018 0.02032767 0.02153130 0.01489296

[[23]]
[1] 0.01481665 0.01469240 0.01401432 0.02246233 0.01880425 0.01530458 0.01849605

[[24]]
[1] 0.02354598 0.01837201 0.02607264 0.01220154 0.02514180

[[25]]
[1] 0.02354598 0.02188032 0.01577283 0.01949232 0.02947957

[[26]]
[1] 0.02155798 0.01745522 0.02212108 0.02220532

[[27]]
[1] 0.02155798 0.02490625 0.01562326

[[28]]
[1] 0.01837201 0.02188032 0.02229549 0.03076171 0.02039506

[[29]]
[1] 0.02490625 0.01686587 0.01395022

[[30]]
[1] 0.02090587

[[31]]
[1] 0.02607264 0.01577283 0.01219005 0.01724850 0.01229012 0.01609781 0.01139438
[8] 0.01150130

[[32]]
[1] 0.01220154 0.01219005 0.01712515 0.01340413 0.01280928 0.01198216 0.01053374
[8] 0.01065655

[[33]]
[1] 0.01949232 0.01745522 0.02229549 0.02090587 0.01979045

[[34]]
[1] 0.03113041 0.03589551 0.02882915

[[35]]
[1] 0.01766299 0.02185795 0.02616766 0.02111721 0.02108253 0.01509020

[[36]]
[1] 0.01724850 0.03113041 0.01571707 0.01860991 0.02073549 0.01680129

[[37]]
[1] 0.01686587 0.02234793 0.01510990 0.01550676

[[38]]
[1] 0.01401432 0.02407426 0.02276151 0.01719415

[[39]]
[1] 0.01229012 0.02172543 0.01711924 0.02629732 0.01896385

[[40]]
[1] 0.01609781 0.01571707 0.02172543 0.01506473 0.01987922 0.01894207

[[41]]
[1] 0.02246233 0.02185795 0.02205991 0.01912542 0.01601083 0.01742892

[[42]]
[1] 0.02212108 0.01562326 0.01395022 0.02234793 0.01711924 0.01836831 0.01683518

[[43]]
[1] 0.01510990 0.02629732 0.01506473 0.01836831 0.03112027 0.01530782

[[44]]
[1] 0.01550676 0.02407426 0.03112027 0.01486508

[[45]]
[1] 0.03589551 0.01860991 0.01987922 0.02205991 0.02107101 0.01982700

[[46]]
[1] 0.03453056 0.04033752 0.02689769

[[47]]
[1] 0.02529256 0.02616766 0.04033752 0.01949145 0.02181458

[[48]]
[1] 0.02313819 0.03370576 0.02289485 0.01630057 0.01818085

[[49]]
[1] 0.03076171 0.02138091 0.02394529 0.01990000

[[50]]
[1] 0.01712515 0.02313819 0.02551427 0.02051530 0.02187179

[[51]]
[1] 0.03370576 0.02138091 0.02873854

[[52]]
[1] 0.02289485 0.02394529 0.02551427 0.02873854 0.03516672

[[53]]
[1] 0.01630057 0.01979945 0.01253977

[[54]]
[1] 0.02514180 0.02039506 0.01340413 0.01990000 0.02051530 0.03516672

[[55]]
[1] 0.01280928 0.01818085 0.02187179 0.01979945 0.01882298

[[56]]
[1] 0.01036340 0.01139438 0.01198216 0.02073549 0.01214479 0.01362855 0.01341697

[[57]]
[1] 0.028079221 0.017643082 0.031423501 0.029114131 0.013520292 0.009903702

[[58]]
[1] 0.01925924 0.03142350 0.02722997 0.01434859 0.01567192

[[59]]
[1] 0.01696711 0.01265572 0.01667105 0.01785036

[[60]]
[1] 0.02419652 0.02670323 0.01696711 0.02343040

[[61]]
[1] 0.02333385 0.01265572 0.02343040 0.02514093 0.02790764 0.01219751 0.02362452

[[62]]
[1] 0.02514093 0.02002219 0.02110260

[[63]]
[1] 0.02986130 0.02790764 0.01407043 0.01805987

[[64]]
[1] 0.02911413 0.01689892

[[65]]
[1] 0.02471705

[[66]]
[1] 0.01574888 0.01726461 0.03068853 0.01954805 0.01810569

[[67]]
[1] 0.01708612 0.01726461 0.01349843 0.01361172

[[68]]
[1] 0.02109502 0.02722997 0.03068853 0.01406357 0.01546511

[[69]]
[1] 0.02174813 0.01645838 0.01419926

[[70]]
[1] 0.02631658 0.01963168 0.02278487

[[71]]
[1] 0.01473997 0.01838483 0.03197403

[[72]]
[1] 0.01874863 0.02247473 0.01476814 0.01593341 0.01963168

[[73]]
[1] 0.01500046 0.02140253 0.02278487 0.01838483 0.01652709

[[74]]
[1] 0.01150924 0.01613589 0.03197403 0.01652709 0.01342099 0.02864567

[[75]]
[1] 0.011883901 0.010533736 0.012539774 0.018822977 0.016458383 0.008217581

[[76]]
[1] 0.01352029 0.01434859 0.01689892 0.02471705 0.01954805 0.01349843 0.01406357

[[77]]
[1] 0.014736909 0.018804247 0.022761507 0.012197506 0.020022195 0.014070428
[7] 0.008440896

[[78]]
[1] 0.02323898 0.02284457 0.01508028 0.01214479 0.01567192 0.01546511 0.01140779

[[79]]
[1] 0.01530458 0.01719415 0.01894207 0.01912542 0.01530782 0.01486508 0.02107101

[[80]]
[1] 0.01500600 0.02882915 0.02111721 0.01680129 0.01601083 0.01982700 0.01949145
[8] 0.01362855

[[81]]
[1] 0.02947957 0.02220532 0.01150130 0.01979045 0.01896385 0.01683518

[[82]]
[1] 0.02327034 0.02644986 0.01849605 0.02108253 0.01742892

[[83]]
[1] 0.023354289 0.017142433 0.015622258 0.016714303 0.014379961 0.014593799
[7] 0.014892965 0.018059871 0.008440896

[[84]]
[1] 0.01872915 0.02902705 0.01810569 0.01361172 0.01342099 0.01297994

[[85]]
 [1] 0.011451133 0.017193502 0.013957649 0.016183544 0.009810297 0.010656545
 [7] 0.013416965 0.009903702 0.014199260 0.008217581 0.011407794

[[86]]
[1] 0.01515314 0.01502980 0.01412874 0.02163800 0.01509020 0.02689769 0.02181458
[8] 0.02864567 0.01297994

[[87]]
[1] 0.01667105 0.02362452 0.02110260 0.02058034

[[88]]
[1] 0.01785036 0.02058034

Step 2: Row-standardised weights matrix

nb2listw()

  • Assign weights to each neighboring polygon. Recall in lecture slides, it is calculated using the inverse of number of neigbours; total of each row in matrix adds to 1, which makes it a row-standardized.
  • In our case study, each neighboring polygon will be assigned equal weight (style="W"). This is accomplished by assigning the fraction 1/(# of neighbours) to each neighboring county then summing the weighted income values.
  • While this is the most intuitive way to summaries the neighbors’ values it has one drawback in that polygons along the edges of the study area will base their lagged values on fewer polygons thus potentially over- or under-estimating the true nature of the spatial autocorrelation in the data.
  • Style="W" option used for this example for simplicity’s sake but more robust options are available, notably style="B".
  • Styles:
    • W: row standardised (sums over all links to n)
    • B: basic binary coding
    • C: globally standardised (sums over all links to n)
    • U: equal to C divided by the number of neighbours (sums over all links to unity)
    • S: variance-stabilizing coding scheme (sums over all links to n)
    • minmax: divides the weights by min of the max row sums and max column sums of the input weights; similar to C/U
  • nb2listw() converts a neighbors list object into a weight list object.
  • The input of nb2listw() must be an object of class nb. The syntax of the function has two major arguments, namely style and zero.poly.
rswm_q <- nb2listw(wm_q, 
                   style="W", 
                   zero.policy = TRUE)

rswm_q
Characteristics of weights list object:
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 448 
Percentage nonzero weights: 5.785124 
Average number of links: 5.090909 

Weights style: W 
Weights constants summary:
   n   nn S0       S1       S2
W 88 7744 88 37.86334 365.9147
rswm_qB <- nb2listw(wm_q, style="B", zero.policy = TRUE)
rswm_qB
Characteristics of weights list object:
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 448 
Percentage nonzero weights: 5.785124 
Average number of links: 5.090909 

Weights style: B 
Weights constants summary:
   n   nn  S0  S1    S2
B 88 7744 448 896 10224
rswm_qF <- nb2listw(wm_q, style="W", zero.policy = FALSE)
rswm_qF
Characteristics of weights list object:
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 448 
Percentage nonzero weights: 5.785124 
Average number of links: 5.090909 

Weights style: W 
Weights constants summary:
   n   nn S0       S1       S2
W 88 7744 88 37.86334 365.9147
  • zero.policy = TRUE: allows for lists of non-neighbors. This should be used with caution since the user may not be aware of missing neighbors in their dataset however, a zero.policy = FALSE would return an error.
    • If zero policy is set to TRUE, weights vectors of zero length are inserted for regions without neighbour in the neighbours list. These will in turn generate lag values of zero, equivalent to the sum of products of the zero row t(rep(0, length=length(neighbours))) %*% x, for arbitrary numerical vector x of length length(neighbours). The spatially lagged value of x for the zero-neighbour region will then be zero, which may (or may not) be a sensible choice.

To see the weight of the first polygon’s eight neighbors type:

rswm_q$weights[10]
[[1]]
[1] 0.125 0.125 0.125 0.125 0.125 0.125 0.125 0.125
  • Each neighbor is assigned a 0.125 of the total weight. All same because style=“W” => equal weights.
  • This means that when R computes the average neighboring income values, each neighbor’s income will be multiplied by 0.2 before being tallied.
  • Using the same method, we can also derive a row standardised distance weight matrix by using the code chunk below.
rswm_ids <- nb2listw(wm_q, 
                     glist=ids, 
                     style="B", 
                     zero.policy=TRUE)
rswm_ids
Characteristics of weights list object:
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 448 
Percentage nonzero weights: 5.785124 
Average number of links: 5.090909 

Weights style: B 
Weights constants summary:
   n   nn       S0        S1     S2
B 88 7744 8.786867 0.3776535 3.8137
rswm_ids$weights[1]
[[1]]
[1] 0.01535405 0.03916350 0.01820896 0.02807922 0.01145113
summary(unlist(rswm_ids$weights))
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
0.008218 0.015088 0.018739 0.019614 0.022823 0.040338 

8 Application of Spatial Weight Matrix

Spatially lagged variable: the weighted sum or a weighted average of the neighboring values for that variable. Used to account for spatial autocorrelation in the data, where values of a variable in 1 location is influenced by values of variable in the nearby locations

4 different spatial lagged variables:

  • spatial lag with row-standardized weights
  • spatial lag as a sum of neighbouring values
  • spatial window average, and
  • spatial window sum

8.1 Spatial lag with row-standardized weights

  • Sums up GDPPC of all the neighbours, except the target location itself
  • Here, we compute the average neighbor GDPPC value for each polygon.
  • These values are often referred to as spatially lagged values
  • These values are often referred to as spatially lagged values.
GDPPC.lag <- lag.listw(rswm_q, hunan$GDPPC)
GDPPC.lag
 [1] 24847.20 22724.80 24143.25 27737.50 27270.25 21248.80 43747.00 33582.71
 [9] 45651.17 32027.62 32671.00 20810.00 25711.50 30672.33 33457.75 31689.20
[17] 20269.00 23901.60 25126.17 21903.43 22718.60 25918.80 20307.00 20023.80
[25] 16576.80 18667.00 14394.67 19848.80 15516.33 20518.00 17572.00 15200.12
[33] 18413.80 14419.33 24094.50 22019.83 12923.50 14756.00 13869.80 12296.67
[41] 15775.17 14382.86 11566.33 13199.50 23412.00 39541.00 36186.60 16559.60
[49] 20772.50 19471.20 19827.33 15466.80 12925.67 18577.17 14943.00 24913.00
[57] 25093.00 24428.80 17003.00 21143.75 20435.00 17131.33 24569.75 23835.50
[65] 26360.00 47383.40 55157.75 37058.00 21546.67 23348.67 42323.67 28938.60
[73] 25880.80 47345.67 18711.33 29087.29 20748.29 35933.71 15439.71 29787.50
[81] 18145.00 21617.00 29203.89 41363.67 22259.09 44939.56 16902.00 16930.00
lag.list <- list(hunan$NAME_3, 
                 lag.listw(rswm_q, hunan$GDPPC))
lag.res <- as.data.frame(lag.list)
colnames(lag.res) <- c("NAME_3", "lag GDPPC")
hunan <- left_join(hunan,
                   lag.res)

The following table shows the average neighboring income values (stored in the Inc.lag object) for each county.

head(hunan)
Simple feature collection with 6 features and 7 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 110.4922 ymin: 28.61762 xmax: 112.3013 ymax: 30.12812
Geodetic CRS:  WGS 84
   NAME_2  ID_3  NAME_3   ENGTYPE_3  County GDPPC lag GDPPC
1 Changde 21098 Anxiang      County Anxiang 23667  24847.20
2 Changde 21100 Hanshou      County Hanshou 20981  22724.80
3 Changde 21101  Jinshi County City  Jinshi 34592  24143.25
4 Changde 21102      Li      County      Li 24473  27737.50
5 Changde 21103   Linli      County   Linli 25554  27270.25
6 Changde 21104  Shimen      County  Shimen 27137  21248.80
                        geometry
1 POLYGON ((112.0625 29.75523...
2 POLYGON ((112.2288 29.11684...
3 POLYGON ((111.8927 29.6013,...
4 POLYGON ((111.3731 29.94649...
5 POLYGON ((111.6324 29.76288...
6 POLYGON ((110.8825 30.11675...
gdppc <- qtm(hunan, "GDPPC") +
    tm_layout(bg.color = "#E4D5C9",
            frame = F)

lag_gdppc <- qtm(hunan, "lag GDPPC") +
    tm_layout(bg.color = "#E4D5C9",
            frame = F)

tmap_arrange(gdppc, 
             lag_gdppc, 
             asp=1, 
             ncol=2) 

8.2 Spatial lag as a sum of neighboring values

We can calculate spatial lag as a sum of neighboring values by assigning binary weights. This requires us to go back to our neighbors list, then apply a function that will assign binary weights, then we use glist = in the nb2listw function to explicitly assign these weights.

  • We start by applying a function that will assign a value of 1 per each neighbor.
  • This is done with lapply, which we have been using to manipulate the neighbors structure throughout the past notebooks.
  • Basically it applies a function across each value in the neighbors structure.
b_weights <- lapply(wm_q, function(x) 0*x + 1)
b_weights2 <- nb2listw(wm_q, 
                       glist = b_weights, 
                       style = "B")
b_weights2
Characteristics of weights list object:
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 448 
Percentage nonzero weights: 5.785124 
Average number of links: 5.090909 

Weights style: B 
Weights constants summary:
   n   nn  S0  S1    S2
B 88 7744 448 896 10224

With the proper weights assigned, we can use lag.listw to compute a lag variable from our weight and GDPPC.

lag_sum <- list(hunan$NAME_3, lag.listw(b_weights2, hunan$GDPPC))
lag.res <- as.data.frame(lag_sum)
colnames(lag.res) <- c("NAME_3", "lag_sum GDPPC")
lag_sum
[[1]]
 [1] "Anxiang"       "Hanshou"       "Jinshi"        "Li"           
 [5] "Linli"         "Shimen"        "Liuyang"       "Ningxiang"    
 [9] "Wangcheng"     "Anren"         "Guidong"       "Jiahe"        
[13] "Linwu"         "Rucheng"       "Yizhang"       "Yongxing"     
[17] "Zixing"        "Changning"     "Hengdong"      "Hengnan"      
[21] "Hengshan"      "Leiyang"       "Qidong"        "Chenxi"       
[25] "Zhongfang"     "Huitong"       "Jingzhou"      "Mayang"       
[29] "Tongdao"       "Xinhuang"      "Xupu"          "Yuanling"     
[33] "Zhijiang"      "Lengshuijiang" "Shuangfeng"    "Xinhua"       
[37] "Chengbu"       "Dongan"        "Dongkou"       "Longhui"      
[41] "Shaodong"      "Suining"       "Wugang"        "Xinning"      
[45] "Xinshao"       "Shaoshan"      "Xiangxiang"    "Baojing"      
[49] "Fenghuang"     "Guzhang"       "Huayuan"       "Jishou"       
[53] "Longshan"      "Luxi"          "Yongshun"      "Anhua"        
[57] "Nan"           "Yuanjiang"     "Jianghua"      "Lanshan"      
[61] "Ningyuan"      "Shuangpai"     "Xintian"       "Huarong"      
[65] "Linxiang"      "Miluo"         "Pingjiang"     "Xiangyin"     
[69] "Cili"          "Chaling"       "Liling"        "Yanling"      
[73] "You"           "Zhuzhou"       "Sangzhi"       "Yueyang"      
[77] "Qiyang"        "Taojiang"      "Shaoyang"      "Lianyuan"     
[81] "Hongjiang"     "Hengyang"      "Guiyang"       "Changsha"     
[85] "Taoyuan"       "Xiangtan"      "Dao"           "Jiangyong"    

[[2]]
 [1] 124236 113624  96573 110950 109081 106244 174988 235079 273907 256221
[11]  98013 104050 102846  92017 133831 158446 141883 119508 150757 153324
[21] 113593 129594 142149 100119  82884  74668  43184  99244  46549  20518
[31] 140576 121601  92069  43258 144567 132119  51694  59024  69349  73780
[41]  94651 100680  69398  52798 140472 118623 180933  82798  83090  97356
[51]  59482  77334  38777 111463  74715 174391 150558 122144  68012  84575
[61] 143045  51394  98279  47671  26360 236917 220631 185290  64640  70046
[71] 126971 144693 129404 284074 112268 203611 145238 251536 108078 238300
[81] 108870 108085 262835 248182 244850 404456  67608  33860

Next, we will append the lag_sum GDPPC field into hunan sf data frame by using the code chunk below.

hunan <- left_join(hunan, lag.res)

Now, We can plot both the GDPPC and Spatial Lag Sum GDPPC for comparison using the code chunk below.

gdppc <- qtm(hunan, "GDPPC") +
    tm_layout(bg.color = "#E4D5C9",
            frame = F)

lag_sum_gdppc <- qtm(hunan, "lag_sum GDPPC") +
    tm_layout(bg.color = "#E4D5C9",
            frame = F)

tmap_arrange(gdppc, lag_sum_gdppc, asp=1, ncol=2)

8.3 Spatial window average

The spatial window average uses row-standardized weights and includes the diagonal element. To do this in R, we need to go back to the neighbors structure and add the diagonal element before assigning weights.

To add the diagonal element to the neighbour list, we just need to use include.self() from spdep.

wm_qs <- include.self(wm_q)
wm_qs
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 536 
Percentage nonzero weights: 6.921488 
Average number of links: 6.090909 

Notice that the Number of nonzero links, Percentage nonzero weights and Average number of links are 536, 6.921488 and 6.090909 respectively as compared to wm_q of 448, 5.785124 and 5.090909

Let us take a good look at the neighbour list of area [1] by using the code chunk below.

wm_qs[[1]]
[1]  1  2  3  4 57 85

Notice that now [1] has six neighbours instead of five.

Now we obtain weights with nb2listw()

wm_qs <- nb2listw(wm_qs)
wm_qs
Characteristics of weights list object:
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 536 
Percentage nonzero weights: 6.921488 
Average number of links: 6.090909 

Weights style: W 
Weights constants summary:
   n   nn S0       S1       S2
W 88 7744 88 30.90265 357.5308

Again, we use nb2listw() and glist() to explicitly assign weight values.

Lastly, we just need to create the lag variable from our weight structure and GDPPC variable.

lag_w_avg_gpdpc <- lag.listw(wm_qs, 
                             hunan$GDPPC)
lag_w_avg_gpdpc
 [1] 24650.50 22434.17 26233.00 27084.60 26927.00 22230.17 47621.20 37160.12
 [9] 49224.71 29886.89 26627.50 22690.17 25366.40 25825.75 30329.00 32682.83
[17] 25948.62 23987.67 25463.14 21904.38 23127.50 25949.83 20018.75 19524.17
[25] 18955.00 17800.40 15883.00 18831.33 14832.50 17965.00 17159.89 16199.44
[33] 18764.50 26878.75 23188.86 20788.14 12365.20 15985.00 13764.83 11907.43
[41] 17128.14 14593.62 11644.29 12706.00 21712.29 43548.25 35049.00 16226.83
[49] 19294.40 18156.00 19954.75 18145.17 12132.75 18419.29 14050.83 23619.75
[57] 24552.71 24733.67 16762.60 20932.60 19467.75 18334.00 22541.00 26028.00
[65] 29128.50 46569.00 47576.60 36545.50 20838.50 22531.00 42115.50 27619.00
[73] 27611.33 44523.29 18127.43 28746.38 20734.50 33880.62 14716.38 28516.22
[81] 18086.14 21244.50 29568.80 48119.71 22310.75 43151.60 17133.40 17009.33

Next, we will convert the lag variable listw object into a data.frame by using as.data.frame().

lag.list.wm_qs <- list(hunan$NAME_3, lag.listw(wm_qs, hunan$GDPPC))
lag_wm_qs.res <- as.data.frame(lag.list.wm_qs)
colnames(lag_wm_qs.res) <- c("NAME_3", "lag_window_avg GDPPC")

Note: The third command line on the code chunk above renames the field names of lag_wm_q1.res object into NAME_3 and lag_window_avg GDPPC respectively.

Next, the code chunk below will be used to append lag_window_avg GDPPC values onto hunan sf data.frame by using left_join() of dplyr package.

hunan <- left_join(hunan, lag_wm_qs.res)

To compare the values of lag GDPPC and Spatial window average, kable() of Knitr package is used to prepare a table using the code chunk below.

hunan %>%
  select("County", 
         "lag GDPPC", 
         "lag_window_avg GDPPC") %>%
  kable() %>% 
  kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
                            fixed_thead = T)
County lag GDPPC lag_window_avg GDPPC geometry
Anxiang 24847.20 24650.50 POLYGON ((112.0625 29.75523...
Hanshou 22724.80 22434.17 POLYGON ((112.2288 29.11684...
Jinshi 24143.25 26233.00 POLYGON ((111.8927 29.6013,...
Li 27737.50 27084.60 POLYGON ((111.3731 29.94649...
Linli 27270.25 26927.00 POLYGON ((111.6324 29.76288...
Shimen 21248.80 22230.17 POLYGON ((110.8825 30.11675...
Liuyang 43747.00 47621.20 POLYGON ((113.9905 28.5682,...
Ningxiang 33582.71 37160.12 POLYGON ((112.7181 28.38299...
Wangcheng 45651.17 49224.71 POLYGON ((112.7914 28.52688...
Anren 32027.62 29886.89 POLYGON ((113.1757 26.82734...
Guidong 32671.00 26627.50 POLYGON ((114.1799 26.20117...
Jiahe 20810.00 22690.17 POLYGON ((112.4425 25.74358...
Linwu 25711.50 25366.40 POLYGON ((112.5914 25.55143...
Rucheng 30672.33 25825.75 POLYGON ((113.6759 25.87578...
Yizhang 33457.75 30329.00 POLYGON ((113.2621 25.68394...
Yongxing 31689.20 32682.83 POLYGON ((113.3169 26.41843...
Zixing 20269.00 25948.62 POLYGON ((113.7311 26.16259...
Changning 23901.60 23987.67 POLYGON ((112.6144 26.60198...
Hengdong 25126.17 25463.14 POLYGON ((113.1056 27.21007...
Hengnan 21903.43 21904.38 POLYGON ((112.7599 26.98149...
Hengshan 22718.60 23127.50 POLYGON ((112.607 27.4689, ...
Leiyang 25918.80 25949.83 POLYGON ((112.9996 26.69276...
Qidong 20307.00 20018.75 POLYGON ((111.7818 27.0383,...
Chenxi 20023.80 19524.17 POLYGON ((110.2624 28.21778...
Zhongfang 16576.80 18955.00 POLYGON ((109.9431 27.72858...
Huitong 18667.00 17800.40 POLYGON ((109.9419 27.10512...
Jingzhou 14394.67 15883.00 POLYGON ((109.8186 26.75842...
Mayang 19848.80 18831.33 POLYGON ((109.795 27.98008,...
Tongdao 15516.33 14832.50 POLYGON ((109.9294 26.46561...
Xinhuang 20518.00 17965.00 POLYGON ((109.227 27.43733,...
Xupu 17572.00 17159.89 POLYGON ((110.7189 28.30485...
Yuanling 15200.12 16199.44 POLYGON ((110.9652 28.99895...
Zhijiang 18413.80 18764.50 POLYGON ((109.8818 27.60661...
Lengshuijiang 14419.33 26878.75 POLYGON ((111.5307 27.81472...
Shuangfeng 24094.50 23188.86 POLYGON ((112.263 27.70421,...
Xinhua 22019.83 20788.14 POLYGON ((111.3345 28.19642...
Chengbu 12923.50 12365.20 POLYGON ((110.4455 26.69317...
Dongan 14756.00 15985.00 POLYGON ((111.4531 26.86812...
Dongkou 13869.80 13764.83 POLYGON ((110.6622 27.37305...
Longhui 12296.67 11907.43 POLYGON ((110.985 27.65983,...
Shaodong 15775.17 17128.14 POLYGON ((111.9054 27.40254...
Suining 14382.86 14593.62 POLYGON ((110.389 27.10006,...
Wugang 11566.33 11644.29 POLYGON ((110.9878 27.03345...
Xinning 13199.50 12706.00 POLYGON ((111.0736 26.84627...
Xinshao 23412.00 21712.29 POLYGON ((111.6013 27.58275...
Shaoshan 39541.00 43548.25 POLYGON ((112.5391 27.97742...
Xiangxiang 36186.60 35049.00 POLYGON ((112.4549 28.05783...
Baojing 16559.60 16226.83 POLYGON ((109.7015 28.82844...
Fenghuang 20772.50 19294.40 POLYGON ((109.5239 28.19206...
Guzhang 19471.20 18156.00 POLYGON ((109.8968 28.74034...
Huayuan 19827.33 19954.75 POLYGON ((109.5647 28.61712...
Jishou 15466.80 18145.17 POLYGON ((109.8375 28.4696,...
Longshan 12925.67 12132.75 POLYGON ((109.6337 29.62521...
Luxi 18577.17 18419.29 POLYGON ((110.1067 28.41835...
Yongshun 14943.00 14050.83 POLYGON ((110.0003 29.29499...
Anhua 24913.00 23619.75 POLYGON ((111.6034 28.63716...
Nan 25093.00 24552.71 POLYGON ((112.3232 29.46074...
Yuanjiang 24428.80 24733.67 POLYGON ((112.4391 29.1791,...
Jianghua 17003.00 16762.60 POLYGON ((111.6461 25.29661...
Lanshan 21143.75 20932.60 POLYGON ((112.2286 25.61123...
Ningyuan 20435.00 19467.75 POLYGON ((112.0715 26.09892...
Shuangpai 17131.33 18334.00 POLYGON ((111.8864 26.11957...
Xintian 24569.75 22541.00 POLYGON ((112.2578 26.0796,...
Huarong 23835.50 26028.00 POLYGON ((112.9242 29.69134...
Linxiang 26360.00 29128.50 POLYGON ((113.5502 29.67418...
Miluo 47383.40 46569.00 POLYGON ((112.9902 29.02139...
Pingjiang 55157.75 47576.60 POLYGON ((113.8436 29.06152...
Xiangyin 37058.00 36545.50 POLYGON ((112.9173 28.98264...
Cili 21546.67 20838.50 POLYGON ((110.8822 29.69017...
Chaling 23348.67 22531.00 POLYGON ((113.7666 27.10573...
Liling 42323.67 42115.50 POLYGON ((113.5673 27.94346...
Yanling 28938.60 27619.00 POLYGON ((113.9292 26.6154,...
You 25880.80 27611.33 POLYGON ((113.5879 27.41324...
Zhuzhou 47345.67 44523.29 POLYGON ((113.2493 28.02411...
Sangzhi 18711.33 18127.43 POLYGON ((110.556 29.40543,...
Yueyang 29087.29 28746.38 POLYGON ((113.343 29.61064,...
Qiyang 20748.29 20734.50 POLYGON ((111.5563 26.81318...
Taojiang 35933.71 33880.62 POLYGON ((112.0508 28.67265...
Shaoyang 15439.71 14716.38 POLYGON ((111.5013 27.30207...
Lianyuan 29787.50 28516.22 POLYGON ((111.6789 28.02946...
Hongjiang 18145.00 18086.14 POLYGON ((110.1441 27.47513...
Hengyang 21617.00 21244.50 POLYGON ((112.7144 26.98613...
Guiyang 29203.89 29568.80 POLYGON ((113.0811 26.04963...
Changsha 41363.67 48119.71 POLYGON ((112.9421 28.03722...
Taoyuan 22259.09 22310.75 POLYGON ((112.0612 29.32855...
Xiangtan 44939.56 43151.60 POLYGON ((113.0426 27.8942,...
Dao 16902.00 17133.40 POLYGON ((111.498 25.81679,...
Jiangyong 16930.00 17009.33 POLYGON ((111.3659 25.39472...

Lastly, qtm() of tmap package is used to plot the lag_gdppc and w_ave_gdppc maps next to each other for quick comparison.

w_avg_gdppc <- qtm(hunan, "lag_window_avg GDPPC") +
  tm_layout(bg.color = "#E4D5C9",
            frame = F)

tmap_arrange(lag_gdppc, w_avg_gdppc, asp=1, ncol=2)

Note: For more effective comparison, it is advicible to use the core tmap mapping functions.

8.4 Spatial window sum

  • The spatial window sum is the counter part of the window average, but without using row-standardized weights.
  • Uses and includes the diagonal element.
  • Sums up GDPPC of all neighbours, including target location itself

To add the diagonal element to the neighbour list, we just need to use include.self() from spdep.

wm_qs <- include.self(wm_q)
wm_qs
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 536 
Percentage nonzero weights: 6.921488 
Average number of links: 6.090909 

Next, we will assign binary weights to the neighbour structure that includes the diagonal element.

b_weights <- lapply(wm_qs, function(x) 0*x + 1)
b_weights[1]
[[1]]
[1] 1 1 1 1 1 1

Notice that now [1] has six neighbours instead of five.

Again, we use nb2listw() and glist() to explicitly assign weight values.

b_weights2 <- nb2listw(wm_qs, 
                       glist = b_weights, 
                       style = "B")
b_weights2
Characteristics of weights list object:
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 536 
Percentage nonzero weights: 6.921488 
Average number of links: 6.090909 

Weights style: B 
Weights constants summary:
   n   nn  S0   S1    S2
B 88 7744 536 1072 14160

With our new weight structure, we can compute the lag variable with lag.listw().

w_sum_gdppc <- list(hunan$NAME_3, lag.listw(b_weights2, hunan$GDPPC))
w_sum_gdppc
[[1]]
 [1] "Anxiang"       "Hanshou"       "Jinshi"        "Li"           
 [5] "Linli"         "Shimen"        "Liuyang"       "Ningxiang"    
 [9] "Wangcheng"     "Anren"         "Guidong"       "Jiahe"        
[13] "Linwu"         "Rucheng"       "Yizhang"       "Yongxing"     
[17] "Zixing"        "Changning"     "Hengdong"      "Hengnan"      
[21] "Hengshan"      "Leiyang"       "Qidong"        "Chenxi"       
[25] "Zhongfang"     "Huitong"       "Jingzhou"      "Mayang"       
[29] "Tongdao"       "Xinhuang"      "Xupu"          "Yuanling"     
[33] "Zhijiang"      "Lengshuijiang" "Shuangfeng"    "Xinhua"       
[37] "Chengbu"       "Dongan"        "Dongkou"       "Longhui"      
[41] "Shaodong"      "Suining"       "Wugang"        "Xinning"      
[45] "Xinshao"       "Shaoshan"      "Xiangxiang"    "Baojing"      
[49] "Fenghuang"     "Guzhang"       "Huayuan"       "Jishou"       
[53] "Longshan"      "Luxi"          "Yongshun"      "Anhua"        
[57] "Nan"           "Yuanjiang"     "Jianghua"      "Lanshan"      
[61] "Ningyuan"      "Shuangpai"     "Xintian"       "Huarong"      
[65] "Linxiang"      "Miluo"         "Pingjiang"     "Xiangyin"     
[69] "Cili"          "Chaling"       "Liling"        "Yanling"      
[73] "You"           "Zhuzhou"       "Sangzhi"       "Yueyang"      
[77] "Qiyang"        "Taojiang"      "Shaoyang"      "Lianyuan"     
[81] "Hongjiang"     "Hengyang"      "Guiyang"       "Changsha"     
[85] "Taoyuan"       "Xiangtan"      "Dao"           "Jiangyong"    

[[2]]
 [1] 147903 134605 131165 135423 134635 133381 238106 297281 344573 268982
[11] 106510 136141 126832 103303 151645 196097 207589 143926 178242 175235
[21] 138765 155699 160150 117145 113730  89002  63532 112988  59330  35930
[31] 154439 145795 112587 107515 162322 145517  61826  79925  82589  83352
[41] 119897 116749  81510  63530 151986 174193 210294  97361  96472 108936
[51]  79819 108871  48531 128935  84305 188958 171869 148402  83813 104663
[61] 155742  73336 112705  78084  58257 279414 237883 219273  83354  90124
[71] 168462 165714 165668 311663 126892 229971 165876 271045 117731 256646
[81] 126603 127467 295688 336838 267729 431516  85667  51028

Next, we will convert the lag variable listw object into a data.frame by using as.data.frame().

w_sum_gdppc.res <- as.data.frame(w_sum_gdppc)
colnames(w_sum_gdppc.res) <- c("NAME_3", "w_sum GDPPC")

Note: The second command line on the code chunk above renames the field names of w_sum_gdppc.res object into NAME_3 and w_sum GDPPC respectively.

Next, the code chunk below will be used to append w_sum GDPPC values onto hunan sf data.frame by using left_join() of dplyr package.

hunan <- left_join(hunan, w_sum_gdppc.res)

To compare the values of lag GDPPC and Spatial window average, kable() of Knitr package is used to prepare a table using the code chunk below.

hunan %>%
  select("County", "lag_sum GDPPC", "w_sum GDPPC") %>%
  kable() %>% 
  kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
                            fixed_thead = T)
County lag_sum GDPPC w_sum GDPPC geometry
Anxiang 124236 147903 POLYGON ((112.0625 29.75523...
Hanshou 113624 134605 POLYGON ((112.2288 29.11684...
Jinshi 96573 131165 POLYGON ((111.8927 29.6013,...
Li 110950 135423 POLYGON ((111.3731 29.94649...
Linli 109081 134635 POLYGON ((111.6324 29.76288...
Shimen 106244 133381 POLYGON ((110.8825 30.11675...
Liuyang 174988 238106 POLYGON ((113.9905 28.5682,...
Ningxiang 235079 297281 POLYGON ((112.7181 28.38299...
Wangcheng 273907 344573 POLYGON ((112.7914 28.52688...
Anren 256221 268982 POLYGON ((113.1757 26.82734...
Guidong 98013 106510 POLYGON ((114.1799 26.20117...
Jiahe 104050 136141 POLYGON ((112.4425 25.74358...
Linwu 102846 126832 POLYGON ((112.5914 25.55143...
Rucheng 92017 103303 POLYGON ((113.6759 25.87578...
Yizhang 133831 151645 POLYGON ((113.2621 25.68394...
Yongxing 158446 196097 POLYGON ((113.3169 26.41843...
Zixing 141883 207589 POLYGON ((113.7311 26.16259...
Changning 119508 143926 POLYGON ((112.6144 26.60198...
Hengdong 150757 178242 POLYGON ((113.1056 27.21007...
Hengnan 153324 175235 POLYGON ((112.7599 26.98149...
Hengshan 113593 138765 POLYGON ((112.607 27.4689, ...
Leiyang 129594 155699 POLYGON ((112.9996 26.69276...
Qidong 142149 160150 POLYGON ((111.7818 27.0383,...
Chenxi 100119 117145 POLYGON ((110.2624 28.21778...
Zhongfang 82884 113730 POLYGON ((109.9431 27.72858...
Huitong 74668 89002 POLYGON ((109.9419 27.10512...
Jingzhou 43184 63532 POLYGON ((109.8186 26.75842...
Mayang 99244 112988 POLYGON ((109.795 27.98008,...
Tongdao 46549 59330 POLYGON ((109.9294 26.46561...
Xinhuang 20518 35930 POLYGON ((109.227 27.43733,...
Xupu 140576 154439 POLYGON ((110.7189 28.30485...
Yuanling 121601 145795 POLYGON ((110.9652 28.99895...
Zhijiang 92069 112587 POLYGON ((109.8818 27.60661...
Lengshuijiang 43258 107515 POLYGON ((111.5307 27.81472...
Shuangfeng 144567 162322 POLYGON ((112.263 27.70421,...
Xinhua 132119 145517 POLYGON ((111.3345 28.19642...
Chengbu 51694 61826 POLYGON ((110.4455 26.69317...
Dongan 59024 79925 POLYGON ((111.4531 26.86812...
Dongkou 69349 82589 POLYGON ((110.6622 27.37305...
Longhui 73780 83352 POLYGON ((110.985 27.65983,...
Shaodong 94651 119897 POLYGON ((111.9054 27.40254...
Suining 100680 116749 POLYGON ((110.389 27.10006,...
Wugang 69398 81510 POLYGON ((110.9878 27.03345...
Xinning 52798 63530 POLYGON ((111.0736 26.84627...
Xinshao 140472 151986 POLYGON ((111.6013 27.58275...
Shaoshan 118623 174193 POLYGON ((112.5391 27.97742...
Xiangxiang 180933 210294 POLYGON ((112.4549 28.05783...
Baojing 82798 97361 POLYGON ((109.7015 28.82844...
Fenghuang 83090 96472 POLYGON ((109.5239 28.19206...
Guzhang 97356 108936 POLYGON ((109.8968 28.74034...
Huayuan 59482 79819 POLYGON ((109.5647 28.61712...
Jishou 77334 108871 POLYGON ((109.8375 28.4696,...
Longshan 38777 48531 POLYGON ((109.6337 29.62521...
Luxi 111463 128935 POLYGON ((110.1067 28.41835...
Yongshun 74715 84305 POLYGON ((110.0003 29.29499...
Anhua 174391 188958 POLYGON ((111.6034 28.63716...
Nan 150558 171869 POLYGON ((112.3232 29.46074...
Yuanjiang 122144 148402 POLYGON ((112.4391 29.1791,...
Jianghua 68012 83813 POLYGON ((111.6461 25.29661...
Lanshan 84575 104663 POLYGON ((112.2286 25.61123...
Ningyuan 143045 155742 POLYGON ((112.0715 26.09892...
Shuangpai 51394 73336 POLYGON ((111.8864 26.11957...
Xintian 98279 112705 POLYGON ((112.2578 26.0796,...
Huarong 47671 78084 POLYGON ((112.9242 29.69134...
Linxiang 26360 58257 POLYGON ((113.5502 29.67418...
Miluo 236917 279414 POLYGON ((112.9902 29.02139...
Pingjiang 220631 237883 POLYGON ((113.8436 29.06152...
Xiangyin 185290 219273 POLYGON ((112.9173 28.98264...
Cili 64640 83354 POLYGON ((110.8822 29.69017...
Chaling 70046 90124 POLYGON ((113.7666 27.10573...
Liling 126971 168462 POLYGON ((113.5673 27.94346...
Yanling 144693 165714 POLYGON ((113.9292 26.6154,...
You 129404 165668 POLYGON ((113.5879 27.41324...
Zhuzhou 284074 311663 POLYGON ((113.2493 28.02411...
Sangzhi 112268 126892 POLYGON ((110.556 29.40543,...
Yueyang 203611 229971 POLYGON ((113.343 29.61064,...
Qiyang 145238 165876 POLYGON ((111.5563 26.81318...
Taojiang 251536 271045 POLYGON ((112.0508 28.67265...
Shaoyang 108078 117731 POLYGON ((111.5013 27.30207...
Lianyuan 238300 256646 POLYGON ((111.6789 28.02946...
Hongjiang 108870 126603 POLYGON ((110.1441 27.47513...
Hengyang 108085 127467 POLYGON ((112.7144 26.98613...
Guiyang 262835 295688 POLYGON ((113.0811 26.04963...
Changsha 248182 336838 POLYGON ((112.9421 28.03722...
Taoyuan 244850 267729 POLYGON ((112.0612 29.32855...
Xiangtan 404456 431516 POLYGON ((113.0426 27.8942,...
Dao 67608 85667 POLYGON ((111.498 25.81679,...
Jiangyong 33860 51028 POLYGON ((111.3659 25.39472...

Lastly, qtm() of tmap package is used to plot the lag_sum GDPPC and w_sum_gdppc maps next to each other for quick comparison.

w_sum_gdppc <- qtm(hunan, "w_sum GDPPC") +
  tm_layout(bg.color = "#E4D5C9",
            frame = F)

tmap_arrange(lag_sum_gdppc, w_sum_gdppc, asp=1, ncol=2)

9 Reference

Kam, T. S. Spatial Weights and Applications. R for Geospatial Data Science and Analytics. https://r4gdsa.netlify.app/chap08.html