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

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

## ----power-effort-------------------------------------------------------------
effort_plan <- power_creel(
  mode = "effort_n",
  target_rse = 0.20,
  strata = c("weekday", "weekend"),
  N_h = c(90, 30),
  ybar_h = c(42, 68),
  s2_h = c(196, 441)
)

effort_plan

## ----power-cpue---------------------------------------------------------------
cpue_plan <- power_creel(
  mode = "cpue_n",
  target_rse = 0.15,
  cv_catch = 0.85,
  cv_effort = 0.55,
  rho = 0.35
)

cpue_plan

## ----power-detect-------------------------------------------------------------
power_plan <- power_creel(
  mode = "power",
  n = 120L,
  cv_historical = 0.42,
  delta_pct = 0.20
)

power_plan

## ----compare-setup, warning = FALSE, message = FALSE--------------------------
data("example_counts")
data("example_interviews")

calendar <- unique(example_counts[, c("date", "day_type")])

design <- creel_design(calendar, 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,
  n_anglers = n_anglers
)

## ----compare-estimates, warning = FALSE---------------------------------------
set.seed(123)

effort_taylor <- estimate_effort(design, variance = "taylor")
effort_bootstrap <- estimate_effort(design, variance = "bootstrap")

design_comparison <- compare_designs(
  list(
    Taylor = effort_taylor,
    Bootstrap = effort_bootstrap
  )
)

design_comparison

## ----compare-plot, fig.cap = "Design comparison across two effort estimators."----
ggplot2::autoplot(design_comparison)

## ----hybrid-design------------------------------------------------------------
calendar <- data.frame(
  date = seq(as.Date("2024-06-01"), as.Date("2024-06-30"), by = "day")
)
calendar$day_type <- ifelse(
  format(calendar$date, "%u") %in% c("6", "7"), "weekend", "weekday"
)

counts <- data.frame(
  date = rep(
    as.Date(c("2024-06-03", "2024-06-08", "2024-06-10", "2024-06-15")),
    times = 2
  ),
  day_type = rep(c("weekday", "weekend", "weekday", "weekend"), times = 2),
  angler_type = rep(c("boat", "bank"), each = 4),
  count = c(12L, 18L, 9L, 21L, 10L, 16L, 8L, 19L)
)

hybrid_design <- as_hybrid_svydesign(
  counts,
  frame_col = "angler_type",
  calendar = calendar,
  fraction = list(
    boat = c(weekday = 0.5, weekend = 0.5),
    bank = c(weekday = 0.5, weekend = 0.5)
  ),
  trips_disjoint = TRUE
)

hybrid_design

## ----hybrid-total-------------------------------------------------------------
survey::svytotal(~count, hybrid_design)

