I am using the Kolmogorov-Smirnov test to study the homogeneity of a point pattern in 3D using the distance transform to a structure as a covariate.
I am using the spatstat book, where it is explained (Section 10.5) that the Kolmogorov-Smirnov test statistic ($D$) can be calculated as the maximum vertical separation between the cumulative distribution function of the spatial covariate evaluated at the data points ($F_0(z)$) and at all locations ($\hat{F}(z)$). It is:
$D = \max\limits_z|\hat{F}(z)-F_0(z)|$
However, I do not understand how to obtain the value $D$ for my null hypothesis (that the points are homogeneously distributed) so that I can compare them and know whether I can reject the null hypothesis.
If there is also a formula to calculate the critical p-value would be excellent, since I read how to calculate it when you compare your data with a defined distribution like Poisson, but in this case I understand that I should use $\hat{F}(z)$ as the distribution to compare.
Furthermore, I could only use this test in spatstat for 2D data, but not for 3D. If will also appreciate any hint about how to do it.
EDIT: Following the example in the spatstat book, what I am doing is:
elev <- bei.extra$elev
cdf.test(bei,elev)
Which produces this result:
Spatial Kolmogorov-Smirnov test of CSR in two dimensions
data: covariate ‘elev’ evaluated at points of ‘bei’
and transformed to uniform distribution under CSR
D = 0.10634, p-value < 2.2e-16
alternative hypothesis: two-sided
As far as I understand, $D$ is calculated with the formula above, however that says nothing about the rejection of the null hypothesis. Now I would like to know how the p-value can be calculated.
As I want to do this test in 3D data, I apply the distance transform in 3D to the structure of interest (covariate), and I compare its result evaluated at all locations $\hat{F}(z)$ and at the observed points ($F_0(z)$). Then, I can compare the distributions as in this figure:
At this point I know how to calculate $D$, but again, that does not tell if the null hypothesis can be rejected. In this figure it looks obvious that the the points are not homogeneously distributed, but I would like to calculate with which p-value I can reject the null hypothesis.
EDIT 2: Two-Sample Kolmogorov-Smirnov test.
Up to now I have been using the two-sample Kolmogorov-Smirnov test as Ege Rubak mentioned in his answer. I was using the MATLAB function kstest2 that calculates the p-value based on the number of samples in $x$ (length(x)
) and $y$ (length(y)
), where $x$ is a vector with the data points and $y$ the covariate values at the grid points.
The problem is that the size of $y$ corresponds to the number of pixels in the image and are not ‘real’ samples. This means that if my image has a small pixel size or I use a smaller grid (independently of the real volume of the sample) it will be easier to reject the null hypothesis. As far as I understand (please correct me if I am wrong) the test should only consider the size of $x$, since it contains the real samples, and not length(y)
.
Now I was checking the R code that Ege Rubak proposed to understand what it is actually doing, and I found that the ks.test
function calls the function psmirnov2x
in C code (which I could not found). The inputs of this function are $D$ (maximum distance between CDFs), length(x)
and length(y)
, so I understand that in this function the p-value is also based on the lengths of both vectors. Am I wrong thinking that this is not the correct approach? Or should I find a different way of doing this test?
spatstat
or generate them artificially. $\endgroup$