casimir user guide: one survey on Island X
Source:vignettes/casimir-02-user-guide.Rmd
casimir-02-user-guide.RmdThis walkthrough designs a catch survey on Island X, rolls it, raises
it to catch per species and fleet segment, and then judges the design by
spinning it many times and running it for five years. Every step is a
plain function; the app (run_app()) calls the same
ones.
1. Build the island
library(casimir)
fish <- cas_fishery(island_x(), seed = 1)
fish
#> <cas_fishery> 164775 landings, 225 boats, 9 sites, 10 species, years 2016-2025
#> segment
#> site canoe small_motor large_motor
#> LS1 2020 4506 2759
#> LS2 19053 1960 0
#> LS3 15270 8730 0
#> LS4 1622 3513 2736
#> LS5 26216 5470 0
#> LS6 10000 3260 0
#> LS7 3493 5752 2849
#> LS8 20071 4866 0
#> LS9 13206 5216 2207Ten years are simulated: five years of burn-in, in which stocks respond to fishing and boats enter and leave, then a five-year sampling programme.
plot(fish)
Where the fish live, where each segment fishes, and where the catch comes from:
plot_species_map(fish)
plot_effort_map(fish, by = "segment")
plot_catch_map(fish, species = c("Scomber colias", "Thunnus alalunga", "Merluccius merluccius"),
by = "segment")
The population. fish$landings holds
every trip: site, date, segment, hours fished, landing hour and the true
landed weight of each species. This census is the answer key: no real
survey has one.
year <- max(fish$years)
truth <- cas_truth(fish, years = year, by = c("year", "segment"))
xtabs(round(kg / 1000, 1) ~ species + segment, truth)
#> segment
#> species canoe large_motor small_motor
#> Conger conger 34.7 0.4 10.4
#> Coryphaena hippurus 3.4 7.7 9.2
#> Dentex dentex 38.5 0.4 10.1
#> Merluccius merluccius 0.0 11.0 16.0
#> Pagellus bogaraveo 0.0 3.1 19.4
#> Pagellus erythrinus 31.5 3.3 47.0
#> Phycis blennoides 0.0 1.9 2.1
#> Scomber colias 214.8 26.8 75.1
#> Seriola dumerili 50.5 5.4 17.6
#> Thunnus alalunga 0.0 13.9 3.12. The frame survey
What the planner knows: boats by site and segment, and fishing days.
frame <- frame_survey(fish)
frame
#> <cas_frame> survey year 2025 | frame survey 2025
#> segment
#> site canoe large_motor small_motor
#> LS1 2 3 5
#> LS2 22 0 3
#> LS3 19 0 10
#> LS4 2 4 5
#> LS5 31 0 7
#> LS6 15 0 7
#> LS7 5 4 6
#> LS8 25 0 6
#> LS9 12 3 7
#> Fishing days by quarter: 77, 78, 79, 793. Design and betting slip
Six site-days per site type and quarter, three landings per segment at each visit, a 10 kg basket from each.
d <- cas_design(m = 6, n = 3, w_kg = 10, name = "A")
d
#> <cas_design> A
#> PSUs per site type x quarter: 6 ( srs )
#> landings per segment and visit: 3
#> subsample: 10 kg in 0.5 kg baskets
design_cost(d, fishery = fish) # enumerator-hours per year
#> [1] 936Betting slip. Before rolling: what CV do you expect for Scomber colias, the main species? And for Dentex dentex, the rarest?
4. Roll
s <- draw_sample(fish, d, year = year, seed = 2)
s
#> <cas_sample> A | year 2025 | 72 site-days, 301 landings sampled, 2538.5 kg subsampled
head(s$A, 6) # Form A: landings counted at each visit, by segment
#> psu date site site_type quarter segment N boats
#> 1 P001 2025-01-04 LS5 Beach 1 canoe 5 31
#> 2 P001 2025-01-04 LS5 Beach 1 small_motor 2 7
#> 3 P001 2025-01-04 LS5 Beach 1 large_motor 0 0
#> 4 P002 2025-01-13 LS8 Beach 1 canoe 0 25
#> 5 P002 2025-01-13 LS8 Beach 1 small_motor 1 6
#> 6 P002 2025-01-13 LS8 Beach 1 large_motor 0 0
head(s$B, 6) # Form B: sampled landings (W landed, w basket)
#> psu trip segment vessel hours W w
#> 1 P001 146712 canoe V87 3.8 23.3 10.00
#> 2 P001 146769 canoe V89 3.5 11.5 10.00
#> 3 P001 147054 canoe V115 5.4 19.3 10.00
#> 4 P001 147173 small_motor V190 3.6 24.6 10.00
#> 5 P001 147184 small_motor V171 2.3 5.1 5.14
#> 6 P002 147375 small_motor V160 13.2 193.8 10.00
head(s$C, 6) # Form C: species weights in the basket
#> psu trip species w_s
#> 1 P001 146712 Dentex dentex 1.5
#> 2 P001 146712 Seriola dumerili 2.0
#> 3 P001 146712 Scomber colias 6.5
#> 4 P001 146769 Pagellus erythrinus 3.0
#> 5 P001 146769 Conger conger 0.5
#> 6 P001 146769 Seriola dumerili 0.5The week x site grid of the training deck, for one stratum:
plot_design_grid(s, "Beach", quarter = 1)
How much of the fishery the sample saw, stage by stage:
plot_funnel(s, fish)
And where the sampled boats had been fishing:
plot_sample_map(s, fish)
5. Raise
e <- estimate_catch(s)
e
#> <cas_estimate> route: psu | year 2025
#> species estimate se cv
#> Dentex dentex 57.74 12.15 0.21
#> Pagellus erythrinus 85.70 13.19 0.15
#> Pagellus bogaraveo 26.16 11.41 0.44
#> Merluccius merluccius 42.05 16.59 0.39
#> Conger conger 47.98 9.35 0.19
#> Phycis blennoides 4.24 2.15 0.51
#> Thunnus alalunga 21.21 8.65 0.41
#> Coryphaena hippurus 24.03 6.59 0.27
#> Seriola dumerili 81.15 10.23 0.13
#> Scomber colias 314.37 44.93 0.14
#> (tonnes)Each step is kept: e$landing (basket to landing),
e$psu (landings to the site-day), e$stratum
(site-days to the stratum).
plot_estimate(e, fish)
By fleet segment. Each segment is raised in its own strata, so catch by species and segment comes straight out:
plot_catch_by(e, by = "segment", fishery = fish)
head(catch_by(e, by = "segment", fishery = fish))
#> year segment species estimate se cv lower
#> 1 2025 canoe Dentex dentex 49410.7547 11776.925 0.2383474 26328.405
#> 2 2025 large_motor Dentex dentex 445.2525 439.432 0.9869275 0.000
#> 3 2025 small_motor Dentex dentex 7883.3550 2965.517 0.3761745 2071.048
#> 4 2025 canoe Pagellus erythrinus 24066.1120 5140.868 0.2136144 13990.196
#> 5 2025 large_motor Pagellus erythrinus 2520.5450 1396.460 0.5540311 0.000
#> 6 2025 small_motor Pagellus erythrinus 59112.3653 12070.699 0.2041992 35454.230
#> upper true rel_error covered
#> 1 72493.104 38514.1486 0.2829248 TRUE
#> 2 1306.523 354.8878 0.2546289 TRUE
#> 3 13695.662 10136.0542 -0.2222462 TRUE
#> 4 34142.028 31517.8103 -0.2364282 TRUE
#> 5 5257.557 3321.8405 -0.2412203 TRUE
#> 6 82770.500 47014.6240 0.2573187 TRUEBlocks can be any combination of segment, site type and quarter:
plot_catch_by(e, by = c("site_type", "segment"), fishery = fish,
species = c("Scomber colias", "Thunnus alalunga", "Merluccius merluccius",
"Pagellus erythrinus"))
Small cells. A species that a segment lands rarely is often missed entirely: an estimate of zero with no interval. Totals are more reliable than their parts.
6. Spin: is the design any good?
One sample is one estimate. Spinning repeats the whole survey on the same island; here design A against design B, which costs about the same but visits more site-days (8 per stratum) and samples one landing per segment.
ev_a <- replicate_design(fish, d, R = 200, seed = 3)
ev_b <- replicate_design(fish, cas_design(m = 8, n = 1, w_kg = 10, name = "B"), R = 200, seed = 3)
c(A = ev_a$cost, B = ev_b$cost)
#> A B
#> 571.3906 562.1402
plot_spin(list(A = ev_a, B = ev_b))
plot_eval(rbind(data.frame(design = "A", ev_a$summary),
data.frame(design = "B", ev_b$summary)))
Reading the spin. The boxes sit on the truth: the estimators are unbiased. The spread is the precision: narrow for the main species, wide for the rare ones. At about the same cost, design A is more precise for every species: on Island X the landings of one day differ a lot (different boats, tactics and grounds), so sampling three of them per visit pays more than visiting more site-days.
7. A five-year programme: catch and effort
The same design in each of the last five years, against the true catch by species and the true effort (landings, i.e. trips) by segment.
sv <- run_survey(fish, d, seed = 4)
plot_series(sv, fish)
plot_effort_series(sv, fish)
One programme is one draw. Spinning the whole programme shows what to expect from it year by year. Here with the CPUE x effort route and the frame survey of the first year used throughout: the fleet grows, the old frame misses the new boats, and the estimates fall further below the truth each year.
old <- spin_series(fish, d, R = 50, route = "effort", frame = "first", seed = 5)
plot_spin_series(old, what = "effort")
aggregate(cbind(mean, true) ~ year, summary_spins(old), sum) # all species, kg
#> year mean true
#> 1 2021 579596.6 582136.5
#> 2 2022 603546.9 638363.9
#> 3 2023 546991.3 597545.7
#> 4 2024 592439.5 692371.5
#> 5 2025 529342.3 657336.3All the estimates, with the truth, as one long table (one row per spin, year, quarter, site type, segment and species; effort rows have no species):
long <- spins_long(old)
head(long)
#> variable iter year quarter site_type segment species estimate
#> 1 catch 1 2021 1 Beach canoe Dentex dentex 5881.814
#> 2 catch 1 2021 1 Beach canoe Pagellus erythrinus 1248.751
#> 3 catch 1 2021 1 Beach canoe Pagellus bogaraveo 0.000
#> 4 catch 1 2021 1 Beach canoe Merluccius merluccius 0.000
#> 5 catch 1 2021 1 Beach canoe Conger conger 6440.053
#> 6 catch 1 2021 1 Beach canoe Phycis blennoides 0.000
#> se true
#> 1 2563.1991 7.128865e+03
#> 2 694.3959 4.093164e+03
#> 3 0.0000 1.486360e+00
#> 4 0.0000 1.906382e-03
#> 5 2353.9824 7.166951e+03
#> 6 0.0000 1.589225e-05
# write.csv(long, "estimates_vs_truth.csv", row.names = FALSE)Catch by segment, for three species, from the single programme:
plot_series(sv, fish, by = "segment",
species = c("Scomber colias", "Thunnus alalunga", "Merluccius merluccius"))
8. Plant a problem
The trainer switches on a field problem; the trainee looks for its signature. Here the enumerator leaves at 13:00.
ev_sw <- replicate_design(fish, d, R = 200, seed = 3, artefacts = artefacts(short_watch = 13))
plot_spin(list(A = ev_a, `A, short watch` = ev_sw))
Warning flag. Long trips land late: the offshore species of the large motor boats almost vanish, while coastal canoe species lose a third. A segment common in the frame but rare in Form A points to the watch hours.
9. Cash out
export_survey(s, dir = "survey_A") # for trainees
export_survey(s, dir = "survey_A_key", answer_key = fish) # with the answer key