# Week 10 Spatial Autocorrelation 空間自相關
###### tags: `GlobalAnalysisMethods`
> 目標:空間自相關的觀念與計算
## 全域分析的方法
* Point data **without attributes**
1. Quantrat Analysis 樣方分析
2. Nearest Neighbor Methods 鄰近分析
3. Ripley's K-function: K(d) and L(d)
* Point/Polygon data **with attributes**
1. Definition of Neighborhoods or Spatial Structures
2. Sparial Autocorrelation Index
* Moran's I & Geary's C ratio
3. Spatial Concentration Index
* General G-statistic
## 空間關聯性與相依性
> Everything is related to everything else, but near things are more related than distant things
>
* Definition of Spatial Autocorrelation
量測空間附近屬性相似性的方法
## 如何定義「鄰近」?
> 1. Spatial adjacency (物理上相鄰的)
> 2. Distances between the centroids (一定距離內的中心點)
>
### 1. Spatial adjacency
* Rook's: 四方位 (units that shares common boundary with length greater than zero
* Queen's: 八方位 (除了邊相鄰之外,相同頂點也算)
#### Order of neighborhood:
* 1st order
* Immediater neighbor
* Defined by Rook's or Queen's criteria
* 2nd order
* higher order
#### Binary Connectivity Matrix
* Symmetrical (對稱)
* Values on diagonal are zeros (0, 1)
* Raw sum = 該unit的鄰居數
* Same as connectivivty matrix for network
* 當目標數量眾多時沒有效率
* 耗費記憶體空間
* 許多0
* 其他替代方式:稀疏矩陣(Sparse Matrix)
##### 稀疏矩陣的儲存方式

#### Stochastic Matrix 又稱列標準化矩陣

### 2. Distances

#### Centroid Distances
* Distance between centroids
* 中心點:幾何中心
* 受polygon的形狀影響
* 中心點有可能在polygon的外部
#### Nearest Distances (比較不常用)
* 兩個polygon之間最短的點的距離
* 相鄰的polygon會是0
## Spatial weights matrix approaches
1. 定義鄰近
* Rook's
* Queen's
2. 選擇相鄰矩陣
* Binary connective matrix
* Row standardized weights matrix
* Centroid distances
* Nearest distances
## 計算空間自相關
* 權數:
* Contiguity
* Common border
* Distance
* Distance band
* Kth-nearest neighbors
### Moran's I


* Interpretations
1. 相似於相關係數,值域在正負一之間
* 0表示沒有空間自相關
* 接近-1或1代表高度自相關
* 正負號表示不同的點的散布模式
2. 可以用來表示點的分布模式
* 0: random
* Positive: more toward clustering
* Negative: more toward dispersion
### Moran's I 的顯著性檢定

### Randomization vs. Normality sampling
* Normalization:
假設你的資料為樣本,有他的常態分佈,且為隨機樣本,每次的推論都包含誤差在內
* Randomization:
做比較少的假設,假定說你的資料模式來自多種分布的其中一種,數值不變,但分布模式會改變

### 蒙地卡羅顯著性檢定
* Permutation test 排列檢定
Null: the data were determined and than assigned to their spatial locations at **random**
Alternative: the assignment to each location **depended on the assignment at that location's neighors**
* Doesn't randomize over the possible sets of data values, it considers them given but conditional on the data observed, considers all possible way of reassigning them to the locations.
* 對n個資料點,有n階乘種排列方式
* Usually no simple analytical expression for the full permutation distribution
* 在相同的權重下重新排列


### Concept of Moran Scatter Plots
> 只有一種變數:例如犯罪事件數與鄰近地區的犯罪事件數
X: 某屬性的值
Spatial lag of X(Lag-X): 鄰近地區某屬性的平均值
若以散布圖呈現,Moran's I 會是回歸線的**斜率**

### Moran correlograms
距離與相關係數(Moran's I)的關係圖

### Getis-ord general G-statistic
> Moran's I 無法區別冷區和熱區
>
利用空間濃度方法 (Spatial concentration)

* General G 可以分辨冷、熱點
G 相對高表示高的值群聚在一起
G 相對低表示低的值群聚在一起
* 跟G的期望值比較來詮釋:
>E(G): 潛在熱區
<E(G): 潛在冷區
=E(G): No spatial association
* 利用Z統計量檢定差異的統計顯著性
### General G 的顯著性檢定:

## R-code 實作
* 流程:定義「鄰近」-->建立鄰近表-->空間自相關運算
* 鄰近
1. 相接相鄰
```r=
tw.nb = poly2nb(TW) # 預設 queen = T
tw.nb = poly2nb(TW, queen=F) # Rook's
```
2. 最近的前幾個
```r=
coords = corrdinates(TW) # 需要坐標
tw.nb = knn2nb(knearneigh(coords, k = 2)) # 前兩鄰近
```
3. 距離在閾值內
```r=
tw.nb = dnearneigh(coords, d1 = 0, d2 = 10000)
```
* 鄰近表
1. 鄰近目錄
```r=
tw.nb.w = nb2listw(tw.nb, zero.policy = T) # 預設 style = "W" (列標準化)
```
2. 鄰近矩陣
```r=
tw.nb.wm = nb2mat(tw.nb, zero.policy = T) # 預設同樣也是列標準化
```
* 空間自相關運算
1. Moran's I
參數:資料點、鄰近目錄
```r=
# Randomization
M = moran.test(dens, listw = tw.nb.w, zero.policy = T)
# Normalization
M = moran.test(dens, randomisation = F, listw = tw.nb.w, zero.policy = T)
```
2. Monte-Carlo simulation
```r=
mc = moran.mc(dens, listw = tw.nb.w, nsim = 999, zero.policy = T)
```
* 畫圖
```r=
hist(mc$res)
abline(v = M$estimate[1], col = "red")
```
3. Moran scatter plot
```r=
moran.plot(dens, tw.nb.w, zero.policy = T)
```
4. Correlogram
參數:neighbors list
```r=
cor = sp.correlogram(tw.nb, dens, order = 10, method = "I", style = "W", zero.policy = T)
print(cor);plot(cor)
```
5. General G
```r=
G = globalG.test(dens, listw = tw.nb.w, zero.policy = T)
```
$$R-G$$
$$\frac{(NIR-B)*\frac{654-481}{864-481}}{R}$$