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

## ----design_counts------------------------------------------------------------
library(tidycreel)

# Load example calendar and counts
data(example_calendar)
data(example_counts)

# Create design with counts
design <- creel_design(example_calendar, date = date, strata = day_type)
design <- add_counts(design, example_counts)

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

## ----interviews---------------------------------------------------------------
# Load example interview data
data(example_interviews)
head(example_interviews)

# Attach interviews to the design
design <- add_interviews(design, example_interviews,
  catch = catch_total,
  effort = hours_fished,
  n_anglers = n_anglers,
  harvest = catch_kept,
  trip_status = trip_status,
  trip_duration = trip_duration
)
print(design)

## ----cpue---------------------------------------------------------------------
# Estimate CPUE
cpue_est <- estimate_catch_rate(design)
print(cpue_est)

## ----total_catch--------------------------------------------------------------
# Estimate total catch
total_catch_est <- estimate_total_catch(design)
print(total_catch_est)

## ----harvest------------------------------------------------------------------
# Estimate HPUE
hpue_est <- estimate_harvest_rate(design)
print(hpue_est)

# Estimate total harvest
total_harvest_est <- estimate_total_harvest(design)
print(total_harvest_est)

## ----grouped, eval=FALSE------------------------------------------------------
# # Estimate CPUE by day_type (not evaluated - example data too small)
# cpue_by_day <- estimate_catch_rate(design, by = day_type)
# 
# # Estimate total catch by day_type
# total_catch_by_day <- estimate_total_catch(design, by = day_type)

## ----variance-----------------------------------------------------------------
# Bootstrap variance estimation
set.seed(42) # For reproducibility
cpue_boot <- estimate_catch_rate(design, variance = "bootstrap")

# Jackknife variance estimation
cpue_jk <- estimate_catch_rate(design, variance = "jackknife")

## ----ages---------------------------------------------------------------------
data(example_ages)
data(example_catch)
head(example_ages)

# Ages are read from a subsample of the catch, so the age-class totals are
# scaled onto the reported catch. Grouping by species needs this table, which
# is the only record of catch per species.
design <- add_catch(
  design,
  example_catch,
  catch_uid     = interview_id,
  interview_uid = interview_id,
  species       = species,
  count         = count,
  catch_type    = catch_type
)

# Attach age data — one row per aged fish
design <- add_ages(
  design,
  example_ages,
  age_uid      = interview_id,
  interview_uid = interview_id,
  species      = species,
  age          = age,
  age_type     = age_type
)

# Estimate weighted age frequency by species
ad <- est_age_distribution(design, by = species)
print(ad)

## ----mean_age-----------------------------------------------------------------
# Compute weighted mean age from the distribution object
est_mean_age(ad)

## ----complete-----------------------------------------------------------------
# Load data
data(example_calendar)
data(example_counts)
data(example_interviews)

# Build design and attach data
complete_design <- creel_design(example_calendar, date = date, strata = day_type) |>
  add_counts(example_counts) |>
  add_interviews(example_interviews,
    catch = catch_total,
    effort = hours_fished,
    n_anglers = n_anglers,
    harvest = catch_kept,
    trip_status = trip_status,
    trip_duration = trip_duration
  )

# Compute all estimates
effort <- estimate_effort(complete_design)
cpue <- estimate_catch_rate(complete_design)
hpue <- estimate_harvest_rate(complete_design)
total_catch <- estimate_total_catch(complete_design)
total_harvest <- estimate_total_harvest(complete_design)

# Print key results
print(effort)
print(total_catch)
print(total_harvest)

