# Diet choice and the zero-one rule
# https://tidyecology.com/posts/diet-choice-and-the-zero-one-rule/
#
# 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_in <- "#3a6b52"; col_out <- "#b5651d"; col_ref <- "#555555"

## two prey types: 1 profitable, 2 poor
e1 <- 8; h1 <- 1; e2 <- 2; h2 <- 1
prof1 <- e1/h1; prof2 <- e2/h2
rate_only1 <- function(l1) l1*e1 / (1 + l1*h1)
include2   <- function(l1) prof2 > rate_only1(l1)          # l2 does not appear
l1_star    <- prof2 / (e1 - h1*prof2)                       # threshold in good-prey encounter rate

## ---- profit ----
e1 <- 8; h1 <- 1     # profitable prey
e2 <- 2; h2 <- 1     # poor prey
c(profitability_1 = e1/h1, profitability_2 = e2/h2)

## ---- zero-one ----
rate_only1 <- function(l1) l1*e1 / (1 + l1*h1)
include2   <- function(l1, l2) (e2/h2) > rate_only1(l1)    # note: l2 is ignored
# vary the POOR prey abundance, hold the good prey fixed
sapply(c(0.1, 1, 5, 20), function(l2) include2(l1 = 0.2, l2 = l2))

## ---- vary-good ----
sapply(c(0.10, 0.30, 0.34, 1.0), function(l1) include2(l1 = l1, l2 = 5))

## ---- fig-grid ----
gr <- expand.grid(l1 = seq(0.02, 1, 0.02), l2 = seq(0.1, 6, 0.15))
gr$eaten <- ifelse((e2/h2) > gr$l1*e1/(1 + gr$l1*h1), "poor prey eaten", "poor prey ignored")
ggplot(gr, aes(l1, l2, fill = eaten)) +
  geom_raster() +
  geom_vline(xintercept = l1_star, colour = col_ref, linetype = "dashed") +
  scale_fill_manual(values = c("poor prey eaten" = col_in, "poor prey ignored" = col_out)) +
  labs(x = "Good-prey encounter rate", y = "Poor-prey encounter rate", fill = NULL,
       title = "The poor prey's own abundance does not matter")

## ---- fig-switch ----
l1g <- seq(0, 1.2, 0.01)
dd <- data.frame(l1 = l1g, rate1 = rate_only1(l1g))
ggplot(dd, aes(l1, rate1)) +
  geom_line(colour = col_in, linewidth = 1) +
  geom_hline(yintercept = e2/h2, colour = col_out, linetype = "dashed") +
  geom_vline(xintercept = l1_star, colour = col_ref, linetype = "dotted") +
  annotate("text", x = 0.02, y = e2/h2 + 0.25, label = "poor prey profitability", hjust = 0, colour = col_out, size = 3.4) +
  annotate("text", x = 0.02, y = 5.5, label = "rate from good prey alone", hjust = 0, colour = col_in, size = 3.4) +
  labs(x = "Good-prey encounter rate", y = "Intake rate",
       title = "Drop the poor prey once the good prey pays better")
