## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4,
  out.width = "100%"
)

## ----load---------------------------------------------------------------------
library(tidycreel)

## ----build-design, warning = FALSE--------------------------------------------
data("example_counts")
data("example_interviews")

cal <- unique(example_counts[, c("date", "day_type")])
design <- creel_design(cal, date = date, strata = day_type)
design <- add_counts(design, example_counts)
design <- add_interviews(
  design, example_interviews,
  catch = catch_total,
  effort = hours_fished,
  trip_status = trip_status
)

## ----compare, warning = FALSE-------------------------------------------------
eff_taylor <- estimate_effort(design, variance = "taylor")
eff_bootstrap <- estimate_effort(design, variance = "bootstrap")
eff_jackknife <- estimate_effort(design, variance = "jackknife")

# Summarise all three for comparison
rbind(
  as.data.frame(summary(eff_taylor)),
  as.data.frame(summary(eff_bootstrap)),
  as.data.frame(summary(eff_jackknife))
)

## ----grouped, warning = FALSE-------------------------------------------------
eff_grp_taylor <- estimate_effort(design, by = day_type, variance = "taylor")
eff_grp_bootstrap <- estimate_effort(design, by = day_type, variance = "bootstrap")

rbind(
  cbind(method = "taylor", as.data.frame(summary(eff_grp_taylor))),
  cbind(method = "bootstrap", as.data.frame(summary(eff_grp_bootstrap)))
)

## ----single-psu, error = TRUE, warning = FALSE--------------------------------
try({
# Build a minimal design with 1 sampled day per stratum
dates_1psu <- c(as.Date("2024-06-03"), as.Date("2024-06-08"))
cal_1psu <- data.frame(date = dates_1psu, day_type = c("weekday", "weekend"))
d_1psu <- creel_design(cal_1psu, date = date, strata = day_type)
counts_1psu <- data.frame(
  date     = dates_1psu,
  day_type = c("weekday", "weekend"),
  count    = c(10L, 15L)
)
d_1psu <- add_counts(d_1psu, counts_1psu)

# Taylor linearization raises an error when variance cannot be estimated
estimate_effort(d_1psu, variance = "jackknife")
})

## ----summary-table, echo = FALSE----------------------------------------------
knitr::kable(
  data.frame(
    Method = c("taylor", "bootstrap", "jackknife"),
    Deterministic = c("Yes", "No (stochastic)", "Yes"),
    `Min PSUs` = c("1 (with lonely-PSU adjust)", "2", "2"),
    Speed = c("Fast", "Slow (500 replicates)", "Moderate"),
    `When to prefer` = c(
      "Default; simple designs",
      "Skewed counts; complex designs",
      "Small samples; deterministic alternative to bootstrap"
    ),
    check.names = FALSE
  ),
  caption = "Variance method comparison"
)

