# Network centrality and sampling effort
# https://tidyecology.com/posts/network-centrality-sampling-effort/
#
# 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)
theme_set(theme_minimal(base_size = 12) +
  theme(plot.background = element_rect(fill = "#f5f4ee", colour = NA),
        panel.background = element_rect(fill = "#f5f4ee", colour = NA),
        panel.grid.minor = element_blank()))
knitr::opts_chunk$set(dev.args = list(bg = "#f5f4ee"))

## ---- metrics ----
sri_from_gbi <- function(gbi) {
  co <- crossprod(gbi); n <- diag(co)
  a <- co / (outer(n, n, "+") - co); diag(a) <- 0; a
}
node_degree   <- function(a) rowSums(a > 0)
node_strength <- function(a) rowSums(a)
eigen_cent    <- function(a) { e <- eigen(a, symmetric = TRUE); v <- abs(e$vectors[, 1]); v / max(v) }

## ---- population ----
set.seed(4)
n_ind <- 21; n_grp <- 55
community <- rep(1:3, each = 7)
effort <- runif(n_ind, 0.85, 1.3)
cohesion <- c(0.70, 0.40, 0.55)[community]
bridge <- 7
gbi <- matrix(0L, n_grp, n_ind)
for (k in 1:n_grp) {
  fc <- sample(1:3, 1)
  base <- ifelse(community == fc, cohesion, 0.05) * effort
  if (fc == 2) base[bridge] <- 0.45
  gbi[k, ] <- rbinom(n_ind, 1, pmin(base, 0.95))
}
gbi <- gbi[rowSums(gbi) > 0, ]; keep <- colSums(gbi) > 0; gbi <- gbi[, keep]
community <- community[keep]; count <- colSums(gbi)

net <- sri_from_gbi(gbi)
deg <- node_degree(net); str <- node_strength(net); eig <- eigen_cent(net)

## ---- rank-cor ----
c(degree_vs_strength = round(cor(deg, str, method = "spearman"), 3),
  degree_vs_eigen    = round(cor(deg, eig, method = "spearman"), 3),
  strength_vs_eigen  = round(cor(str, eig, method = "spearman"), 3))

## ---- ranks ----
ranks <- data.frame(
  id = seq_len(ncol(gbi)), community = community, count = count,
  degree_rank = rank(-deg), strength_rank = rank(-str), eigen_rank = rank(-eig))
ranks[order(ranks$strength_rank), ][1:6, ]

## ---- fig-disagree ----
df <- data.frame(id = seq_len(ncol(gbi)), degree = deg, eigen = eig,
                 community = factor(community), count = count)
ggplot(df, aes(degree, eigen, colour = community)) +
  geom_point(aes(size = count), alpha = 0.85) +
  annotate("text", x = df$degree[bridge], y = df$eigen[bridge],
           label = "bridge (id 7)", vjust = -1, hjust = 1, size = 3.4) +
  scale_colour_manual(values = c("#2b5d34", "#c46a1b", "#3a6ea5")) +
  scale_size_continuous(name = "sightings", range = c(2, 6)) +
  labs(title = "Degree and eigenvector centrality measure different things",
       x = "degree (number of associates)", y = "eigenvector centrality")

## ---- effort-confound ----
c(cor_strength_count = round(cor(str, count), 3),
  count_range_low  = min(count),
  count_range_high = max(count))

## ---- fig-effort ----
df$strength <- str
ggplot(df, aes(count, strength, colour = community)) +
  geom_point(size = 3, alpha = 0.85) +
  scale_colour_manual(values = c("#2b5d34", "#c46a1b", "#3a6ea5"), guide = "none") +
  labs(title = "Raw strength rises with how often you looked",
       x = "number of sightings", y = "network strength")
