Authors: Serge Rey sjsrey@gmail.com, Wei Kang weikang9009@gmail.com and Hu Shao shaohutiger@gmail.com
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 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:
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.
$$\chi^2 = \sum^m_{i=1} \sum^k_{j=1} \frac{[x_{i,j}-E(x_{i,j})]^2}{\lambda |A_{i,j}|}$$There are two ways to construct the sampling distribution and acquire a p-value:
$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.
In [1]:
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.
In [2]:
from pysal.explore import pointpats.quadrat_statistics as qs
In [3]:
dir(qs)
Out[3]:
Open the point shapefile "juvenile.shp".
In [4]:
juv = ps.io.open(ps.examples.get_path("juvenile.shp"))
In [5]:
len(juv) # 168 point events in total
Out[5]:
In [6]:
juv_points = np.array([event for event in juv]) # get x,y coordinates for all the points
juv_points
Out[6]:
Construct a point pattern from numpy array juv_points.
In [7]:
pp_juv = PointPattern(juv_points)
pp_juv
Out[7]:
In [8]:
pp_juv.summary()
In [9]:
pp_juv.plot(window= True, title= "Point pattern")
In [10]:
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.
In [11]:
q_r.plot()
In [12]:
q_r.chi2 #chi-squared test statistic for the observed point pattern
Out[12]:
In [13]:
q_r.df #degree of freedom
Out[13]:
In [14]:
q_r.chi2_pvalue # analytical pvalue
Out[14]:
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.
In [15]:
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.
In [16]:
q_r_e = qs.QStatistic(pp_juv,shape= "rectangle",nx = 3, ny = 3, realizations = csr_process)
In [17]:
q_r_e.chi2_r_pvalue
Out[17]:
The pseudo p-value is 0.002, which is smaller than 0.05. Thus, we reject the null at the $95\%$ confidence level.
In [18]:
q_h = qs.QStatistic(pp_juv,shape= "hexagon",lh = 15)
In [19]:
q_h.plot()
In [20]:
q_h.chi2 #chi-squared test statistic for the observed point pattern
Out[20]:
In [21]:
q_h.df #degree of freedom
Out[21]:
In [22]:
q_h.chi2_pvalue # analytical pvalue
Out[22]:
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.
In [23]:
q_h_e = qs.QStatistic(pp_juv,shape= "hexagon",lh = 15, realizations = csr_process)
In [24]:
q_h_e.chi2_r_pvalue
Out[24]:
Because 0.001 is smaller than 0.05, we reject the null.
In [ ]: