# Two-species occupancy and interactions
# https://tidyecology.com/posts/two-species-occupancy-interactions/
#
# 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-16

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

te_ink <- "#16241d"; te_forest <- "#275139"; te_gold <- "#c9b458"
te_rust <- "#b5534e"; te_sage <- "#93a87f"

theme_te <- function() {
  theme_minimal(base_size = 11) +
    theme(
          plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
          panel.background = element_rect(fill = "#f5f4ee", colour = NA),
          panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = "grey90", linewidth = 0.3),
          plot.title = element_text(colour = te_ink, face = "bold", size = 11),
          plot.subtitle = element_text(colour = "grey35", size = 9),
          axis.title = element_text(colour = te_ink, size = 9),
          axis.text = element_text(colour = "grey30", size = 8),
          legend.position = "bottom",
          legend.title = element_blank(),
          legend.text = element_text(size = 8))
}
two_panel <- function(p1, p2) {
  grid.newpage()
  pushViewport(viewport(layout = grid.layout(1, 2)))
  print(p1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
  print(p2, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
}

## ---- model ----
state_probs <- function(f1, f2, f12) {
  num <- cbind(exp(f1 + f2 + f12), exp(f1), exp(f2), 1)
  num / rowSums(num)
}
rota_nll <- function(par, dA, dB, K, X = NULL) {
  n <- length(dA)
  if (is.null(X)) { f1 <- rep(par[1], n); f2 <- rep(par[2], n); f12 <- par[3]
    pA <- plogis(par[4]); pB <- plogis(par[5])
  } else { f1 <- par[1] + par[6] * X; f2 <- par[2] + par[7] * X; f12 <- par[3]
    pA <- plogis(par[4]); pB <- plogis(par[5])
  }
  P <- state_probs(f1, f2, f12)
  LA1 <- pA^dA * (1 - pA)^(K - dA); LA0 <- as.numeric(dA == 0)
  LB1 <- pB^dB * (1 - pB)^(K - dB); LB0 <- as.numeric(dB == 0)
  -sum(log(pmax(P[, 1] * LA1 * LB1 + P[, 2] * LA1 * LB0 +
                P[, 3] * LA0 * LB1 + P[, 4] * LA0 * LB0, 1e-300)))
}
ct <- list(maxit = 20000, reltol = 1e-12)
fit_rota <- function(dA, dB, K, X = NULL, start = NULL) {
  if (is.null(start)) start <- if (is.null(X)) rep(0, 5) else rep(0, 7)
  o <- optim(start, rota_nll, dA = dA, dB = dB, K = K, X = X,
             method = "Nelder-Mead", control = ct)
  o <- optim(o$par, rota_nll, dA = dA, dB = dB, K = K, X = X,
             method = "Nelder-Mead", control = ct)
  H <- optimHess(o$par, rota_nll, dA = dA, dB = dB, K = K, X = X)
  list(par = o$par, se = sqrt(diag(solve(H))), nll = o$value,
       aic = 2 * o$value + 2 * length(o$par))
}
gen <- function(R, f1, f2, f12, pA, pB, K, seed) {
  set.seed(seed)
  P <- state_probs(f1, f2, f12)
  s <- apply(P, 1, function(pr) sample.int(4, 1, prob = pr))
  zA <- as.integer(s %in% c(1, 2)); zB <- as.integer(s %in% c(1, 3))
  list(dA = rbinom(R, K, zA * pA), dB = rbinom(R, K, zB * pB), zA = zA, zB = zB)
}

## ---- identity ----
sif <- function(f1, f2, f12) {
  P <- state_probs(f1, f2, f12)
  psiA <- P[1, 1] + P[1, 2]; psiB <- P[1, 1] + P[1, 3]
  P[1, 1] / (psiA * psiB)
}
sif_null <- sif(-0.2, -0.1, 0)
sif_pos <- sif(-0.2, -0.1, 1.2)

## ---- demo ----
R <- 400L; K <- 5L
f1_t <- -0.2; f2_t <- -0.1; f12_t <- 1.2; pA_t <- 0.45; pB_t <- 0.40
g <- gen(R, rep(f1_t, R), rep(f2_t, R), f12_t, pA_t, pB_t, K, 4276)
fit <- fit_rota(g$dA, g$dB, K)
f12_ci <- fit$par[3] + c(-1.96, 1.96) * fit$se[3]
both_true <- sum(g$zA & g$zB)
both_seen <- sum(g$dA > 0 & g$dB > 0)

## ---- mc ----
M <- 200L
mc_run <- function(f12, seed0) {
  t(sapply(1:M, function(m) {
    gg <- gen(R, rep(f1_t, R), rep(f2_t, R), f12, pA_t, pB_t, K, seed0 + m)
    ff <- fit_rota(gg$dA, gg$dB, K)
    c(est = ff$par[3], se = ff$se[3])
  }))
}
mc_alt <- mc_run(f12_t, 50000L)
mc_null <- mc_run(0, 60000L)
z_alt <- mc_alt[, "est"] / mc_alt[, "se"]
z_null <- mc_null[, "est"] / mc_null[, "se"]
cov95 <- mean(abs(mc_alt[, "est"] - f12_t) < 1.96 * mc_alt[, "se"])
power <- mean(abs(z_alt) > 1.96)
fpr <- mean(abs(z_null) > 1.96)

## ---- fig-mc ----
P_hat <- state_probs(fit$par[1], fit$par[2], fit$par[3])[1, ]
P_true <- state_probs(f1_t, f2_t, f12_t)[1, ]
lab <- c("both", "only A", "only B", "neither")
df1 <- data.frame(state = factor(rep(lab, 2), levels = lab),
                  prob = c(P_true, P_hat),
                  src = rep(c("true", "fitted"), each = 4))
p1 <- ggplot(df1, aes(state, prob, fill = src)) +
  geom_col(position = position_dodge(width = 0.7), width = 0.62) +
  scale_fill_manual(values = c("true" = te_sage, "fitted" = te_forest)) +
  labs(title = "The latent states are recovered", x = NULL, y = "probability",
       subtitle = "from ticks alone, no state is ever seen") +
  theme_te()

df2 <- data.frame(est = c(mc_null[, "est"], mc_alt[, "est"]),
                  scenario = rep(c("no interaction", "interaction of 1.2"), each = M))
p2 <- ggplot(df2, aes(est, fill = scenario)) +
  geom_histogram(bins = 26, alpha = 0.65, position = "identity") +
  geom_vline(xintercept = c(0, f12_t), linetype = "dashed", colour = te_ink,
             linewidth = 0.3) +
  scale_fill_manual(values = c("no interaction" = te_gold,
                               "interaction of 1.2" = te_forest)) +
  labs(title = "Calibrated and unbiased", x = "estimated interaction", y = "replicates",
       subtitle = "dashed: the two truths") +
  theme_te()
two_panel(p1, p2)

## ---- confound ----
set.seed(4283)
x <- rnorm(R)
b1 <- 1.4; b2 <- 1.2; a1 <- -0.3; a2 <- -0.2
gc <- gen(R, a1 + b1 * x, a2 + b2 * x, 0, pA_t, pB_t, K, 4284)
fit_naive <- fit_rota(gc$dA, gc$dB, K)
fit_adj <- fit_rota(gc$dA, gc$dB, K, X = x)
ci_naive <- fit_naive$par[3] + c(-1.96, 1.96) * fit_naive$se[3]
ci_adj <- fit_adj$par[3] + c(-1.96, 1.96) * fit_adj$se[3]
z_naive <- fit_naive$par[3] / fit_naive$se[3]
z_adj <- fit_adj$par[3] / fit_adj$se[3]

## ---- err ----
sd_u <- c(0, 0.25, 0.5, 1.0, 2.0)
err <- t(sapply(sd_u, function(s) {
  set.seed(4290)
  w <- x + rnorm(R, 0, s)
  ff <- fit_rota(gc$dA, gc$dB, K, X = w)
  c(sd_u = s, est = ff$par[3], se = ff$se[3],
    rel = var(x) / (var(x) + s^2))
}))
err <- as.data.frame(err)
err$z <- err$est / err$se

## ---- fig-conf ----
df3 <- data.frame(
  what = factor(c("no covariate", "covariate in model"),
                levels = c("no covariate", "covariate in model")),
  est = c(fit_naive$par[3], fit_adj$par[3]),
  lo = c(ci_naive[1], ci_adj[1]), hi = c(ci_naive[2], ci_adj[2]))
p3 <- ggplot(df3, aes(est, what)) +
  geom_vline(xintercept = 0, linetype = "dashed", colour = te_ink, linewidth = 0.4) +
  geom_errorbar(aes(xmin = lo, xmax = hi), orientation = "y", width = 0.12, colour = te_forest,
                linewidth = 0.7) +
  geom_point(size = 2.6, colour = te_rust) +
  labs(title = "There is no interaction in these data",
       x = "estimated interaction", y = NULL,
       subtitle = "dashed: the truth") +
  theme_te()

p4 <- ggplot(err, aes(sd_u, est)) +
  geom_hline(yintercept = 0, linetype = "dashed", colour = te_ink, linewidth = 0.3) +
  geom_ribbon(aes(ymin = est - 1.96 * se, ymax = est + 1.96 * se),
              fill = te_sage, alpha = 0.35) +
  geom_line(linewidth = 0.8, colour = te_forest) +
  geom_point(size = 1.8, colour = te_rust) +
  labs(title = "The fix is only as good as the proxy",
       x = "measurement error in the covariate",
       y = "estimated interaction",
       subtitle = "reliability falls from 1 to about 0.2 across this range") +
  theme_te()
two_panel(p3, p4)
