# Ripley K function value in a specific radius and dataset using R's Kest function

I'm having general trouble with calculating Ripley's K function values.

The following is a simple spatial point pattern, where both X and Y range from 0 to 200:

Here's its corresponding Ripley K chart in R:

The problem is, I'm completely failing to understand how does R get K(10) ~ 21000.

Here's my step by step solution:

The Ripley K function, according to "Spatial point pattern analysis of available and exploited resources", Lancaster, Downes, 2004, is defined as: $$K(r) = {n^{-2}A \sum_{i}^n \sum_{j\ne i}^n w_{ij}I_r(u_{ij})}$$

Where:

First off, let's calculate the weights: Weight of point #1 obviously equals 1. However, #2 and #3 are near the domain edge, and since they have the same x coordinate, and aren't near the upper boundaries, they're going to be equal.

The weight will be the blue, boundary area divided by the area of the red sphere. Boundary area is equal to the area of the red sphere minus the area of the white circular segment.

The circular segment's area equals exactly 79.27, so:

$$w_{2,1}=w_{2,3}=w_{3,1}=w_{3,2}= {\pi10^2-79.27\over \pi10^2}={234.73\over 314}\approx0.75$$

Only the points #2 and #3 are within each other's radius, so the indicator function will be set to 1 only twice: between #2 and #3 and vice versa.

So the final result should be:

$$K(r)=3^{-2}\cdot40000 \cdot(1\cdot0+1\cdot0+0.75\cdot0+0.75\cdot1+0.75\cdot0+0.75\cdot1)=\\=4444.(4)\cdot1.5=6666.(6)$$

Which is vastly smaller than R's ~21000.

Also, the Kest function has a different definition of K function:

Kest(r) = (a/(n * (n-1))) * sum[i,j] I(d[i,j] <= r) e[i,j])

In that case:

$$Kest(r)=(40000/(3\cdot(3-1)))\cdot1.5=10000$$

Also wrong.

What am I doing wrong here? Is this some simple math mistake I've made somewhere, or is there something about R's Kest function that I'm missing?

• Thank you for a detailed and well written question. I don't have time to check what you are doing in detail right now, but at least I can refer you to the spatstat book for details about the spatstat implementation. Chapter 7 is a free sample chapter which includes everything about the K-function: <book.spatstat.org/sample-chapters/chapter07.pdf> Mar 16 '19 at 9:35

Once again: Thank you for a detailed and well written question. The main points are:

• The weights are given in terms of circumference and not area.
• The weights are greater than or equal to 1 (inverses of what you describe – with area replaced by circumference).
• You can find the weights in spatstat using edge.Ripley().

The following example has two points separated by r = 1 with 1/4 of the circumference outside the 10x10 window (i.e. area 100). Thus the value should jump from 0 to 100/(2 ⋅ 1) ⋅ ((3/4) − 1 + (3/4) − 1) = 100 ⋅ 4/3 = 133.333.

library(spatstat)
x <- c(4.5,5.5)
y <- c(10,10)-sqrt(2)/2
W <- square(10)
X <- ppp(x, y, W)
plot(X %mark% 1, markscale = 2, main = "", legend = FALSE)


Kfun <- Kest(X, correction = "iso")
plot(Kfun)


Kval <- as.data.frame(Kest(X, correction = "iso"))
Kval[Kval$$r>.99 & Kval$$r<1.01,]
#>             r     theo      iso
#> 204 0.9912109 3.086612   0.0000
#> 205 0.9960938 3.117097   0.0000
#> 206 1.0009766 3.147732 133.3333
#> 207 1.0058594 3.178516 133.3333
edge.Ripley(X, c(1,1))
#>          [,1]
#> [1,] 1.333333
#> [2,] 1.333333


Created on 2019-03-21 by the reprex package (v0.2.1)

• Thank you for the answer, however I have one more question due to the multidimensional nature of the project I'm working on: How does K function behave in dimensions other than 2D? In 2D, for homogenous Poisson point process it's roughly equal to pi*r^2, which is obviously the formula for the area of a 2D sphere. Thinking this way, is it just equal to n-sphere volume formulas in n-dimensions, e.g. 4/3 * pi * r^3 for 3D, or is it more complicated? Apr 1 '19 at 20:35
• That is correct. It is related to the volume of a sphere because it is counting the number of points occuring within distance r of a point of the process... I hope it makes sense. Apr 1 '19 at 21:09