# Checking a storage-effect analysis
# https://tidyecology.com/posts/checking-a-storage-effect-analysis/
#
# 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-15

## ---- setup ----
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE,
                      dev = "png", dpi = 120, fig.path = "figures/",
                      fig.width = 7, fig.height = 4.4)
library(ggplot2)
theme_te <- theme_minimal(base_size = 12) +
  theme(panel.background = element_rect(fill = "#f5f4ee", colour = NA),
        plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
        panel.grid.minor = element_blank(),
        plot.title = element_text(face = "bold", size = 12))
theme_set(theme_te)
col_yes <- "#3a6b52"; col_no <- "#b5651d"; col_ref <- "#555555"

states     <- list(c(3, 0.5), c(0.5, 3))     # differential response (negatively correlated)
states_id  <- list(c(3, 3),   c(0.5, 0.5))   # identical response (positively correlated)
states_hi  <- list(c(6, 6),   c(0.25, 0.25)) # larger variance, still identical response
inv_growth <- function(d, st) mean(log(sapply(st, function(s) (1 - d) + d * s[1] / s[2])))

r_buffered   <- inv_growth(0.1, states)
r_nobuffer   <- inv_growth(1.0, states)
r_identical  <- inv_growth(0.1, states_id)
r_hi_ident   <- inv_growth(0.1, states_hi)
r_const      <- log((1 - 0.1) + 0.1 * 1)     # frozen environment, mean ratio 1

## ---- buffer ----
states <- list(c(3, 0.5), c(0.5, 3))
inv_growth <- function(d, st) mean(log(sapply(st, function(s) (1 - d) + d * s[1] / s[2])))
c(buffered_longlived = inv_growth(0.1, states),   # adults survive, gains are stored
  annual_no_buffer   = inv_growth(1.0, states))   # all adults die each year, nothing stored

## ---- fig-buffer ----
db <- data.frame(life = c("long-lived\n(buffered)", "annual\n(no buffer)"),
                 r = c(r_buffered, r_nobuffer))
ggplot(db, aes(life, r, fill = life)) +
  geom_col(width = 0.55) +
  geom_hline(yintercept = 0, colour = col_ref, linewidth = 0.4) +
  scale_fill_manual(values = c("long-lived\n(buffered)" = col_yes, "annual\n(no buffer)" = col_no),
                    guide = "none") +
  labs(x = NULL, y = "Rare invader's growth rate",
       title = "No buffer, no storage effect")

## ---- response ----
states_diff <- list(c(3, 0.5), c(0.5, 3))    # good for one is bad for the other
states_same <- list(c(3, 3),   c(0.5, 0.5))  # good and bad years shared by both
c(different_response = inv_growth(0.1, states_diff),
  identical_response = inv_growth(0.1, states_same))

## ---- variance ----
states_bigvar <- list(c(6, 6), c(0.25, 0.25))   # much larger swings, but still identical response
c(more_variance_same_response = inv_growth(0.1, states_bigvar),
  frozen_environment          = r_const)

## ---- fig-grid ----
dg <- data.frame(
  scenario = factor(c("buffered +\ndifferent response", "no buffer", "identical\nresponse",
                      "high variance,\nidentical response"),
                    levels = c("buffered +\ndifferent response", "no buffer", "identical\nresponse",
                               "high variance,\nidentical response")),
  r = c(r_buffered, r_nobuffer, r_identical, r_hi_ident))
ggplot(dg, aes(scenario, r, fill = r > 1e-6)) +
  geom_col(width = 0.62) +
  geom_hline(yintercept = 0, colour = col_ref, linewidth = 0.4) +
  scale_fill_manual(values = c("TRUE" = col_yes, "FALSE" = col_no), guide = "none") +
  labs(x = NULL, y = "Rare invader's growth rate",
       title = "All three ingredients, or nothing") +
  theme(axis.text.x = element_text(size = 8))
