# Misread rings and the dispersal tail
# https://tidyecology.com/posts/false-matches-and-the-dispersal-tail/
#
# 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-10-07

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

te_paper  <- "#f5f4ee"
te_ink    <- "#16241d"
te_body   <- "#2c3a31"
te_forest <- "#275139"
te_rust   <- "#b5534e"
te_gold   <- "#c9b458"
te_line   <- "#dad9ca"

theme_datasheet <- function() {
  theme_minimal(base_size = 12) +
    theme(plot.background  = element_rect(fill = te_paper, colour = NA),
          panel.background = element_rect(fill = te_paper, colour = NA),
          panel.grid.major = element_line(colour = te_line, linewidth = 0.3),
          panel.grid.minor = element_blank(),
          text             = element_text(colour = te_body),
          plot.title       = element_text(colour = te_ink, face = "bold"),
          plot.subtitle    = element_text(colour = te_body),
          axis.text        = element_text(colour = te_body))
}

## ---- sci-tex-helper ----
sci_tex <- function(x, digits = 2) {
  s <- formatC(x, format = "e", digits = digits)
  paste0("$", sub("e.*", "", s), " \\times 10^{", as.integer(sub(".*e", "", s)), "}$")
}

## ---- simulator ----
n_rec <- 400L
n_site <- 60L
side_km <- 300
far_km <- 50
mid_km <- 20
n_ds <- 200L
thin_scale <- 4
fat_a <- 4
fat_p <- 1.2

ld_2dt <- function(r, a, p) log(2 * p * r / a^2) - (p + 1) * log1p(r^2 / a^2)
r_2dt <- function(n, a, p) a * sqrt(runif(n)^(-1 / p) - 1)
surv_2dt <- function(r, a, p) (1 + r^2 / a^2)^(-p)
surv_exp2 <- function(r, a) exp(-r / a) * (1 + r / a)
mean_2dt <- function(a, p) ifelse(p > 0.5, a * gamma(p - 0.5) * gamma(1.5) / gamma(p), Inf)

make_ds <- function(harm, truth) {
  link_rate <- harm * n_site / (n_site - 1)
  sx <- runif(n_site, 0, side_km)
  sy <- runif(n_site, 0, side_km)
  origin <- sample.int(n_site, n_rec, replace = TRUE)
  moved <- if (truth == "thin") rgamma(n_rec, 2, scale = thin_scale) else
    r_2dt(n_rec, fat_a, fat_p)
  angle <- runif(n_rec, 0, 2 * pi)
  rx <- sx[origin] + moved * cos(angle)
  ry <- sy[origin] + moved * sin(angle)
  relinked <- runif(n_rec) < link_rate
  link <- origin
  link[relinked] <- sample.int(n_site, sum(relinked), replace = TRUE)
  r_obs <- pmax(sqrt((rx - sx[link])^2 + (ry - sy[link])^2), 1e-3)
  dist_all <- sqrt(outer(rx, sx, "-")^2 + outer(ry, sy, "-")^2)
  list(r_obs = r_obs, dist_all = dist_all, moved = moved,
       harmful = relinked & link != origin, relinked = relinked)
}
fat_mean <- mean_2dt(fat_a, fat_p)

## ---- fitters ----
fit_exp <- function(r) {
  a <- mean(r) / 2
  list(a = a, ll = sum(dgamma(r, 2, scale = a, log = TRUE)))
}
fit_2dt <- function(r) {
  nll <- function(th) -sum(ld_2dt(r, exp(th[1]), exp(th[2])))
  o <- optim(c(log(median(r)), log(1.5)), nll, method = "BFGS")
  list(a = exp(o$par[1]), p = exp(o$par[2]), ll = -o$value)
}
fit_mix_exp <- function(r_own, dist_all) {
  n <- length(r_own)
  nll <- function(th) {
    a <- exp(th[1]); lam <- plogis(th[2])
    g_own <- exp(-r_own / a)
    f_null <- rowMeans(exp(-dist_all / a))
    -sum(log((1 - lam) * g_own + lam * f_null)) + n * log(2 * pi * a^2)
  }
  grad <- function(th) {
    a <- exp(th[1]); lam <- plogis(th[2])
    g_own <- exp(-r_own / a)
    g_all <- exp(-dist_all / a)
    f_null <- rowMeans(g_all)
    dens <- (1 - lam) * g_own + lam * f_null
    c(-sum(((1 - lam) * g_own * r_own / a + lam * rowMeans(g_all * dist_all / a)) / dens) + 2 * n,
      -sum(lam * (1 - lam) * (f_null - g_own) / dens))
  }
  o <- optim(c(log(median(r_own) / 1.7), qlogis(0.01)), nll, grad, method = "BFGS")
  list(a = exp(o$par[1]), lam = plogis(o$par[2]), mean = 2 * exp(o$par[1]))
}
fit_mix_2dt <- function(r_own, dist_all) {
  n <- length(r_own)
  r2 <- r_own^2
  d2 <- dist_all^2
  parts <- function(th) {
    a2 <- exp(2 * th[1]); p <- exp(th[2]); lam <- plogis(th[3])
    q_own <- r2 / a2; l_own <- log1p(q_own); g_own <- exp(-(p + 1) * l_own)
    q_all <- d2 / a2; l_all <- log1p(q_all); g_all <- exp(-(p + 1) * l_all)
    f_null <- rowMeans(g_all)
    list(a2 = a2, p = p, lam = lam, q_own = q_own, l_own = l_own, g_own = g_own,
         q_all = q_all, l_all = l_all, g_all = g_all, f_null = f_null,
         dens = (1 - lam) * g_own + lam * f_null)
  }
  nll <- function(th) {
    z <- parts(th)
    -sum(log(z$dens)) - n * log(z$p / (pi * z$a2))
  }
  grad <- function(th) {
    z <- parts(th)
    d1_own <- z$g_own * 2 * (z$p + 1) * z$q_own / (1 + z$q_own)
    d1_all <- rowMeans(z$g_all * 2 * (z$p + 1) * z$q_all / (1 + z$q_all))
    d2_own <- -z$g_own * z$p * z$l_own
    d2_all <- rowMeans(-z$g_all * z$p * z$l_all)
    c(-sum(((1 - z$lam) * d1_own + z$lam * d1_all) / z$dens) + 2 * n,
      -sum(((1 - z$lam) * d2_own + z$lam * d2_all) / z$dens) - n,
      -sum(z$lam * (1 - z$lam) * (z$f_null - z$g_own) / z$dens))
  }
  st <- c(log(median(r_own)), log(1.5), qlogis(0.01))
  o <- tryCatch(optim(st, nll, grad, method = "BFGS"),
                error = function(e) optim(st, nll, method = "Nelder-Mead",
                                          control = list(maxit = 3000)))
  list(a = exp(o$par[1]), p = exp(o$par[2]), lam = plogis(o$par[3]),
       mean = mean_2dt(exp(o$par[1]), exp(o$par[2])))
}

## ---- example ----
set.seed(32701)
ex <- make_ds(0.02, "thin")
ex_naive <- fit_2dt(ex$r_obs)
ex_mix <- fit_mix_2dt(ex$r_obs, ex$dist_all)
ex_n_false <- sum(ex$harmful)
ex_min_false <- min(ex$r_obs[ex$harmful])
ex_max_true <- max(ex$r_obs[!ex$harmful])
print(round(c(n_false = ex_n_false, min_false_km = ex_min_false,
              max_true_km = ex_max_true, naive_p = ex_naive$p,
              mix_p = ex_mix$p, mix_lambda = ex_mix$lam), 3))

## ---- fig-example ----
ex_sorted <- order(ex$r_obs)
ex_pts <- data.frame(r = ex$r_obs[ex_sorted],
                     surv = 1 - (seq_len(n_rec) - 0.5) / n_rec,
                     link = ifelse(ex$harmful[ex_sorted], "false link", "true link"))
r_grid <- exp(seq(log(0.3), log(400), length.out = 300))
ex_curves <- rbind(
  data.frame(r = r_grid, surv = surv_exp2(r_grid, thin_scale), fit = "true kernel"),
  data.frame(r = r_grid, surv = surv_2dt(r_grid, ex_naive$a, ex_naive$p), fit = "naive 2Dt"),
  data.frame(r = r_grid, surv = surv_2dt(r_grid, ex_mix$a, ex_mix$p),
             fit = "2Dt + register null"))
ex_curves$fit <- factor(ex_curves$fit, levels = c("true kernel", "naive 2Dt",
                                                  "2Dt + register null"))
ex_curves <- ex_curves[ex_curves$surv > 1e-4, ]
ggplot() +
  geom_line(data = ex_curves, aes(r, surv, colour = fit, linetype = fit), linewidth = 0.8) +
  geom_point(data = ex_pts, aes(r, surv, shape = link, fill = link), size = 1.6,
             colour = te_ink, stroke = 0.3) +
  scale_x_log10(breaks = c(0.1, 1, 10, 100), labels = c("0.1", "1", "10", "100")) +
  scale_y_log10(breaks = 10^(-4:0), labels = c("0.0001", "0.001", "0.01", "0.1", "1")) +
  scale_colour_manual(values = c("true kernel" = te_ink, "naive 2Dt" = te_rust,
                                 "2Dt + register null" = te_forest), name = NULL) +
  scale_linetype_manual(values = c("true kernel" = 2, "naive 2Dt" = 1,
                                   "2Dt + register null" = 1), name = NULL) +
  scale_shape_manual(values = c("true link" = 21, "false link" = 24), name = NULL) +
  scale_fill_manual(values = c("true link" = te_line, "false link" = te_rust), name = NULL) +
  labs(x = "Recorded distance r (km, log scale)", y = "P(distance > r)",
       title = sprintf("%d wrong rings make a fat tail", ex_n_false)) +
  theme_datasheet() +
  theme(legend.position = "bottom", legend.box = "vertical",
        legend.spacing.y = unit(0, "pt"), legend.margin = margin(0, 0, 0, 0))

## ---- cells ----
one_ds <- function(harm, truth) {
  ds <- make_ds(harm, truth)
  r_obs <- ds$r_obs
  fe <- fit_exp(r_obs)
  ft <- fit_2dt(r_obs)
  ftrim <- fit_2dt(r_obs[r_obs <= quantile(r_obs, 0.95)])
  me <- fit_mix_exp(r_obs, ds$dist_all)
  mt <- fit_mix_2dt(r_obs, ds$dist_all)
  far <- r_obs > far_km
  mid <- r_obs > mid_km & !far
  g_own <- exp(-r_obs / me$a)
  f_null <- rowMeans(exp(-ds$dist_all / me$a))
  me_post <- me$lam * f_null / ((1 - me$lam) * g_own + me$lam * f_null)
  c(harm = mean(ds$harmful), relinked = mean(ds$relinked),
    n_far = sum(far), n_far_false = sum(far & ds$harmful),
    t_wins = as.numeric(-2 * ft$ll + 4 < -2 * fe$ll + 2),
    true_mean = mean(ds$moved), naive_mean = mean(r_obs),
    naive_p = ft$p, trim_p = ftrim$p,
    me_lam = me$lam, me_mean = me$mean, me_post_far = sum(me_post[far]),
    me_post_mid = sum(me_post[mid]), me_post_all = sum(me_post),
    mt_lam = mt$lam, mt_p = mt$p, mt_mean = mt$mean)
}
cells <- data.frame(truth = c(rep("thin", 5), "fat", "fat"),
                    harm = c(0, 0.0025, 0.005, 0.01, 0.02, 0, 0.01))
per_ds <- vector("list", nrow(cells))
for (i in seq_len(nrow(cells))) {
  set.seed(32710 + i)
  m <- t(replicate(n_ds, one_ds(cells$harm[i], cells$truth[i])))
  per_ds[[i]] <- data.frame(truth = cells$truth[i], harm_set = cells$harm[i], m)
}
summ <- do.call(rbind, lapply(per_ds, function(d) data.frame(
  truth = d$truth[1], harm_set = d$harm_set[1],
  relinked = mean(d$relinked), harm = mean(d$harm),
  naive_p = median(d$naive_p), naive_p_lo = quantile(d$naive_p, 0.1),
  naive_p_hi = quantile(d$naive_p, 0.9),
  mt_p = median(d$mt_p), mt_p_lo = quantile(d$mt_p, 0.1), mt_p_hi = quantile(d$mt_p, 0.9),
  trim_p = median(d$trim_p), trim_ratio = median(d$trim_p / d$naive_p),
  trim_ratio_min = min(d$trim_p / d$naive_p),
  naive_mean = mean(d$naive_mean), true_mean = mean(d$true_mean),
  me_lam = mean(d$me_lam), me_lam_se = sd(d$me_lam) / sqrt(nrow(d)),
  mt_lam = mean(d$mt_lam), mt_lam_se = sd(d$mt_lam) / sqrt(nrow(d)),
  me_mean = mean(d$me_mean), mt_mean = median(d$mt_mean),
  far_false = sum(d$n_far_false) / sum(d$n_far), n_far = mean(d$n_far),
  t_wins = mean(d$t_wins))))
rownames(summ) <- NULL
th <- summ[summ$truth == "thin", ]
ft0 <- summ[summ$truth == "fat" & summ$harm_set == 0, ]
ft1 <- summ[summ$truth == "fat" & summ$harm_set == 0.01, ]
print(summ[, c("truth", "harm_set", "relinked", "harm", "naive_p", "mt_p",
               "naive_mean", "me_lam", "mt_lam")], digits = 3)

## ---- fig-exponent-drift ----
drift <- rbind(
  data.frame(harm = th$harm, p = th$naive_p, lo = th$naive_p_lo, hi = th$naive_p_hi,
             fit = "naive 2Dt"),
  data.frame(harm = th$harm, p = th$mt_p, lo = th$mt_p_lo, hi = th$mt_p_hi,
             fit = "2Dt + register null"))
drift$fit <- factor(drift$fit, levels = c("naive 2Dt", "2Dt + register null"))
drift$x <- 100 * drift$harm + ifelse(drift$fit == "naive 2Dt", -0.03, 0.03)
ref_lines <- data.frame(y = c(th$naive_p[1], fat_p),
                        lab = c("clean thin sample, naive reading", "fat truth, p = 1.2"))
ggplot(drift, aes(x, p, colour = fit)) +
  geom_hline(data = ref_lines, aes(yintercept = y), linetype = 2,
             colour = c(te_body, te_gold), linewidth = 0.5) +
  annotate("text", x = c(1.5, 0.6), y = ref_lines$y + c(0.07, -0.07), label = ref_lines$lab,
           hjust = 0.5, size = 3.3, colour = te_body) +
  geom_errorbar(aes(ymin = lo, ymax = hi), width = 0.05, linewidth = 0.5) +
  geom_line(linewidth = 0.6) +
  geom_point(size = 2.4) +
  scale_colour_manual(values = c("naive 2Dt" = te_rust, "2Dt + register null" = te_forest),
                      name = NULL) +
  labs(x = "Harmful false links (per cent of recoveries)", y = "Fitted tail exponent p",
       title = "The join picks the tail") +
  theme_datasheet() + theme(legend.position = "bottom")

## ---- arithmetic ----
set.seed(32720)
n_pair <- 1e6
pair_d <- sqrt((runif(n_pair, 0, side_km) - runif(n_pair, 0, side_km))^2 +
               (runif(n_pair, 0, side_km) - runif(n_pair, 0, side_km))^2)
p_far_pair <- mean(pair_d > far_km)
mean_pair <- mean(pair_d)
p_far_thin <- surv_exp2(far_km, thin_scale)
e_far <- n_rec * (th$harm * p_far_pair + p_far_thin)
aic_pred <- 1 - exp(-e_far)
aic_se <- sqrt(th$t_wins * (1 - th$t_wins) / n_ds)
mean_pred <- (1 - th$harm) * 8 + th$harm * mean_pair
far_pred <- th$harm * p_far_pair / (th$harm * p_far_pair + (1 - th$harm) * p_far_thin)
thin_false_ds <- do.call(rbind, per_ds[2:5])
thin_false_ds <- thin_false_ds[thin_false_ds$n_far_false > 0, ]
flip_false <- mean(thin_false_ds$t_wins)
clean_wins <- sum(per_ds[[1]]$t_wins)
clean_wins_nofar <- sum(per_ds[[1]]$t_wins == 1 & per_ds[[1]]$n_far == 0)
print(round(c(flip_given_false_far = flip_false, clean_wins = clean_wins,
              clean_wins_no_far = clean_wins_nofar), 3))
print(round(rbind(far_false = th$far_false, far_closed = far_pred, aic_share = th$t_wins, aic_poisson = aic_pred,
                  naive_mean = th$naive_mean, mean_closed = mean_pred), 3))

## ---- trimming ----
trim_ratio <- function(d) d$trim_p / d$naive_p
tr_thin2 <- trim_ratio(per_ds[[5]])
tr_fat0 <- trim_ratio(per_ds[[6]])
tr_thin0 <- trim_ratio(per_ds[[1]])
jump_rule <- 1.5
share_jump <- c(thin2 = mean(tr_thin2 > jump_rule), fat0 = mean(tr_fat0 > jump_rule),
                thin0 = mean(tr_thin0 > jump_rule))
p_order <- mean(outer(tr_thin2, tr_fat0, ">"))
p_order0 <- mean(outer(tr_thin2, tr_thin0, ">"))
print(round(rbind(median = c(median(tr_thin2), median(tr_fat0), median(tr_thin0)),
                  minimum = c(min(tr_thin2), min(tr_fat0), min(tr_thin0)),
                  share_over_rule = share_jump), 3))

## ---- recovery ----
lam_ratio_thin <- th$mt_lam[-1] / th$relinked[-1]
lam_ratio_fat <- ft1$mt_lam / ft1$relinked
print(round(rbind(harm = summ$harm, relinked = summ$relinked, lam_exp_null = summ$me_lam, se_exp = summ$me_lam_se,
                  lam_2dt_null = summ$mt_lam, se_2dt = summ$mt_lam_se,
                  mean_exp_null = summ$me_mean, mean_2dt_null = summ$mt_mean), 4))

## ---- failure ----
fat0_ds <- per_ds[[6]]
fat1_ds <- per_ds[[7]]
fail_lam0 <- mean(fat0_ds$me_lam)
fail_lam0_q <- quantile(fat0_ds$me_lam, c(0.1, 0.9))
fail_lam1 <- mean(fat1_ds$me_lam)
fail_excess <- fail_lam1 - ft1$relinked
fail_mean0 <- ft0$me_mean
real_far0 <- ft0$n_far
post_far <- mean(fat0_ds$me_post_far)
post_far_share <- sum(fat0_ds$me_post_far) / sum(fat0_ds$n_far)
post_mid <- mean(fat0_ds$me_post_mid)
post_all <- mean(fat0_ds$me_post_all)
print(round(c(post_far = post_far, post_far_share = post_far_share, post_mid = post_mid,
              post_all = post_all, n_times_lam = n_rec * fail_lam0), 3))
print(round(c(exp_null_lam_0 = fail_lam0, q10 = fail_lam0_q[[1]], q90 = fail_lam0_q[[2]],
              twodt_null_lam_0 = ft0$mt_lam, exp_null_lam_1 = fail_lam1,
              harm_1 = ft1$harm, exp_null_mean_0 = fail_mean0,
              real_far_records = real_far0), 4))

## ---- fig-lambda ----
lam_long <- do.call(rbind, lapply(per_ds, function(d) rbind(
  data.frame(truth = d$truth, harm_set = d$harm_set, lam = d$me_lam, fit = "exponential + null"),
  data.frame(truth = d$truth, harm_set = d$harm_set, lam = d$mt_lam, fit = "2Dt + null"))))
lam_long$cell <- factor(sprintf("%s kernel, %.2f%%", lam_long$truth, 100 * lam_long$harm_set),
                        levels = unique(sprintf("%s kernel, %.2f%%", cells$truth, 100 * cells$harm)))
lam_long$fit <- factor(lam_long$fit, levels = c("exponential + null", "2Dt + null"))
harm_ref <- data.frame(cell = levels(lam_long$cell), harm = summ$harm)
harm_ref$cell <- factor(harm_ref$cell, levels = levels(lam_long$cell))
ggplot(lam_long, aes(cell, 100 * lam, fill = fit)) +
  geom_boxplot(outlier.size = 0.5, linewidth = 0.3, colour = te_ink,
               position = position_dodge(width = 0.8), width = 0.7) +
  geom_errorbar(data = harm_ref, aes(x = cell, ymin = 100 * harm, ymax = 100 * harm),
                inherit.aes = FALSE, width = 0.85, linewidth = 1, colour = te_rust) +
  scale_fill_manual(values = c("exponential + null" = te_gold, "2Dt + null" = te_forest),
                    name = NULL) +
  labs(x = NULL, y = "Estimated false links (per cent)",
       title = "A thin family eats real dispersal") +
  theme_datasheet() +
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 30, hjust = 1))

## ---- colonial ----
move_mat <- function(d2_col, stay, a, p) {
  wt <- exp(-(p + 1) * log1p(d2_col / a^2))
  diag(wt) <- 0
  pm <- (1 - stay) * wt / rowSums(wt)
  diag(pm) <- stay
  pm
}
one_col <- function(harm, continuous = FALSE) {
  link_rate <- harm * n_site / (n_site - 1)
  sx <- runif(n_site, 0, side_km)
  sy <- runif(n_site, 0, side_km)
  d2_col <- outer(sx, sx, "-")^2 + outer(sy, sy, "-")^2
  pm <- move_mat(d2_col, 0.6, fat_a, fat_p)
  origin <- sample.int(n_site, n_rec, replace = TRUE)
  cum_p <- t(apply(pm, 1, cumsum))
  found <- 1L + rowSums(runif(n_rec) > cum_p[origin, , drop = FALSE])
  relinked <- runif(n_rec) < link_rate
  link <- origin
  link[relinked] <- sample.int(n_site, sum(relinked), replace = TRUE)
  nll <- function(th, mix) {
    pm_h <- move_mat(d2_col, plogis(th[1]), exp(th[2]), exp(th[3]))
    lam_h <- if (mix) plogis(th[4]) else 0
    -sum(log((1 - lam_h) * pm_h[cbind(link, found)] + lam_h * colMeans(pm_h)[found]))
  }
  o_naive <- optim(c(0, log(fat_a), log(fat_p)), nll, mix = FALSE,
                   method = "Nelder-Mead", control = list(maxit = 2000))
  o_mix <- optim(c(o_naive$par, qlogis(0.01)), nll, mix = TRUE,
                 method = "Nelder-Mead", control = list(maxit = 3000))
  r_own <- pmax(sqrt(d2_col[cbind(link, found)]), 1e-3)
  out <- c(harm = mean(relinked & link != origin), naive_p = exp(o_naive$par[3]),
           mix_p = exp(o_mix$par[3]), mix_lam = plogis(o_mix$par[4]),
           stay_hat = plogis(o_mix$par[1]), stayed = mean(r_own < 1))
  if (continuous) {
    cont <- fit_mix_2dt(r_own, sqrt(d2_col[found, , drop = FALSE]))
    out <- c(out, cont_lam = cont$lam, cont_p = cont$p)
  }
  out
}
col_ds <- lapply(c(0, 0.01), function(h) {
  set.seed(32790 + round(1e4 * h))
  data.frame(harm_set = h, t(replicate(n_ds, one_col(h))))
})
col_sum <- do.call(rbind, lapply(col_ds, function(d) data.frame(
  harm_set = d$harm_set[1], harm = mean(d$harm), naive_p = median(d$naive_p),
  mix_p = median(d$mix_p), mix_p_lo = quantile(d$mix_p, 0.1),
  mix_p_hi = quantile(d$mix_p, 0.9), mix_lam = mean(d$mix_lam),
  mix_lam_se = sd(d$mix_lam) / sqrt(nrow(d)), stay_hat = median(d$stay_hat),
  stayed = mean(d$stayed))))
rownames(col_sum) <- NULL
col0 <- col_sum[1, ]
col1 <- col_sum[2, ]
n_cont <- 25L
set.seed(32799)
cont_ds <- data.frame(t(replicate(n_cont, one_col(0, continuous = TRUE))))
cont_lam_q <- quantile(cont_ds$cont_lam, c(0, 0.5))
cont_p_min <- min(cont_ds$cont_p)
cont_n_one <- sum(1 - cont_ds$cont_lam <= .Machine$double.eps)
print(col_sum, digits = 3)
print(signif(c(cont_lam_min = cont_lam_q[[1]], cont_lam_median = cont_lam_q[[2]],
               cont_p_min = cont_p_min, cont_n_one = cont_n_one), 3))

## ---- fig-colonial ----
col_long <- do.call(rbind, lapply(col_ds, function(d) rbind(
  data.frame(harm_set = d$harm_set, p = d$naive_p, fit = "naive discrete kernel"),
  data.frame(harm_set = d$harm_set, p = d$mix_p, fit = "discrete kernel + register null"))))
col_long$fit <- factor(col_long$fit, levels = c("naive discrete kernel",
                                                "discrete kernel + register null"))
col_long$cell <- factor(ifelse(col_long$harm_set == 0, "no false links", "1% false links"),
                        levels = c("no false links", "1% false links"))
ggplot(col_long, aes(cell, p, fill = fit)) +
  geom_hline(yintercept = fat_p, linetype = 2, colour = te_body, linewidth = 0.5) +
  geom_boxplot(outlier.size = 0.5, linewidth = 0.3, colour = te_ink, width = 0.6,
               position = position_dodge(width = 0.75)) +
  scale_fill_manual(values = c("naive discrete kernel" = te_rust,
                               "discrete kernel + register null" = te_forest), name = NULL) +
  labs(x = NULL, y = "Fitted tail exponent p", title = "Colonies need the discrete form") +
  theme_datasheet() + theme(legend.position = "bottom")
