# When redrawing zones flips the sign
# https://tidyecology.com/posts/zoning-and-the-sign-of-a-relationship/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-11-04

## ---- setup ----
library(ggplot2)

knitr::opts_chunk$set(dev.args = list(bg = "#f5f4ee"))

te_pal <- list(forest = "#275139", green = "#2f8f63", sage = "#93a87f",
               clay = "#b5534e", gold = "#cda23f", line = "#dad9ca",
               ink = "#16241d", paper = "#f5f4ee")

theme_te <- function() {
  theme_minimal(base_size = 12) +
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = "#e7e6dc"),
          plot.background = element_rect(fill = "#f5f4ee", colour = NA),
          panel.background = element_rect(fill = "#f5f4ee", colour = NA),
          plot.title = element_text(face = "bold", colour = te_pal$ink),
          axis.title = element_text(colour = "#2c3a31"))
}

## ---- field ----
set.seed(227)
n_side <- 120L
gx <- matrix(rep(seq_len(n_side), each = n_side), n_side, n_side)
gy <- matrix(rep(seq_len(n_side), times = n_side), n_side, n_side)

smooth_field <- function(nn, sigma) {
  zf <- matrix(rnorm(nn * nn), nn, nn)
  kk <- ceiling(3 * sigma); dd <- (-kk):kk
  wt <- exp(-(dd^2) / (2 * sigma^2)); wt <- wt / sum(wt)
  zp <- rbind(zf[kk:1, , drop = FALSE], zf,
              zf[nn:(nn - kk + 1), , drop = FALSE])
  zp <- cbind(zp[, kk:1, drop = FALSE], zp,
              zp[, nn:(nn - kk + 1), drop = FALSE])
  o1 <- matrix(0, nrow(zp), ncol(zp))
  for (j in seq_along(dd))
    o1 <- o1 + wt[j] * zp[, pmin(pmax(seq_len(ncol(zp)) + dd[j], 1),
                                 ncol(zp)), drop = FALSE]
  o2 <- matrix(0, nrow(zp), ncol(zp))
  for (j in seq_along(dd))
    o2 <- o2 + wt[j] * o1[pmin(pmax(seq_len(nrow(zp)) + dd[j], 1),
                               nrow(zp)), , drop = FALSE]
  sm <- o2[(kk + 1):(kk + nn), (kk + 1):(kk + nn)]
  (sm - mean(sm)) / sd(sm)
}

## ---- processes ----
broad <- (gy - mean(gy)) / sd(as.vector(gy))
fine  <- smooth_field(n_side, 3)
noise <- smooth_field(n_side, 3)

xv <- fine + 1.15 * broad
yv <- -1.0 * fine + 1.15 * broad + 0.25 * noise

ind_slope  <- unname(coef(lm(as.vector(yv) ~ as.vector(xv)))[2])
fine_slope <- unname(coef(lm(as.vector(yv) ~ as.vector(fine)))[2])
fine_gap   <- 100 * abs(fine_slope - (-1.0))
broad_var  <- var(as.vector(broad))
n_cell     <- n_side * n_side
print(round(c(cells = n_cell, individual = ind_slope,
              on_fine_field = fine_slope, fine_gap_pct = fine_gap,
              gradient_variance = broad_var), 3))

## ---- fig-fields ----
map_long <- rbind(
  data.frame(px = as.vector(gx), py = as.vector(gy),
             v = as.vector(broad), panel = "regional gradient"),
  data.frame(px = as.vector(gx), py = as.vector(gy),
             v = as.vector(xv), panel = "x"),
  data.frame(px = as.vector(gx), py = as.vector(gy),
             v = as.vector(yv), panel = "y"))

ggplot(map_long, aes(px, py, fill = v)) +
  geom_raster() +
  facet_wrap(~panel) +
  coord_equal() +
  scale_fill_gradient2(low = te_pal$clay, mid = "#efeee2",
                       high = te_pal$forest, midpoint = 0, name = NULL) +
  labs(title = "One gradient, two variables, opposite fine-scale signs",
       x = NULL, y = NULL) +
  theme_te() +
  theme(axis.text = element_blank(), panel.grid = element_blank(),
        legend.key.height = unit(0.9, "lines"))

## ---- plan ----
n_unit <- 60L
unit_n <- n_cell %/% n_unit
unit_w <- n_side %/% n_unit
print(c(cells = n_cell, units = n_unit, cells_per_unit = unit_n,
        strip_width = unit_w))

## ---- zonations ----
bands  <- matrix((gy - 1L) %/% unit_w + 1L, n_side, n_side)
strips <- matrix((gx - 1L) %/% unit_w + 1L, n_side, n_side)

rand_zones <- function() {
  sx <- runif(n_unit, 0.5, n_side + 0.5)
  sy <- runif(n_unit, 0.5, n_side + 0.5)
  d2 <- outer(as.vector(gx), sx, "-")^2 + outer(as.vector(gy), sy, "-")^2
  zone <- integer(n_cell)
  room <- rep(unit_n, n_unit)
  repeat {
    free <- which(zone == 0L)
    if (!length(free)) break
    open <- which(room > 0L)
    dd <- d2[free, open, drop = FALSE]
    j <- max.col(-dd, ties.method = "first")
    want <- open[j]
    near <- dd[cbind(seq_along(free), j)]
    ok <- order(want, near)
    ws <- want[ok]
    took <- sequence(rle(ws)$lengths) <= room[ws]
    zone[free[ok][took]] <- ws[took]
    room <- room - tabulate(ws[took], n_unit)
  }
  matrix(zone, n_side, n_side)
}

agg_slope <- function(zn, xf, yf) {
  mx <- tapply(as.vector(xf), as.vector(zn), mean)
  my <- tapply(as.vector(yf), as.vector(zn), mean)
  unname(coef(lm(my ~ mx))[2])
}

## ---- designs ----
band_slope  <- agg_slope(bands, xv, yv)
strip_slope <- agg_slope(strips, xv, yv)
design_tab <- data.frame(
  design = c("bands along the gradient", "strips across the gradient"),
  units = c(length(unique(as.vector(bands))),
            length(unique(as.vector(strips)))),
  smallest_unit = c(min(table(bands)), min(table(strips))),
  largest_unit = c(max(table(bands)), max(table(strips))),
  slope = round(c(band_slope, strip_slope), 3))
print(design_tab, row.names = FALSE)
swing <- band_slope - strip_slope
print(round(c(swing = swing), 2))

## ---- zonemap ----
zone_map <- function(zn, lab) {
  mv <- tapply(as.vector(broad), as.vector(zn), mean)
  data.frame(px = as.vector(gx), py = as.vector(gy),
             v = as.numeric(mv[as.character(as.vector(zn))]), panel = lab)
}

## ---- bvar ----
retained <- function(zn, vf) {
  vv <- as.vector(vf)
  zz <- as.vector(zn)
  wt <- table(zz)
  mu <- tapply(vv, zz, mean)
  between <- sum(wt * (mu - mean(vv))^2) / length(vv)
  between / mean((vv - mean(vv))^2)
}

rand_draw <- replicate(200, {
  zn <- rand_zones()
  c(slope = agg_slope(zn, xv, yv), lo = min(table(zn)), hi = max(table(zn)))
})
rand_slopes <- rand_draw["slope", ]
z_rand <- rand_zones()

keep_share <- c(bands = retained(bands, broad), strips = retained(strips, broad),
                random = retained(z_rand, broad))
naive <- var(tapply(as.vector(broad), as.vector(bands), mean))
print(round(c(keep_share, unweighted_var_on_bands = naive), 4))
print(c(smallest_unit_over_draws = min(rand_draw["lo", ]),
        largest_unit_over_draws = max(rand_draw["hi", ])))

## ---- fig-zoning ----
zone_long <- rbind(
  zone_map(bands, sprintf("bands (%.4f)", keep_share[["bands"]])),
  zone_map(strips, sprintf("strips (%.4f)", keep_share[["strips"]])),
  zone_map(z_rand, sprintf("random (%.4f)", keep_share[["random"]])))
zone_long$panel <- factor(zone_long$panel, levels = unique(zone_long$panel))

ggplot(zone_long, aes(px, py, fill = v)) +
  geom_raster() +
  facet_wrap(~panel) +
  coord_equal() +
  scale_fill_gradient2(low = te_pal$clay, mid = "#efeee2",
                       high = te_pal$forest, midpoint = 0,
                       limits = range(broad), name = NULL) +
  labs(title = "What each design does to the regional gradient",
       subtitle = "share of the gradient's variance retained between units",
       x = NULL, y = NULL) +
  theme_te() +
  theme(axis.text = element_blank(), panel.grid = element_blank(),
        legend.key.height = unit(0.9, "lines"))

## ---- randomsummary ----
rand_summary <- c(median = median(rand_slopes), min = min(rand_slopes),
                  max = max(rand_slopes), positive = sum(rand_slopes > 0),
                  negative = sum(rand_slopes < 0))
print(round(rand_summary, 3))
rand_spread <- max(rand_slopes) / min(rand_slopes)
rand_amp <- median(rand_slopes) / ind_slope
rand_slope <- agg_slope(z_rand, xv, yv)
n_rand <- length(rand_slopes)
print(round(c(end_to_end_factor = rand_spread,
              amplification_over_cells = rand_amp,
              slope_of_the_mapped_draw = rand_slope), 3))

## ---- fig-null ----
mark_df <- data.frame(
  v = c(ind_slope, band_slope, strip_slope),
  lab = c("cells", "bands", "strips"))

hist_bw <- 0.02
brk <- seq(floor(min(rand_slopes) / hist_bw) * hist_bw,
           ceiling(max(rand_slopes) / hist_bw) * hist_bw, by = hist_bw)
lab_y <- max(hist(rand_slopes, breaks = brk, plot = FALSE)$counts)

ggplot(data.frame(v = rand_slopes), aes(v)) +
  geom_histogram(breaks = brk, fill = te_pal$green, colour = te_pal$paper,
                 linewidth = 0.2) +
  geom_vline(data = mark_df, aes(xintercept = v, colour = lab),
             linetype = "dashed", linewidth = 0.7) +
  geom_text(data = mark_df, aes(x = v, y = lab_y, label = lab, colour = lab),
            angle = 90, vjust = -0.4, hjust = 1, size = 3.5,
            show.legend = FALSE) +
  scale_colour_manual(values = c(cells = te_pal$ink, bands = te_pal$gold,
                                 strips = te_pal$clay), guide = "none") +
  labs(title = "Arbitrary boundaries are stable, informative ones are not",
       subtitle = sprintf("%d random zonations, %d units of %d cells each",
                          length(rand_slopes), n_unit, unit_n),
       x = "aggregate slope", y = "zonations") +
  theme_te()

## ---- fig-scatter ----
unit_means <- function(zn, lab) {
  data.frame(mx = as.numeric(tapply(as.vector(xv), as.vector(zn), mean)),
             my = as.numeric(tapply(as.vector(yv), as.vector(zn), mean)),
             panel = lab)
}
lab3 <- c(sprintf("bands: %+.3f", band_slope),
          sprintf("strips: %+.3f", strip_slope),
          sprintf("random: %+.3f", rand_slope))
scat <- rbind(unit_means(bands, lab3[1]), unit_means(strips, lab3[2]),
              unit_means(z_rand, lab3[3]))
scat$panel <- factor(scat$panel, levels = lab3)

ggplot(scat, aes(mx, my)) +
  geom_point(colour = te_pal$forest, size = 1.8, alpha = 0.85) +
  geom_smooth(method = "lm", formula = y ~ x, se = FALSE,
              colour = te_pal$clay, linewidth = 0.8) +
  facet_wrap(~panel) +
  labs(title = "Same cells, same unit count, three answers",
       x = "unit mean of x", y = "unit mean of y") +
  theme_te()

## ---- control ----
set.seed(227)
xa <- smooth_field(n_side, 6)
ya <- -1.0 * xa + 0.8 * smooth_field(n_side, 6)
ctrl_ind <- unname(coef(lm(as.vector(ya) ~ as.vector(xa)))[2])
ctrl_slopes <- replicate(300, agg_slope(rand_zones(), xa, ya))
ctrl_bend <- 100 * max(abs(ctrl_slopes - ctrl_ind)) / abs(ctrl_ind)
print(round(c(individual = ctrl_ind, median = median(ctrl_slopes),
              min = min(ctrl_slopes), max = max(ctrl_slopes),
              negative = sum(ctrl_slopes < 0),
              positive = sum(ctrl_slopes > 0), bend_pct = ctrl_bend), 3))
