Where points fall
When the locations themselves are the data, test complete spatial randomness with Clark–Evans, Ripley's K and L, and quadrat counts.
What runs in CodeSplash
- STAT256Clark–Evans nearest neighbor test Runs in CodeSplash
- STAT257Ripley K or L envelope test Runs in CodeSplash
- Point patterns (Clark–Evans, Ripley's K and L envelopes) Verified in Python and R
- Quadrat counts (test of complete spatial randomness) Verified in Python and R
You will be able to
- State what complete spatial randomness assumes
- Read a Clark–Evans ratio and a Ripley L envelope as clustered or regular
- Explain why edges and global envelopes matter
Read first
The locations are the data
ReadSo far each site carried a value. Now nothing is measured except where things are: trees, cells, stores, crimes.
The reference is complete spatial randomnessComplete spatial randomnessPoints placed independently and uniformly over the window, the usual null for point patterns. (CSR): each point lands anywhere in the window with equal chance, independently of the others. Against that, a pattern can be clustered, with points closer together than chance, or regular, with points kept apart.
Nearest neighbors
PredictPredict first
The Clark–EvansClark–Evans ratioMean nearest-neighbor distance over what random points would give; below 1 clustered, above 1 regular. ratio divides the mean nearest-neighbor distance by the value expected under CSR:
Biological cells can't overlap, so each keeps its distance from the next. What do you expect for ?
Reveal
Above 1: = 1.56, p < .001. Clustered redwood seedlings go the other way: = 0.58, p = .005 from simulated patterns.
The edge problem
ReadA point near the boundary has neighbors you can't see outside the window, so its nearest-neighbor distance is too long.
On 80 random points, ignoring this gives = 1.036. Donnelly's correction raises the expected distance and gives = 0.987, p = .831.
Every distance at once
ReadNearest neighbors only see the smallest scale. Ripley's KRipley's K and LHow many other points lie within distance r of a typical point, against what random points would give. L(r) = r under randomness. counts how many other points lie within distance , for every :
Under CSR, . So above zero means more neighbors than chance at that distance, and below zero means fewer.
Read the curve
PredictPredict first
At short distances, which way does go for the cells, whose points keep apart?
Reveal
Below zero: the largest deviation is −0.085 at = 0.11, with p = .010.
The redwoods go the other way: 0.051 at = 0.06, p = .010.
Global, not pointwise
ReadComparing the curve with an envelope at each distance separately is one test per distance, so somewhere the curve is likely to poke out by chance.
A global envelope sets one band for the whole range. Its width comes from the maximum deviation across all distances in each simulated pattern.
T = 0.0509, p = .010, n = 62
- T
- 0.0509
- p
- .010
- Distance of largest gap
- 0.0645
Plots
View data
| x | y |
|---|---|
| 0.3600 | −0.0800 |
| 0.4400 | −0.1000 |
| 0.4800 | −0.0800 |
| 0.4800 | −0.1400 |
| 0.5000 | −0.1000 |
| 0.7600 | −0.1400 |
| 0.7800 | −0.1200 |
| 0.7800 | −0.1600 |
| 0.8400 | −0.0800 |
| 0.8600 | −0.1800 |
| 0.9000 | −0.0800 |
| 0.9000 | −0.1000 |
| 0.9000 | −0.1600 |
| 0.9000 | −0.2000 |
| 0.1800 | −0.4000 |
| 0.1800 | −0.3800 |
| 0.2000 | −0.4200 |
| 0.6000 | −0.3400 |
| 0.6200 | −0.3400 |
| 0.6400 | −0.3600 |
| 0.6400 | −0.2800 |
| 0.6800 | −0.3200 |
| 0.6800 | −0.2400 |
| 0.7000 | −0.2800 |
| 0.7200 | −0.2600 |
| 0.1000 | −0.5800 |
| 0.1200 | −0.5800 |
| 0.1200 | −0.6200 |
| 0.1400 | −0.5800 |
| 0.1800 | −0.5600 |
| 0.1800 | −0.5400 |
| 0.2000 | −0.5200 |
| 0.2000 | −0.5000 |
| 0.2200 | −0.4600 |
| 0.2400 | −0.4800 |
| 0.5200 | −0.5800 |
| 0.5000 | −0.6000 |
| 0.5800 | −0.5800 |
| 0.9400 | −0.5200 |
| 0.9600 | −0.5400 |
| 0.2200 | −0.8000 |
| 0.2200 | −0.8400 |
| 0.2600 | −0.7000 |
| 0.4400 | −0.7600 |
| 0.4800 | −0.7800 |
| 0.5000 | −0.7600 |
| 0.9000 | −0.7600 |
| 0.9000 | −0.7800 |
| 0.4800 | −0.6800 |
| 0.5200 | −0.6600 |
| 0.5600 | −0.6400 |
| 0.9990 | −0.8400 |
| 0.4400 | −0.8200 |
| 0.4800 | −0.8200 |
| 0.3400 | −0.8400 |
| 0.3800 | −0.8400 |
| 0.2600 | −0.8600 |
| 0.4000 | −0.8600 |
| 0.2800 | −0.8600 |
| 0.7400 | −0.9000 |
First 60 of 62 rows.
View data
| L(r) | Pointwise high | Global high | Pointwise low | Global low | Distance r |
|---|---|---|---|---|---|
| 0 | 0 | 0.0234 | 0 | −0.0234 | 0 |
| 0 | 0.0130 | 0.0254 | 0 | −0.0215 | 0.0020 |
| 0 | 0.0130 | 0.0273 | 0 | −0.0195 | 0.0039 |
| 0 | 0.0183 | 0.0293 | 0 | −0.0176 | 0.0059 |
| 0 | 0.0183 | 0.0313 | 0 | −0.0156 | 0.0078 |
| 0 | 0.0225 | 0.0332 | 0 | −0.0137 | 0.0098 |
| 0 | 0.0259 | 0.0352 | 0 | −0.0117 | 0.0117 |
| 0 | 0.0259 | 0.0371 | 0 | −0.0098 | 0.0137 |
| 0 | 0.0259 | 0.0391 | 0 | −0.0078 | 0.0156 |
| 0 | 0.0290 | 0.0410 | 0 | −0.0059 | 0.0176 |
| 0 | 0.0295 | 0.0430 | 0 | −0.0039 | 0.0195 |
| 0.0389 | 0.0320 | 0.0449 | 0 | −0.0020 | 0.0215 |
| 0.0389 | 0.0362 | 0.0469 | 0 | 0 | 0.0234 |
| 0.0389 | 0.0367 | 0.0488 | 0 | 0.0020 | 0.0254 |
| 0.0389 | 0.0378 | 0.0508 | 0 | 0.0039 | 0.0273 |
| 0.0595 | 0.0397 | 0.0527 | 0 | 0.0059 | 0.0293 |
| 0.0595 | 0.0461 | 0.0547 | 0.0183 | 0.0078 | 0.0313 |
| 0.0595 | 0.0485 | 0.0566 | 0.0183 | 0.0098 | 0.0332 |
| 0.0595 | 0.0513 | 0.0586 | 0.0183 | 0.0117 | 0.0352 |
| 0.0595 | 0.0515 | 0.0605 | 0.0225 | 0.0137 | 0.0371 |
| 0.0595 | 0.0535 | 0.0625 | 0.0225 | 0.0156 | 0.0391 |
| 0.0699 | 0.0546 | 0.0645 | 0.0230 | 0.0176 | 0.0410 |
| 0.0699 | 0.0590 | 0.0664 | 0.0259 | 0.0195 | 0.0430 |
| 0.0917 | 0.0636 | 0.0684 | 0.0264 | 0.0215 | 0.0449 |
| 0.0917 | 0.0652 | 0.0703 | 0.0294 | 0.0234 | 0.0469 |
| 0.0917 | 0.0665 | 0.0723 | 0.0343 | 0.0254 | 0.0488 |
| 0.0917 | 0.0667 | 0.0742 | 0.0343 | 0.0273 | 0.0508 |
| 0.0917 | 0.0693 | 0.0762 | 0.0367 | 0.0293 | 0.0527 |
| 0.0917 | 0.0729 | 0.0781 | 0.0389 | 0.0313 | 0.0547 |
| 0.0988 | 0.0768 | 0.0801 | 0.0410 | 0.0332 | 0.0566 |
| 0.0988 | 0.0779 | 0.0820 | 0.0430 | 0.0352 | 0.0586 |
| 0.1054 | 0.0800 | 0.0840 | 0.0430 | 0.0371 | 0.0605 |
| 0.1054 | 0.0815 | 0.0859 | 0.0468 | 0.0391 | 0.0625 |
| 0.1153 | 0.0829 | 0.0879 | 0.0492 | 0.0410 | 0.0645 |
| 0.1153 | 0.0829 | 0.0898 | 0.0500 | 0.0430 | 0.0664 |
| 0.1153 | 0.0870 | 0.0918 | 0.0531 | 0.0449 | 0.0684 |
| 0.1153 | 0.0880 | 0.0938 | 0.0556 | 0.0469 | 0.0703 |
| 0.1224 | 0.0901 | 0.0957 | 0.0579 | 0.0488 | 0.0723 |
| 0.1224 | 0.0908 | 0.0977 | 0.0595 | 0.0508 | 0.0742 |
| 0.1224 | 0.0917 | 0.0996 | 0.0627 | 0.0527 | 0.0762 |
| 0.1224 | 0.0936 | 0.1016 | 0.0645 | 0.0547 | 0.0781 |
| 0.1258 | 0.0936 | 0.1035 | 0.0670 | 0.0566 | 0.0801 |
| 0.1258 | 0.0950 | 0.1055 | 0.0692 | 0.0586 | 0.0820 |
| 0.1317 | 0.0967 | 0.1074 | 0.0698 | 0.0605 | 0.0840 |
| 0.1349 | 0.0990 | 0.1094 | 0.0727 | 0.0625 | 0.0859 |
| 0.1349 | 0.1000 | 0.1113 | 0.0741 | 0.0645 | 0.0879 |
| 0.1392 | 0.1027 | 0.1133 | 0.0753 | 0.0664 | 0.0898 |
| 0.1392 | 0.1053 | 0.1152 | 0.0764 | 0.0684 | 0.0918 |
| 0.1392 | 0.1067 | 0.1172 | 0.0775 | 0.0703 | 0.0938 |
| 0.1392 | 0.1101 | 0.1191 | 0.0804 | 0.0723 | 0.0957 |
| 0.1392 | 0.1117 | 0.1211 | 0.0804 | 0.0742 | 0.0977 |
| 0.1392 | 0.1132 | 0.1230 | 0.0817 | 0.0762 | 0.0996 |
| 0.1487 | 0.1153 | 0.1250 | 0.0836 | 0.0781 | 0.1016 |
| 0.1522 | 0.1169 | 0.1270 | 0.0856 | 0.0801 | 0.1035 |
| 0.1522 | 0.1194 | 0.1289 | 0.0881 | 0.0820 | 0.1055 |
| 0.1522 | 0.1221 | 0.1309 | 0.0893 | 0.0840 | 0.1074 |
| 0.1556 | 0.1250 | 0.1328 | 0.0925 | 0.0859 | 0.1094 |
| 0.1556 | 0.1271 | 0.1348 | 0.0952 | 0.0879 | 0.1113 |
| 0.1567 | 0.1309 | 0.1367 | 0.0968 | 0.0898 | 0.1133 |
| 0.1567 | 0.1326 | 0.1387 | 0.0992 | 0.0918 | 0.1152 |
First 60 of 129 rows.
Assumptions
- Observation window known — The window is the rectangle you stated.
- Constant intensity — Density that varies across the window mimics clustering between points. These tests cannot tell the two apart.
- Distances fixed in advance — Pointwise envelopes do not control error over the whole range; use the global test's p.
L(r) with its envelopes
| r | K(r) | L(r) | Pointwise low | Pointwise high | Global low | Global high |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | −0.0234 | 0.0234 |
| 0.0059 | 0 | 0 | 0 | 0.0183 | −0.0176 | 0.0293 |
| 0.0117 | 0 | 0 | 0 | 0.0259 | −0.0117 | 0.0352 |
| 0.0176 | 0 | 0 | 0 | 0.0290 | −0.0059 | 0.0410 |
| 0.0234 | 0.0048 | 0.0389 | 0 | 0.0362 | 0 | 0.0469 |
| 0.0293 | 0.0111 | 0.0595 | 0 | 0.0397 | 0.0059 | 0.0527 |
| 0.0352 | 0.0111 | 0.0595 | 0.0183 | 0.0513 | 0.0117 | 0.0586 |
| 0.0410 | 0.0153 | 0.0699 | 0.0230 | 0.0546 | 0.0176 | 0.0645 |
| 0.0469 | 0.0264 | 0.0917 | 0.0294 | 0.0652 | 0.0234 | 0.0703 |
| 0.0527 | 0.0264 | 0.0917 | 0.0367 | 0.0693 | 0.0293 | 0.0762 |
| 0.0586 | 0.0307 | 0.0988 | 0.0430 | 0.0779 | 0.0352 | 0.0820 |
| 0.0645 | 0.0418 | 0.1153 | 0.0492 | 0.0829 | 0.0410 | 0.0879 |
| 0.0703 | 0.0418 | 0.1153 | 0.0556 | 0.0880 | 0.0469 | 0.0938 |
| 0.0762 | 0.0471 | 0.1224 | 0.0627 | 0.0917 | 0.0527 | 0.0996 |
| 0.0820 | 0.0497 | 0.1258 | 0.0692 | 0.0950 | 0.0586 | 0.1055 |
| 0.0879 | 0.0571 | 0.1349 | 0.0741 | 0.1000 | 0.0645 | 0.1113 |
| 0.0938 | 0.0608 | 0.1392 | 0.0775 | 0.1067 | 0.0703 | 0.1172 |
| 0.0996 | 0.0608 | 0.1392 | 0.0817 | 0.1132 | 0.0762 | 0.1230 |
| 0.1055 | 0.0727 | 0.1522 | 0.0881 | 0.1194 | 0.0820 | 0.1289 |
| 0.1113 | 0.0761 | 0.1556 | 0.0952 | 0.1271 | 0.0879 | 0.1348 |
| 0.1172 | 0.0822 | 0.1618 | 0.1030 | 0.1329 | 0.0938 | 0.1406 |
| 0.1230 | 0.0889 | 0.1682 | 0.1063 | 0.1388 | 0.0996 | 0.1465 |
| 0.1289 | 0.1007 | 0.1791 | 0.1113 | 0.1442 | 0.1055 | 0.1523 |
| 0.1348 | 0.1034 | 0.1814 | 0.1173 | 0.1508 | 0.1113 | 0.1582 |
| 0.1406 | 0.1050 | 0.1828 | 0.1257 | 0.1550 | 0.1172 | 0.1641 |
| 0.1465 | 0.1164 | 0.1925 | 0.1332 | 0.1628 | 0.1230 | 0.1699 |
| 0.1523 | 0.1206 | 0.1960 | 0.1384 | 0.1679 | 0.1289 | 0.1758 |
| 0.1582 | 0.1224 | 0.1974 | 0.1435 | 0.1742 | 0.1348 | 0.1816 |
| 0.1641 | 0.1285 | 0.2022 | 0.1494 | 0.1805 | 0.1406 | 0.1875 |
| 0.1699 | 0.1319 | 0.2049 | 0.1554 | 0.1841 | 0.1465 | 0.1934 |
| 0.1758 | 0.1337 | 0.2063 | 0.1615 | 0.1911 | 0.1523 | 0.1992 |
| 0.1816 | 0.1388 | 0.2102 | 0.1697 | 0.1964 | 0.1582 | 0.2051 |
| 0.1875 | 0.1431 | 0.2134 | 0.1741 | 0.2034 | 0.1641 | 0.2109 |
| 0.1934 | 0.1517 | 0.2197 | 0.1783 | 0.2109 | 0.1699 | 0.2168 |
| 0.1992 | 0.1553 | 0.2223 | 0.1834 | 0.2163 | 0.1758 | 0.2227 |
| 0.2051 | 0.1586 | 0.2247 | 0.1908 | 0.2219 | 0.1816 | 0.2285 |
| 0.2109 | 0.1632 | 0.2279 | 0.1965 | 0.2273 | 0.1875 | 0.2344 |
| 0.2168 | 0.1714 | 0.2335 | 0.2043 | 0.2329 | 0.1934 | 0.2402 |
| 0.2227 | 0.1743 | 0.2356 | 0.2096 | 0.2382 | 0.1992 | 0.2461 |
| 0.2285 | 0.1865 | 0.2436 | 0.2154 | 0.2451 | 0.2051 | 0.2520 |
First 40 of 43 rows.
- Window area 1; isotropic edge correction; global test over distances up to 0.25 from 99 uniform patterns.
The exact R script · R R version 4.5.1 (2025-06-13)
# Point patterns (Clark–Evans, Ripley's K and L envelopes)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method spatial.points 1.0.1 · spec bdfc18810b498cd7
# Dataset: sp_redwood (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/sp_redwood.parquet", col_select = c("x", "y")))
n_input <- nrow(df)
df <- df[stats::complete.cases(df[c("x", "y")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("x", "y")) # infinite values stop the run (registry/errors.yaml)
# %% Test
# Point patterns against complete spatial randomness: Clark & Evans (1954) with Donnelly's (1978) correction, and
# Ripley's K (1977) and Besag's L with an envelope of uniform patterns from the shared random stream.
X <- "x"
Y <- "y"
test <- "ripley" # ripley | clark_evans
coordinates <- "planar" # planar | lonlat
n_sim <- as.integer(99)
alpha <- 0.05
seed <- 42
pr <- cs_spp_project(df[[X]], df[[Y]], coordinates)
px <- pr$x
py <- pr$y
n <- length(px)
wc <- cs_spp_project(c(0, 1), c(-1, 0), coordinates)
w <- cs_spp_window(px, py, "custom", wc$x[1], wc$x[2], wc$y[1], wc$y[2], names = c(X, Y))
intensity <- n / w$area
values <- list()
tables <- list()
charts <- list()
if (test == "clark_evans") {
correction <- "donnelly" # donnelly | none
calibration <- "normal" # normal | simulation
alternative <- "two_sided" # two_sided | less | greater
res <- cs_spp_clark_evans(px, py, w, correction, calibration, alternative, n_sim, seed)
statistic <- res$R
p <- res$p
test_label <- "Clark–Evans test"
test_symbol <- "R"
pattern <- if (statistic < 1) "The points are closer together than random points, a clustered pattern." else "The points are further apart than random points, a regular pattern."
values <- c(values, list(z = res$z, p_analytic = res$p_analytic, mean_nn = res$mean_nn, expected_nn = res$expected_nn, se_nn = res$se_nn,
n_sim = if (calibration == "simulation") n_sim else 0L))
detail <- sprintf("R = %.3f (mean nearest-neighbor distance %.4g, expected %.4g), %s", statistic, res$mean_nn, res$expected_nn, cs_fmt_p(p))
statistics <- list(list(key = "statistic", label = "R", value = statistic), list(key = "z", label = "z", value = res$z), list(key = "p", label = "p", value = p, format = "p"),
list(key = "mean_nn", label = "Mean nearest-neighbor distance", value = res$mean_nn))
how <- paste0(if (correction == "donnelly") "Donnelly's edge-corrected mean and standard error" else "no edge correction",
if (calibration == "simulation") "; p from simulated random patterns" else "; normal approximation")
} else {
correction <- "isotropic" # isotropic | translation | none
global_stat <- "mad" # mad | dclf
rmax <- NULL
r <- cs_spp_r_grid(w, n, rmax, as.integer(129))
res <- cs_spp_ripley_envelope(px, py, w, r, correction, global_stat, n_sim, alpha, seed)
statistic <- res$T
p <- res$p
test_label <- "Ripley's L envelope test"
test_symbol <- "T"
pattern <- if (res$dev_at_max > 0) {
sprintf("The largest gap is above randomness near distance %.3g: points cluster at that scale.", res$r_at_max)
} else {
sprintf("The largest gap is below randomness near distance %.3g: points keep apart at that scale.", res$r_at_max)
}
values <- c(values, list(T = res$T, band = res$band, r_max = r[length(r)], r_at_max = res$r_at_max, dev_at_max = res$dev_at_max,
L_at_rmax = res$L[length(r)], K_at_rmax = res$K[length(r)], n_sim = n_sim, level = res$level))
detail <- if (global_stat == "mad") sprintf("largest |L(r) − r| = %.4g at distance %.3g, %s", res$T, res$r_at_max, cs_fmt_p(p)) else sprintf("integrated squared gap %.4g, %s", res$T, cs_fmt_p(p))
statistics <- list(list(key = "statistic", label = "T", value = statistic), list(key = "p", label = "p", value = p, format = "p"),
list(key = "r_at_max", label = "Distance of largest gap", value = res$r_at_max))
how <- sprintf("%s edge correction; global test over distances up to %.3g from %d uniform patterns", correction, r[length(r)], n_sim)
step <- max(1L, length(r) %/% 33L)
idx <- seq(1L, length(r), by = step)
tables[[1]] <- list(key = "curve", title = "L(r) with its envelopes", columns = list(
list(key = "r", label = "r", type = "number"), list(key = "K", label = "K(r)", type = "number"), list(key = "L", label = "L(r)", type = "number"),
list(key = "lo", label = "Pointwise low", type = "number"), list(key = "hi", label = "Pointwise high", type = "number"),
list(key = "lo_global", label = "Global low", type = "number"), list(key = "hi_global", label = "Global high", type = "number")),
rows = lapply(idx, function(k) list(r = r[k], K = res$K[k], L = res$L[k], lo = res$lo[k], hi = res$hi[k], lo_global = res$lo_global[k], hi_global = res$hi_global[k])))
charts[[1]] <- cs_chart("envelope", "ripley-envelope", sprintf("L(r) against %d random patterns", n_sim), list(
r = cs_col("Distance r", r), L = cs_col("L(r)", res$L), lo = cs_col("Pointwise low", res$lo), hi = cs_col("Pointwise high", res$hi),
lo_global = cs_col("Global low", res$lo_global), hi_global = cs_col("Global high", res$hi_global)), controls = list(alpha = alpha))
}
charts <- c(list(cs_chart("points", "scatter", sprintf("%d points", n), list(x = cs_col(X, as.numeric(df[[X]])), y = cs_col(Y, as.numeric(df[[Y]]))))), charts)
# %% Assumptions
custom_window <- TRUE
assumptions <- list(
list(key = "window_known", label = "Observation window known", status = if (custom_window) "pass" else "info",
detail = if (custom_window) {
"The window is the rectangle you stated."
} else {
"The window is the points' bounding box, slightly too small, which makes patterns look more regular. State the searched rectangle if you know it."
}),
list(key = "homogeneous", label = "Constant intensity", status = "info",
detail = "Density that varies across the window mimics clustering between points. These tests cannot tell the two apart."),
list(key = "range_fixed", label = "Distances fixed in advance", status = "info",
detail = if (test == "ripley") "Pointwise envelopes do not control error over the whole range; use the global test's p." else "Clark–Evans looks only at nearest neighbors, so it can miss a pattern at larger scales.")
)
# %% Result
verdict <- if (p < alpha) pattern else "The points are compatible with complete spatial randomness at this scale."
summary <- sprintf("%s for %d points: %s. %s", test_label, n, detail, verdict)
values <- c(values, list(statistic = statistic, p = p, n = n, area = w$area, intensity = intensity, missing_share = if (n_input > 0) n_dropped / n_input else 0,
test_label = test_label, test_symbol = test_symbol, pattern = pattern))
for (key in c("z", "p_analytic", "mean_nn", "expected_nn", "se_nn", "T", "band", "r_max", "r_at_max", "dev_at_max", "L_at_rmax", "K_at_rmax", "level")) if (is.null(values[[key]])) values[[key]] <- NA_real_ # only some options produce these
cs_publish(list(
method = "spatial.points",
methodVersion = "1.0.1",
title = test_label,
engine = cs_engine_info("stats"),
n = n,
summary = summary,
apa = sprintf("%s = %.3g, %s, n = %d", test_symbol, statistic, cs_fmt_p(p), n),
statistics = statistics,
tables = tables,
assumptions = assumptions,
warnings = warnings,
notes = list(sprintf("Window area %.4g; %s.", w$area, how)),
values = values,
charts = charts
))
T = 0.0509, p = .010, n = 62
- T
- 0.0509
- p
- .010
- Distance of largest gap
- 0.0645
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Observation window known — The window is the rectangle you stated.
- Constant intensity — Density that varies across the window mimics clustering between points. These tests cannot tell the two apart.
- Distances fixed in advance — Pointwise envelopes do not control error over the whole range; use the global test's p.
L(r) with its envelopes
| r | K(r) | L(r) | Pointwise low | Pointwise high | Global low | Global high |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | −0.0234 | 0.0234 |
| 0.0059 | 0 | 0 | 0 | 0.0183 | −0.0176 | 0.0293 |
| 0.0117 | 0 | 0 | 0 | 0.0259 | −0.0117 | 0.0352 |
| 0.0176 | 0 | 0 | 0 | 0.0290 | −0.0059 | 0.0410 |
| 0.0234 | 0.0048 | 0.0389 | 0 | 0.0362 | 0 | 0.0469 |
| 0.0293 | 0.0111 | 0.0595 | 0 | 0.0397 | 0.0059 | 0.0527 |
| 0.0352 | 0.0111 | 0.0595 | 0.0183 | 0.0513 | 0.0117 | 0.0586 |
| 0.0410 | 0.0153 | 0.0699 | 0.0230 | 0.0546 | 0.0176 | 0.0645 |
| 0.0469 | 0.0264 | 0.0917 | 0.0294 | 0.0652 | 0.0234 | 0.0703 |
| 0.0527 | 0.0264 | 0.0917 | 0.0367 | 0.0693 | 0.0293 | 0.0762 |
| 0.0586 | 0.0307 | 0.0988 | 0.0430 | 0.0779 | 0.0352 | 0.0820 |
| 0.0645 | 0.0418 | 0.1153 | 0.0492 | 0.0829 | 0.0410 | 0.0879 |
| 0.0703 | 0.0418 | 0.1153 | 0.0556 | 0.0880 | 0.0469 | 0.0938 |
| 0.0762 | 0.0471 | 0.1224 | 0.0627 | 0.0917 | 0.0527 | 0.0996 |
| 0.0820 | 0.0497 | 0.1258 | 0.0692 | 0.0950 | 0.0586 | 0.1055 |
| 0.0879 | 0.0571 | 0.1349 | 0.0741 | 0.1000 | 0.0645 | 0.1113 |
| 0.0938 | 0.0608 | 0.1392 | 0.0775 | 0.1067 | 0.0703 | 0.1172 |
| 0.0996 | 0.0608 | 0.1392 | 0.0817 | 0.1132 | 0.0762 | 0.1230 |
| 0.1055 | 0.0727 | 0.1522 | 0.0881 | 0.1194 | 0.0820 | 0.1289 |
| 0.1113 | 0.0761 | 0.1556 | 0.0952 | 0.1271 | 0.0879 | 0.1348 |
| 0.1172 | 0.0822 | 0.1618 | 0.1030 | 0.1329 | 0.0938 | 0.1406 |
| 0.1230 | 0.0889 | 0.1682 | 0.1063 | 0.1388 | 0.0996 | 0.1465 |
| 0.1289 | 0.1007 | 0.1791 | 0.1113 | 0.1442 | 0.1055 | 0.1523 |
| 0.1348 | 0.1034 | 0.1814 | 0.1173 | 0.1508 | 0.1113 | 0.1582 |
| 0.1406 | 0.1050 | 0.1828 | 0.1257 | 0.1550 | 0.1172 | 0.1641 |
| 0.1465 | 0.1164 | 0.1925 | 0.1332 | 0.1628 | 0.1230 | 0.1699 |
| 0.1523 | 0.1206 | 0.1960 | 0.1384 | 0.1679 | 0.1289 | 0.1758 |
| 0.1582 | 0.1224 | 0.1974 | 0.1435 | 0.1742 | 0.1348 | 0.1816 |
| 0.1641 | 0.1285 | 0.2022 | 0.1494 | 0.1805 | 0.1406 | 0.1875 |
| 0.1699 | 0.1319 | 0.2049 | 0.1554 | 0.1841 | 0.1465 | 0.1934 |
| 0.1758 | 0.1337 | 0.2063 | 0.1615 | 0.1911 | 0.1523 | 0.1992 |
| 0.1816 | 0.1388 | 0.2102 | 0.1697 | 0.1964 | 0.1582 | 0.2051 |
| 0.1875 | 0.1431 | 0.2134 | 0.1741 | 0.2034 | 0.1641 | 0.2109 |
| 0.1934 | 0.1517 | 0.2197 | 0.1783 | 0.2109 | 0.1699 | 0.2168 |
| 0.1992 | 0.1553 | 0.2223 | 0.1834 | 0.2163 | 0.1758 | 0.2227 |
| 0.2051 | 0.1586 | 0.2247 | 0.1908 | 0.2219 | 0.1816 | 0.2285 |
| 0.2109 | 0.1632 | 0.2279 | 0.1965 | 0.2273 | 0.1875 | 0.2344 |
| 0.2168 | 0.1714 | 0.2335 | 0.2043 | 0.2329 | 0.1934 | 0.2402 |
| 0.2227 | 0.1743 | 0.2356 | 0.2096 | 0.2382 | 0.1992 | 0.2461 |
| 0.2285 | 0.1865 | 0.2436 | 0.2154 | 0.2451 | 0.2051 | 0.2520 |
First 40 of 43 rows.
- Window area 1; isotropic edge correction; global test over distances up to 0.25 from 99 uniform patterns.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Point patterns (Clark–Evans, Ripley's K and L envelopes)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method spatial.points 1.0.1 · spec bdfc18810b498cd7
# Dataset: sp_redwood (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.stats import spatial_points as spp
# %% Data
df = pd.read_parquet("tests/parity/fixtures/sp_redwood.parquet", columns=["x", "y"])
n_input = len(df)
df = df.dropna(subset=["x", "y"]) # listwise deletion over the analysis columns
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["x", "y"]) # infinite values stop the run (registry/errors.yaml)
# %% Test
# Point patterns against complete spatial randomness: Clark & Evans (1954) with Donnelly's (1978) correction, and
# Ripley's K (1977) and Besag's L with an envelope of uniform patterns from the shared random stream.
X = "x"
Y = "y"
test = "ripley" # ripley | clark_evans
coordinates = "planar" # planar | lonlat
n_sim = int(99)
alpha = 0.05
seed = 42
px, py = spp.project(df[X].to_numpy(dtype=float), df[Y].to_numpy(dtype=float), coordinates)
n = px.size
wx0, wy0 = spp.project(np.array([0, 1]), np.array([-1, 0]), coordinates)
w = spp.window(px, py, "custom", wx0[0], wx0[1], wy0[0], wy0[1], names=(X, Y))
intensity = n / w["area"]
values = {}
tables = []
charts = []
statistics = []
if test == "clark_evans":
correction = "donnelly" # donnelly | none
calibration = "normal" # normal | simulation
alternative = "two_sided" # two_sided | less | greater
res = spp.clark_evans(px, py, w, correction, calibration, alternative, n_sim, seed)
statistic, p = res["R"], res["p"]
test_label, test_symbol = "Clark–Evans test", "R"
if statistic < 1:
pattern = "The points are closer together than random points, a clustered pattern."
else:
pattern = "The points are further apart than random points, a regular pattern."
values.update({"z": res["z"], "p_analytic": res["p_analytic"], "mean_nn": res["mean_nn"], "expected_nn": res["expected_nn"], "se_nn": res["se_nn"],
"n_sim": n_sim if calibration == "simulation" else 0})
detail = f"R = {statistic:.3f} (mean nearest-neighbor distance {res['mean_nn']:.4g}, expected {res['expected_nn']:.4g}), {cs.fmt_p(p)}"
statistics = [{"key": "statistic", "label": "R", "value": statistic}, {"key": "z", "label": "z", "value": res["z"]}, {"key": "p", "label": "p", "value": p, "format": "p"},
{"key": "mean_nn", "label": "Mean nearest-neighbor distance", "value": res["mean_nn"]}]
how = ("Donnelly's edge-corrected mean and standard error" if correction == "donnelly" else "no edge correction") + ("; p from simulated random patterns" if calibration == "simulation" else "; normal approximation")
else:
correction = "isotropic" # isotropic | translation | none
global_stat = "mad" # mad | dclf
rmax = None
r = spp.r_grid(w, n, rmax, int(129))
res = spp.ripley_envelope(px, py, w, r, correction, global_stat, n_sim, alpha, seed)
statistic, p = res["T"], res["p"]
test_label, test_symbol = "Ripley's L envelope test", "T"
if res["dev_at_max"] > 0:
pattern = f"The largest gap is above randomness near distance {res['r_at_max']:.3g}: points cluster at that scale."
else:
pattern = f"The largest gap is below randomness near distance {res['r_at_max']:.3g}: points keep apart at that scale."
values.update({"T": res["T"], "band": res["band"], "r_max": float(r[-1]), "r_at_max": res["r_at_max"], "dev_at_max": res["dev_at_max"],
"L_at_rmax": float(res["L"][-1]), "K_at_rmax": float(res["K"][-1]), "n_sim": n_sim, "level": res["level"]})
detail = f"largest |L(r) − r| = {res['T']:.4g} at distance {res['r_at_max']:.3g}, {cs.fmt_p(p)}" if global_stat == "mad" else f"integrated squared gap {res['T']:.4g}, {cs.fmt_p(p)}"
statistics = [{"key": "statistic", "label": "T", "value": statistic}, {"key": "p", "label": "p", "value": p, "format": "p"},
{"key": "r_at_max", "label": "Distance of largest gap", "value": res["r_at_max"]}]
how = f"{correction} edge correction; global test over distances up to {float(r[-1]):.3g} from {n_sim} uniform patterns"
step = max(1, r.size // 33)
idx = np.arange(0, r.size, step)
tables.append({"key": "curve", "title": "L(r) with its envelopes", "columns": [
{"key": "r", "label": "r", "type": "number"}, {"key": "K", "label": "K(r)", "type": "number"}, {"key": "L", "label": "L(r)", "type": "number"},
{"key": "lo", "label": "Pointwise low", "type": "number"}, {"key": "hi", "label": "Pointwise high", "type": "number"},
{"key": "lo_global", "label": "Global low", "type": "number"}, {"key": "hi_global", "label": "Global high", "type": "number"}],
"rows": [{"r": float(r[k]), "K": float(res["K"][k]), "L": float(res["L"][k]), "lo": float(res["lo"][k]), "hi": float(res["hi"][k]),
"lo_global": float(res["lo_global"][k]), "hi_global": float(res["hi_global"][k])} for k in idx]})
charts.append(cs.chart("envelope", "ripley-envelope", f"L(r) against {n_sim} random patterns", {
"r": cs.col("Distance r", r), "L": cs.col("L(r)", res["L"]), "lo": cs.col("Pointwise low", res["lo"]), "hi": cs.col("Pointwise high", res["hi"]),
"lo_global": cs.col("Global low", res["lo_global"]), "hi_global": cs.col("Global high", res["hi_global"])}, controls={"alpha": alpha}))
charts.insert(0, cs.chart("points", "scatter", f"{n} points", {"x": cs.col(X, df[X].to_numpy(dtype=float)), "y": cs.col(Y, df[Y].to_numpy(dtype=float))}))
# %% Assumptions
assumptions = [
{"key": "window_known", "label": "Observation window known", "status": "pass" if True else "info",
"detail": ("The window is the rectangle you stated." if True
else "The window is the points' bounding box, slightly too small, which makes patterns look more regular. State the searched rectangle if you know it.")},
{"key": "homogeneous", "label": "Constant intensity", "status": "info",
"detail": "Density that varies across the window mimics clustering between points. These tests cannot tell the two apart."},
{"key": "range_fixed", "label": "Distances fixed in advance", "status": "info",
"detail": "Pointwise envelopes do not control error over the whole range; use the global test's p." if test == "ripley" else "Clark–Evans looks only at nearest neighbors, so it can miss a pattern at larger scales."},
]
# %% Result
if p < alpha:
verdict = pattern
else:
verdict = "The points are compatible with complete spatial randomness at this scale."
summary = f"{test_label} for {n} points: {detail}. {verdict}"
values.update({"statistic": statistic, "p": p, "n": n, "area": w["area"], "intensity": intensity, "missing_share": n_dropped / n_input if n_input else 0.0,
"test_label": test_label, "test_symbol": test_symbol, "pattern": pattern})
for key in ["z", "p_analytic", "mean_nn", "expected_nn", "se_nn", "T", "band", "r_max", "r_at_max", "dev_at_max", "L_at_rmax", "K_at_rmax", "level"]: # only some options produce these
values.setdefault(key, None)
cs.publish({
"method": "spatial.points",
"methodVersion": "1.0.1",
"title": test_label,
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": n,
"summary": summary,
"apa": f"{test_symbol} = {statistic:.3g}, {cs.fmt_p(p)}, n = {n}",
"statistics": statistics,
"tables": tables,
"assumptions": assumptions,
"warnings": warnings,
"notes": [f"Window area {w['area']:.4g}; {how}."],
"values": values,
"charts": charts,
})
With 99 simulations, the smallest possible p is . On the random points, the same test gives p = .840.
Counting in a grid
PredictPredict first
Quadrat countsQuadrat countsCounts in a grid of equal cells, compared with what random points would give. are the plain version: split the window into equal cells and compare counts with what's expected. The variance-to-mean ratio is near 1 under CSR.
Clustered points pile up in a few cells. Where does their ratio land?
Reveal
Above 1. Redwoods: ratio 2.82, p = .005.
Cells lean below 1: ratio 0.54, p = .170, with little evidence. The result depends on the grid, and a coarse grid can miss what Clark–Evans finds.
Choosing a test and a mechanism
ReflectCrimes cluster in a city. But people cluster too: if the population isn't spread evenly, crimes would cluster without any contagion.
For a point pattern you know, what could make its intensity vary across the window? Would the CSR null answer the question you care about, or do you need a null that follows that intensity?
Check your understanding
CheckpointCheck yourself
A Clark–Evans test gives with a small p-value. What does that suggest?
Show the answer
Answer: a
below 1 means nearest neighbors are closer than expected, so the pattern is clustered. Attraction is only one possible cause: varying intensity gives the same result.
Check yourself
Why use a global envelope rather than a pointwise one?
Show the answer
Answer: c
A global envelope controls the error over the whole range of . It needs a fixed range, chosen before you look.
Check yourself
A pattern has intensity points per unit area. What mean nearest-neighbor distance does give under CSR?
Show the answer
Answer: 0.05
, before any edge correction.
Check yourself
A 4 × 4 grid of quadrats gives a chi-square test of equal counts. How many degrees of freedom does it have?
Show the answer
Answer: 15
: the number of cells minus one, because the total is fixed.
Check yourself
Quadrat counts give a variance-to-mean ratio well above 1. What does that suggest?
Show the answer
Answer: a
Under CSR the counts are close to Poisson, so the ratio is near 1. Above 1 means more variation than that, and below 1 means less.
Check yourself
You try 3 × 3, 4 × 4 and 5 × 5 grids, and only 4 × 4 is significant. What should you do?
Show the answer
Answer: b
The result depends on the grid. Trying several and reporting the best is shopping for a p-value, and Ripley's L doesn't need a grid at all.
Do it in CodeSplash
In the appPick Ripley's K and L with an envelope or Clark–Evans. Under Observation window, use a Stated rectangle if you know the study area: the bounding box of the points is smaller than the true window and makes them look more spread out.
This script reproduces the uncorrected ratio and Donnelly's expected distance, 0.0587.
import numpy as np
import pandas as pd
d = pd.read_csv("sp_guide_points.csv").to_numpy()
n, area, perimeter = len(d), 1.0, 4.0
dist = np.linalg.norm(d[:, None] - d[None, :], axis=2)
np.fill_diagonal(dist, np.inf)
mean_nn = dist.min(axis=1).mean()
csr = 0.5 * np.sqrt(area / n)
donnelly = csr + (0.0514 + 0.041 / np.sqrt(n)) * perimeter / n
print(mean_nn / csr, donnelly, mean_nn / donnelly)d <- as.matrix(read.csv("sp_guide_points.csv"))
n <- nrow(d); area <- 1; perimeter <- 4
dist <- as.matrix(dist(d))
diag(dist) <- Inf
mean_nn <- mean(apply(dist, 1, min))
csr <- 0.5 * sqrt(area / n)
donnelly <- csr + (0.0514 + 0.041 / sqrt(n)) * perimeter / n
c(mean_nn / csr, donnelly, mean_nn / donnelly)Recap
Recap- Complete spatial randomness is the reference: equal chance everywhere, with independent points.
- Clark–Evans looks at nearest neighbors; Ripley's L looks at every distance; quadrats count in a grid.
- Correct for edges, and use a global envelope over a range you fixed in advance.
- Clustering is compatible with attraction and with intensity that varies across the window.
Concepts in this lesson