## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----design-------------------------------------------------------------------
library(tidycreel)

# Sampling frame: 4 sites, 1 circuit
# p_site values are proportional to expected fishing effort at each site
sampling_frame <- data.frame(
  site     = c("A", "B", "C", "D"),
  circuit  = "circ1",
  p_site   = c(0.30, 0.25, 0.40, 0.05), # must sum to 1.0
  p_period = 0.50 # uniform period sampling
)
# Inclusion probability: pi_i = p_site * p_period
# Site A: 0.30 * 0.50 = 0.15
# Site B: 0.25 * 0.50 = 0.125
# Site C: 0.40 * 0.50 = 0.20
# Site D: 0.05 * 0.50 = 0.025

# Survey calendar: 4 weekdays
calendar <- data.frame(
  date     = as.Date(c("2024-06-01", "2024-06-02", "2024-06-03", "2024-06-04")),
  day_type = "weekday"
)

# Build the bus-route design
design <- creel_design(
  calendar,
  date = date,
  strata = day_type,
  survey_type = "bus_route",
  sampling_frame = sampling_frame,
  site = site,
  circuit = circuit,
  p_site = p_site,
  p_period = p_period
)
print(design)

## ----interviews---------------------------------------------------------------
# 15 interviews across 4 sites and 4 days
# Site A: 4 interviews (all 4 parties interviewed)
# Site B: 3 interviews (all 3 parties)
# Site C: 6 interviews (all 6 parties) — highest effort site
# Site D: 2 interviews (all 2 parties)
interviews_raw <- data.frame(
  date = as.Date(c(
    "2024-06-01", "2024-06-01", "2024-06-02", "2024-06-02", # Site A
    "2024-06-01", "2024-06-02", "2024-06-03", # Site B
    "2024-06-01", "2024-06-02", "2024-06-03", # Site C (rows 1-3)
    "2024-06-03", "2024-06-04", # Site C (rows 4-5)
    "2024-06-03", "2024-06-04", # Site D
    "2024-06-03" # Site C (row 6)
  )),
  day_type = "weekday",
  site = c(
    "A", "A", "A", "A",
    "B", "B", "B",
    "C", "C", "C",
    "C", "C",
    "D", "D",
    "C"
  ),
  circuit = "circ1",
  hours_fished = c(
    7.5, 7.5, 7.5, 7.5, # Site A: 4 x 7.5 = 30 h total
    20.0 / 3, 20.0 / 3, 20.0 / 3, # Site B: 3 x 6.67 = 20 h total
    57.5 / 6, 57.5 / 6, 57.5 / 6, # Site C rows 1-3
    57.5 / 6, 57.5 / 6, # Site C rows 4-5
    2.5, 2.5, # Site D: 2 x 2.5 = 5 h total
    57.5 / 6 # Site C row 6: total = 57.5 h
  ),
  fish_kept = c(
    1L, 0L, 1L, 0L,
    1L, 0L, 1L,
    1L, 1L, 0L,
    1L, 0L,
    0L, 0L,
    1L
  ),
  fish_caught = c(
    2L, 1L, 1L, 1L,
    2L, 1L, 1L,
    2L, 1L, 1L,
    1L, 1L,
    1L, 1L,
    1L
  ),
  n_counted = c(
    4L, 4L, 4L, 4L,
    3L, 3L, 3L,
    6L, 6L, 6L,
    6L, 6L,
    2L, 2L,
    6L
  ),
  n_interviewed = c(
    4L, 4L, 4L, 4L,
    3L, 3L, 3L,
    6L, 6L, 6L,
    6L, 6L,
    2L, 2L,
    6L
  ),
  trip_status = "complete"
)

# Attach interviews — pi_i is joined automatically from the sampling frame
design <- add_interviews(
  design,
  interviews_raw,
  effort        = hours_fished,
  catch         = fish_caught,
  harvest       = fish_kept,
  n_counted     = n_counted,
  n_interviewed = n_interviewed,
  trip_status   = trip_status
)
print(design)

## ----effort-------------------------------------------------------------------
# Estimate total angler-hours
effort_est <- estimate_effort(design)
print(effort_est)

# Inspect per-site contributions to the total
site_contribs <- get_site_contributions(effort_est)
print(site_contribs)

## ----harvest------------------------------------------------------------------
# Estimate total fish kept (harvest)
harvest_est <- estimate_harvest_rate(design)
print(harvest_est)

# Estimate total catch (kept + released)
catch_est <- estimate_total_catch(design)
print(catch_est)

