# Quadrat Based Statistical Method for Planar Point Patterns

**Authors: Serge Rey sjsrey@gmail.com, Wei Kang weikang9009@gmail.com and Hu Shao shaohutiger@gmail.com**

## Introduction

In this notebook, we are going to introduce how to apply quadrat statistics to a point pattern to infer whether it comes from a CSR process.

- In Quadrat Statistic we introduce the concept of quadrat based method.
- We illustrate how to use the module
**quadrat_statistics.py**through an example dataset**juvenile**in Juvenile Example

## Quadrat Statistic

In the previous notebooks, we introduced the concept of Complete Spatial Randomness (CSR) process which serves as the benchmark process. Utilizing CSR properties, we can discriminate those that are not from a CSR process. Quadrat statistic is one such method. Since a CSR process has two major characteristics:

- Uniform: each location has equal probability of getting a point (where an event happens).
- Independent: location of event points are independent.

We can imagine that for any point pattern, if the underlying process is a CSR process, the expected point counts inside any cell of area $ | A | $ should be $\lambda | A | $ ($\lambda$ is the intensity which is uniform across the study area for a CSR). Thus, if we impose a $m \times k$ rectangular tessellation over the study area (window), we can easily calculate the expected number of points inside each cell under the null of CSR. By comparing the observed point counts against the expected counts and calculate a $\chi^2$ test statistic, we can decide whether to reject the null based on the position of the $\chi^2$ test statistic in the sampling distribution. |

There are two ways to construct the sampling distribution and acquire a p-value:

- Analytical sampling distribution: a $\chi^2$ distribution of $m \times k -1$ degree of freedom. We can refer to the $\chi^2$ distribution table to acquire the p-value. If it is smaller than $0.05$, we will reject the null at the $95\%$ confidence level.
- Empirical sampling distribution: a distribution constructed from a large number of $\chi^2$ test statistics for simulations under the null of CSR. If the $\chi^2$ test statistic for the observed point pattern is among the largest $5%$ test statistics, we would say that it is very unlikely that it is the outcome of a CSR process at the $95\%$ confidence level. Then, the null is rejected. A pseudo p-value can be calculated based on which we can use the same rule as p-value to make the decision: where

$nsim$ is the number of simulations, $\psi_i^2$ is the $\chi^2$ test statistic calculated for each simulated point pattern, $\chi^2$ is the $\chi^2$ test statistic calculated for the observed point pattern, $\phi_i$ is an indicator variable.

We are going to introduce how to use the **quadrat_statistics.py** module to perform quadrat based method using either of the above two approaches to constructing the sampling distribution and acquire a p-value.

## Juvenile Example

```
import pysal.lib as ps
import numpy as np
from pysal.explore.pointpats import PointPattern, as_window
from pysal.explore.pointpats import PoissonPointProcess as csr
%matplotlib inline
import matplotlib.pyplot as plt
```

Import the quadrat_statistics module to conduct quadrat-based method.

Among the three major classes in the module, **RectangleM, HexagonM, QStatistic**, the first two are aimed at imposing a tessellation (rectangular or hexagonal shape) over the minimum bounding rectangle of the point pattern and calculate the number of points falling in each cell; **QStatistic** is the main class with which we can calculate a p-value, as well as a pseudo p-value to help us make the decision of rejecting the null or not.

```
from pysal.explore import pointpats.quadrat_statistics as qs
```

```
dir(qs)
```

Open the point shapefile “juvenile.shp”.

```
juv = ps.io.open(ps.examples.get_path("juvenile.shp"))
```

```
len(juv) # 168 point events in total
```

```
juv_points = np.array([event for event in juv]) # get x,y coordinates for all the points
juv_points
```

Construct a point pattern from numpy array **juv_points**.

```
pp_juv = PointPattern(juv_points)
pp_juv
```

```
pp_juv.summary()
```

```
pp_juv.plot(window= True, title= "Point pattern")
```

### Rectangle quadrats & analytical sampling distribution

We can impose rectangle tessellation over mbb of the point pattern by specifying **shape** as “rectangle”. We can also specify the number of rectangles in each row and column. For the current analysis, we use the $3 \times 3$ rectangle grids.

```
q_r = qs.QStatistic(pp_juv,shape= "rectangle",nx = 3, ny = 3)
```

Use the plot method to plot the quadrats as well as the number of points falling in each quadrat.

```
q_r.plot()
```

```
q_r.chi2 #chi-squared test statistic for the observed point pattern
```

```
q_r.df #degree of freedom
```

```
q_r.chi2_pvalue # analytical pvalue
```

Since the p-value based on the analytical $\chi^2$ distribution (degree of freedom = 8) is 0.0000589, much smaller than 0.05. We might determine that the underlying process is not CSR. We can also turn to empirical sampling distribution to ascertain our decision.

### Rectangle quadrats & empirical sampling distribution

To construct a empirical sampling distribution, we need to simulate CSR within the window of the observed point pattern a lot of times. Here, we generate 999 point patterns under the null of CSR.

```
csr_process = csr(pp_juv.window, pp_juv.n, 999, asPP=True)
```

We specify parameter **realizations** as the point process instance which contains 999 CSR realizations.

```
q_r_e = qs.QStatistic(pp_juv,shape= "rectangle",nx = 3, ny = 3, realizations = csr_process)
```

```
q_r_e.chi2_r_pvalue
```

The pseudo p-value is 0.002, which is smaller than 0.05. Thus, we reject the null at the $95\%$ confidence level.

### Hexagon quadrats & analytical sampling distribution

We can also impose hexagon tessellation over mbb of the point pattern by specifying **shape** as “hexagon”. We can also specify the length of the hexagon edge. For the current analysis, we specify it as 15.

```
q_h = qs.QStatistic(pp_juv,shape= "hexagon",lh = 15)
```

```
q_h.plot()
```

```
q_h.chi2 #chi-squared test statistic for the observed point pattern
```

```
q_h.df #degree of freedom
```

```
q_h.chi2_pvalue # analytical pvalue
```

Similar to the inference of rectangle tessellation, since the analytical p-value is much smaller than 0.05, we reject the null of CSR. The point pattern is not random.

### Hexagon quadrats & empirical sampling distribution

```
q_h_e = qs.QStatistic(pp_juv,shape= "hexagon",lh = 15, realizations = csr_process)
```

```
q_h_e.chi2_r_pvalue
```

Because 0.001 is smaller than 0.05, we reject the null.