## ----setup, include = FALSE--------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----data-setup--------------------------------------------------------------- library(tidycreel) data(example_calendar) data(example_counts) data(example_interviews) head(example_calendar) head(example_counts) head(example_interviews) ## ----raw-design--------------------------------------------------------------- library(survey) # Stratum sizes: total days per day_type in the calendar stratum_sizes <- table(example_calendar$day_type) stratum_sizes # Attach FPC to the count frame counts_frame <- example_counts counts_frame$fpc <- as.integer(stratum_sizes[counts_frame$day_type]) counts_frame # Build the stratified survey design svy_counts <- svydesign( ids = ~1, strata = ~day_type, fpc = ~fpc, data = counts_frame ) svy_counts ## ----raw-effort--------------------------------------------------------------- effort_total <- svytotal(~effort_hours, svy_counts) effort_total confint(effort_total) ## ----raw-cpue----------------------------------------------------------------- # Use complete trips only (standard practice) complete_trips <- subset(example_interviews, trip_status == "complete") nrow(complete_trips) # Build a simple survey design over the interview frame svy_interviews <- svydesign(ids = ~1, data = complete_trips) # Ratio estimator: total catch / total effort (ratio-of-means CPUE) cpue_ratio <- svyratio(~catch_total, ~hours_fished, svy_interviews) cpue_ratio ## ----raw-total-catch---------------------------------------------------------- effort_coef <- coef(effort_total)[[1]] cpue_coef <- coef(cpue_ratio)[[1]] var_effort <- vcov(effort_total)[[1]] var_cpue <- vcov(cpue_ratio)[[1]] # Delta method: Var(effort * cpue) = effort^2 * Var(cpue) + cpue^2 * Var(effort) total_catch_est <- effort_coef * cpue_coef total_catch_var <- effort_coef^2 * var_cpue + cpue_coef^2 * var_effort total_catch_se <- sqrt(total_catch_var) cat("Total catch estimate:", round(total_catch_est, 1), "\n") cat("Standard error: ", round(total_catch_se, 1), "\n") cat( "95% CI: [", round(total_catch_est - 1.96 * total_catch_se, 1), ",", round(total_catch_est + 1.96 * total_catch_se, 1), "]\n" ) ## ----tc-design---------------------------------------------------------------- design <- creel_design(example_calendar, date = date, strata = day_type) design <- add_counts(design, example_counts) design ## ----tc-effort---------------------------------------------------------------- effort_est <- estimate_effort(design) effort_est ## ----tc-interviews------------------------------------------------------------ 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 ) ## ----tc-cpue------------------------------------------------------------------ cpue_est <- estimate_catch_rate(design) cpue_est ## ----tc-total-catch----------------------------------------------------------- total_catch <- estimate_total_catch(design) total_catch ## ----extract-design, eval = FALSE--------------------------------------------- # # Extract the internal svydesign object for advanced use # internal_svy <- as_creel_svydesign(design) # # Now use any survey package function directly # svymean(~effort_hours, internal_svy)