Hands-on Exercise 2: Spatial Weights & Spatial Autocorrelation

On this page, I address Hands-On Exercise for Chapter 02:

3. Local Measures of Spatial Autocorrelation

  • Calculating Local Moran's I for each region
  • Creating LISA Clustermap
  • Creating Hotspot & Coldspot
  • Calculating Getis and Ord’s Gi* values using fixed and adaptive distances

3.1 Analytical Question

  • Identify if development is equally distributed geographically in Hunan province
  • If NO, then ask: Is there signs of spatial clustering?
  • If YES, then ask: Where is the spatial clustering?

3.2 Import Datasets

3.3 Import Packages

  • Similar datasets are used in Hands-on Ex02:
    • /data/geospatial/Hunan.###: This is a geospatial data set in ESRI shapefile format.
    • /data/geospatial/Hunan.###: This is a geospatial data set in ESRI shapefile format.
  • Similarly, the same packages are used, see Section 1.3
    • sf, spdep, tmap, tidyverse
show code
print("Importing packages...")
[1] "Importing packages..."
show code
pacman::p_load(sf, spdep, tmap, tidyverse, knitr)


hunan <- st_read(dsn = "data/geospatial", 
                 layer = "Hunan")
Reading layer `Hunan' from data source 
  `C:\1darren\ISSS624\Hands-on_Ex02\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
show code
hunan2012 <- read_csv("data/aspatial/Hunan_2012.csv")
Rows: 88 Columns: 29
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (2): County, City
dbl (27): avg_wage, deposite, FAI, Gov_Rev, Gov_Exp, GDP, GDPPC, GIO, Loan, ...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
show code
hunan <- left_join(hunan,hunan2012) %>%
  select(1:4, 7, 15)
Joining with `by = join_by(County)`
show code
kable(head(hunan, 5))
NAME_2 ID_3 NAME_3 ENGTYPE_3 County GDPPC geometry
Changde 21098 Anxiang County Anxiang 23667 POLYGON ((112.0625 29.75523…
Changde 21100 Hanshou County Hanshou 20981 POLYGON ((112.2288 29.11684…
Changde 21101 Jinshi County City Jinshi 34592 POLYGON ((111.8927 29.6013,…
Changde 21102 Li County Li 24473 POLYGON ((111.3731 29.94649…
Changde 21103 Linli County Linli 25554 POLYGON ((111.6324 29.76288…
show code
wm_q <- poly2nb(hunan, 
                queen=TRUE)
# summary(wm_q)
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

3.6 Cluster & Outlier Analysis

  • use of LISA (Local Indicators of Spatial Association) to detect clusters/outliers from Hunan GDPPC

3.6.1 Local Moran’s I

  • (Global) Moran’s I is performed over the entire dataset, looking for pattern across all the datapoints considered
    • Local Moran’s I checks each individual datapoint for spatial correlation to detect individual clusters
  • localmoran() thus identifies a Local Moran’s I score for each of 88 regions
show code
fips <- order(hunan$County)
localMI <- localmoran(hunan$GDPPC, rswm_q)
print("Printing first 6 rows of `localMI`\n")
[1] "Printing first 6 rows of `localMI`\n"
show code
head(localMI)
            Ii          E.Ii       Var.Ii        Z.Ii Pr(z != E(Ii))
1 -0.001468468 -2.815006e-05 4.723841e-04 -0.06626904      0.9471636
2  0.025878173 -6.061953e-04 1.016664e-02  0.26266425      0.7928094
3 -0.011987646 -5.366648e-03 1.133362e-01 -0.01966705      0.9843090
4  0.001022468 -2.404783e-07 5.105969e-06  0.45259801      0.6508382
5  0.014814881 -6.829362e-05 1.449949e-03  0.39085814      0.6959021
6 -0.038793829 -3.860263e-04 6.475559e-03 -0.47728835      0.6331568
  • Print coefficient matrix of localMoran (warning: long!)
printCoefmat(data.frame(
  localMI[fips,], 
  row.names=hunan$County[fips]),
  check.names=FALSE)
                       Ii        E.Ii      Var.Ii        Z.Ii Pr.z....E.Ii..
Anhua         -2.2493e-02 -5.0048e-03  5.8235e-02 -7.2467e-02         0.9422
Anren         -3.9932e-01 -7.0111e-03  7.0348e-02 -1.4791e+00         0.1391
Anxiang       -1.4685e-03 -2.8150e-05  4.7238e-04 -6.6269e-02         0.9472
Baojing        3.4737e-01 -5.0089e-03  8.3636e-02  1.2185e+00         0.2230
Chaling        2.0559e-02 -9.6812e-04  2.7711e-02  1.2932e-01         0.8971
Changning     -2.9868e-05 -9.0010e-09  1.5105e-07 -7.6828e-02         0.9388
Changsha       4.9022e+00 -2.1348e-01  2.3194e+00  3.3590e+00         0.0008
Chengbu        7.3725e-01 -1.0534e-02  2.2132e-01  1.5895e+00         0.1119
Chenxi         1.4544e-01 -2.8156e-03  4.7116e-02  6.8299e-01         0.4946
Cili           7.3176e-02 -1.6747e-03  4.7902e-02  3.4200e-01         0.7324
Dao            2.1420e-01 -2.0824e-03  4.4123e-02  1.0297e+00         0.3032
Dongan         1.5210e-01 -6.3485e-04  1.3471e-02  1.3159e+00         0.1882
Dongkou        5.2918e-01 -6.4461e-03  1.0748e-01  1.6338e+00         0.1023
Fenghuang      1.8013e-01 -6.2832e-03  1.3257e-01  5.1198e-01         0.6087
Guidong       -5.9160e-01 -1.3086e-02  3.7003e-01 -9.5104e-01         0.3416
Guiyang        1.8240e-01 -3.6908e-03  3.2610e-02  1.0305e+00         0.3028
Guzhang        2.8466e-01 -8.5054e-03  1.4152e-01  7.7931e-01         0.4358
Hanshou        2.5878e-02 -6.0620e-04  1.0167e-02  2.6266e-01         0.7928
Hengdong       9.9964e-03 -4.9063e-04  6.7742e-03  1.2742e-01         0.8986
Hengnan        2.8064e-02 -3.2160e-04  3.7597e-03  4.6294e-01         0.6434
Hengshan      -5.8201e-03 -3.0437e-05  5.1076e-04 -2.5618e-01         0.7978
Hengyang       6.2997e-02 -1.3046e-03  2.1865e-02  4.3486e-01         0.6637
Hongjiang      1.8790e-01 -2.3019e-03  3.1725e-02  1.0678e+00         0.2856
Huarong       -1.5389e-02 -1.8667e-03  8.1030e-02 -4.7503e-02         0.9621
Huayuan        8.3772e-02 -8.5569e-04  2.4495e-02  5.4072e-01         0.5887
Huitong        2.5997e-01 -5.2447e-03  1.1077e-01  7.9685e-01         0.4255
Jiahe         -1.2431e-01 -3.0550e-03  5.1111e-02 -5.3633e-01         0.5917
Jianghua       2.8651e-01 -3.8280e-03  8.0968e-02  1.0204e+00         0.3076
Jiangyong      2.4337e-01 -2.7082e-03  1.1746e-01  7.1800e-01         0.4728
Jingzhou       1.8270e-01 -8.5106e-04  2.4363e-02  1.1759e+00         0.2396
Jinshi        -1.1988e-02 -5.3666e-03  1.1334e-01 -1.9667e-02         0.9843
Jishou        -2.8680e-01 -2.6305e-03  4.4028e-02 -1.3543e+00         0.1756
Lanshan        6.3334e-02 -9.6365e-04  2.0441e-02  4.4972e-01         0.6529
Leiyang        1.1581e-02 -1.4948e-04  2.5082e-03  2.3422e-01         0.8148
Lengshuijiang -1.7903e+00 -8.2129e-02  2.1598e+00 -1.1623e+00         0.2451
Li             1.0225e-03 -2.4048e-07  5.1060e-06  4.5260e-01         0.6508
Lianyuan      -1.4672e-01 -1.8983e-03  1.9145e-02 -1.0467e+00         0.2952
Liling         1.3774e+00 -1.5097e-02  4.2601e-01  2.1335e+00         0.0329
Linli          1.4815e-02 -6.8294e-05  1.4499e-03  3.9086e-01         0.6959
Linwu         -2.4621e-03 -9.0703e-06  1.9258e-04 -1.7676e-01         0.8597
Linxiang       6.5904e-02 -2.9028e-03  2.5470e-01  1.3634e-01         0.8916
Liuyang        3.3688e+00 -7.7502e-02  1.5180e+00  2.7972e+00         0.0052
Longhui        8.0801e-01 -1.1377e-02  1.5538e-01  2.0787e+00         0.0376
Longshan       7.5663e-01 -1.1100e-02  3.1449e-01  1.3690e+00         0.1710
Luxi           1.8177e-01 -2.4855e-03  3.4249e-02  9.9561e-01         0.3194
Mayang         2.1852e-01 -5.8773e-03  9.8049e-02  7.1663e-01         0.4736
Miluo          1.8704e+00 -1.6927e-02  2.7925e-01  3.5715e+00         0.0004
Nan           -9.5789e-03 -4.9497e-04  6.8341e-03 -1.0988e-01         0.9125
Ningxiang      1.5607e+00 -7.3878e-02  8.0012e-01  1.8274e+00         0.0676
Ningyuan       2.0910e-01 -7.0884e-03  8.2306e-02  7.5356e-01         0.4511
Pingjiang     -9.8964e-01 -2.6457e-03  5.6027e-02 -4.1698e+00         0.0000
Qidong         1.1806e-01 -2.1207e-03  2.4747e-02  7.6396e-01         0.4449
Qiyang         6.1966e-02 -7.3374e-04  8.5743e-03  6.7712e-01         0.4983
Rucheng       -3.6992e-01 -8.8999e-03  2.5272e-01 -7.1814e-01         0.4727
Sangzhi        2.5053e-01 -4.9470e-03  6.8000e-02  9.7972e-01         0.3272
Shaodong      -3.2659e-02 -3.6592e-05  5.0546e-04 -1.4510e+00         0.1468
Shaoshan       2.1223e+00 -5.0227e-02  1.3668e+00  1.8583e+00         0.0631
Shaoyang       5.9499e-01 -1.1253e-02  1.3012e-01  1.6807e+00         0.0928
Shimen        -3.8794e-02 -3.8603e-04  6.4756e-03 -4.7729e-01         0.6332
Shuangfeng     9.2835e-03 -2.2867e-03  3.1516e-02  6.5174e-02         0.9480
Shuangpai      8.0591e-02 -3.1366e-04  8.9838e-03  8.5358e-01         0.3933
Suining        3.7585e-01 -3.5933e-03  4.1870e-02  1.8544e+00         0.0637
Taojiang      -2.5394e-01 -1.2395e-03  1.4477e-02 -2.1002e+00         0.0357
Taoyuan        1.4729e-02 -1.2039e-04  8.5103e-04  5.0903e-01         0.6107
Tongdao        4.6482e-01 -6.9870e-03  1.9879e-01  1.0582e+00         0.2900
Wangcheng      4.4220e+00 -1.1067e-01  1.3596e+00  3.8873e+00         0.0001
Wugang         7.1003e-01 -7.8144e-03  1.0710e-01  2.1935e+00         0.0283
Xiangtan       2.4530e-01 -3.6457e-04  3.2319e-03  4.3213e+00         0.0000
Xiangxiang     2.6271e-01 -1.2703e-03  2.1290e-02  1.8092e+00         0.0704
Xiangyin       5.4525e-01 -4.7442e-03  7.9236e-02  1.9539e+00         0.0507
Xinhua         1.1810e-01 -6.2649e-03  8.6001e-02  4.2409e-01         0.6715
Xinhuang       1.5725e-01 -4.1820e-03  3.6648e-01  2.6667e-01         0.7897
Xinning        6.8928e-01 -9.6674e-03  2.0328e-01  1.5502e+00         0.1211
Xinshao        5.7578e-02 -8.5932e-03  1.1769e-01  1.9289e-01         0.8470
Xintian       -7.4050e-03 -5.1493e-03  1.0877e-01 -6.8395e-03         0.9945
Xupu           3.2406e-01 -5.7468e-03  5.7735e-02  1.3726e+00         0.1699
Yanling       -6.9021e-02 -5.9211e-04  9.9306e-03 -6.8667e-01         0.4923
Yizhang       -2.6844e-01 -2.2463e-03  4.7588e-02 -1.2202e+00         0.2224
Yongshun       6.3064e-01 -1.1350e-02  1.8830e-01  1.4795e+00         0.1390
Yongxing       4.3411e-01 -9.0735e-03  1.5088e-01  1.1409e+00         0.2539
You            7.8750e-02 -7.2728e-03  1.2116e-01  2.4714e-01         0.8048
Yuanjiang      2.0004e-04 -1.7760e-04  2.9798e-03  6.9181e-03         0.9945
Yuanling       8.7298e-03 -2.2981e-06  2.3221e-05  1.8121e+00         0.0700
Yueyang        4.1189e-02 -1.9768e-04  2.3113e-03  8.6085e-01         0.3893
Zhijiang       1.0476e-01 -7.8123e-04  1.3100e-02  9.2214e-01         0.3565
Zhongfang     -2.2685e-01 -2.1455e-03  3.5927e-02 -1.1855e+00         0.2358
Zhuzhou        3.2864e-01 -5.2432e-04  7.2391e-03  3.8688e+00         0.0001
Zixing        -7.6849e-01 -8.8210e-02  9.4057e-01 -7.0144e-01         0.4830

3.6.1.1 Mapping Local Moran’s I

  • first we append the output coefficient matrix from localMI to hunan before mapping
show code
hunan.localMI <- cbind(hunan,localMI) %>%
  rename(Pr.Ii = Pr.z....E.Ii..)

max_row <- hunan.localMI[which.max(hunan.localMI$Ii), ]
print(paste0("The region of deepest blue is: ", max_row$County))
[1] "The region of deepest blue is: Changsha"
show code
local_moran_stats <- tm_shape(hunan.localMI) +
  tm_fill(col = "Ii", 
          style = "pretty",
          palette = "RdBu",
          title = "Local Moran Statistics") +
  tm_borders(alpha = 0.5) +
  tm_layout(main.title = "Local Moran Statistics")

local_moran_p <- tm_shape(hunan.localMI) +
  tm_fill(col = "Pr.Ii", 
          breaks=c(-Inf, 0.001, 0.01, 0.05, 0.1, Inf),
          palette="-Greens", 
          title = "local Moran's I p-values") +
  tm_borders(alpha = 0.5) +
  tm_layout(main.title = "Local Moran's I p-values")

tmap_arrange(local_moran_stats, 
             local_moran_p, 
             asp=1, 
             ncol=2)
Variable(s) "Ii" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

  • Overall, looks like generally weakly positive spatial autocorrelation
  • Region of darkest blue (i.e. most influenced by neighbours) is Changsha, 4.9022 (or Wangcheng, 4.4220)
    • confidently so, as p-values of regions are extremely low

3.7 LISA Cluster Maps

  • plot significant locations of spatial autocorrelation

3.7.1 Moran scatterplot

  • scatterplot of spatially lag GDPPC (y-axis) against region’s GDPPC (x-axis)
  • top-right quadrant show regions of high GDPPC and high GDPPC-neighbours
show code
nci <- moran.plot(hunan$GDPPC, rswm_q,
                  labels=as.character(hunan$County), 
                  xlab="GDPPC 2012", 
                  ylab="Spatially Lag GDPPC 2012")                    

3.7.2 Rescaling scatterplot to centre

  • R-code is really quite powerful for statistics
  • use of scale for standardisation of values to “centre” the plot
show code
hunan$Z.GDPPC <- scale(hunan$GDPPC) %>% 
  as.vector                  
nci2 <- moran.plot(hunan$Z.GDPPC, rswm_q,
                   labels=as.character(hunan$County),
                   xlab="z-GDPPC 2012", 
                   ylab="Spatially Lag z-GDPPC 2012")

3.7.3 Preparing LISA Cluster Map:

  • creates a vector called quadrant of length same as localMI (i.e. 88 regions)
  • creates spatially lagged GDPPC and centre by removing mean
  • also centre local moran’s I value around its mean
  • set statistical threshold of 0.05
  • partition 4 quadrants from low-low to high-high
    • “5th quadrant” of non-statistically significant regions
show code
quadrant <- vector(mode="numeric",length=nrow(localMI))
hunan$lag_GDPPC <- lag.listw(rswm_q, hunan$GDPPC)
DV <- hunan$lag_GDPPC - mean(hunan$lag_GDPPC)     
LM_I <- localMI[,1]   
signif <- 0.05       

quadrant[DV <0 & LM_I>0] <- 1
quadrant[DV >0 & LM_I<0] <- 2
quadrant[DV <0 & LM_I<0] <- 3  
quadrant[DV >0 & LM_I>0] <- 4    
quadrant[localMI[,5]>signif] <- 0

3.7.3 Now we plot the map LISA Cluster Map:

show code
gdppc <- qtm(hunan, "GDPPC")+
  tm_layout(main.title = "Map of GDPPC")

hunan.localMI$quadrant <- quadrant
colors <- c("#ffffff", "#2c7bb6", "#abd9e9", "#fdae61", "#d7191c")
clusters <- c("insignificant", "low-low", "low-high", "high-low", "high-high")

LISAmap <- tm_shape(hunan.localMI) +
  tm_fill(col = "quadrant", 
          style = "cat", 
          palette = colors[c(sort(unique(quadrant)))+1], 
          labels = clusters[c(sort(unique(quadrant)))+1],
          popup.vars = c("")) +
  tm_view(set.zoom.limits = c(11,17)) +
  tm_borders(alpha=0.5)  +
  tm_layout(main.title = "LISA map by Quadrant")

tmap_arrange(gdppc, LISAmap, local_moran_stats, 
             local_moran_p, 
             asp=2, ncol=2)
Variable(s) "Ii" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

3.8 HotSpot & ColdSPot Area Analysis

  • Hot Spot refers to region with value higher than surrounding; similarly Cold Spot for lower

3.8.1 Getis and Ord’s Gi*-Statistics

  • ultimate output wm62_lw is a weight matrix, using 62km as neighbour threshold, as a list of spatial weights
    • to find centroid, use Section 1.5.1
    • to find cut-off distance, use Section 1.6.1
    • to computed fixed-distance weight, use Section 1.6.2
show code
longitude <- map_dbl(hunan$geometry, ~st_centroid(.x)[[1]])
latitude <- map_dbl(hunan$geometry, ~st_centroid(.x)[[2]])
coords <- cbind(longitude, latitude)

k1 <- knn2nb(knearneigh(coords))
k1dists <- unlist(nbdists(k1, coords, longlat = TRUE))
summary(k1dists)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  24.79   32.57   38.01   39.07   44.52   61.79 
show code
wm_d62 <- dnearneigh(coords, 0, 62, longlat = TRUE)
wm62_lw <- nb2listw(wm_d62, style = 'B')
summary(wm62_lw)
Characteristics of weights list object:
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 324 
Percentage nonzero weights: 4.183884 
Average number of links: 3.681818 
Link number distribution:

 1  2  3  4  5  6 
 6 15 14 26 20  7 
6 least connected regions:
6 15 30 32 56 65 with 1 link
7 most connected regions:
21 28 35 45 50 52 82 with 6 links

Weights style: B 
Weights constants summary:
   n   nn  S0  S1   S2
B 88 7744 324 648 5440

3.8.3 Computing adaptive distance weight matrix

  • use knn with neighbours=8 (max) to enforce “smoothing of neighbour relationship”
    • note below, all 88 regions are “least connected” and “most connected”
  • use nb2listw to convert nb object to list of spatial weights
    • note binary style i.e.
show code
knn <- knn2nb(knearneigh(coords, k=8))
knn_lw <- nb2listw(knn, style = 'B')
summary(knn_lw)
Characteristics of weights list object:
Neighbour list object:
Number of regions: 88 
Number of nonzero links: 704 
Percentage nonzero weights: 9.090909 
Average number of links: 8 
Non-symmetric neighbours list
Link number distribution:

 8 
88 
88 least connected regions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 with 8 links
88 most connected regions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 with 8 links

Weights style: B 
Weights constants summary:
   n   nn  S0   S1    S2
B 88 7744 704 1300 23014

3.9 Computing Gi* statistics

3.9.1 Computing Gi* statistics with Fixed Distance

  • Using wm62_lw from above, creating fixed distance to calculate Z-score for Getis-Ord’s Gi*-statistics
    • Z-score, higher value == greater intensity of clustering
    • (+) high clusters, (-) low clusters
  • cbind back to create hunan.gi SpatialPolygonDataFrame
show code
fips <- order(hunan$County)
gi.fixed <- localG(hunan$GDPPC, wm62_lw)
hunan.gi <- cbind(hunan, as.matrix(gi.fixed)) %>%
  rename(gstat_fixed = as.matrix.gi.fixed.)

max_row <- hunan.gi[which.max(hunan.gi$gstat_fixed), ]
print(paste0("The region of deepest blue is: ", max_row$County))
[1] "The region of deepest blue is: Wangcheng"
show code
head(hunan.gi, 5)
Simple feature collection with 5 features and 9 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 111.2145 ymin: 28.61762 xmax: 112.3013 ymax: 29.95847
Geodetic CRS:  WGS 84
   NAME_2  ID_3  NAME_3   ENGTYPE_3  County GDPPC      Z.GDPPC lag_GDPPC
1 Changde 21098 Anxiang      County Anxiang 23667 -0.049205949  24847.20
2 Changde 21100 Hanshou      County Hanshou 20981 -0.228341158  22724.80
3 Changde 21101  Jinshi County City  Jinshi 34592  0.679406172  24143.25
4 Changde 21102      Li      County      Li 24473  0.004547952  27737.50
5 Changde 21103   Linli      County   Linli 25554  0.076642204  27270.25
  gstat_fixed                       geometry
1  0.43607584 POLYGON ((112.0625 29.75523...
2 -0.26550565 POLYGON ((112.2288 29.11684...
3 -0.07303367 POLYGON ((111.8927 29.6013,...
4  0.41301703 POLYGON ((111.3731 29.94649...
5  0.27307058 POLYGON ((111.6324 29.76288...
  • draw map:

    • from above, darkest red Local Gi region is Wangcheng
    show code
    gdppc <- qtm(hunan, "GDPPC")  +
      tm_layout(main.title = "GDPPC", main.title.position ="right")
    
    Gimap <-tm_shape(hunan.gi) +
      tm_fill(col = "gstat_fixed", 
              style = "pretty",
              palette="-RdBu",
              title = "local Gi*") +
      tm_borders(alpha = 0.5)  +
      tm_layout(main.title = "Local Gi*, fixed dist", main.title.position ="right")
    
    tmap_arrange(gdppc, Gimap, asp=1, ncol=2)
    Variable(s) "gstat_fixed" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

    ::: callout-note ## Quiz: What statistical conclusion can you draw from the output above?

  • Wangcheng shows slightly higher Gi* value than Changsha, though both were identified earlier

  • similar colour palette as quadrant mapping earlier; red regions are high-high, blue regions are low-low

    • overall, looks like there is some agreement of spatial auto-correlation when analysed like this

:::

3.9.2 Computing Gi statistics using Adaptive Distance

  • Using knn_lw from above with 8 fixed neighours for each region

    • I guess “adaptive distance” means “whatever distance needed to clock as neighbours”
    show code
    fips <- order(hunan$County)
    gi.adaptive <- localG(hunan$GDPPC, knn_lw)
    hunan.gi <- cbind(hunan, as.matrix(gi.adaptive)) %>%
      rename(gstat_adaptive = as.matrix.gi.adaptive.)
    
    gdppc<- qtm(hunan, "GDPPC")  +
      tm_layout(main.title = "GDPPC", main.title.position ="right")
    coolwarm <- colorRampPalette(c("blue", "white", "red"))(10)
    
    
    Gimap_adaptive <- tm_shape(hunan.gi) + 
      tm_fill(col = "gstat_adaptive", 
              style = "pretty", 
              palette=coolwarm, 
              title = "local Gi* adaptive") + 
      tm_borders(alpha = 0.5)  +
      tm_layout(main.title = "Local Gi* adaptive dist", main.title.position ="right")
    
    tmap_arrange(gdppc, 
                 Gimap,
                 Gimap_adaptive,
                 asp=1, 
                 ncol=3)
    Some legend labels were too wide. These labels have been resized to 0.47, 0.47, 0.47, 0.43. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.
    Variable(s) "gstat_fixed" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
    Variable(s) "gstat_adaptive" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

Quiz: What statistical conclusion can you draw from the output above?
  • Using both fixed- and adaptive-distance neigbours generates similar results; values are largely in agreement
  • knn=8 feels like it’s not very logical, but maybe there’s not too much difference