# Week 6 Point Pattern Analysis: Quadrat Analysis
> 分析點之間的散布是隨機、分散抑或是群聚的
## Detection of Point Pattern
* Random: 點出現在任何地方的機率都相同,不受時間或空間影響
* Uniform(Dispersion): 點平均分散
* Clustered: 很多點聚集在一起

## Quadarat Analysis
1. 分析點在空間之中的分布與改變
2. 檢驗點之間的分布是否是隨機分布
## Poisson Distribution
> a discrete probability distribution that expresses the **probability of a given number** of events occurring in a fixed interval of time or space if these events occur with a known constant mean rate and **independently of the time** since the last event
當 n 值很大而 p 值很小,二項分布會趨近於波以松分布 (期望值等於變異數)
* $\lambda=n*p$
* $f(k,\lambda) = Pr(X=k)=\frac{\lambda^k*e^{-\lambda}}{k!}$
* $\lambda=E(X)=Var(X)$
## Variance-Mean Ratio Test
* $N=網格數$
* $Variance=\frac{\Sigma{x^2}-[(\Sigma x)^2/N]}{N-1}$
* $StandardError=(\frac{2}{k-1})^2$
* $t = \frac{vmr-1}{StandardError}$
問題:網格要切多大?--->網格的邊長大小會影響檢定結果,通常網格的邊長越小,網格切得越細小,網格之間的差異會越大,則越容易是群聚的
## 操作程序:
>以作業為例
>
### 1. 建立網格(fishnet)
```r=
bb = bbox(mazu) # 媽祖的範圍比較廣
xmin = bb[1,1]
xmax = bb[1,2]
ymin = bb[2,1]
ymax = bb[2,2]
max = c(xmax, ymax)
min = c(xmin, ymin)
cellsize = c(20000,20000) # 20km x 20km
cells.dim = ceiling((max-min)/cellsize +0.5)
grd = GridTopology(cellcentre.offset = c(xmin-200, ymin-200), cellsize = cellsize, cells.dim = cells.dim)
GRD_Layer = as.SpatialPolygons.GridTopology(grd, proj4string = mazu@proj4string)
N = length(GRD_Layer)
```
### 2. 計算網格內點數
```r=+
mazu.count = poly.counts(mazu, GRD_Layer)
```
### 3. 計算VMR
```r=+
mazu.var = var(mazu.count)
mazu.mean = mean(mazu.count)
mazu.vmr = mazu.var / mazu.mean
```
### 4. 假設檢定
```r=+
mazu.se = sqrt(2/(N-1))
mazu.t = (mazu.vmr-1)/mazu.se;mazu.t
mazu.p = pt(mazu.t, df=N-1, lower.tail = F);mazu.p
```
#### Step 1. Determin the null and alternative hypotheses.
$H_0:VMR<=1$,空間分布是隨機的
$H_a:VMR>1$,空間分布是群聚的
#### Step 2. Verify necessary conditions and use an an appropriate test statistic.
t = 125.4839
#### Step 3. Assuming the null hypothesis is true, find the p-value
以0.05的顯著水準進行檢定,p-value遠小於顯著水準 (1.345009e-78)
#### Step 4. Deside whether or not the result is statistically significant based on the p-value
p-value 遠小於顯著水準,結果統計上顯著,拒絕虛無假設
#### Step 5. Make the conclusion
空間分布是群聚的