| Title: | Catch Sampling Inference Methods in R |
|---|---|
| Description: | A catch estimator simulator for training in multi-stage landing-site surveys (catch assessment surveys). A spatial multispecies fishery with landing sites, fishing ranges by segment and a calendar (the logbook simulator of 'cpueTargeting') provides the true landings. Trainees design a stratified multi-stage survey (landing site x day, landings, species subsamples), draw samples with optional planted field artefacts, raise them to catch per species with the estimators of the FAO training, and compare designs by bias, precision and cost over replicates. Includes a 'shiny' app with a hide-truth switch for trainers. |
| Authors: | Henning Winker [aut, cre], Pedro Barros [aut] |
| Maintainer: | Henning Winker <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.0.2 |
| Built: | 2026-12-01 10:04:02 UTC |
| Source: | https://github.com/Henning-Winker/casimir |
Field problems a trainer can switch on. Each is recorded in the answer key
(planted_artefacts()).
artefacts( high_roller = 0, short_watch = NULL, easy_sites = NULL, dropped_zeros = FALSE, top_of_box = 0, old_frame = 0 )artefacts( high_roller = 0, short_watch = NULL, easy_sites = NULL, dropped_zeros = FALSE, top_of_box = 0, old_frame = 0 )
high_roller |
0-1: strength of size-biased selection of landings (the
enumerator picks the big boats); selection probability is proportional to
|
short_watch |
Hour at which the enumerator leaves the site (landings
after it are neither counted nor sampled); |
easy_sites |
Sites the enumerator visits (others are never sampled but
still counted in M); |
dropped_zeros |
Logical: visits with no landings are not recorded. |
top_of_box |
0-1: the subsample over-represents valuable species
(basket probabilities multiplied by |
old_frame |
Years between the frame survey and the survey (0 = current). |
A cas_artefacts list.
A stratified multi-stage design as in the FAO training: strata are site
type x quarter (x segment, as post-strata within each visit); the PSU is a
landing site on a fishing day; within a visit the enumerator counts the
landings of each segment (N) and samples n of them, weighs each sampled
landing (W), takes a species subsample (w) and splits it by species.
cas_design( m = 6, n = 3, psu_select = c("srs", "systematic"), w_kg = 10, unit_kg = 0.5, cost = c(psu = 4, landing = 0.5, kg = 0.05), name = "design" )cas_design( m = 6, n = 3, psu_select = c("srs", "systematic"), w_kg = 10, unit_kg = 0.5, cost = c(psu = 4, landing = 0.5, kg = 0.05), name = "design" )
m |
PSUs (site-days) per site type and quarter: a single number, or a named vector by site type. |
n |
Landings sampled per segment and visit (a number, or named by segment). |
psu_select |
|
w_kg |
Target subsample weight (kg); the whole landing if smaller. |
unit_kg |
Basket size (kg) of the subsample model. |
cost |
Costs per PSU visit, per landing sampled and per kg subsampled (in enumerator-hours). |
name |
Label. |
A cas_design.
The answer key for effort: the number of landings (fishing trips) by year, quarter, site type and segment, from the full census of landings.
cas_effort_truth( fishery, years = NULL, by = c("year", "quarter", "site_type", "segment") )cas_effort_truth( fishery, years = NULL, by = c("year", "quarter", "site_type", "segment") )
fishery |
A |
years |
Years (default: all). |
by |
Grouping columns. |
A data frame with trips per group.
Runs cpueTargeting::simulate_logbook() with landing sites and keeps what
the survey needs: every trip with its site, day, segment and true landed
weight by species (landings), the sites and the fleet (vessels).
cas_fishery( settings = island_x(), seed = 1, cache = getOption("casimir.cache", FALSE) )cas_fishery( settings = island_x(), seed = 1, cache = getOption("casimir.cache", FALSE) )
settings |
Settings from |
seed |
Seed. |
cache |
Keep built islands on disk (in |
An object of class cas_fishery: landings, sites, vessels,
species, segments, years, env, plots, settings, seed.
## Not run: fish <- cas_fishery(island_x(), seed = 1) fish plot(fish) ## End(Not run)## Not run: fish <- cas_fishery(island_x(), seed = 1) fish plot(fish) ## End(Not run)
The answer key: true landed weight (kg) by year, quarter, site type, segment and species, from the full census of landings.
cas_truth( fishery, years = NULL, by = c("year", "quarter", "site_type", "segment") )cas_truth( fishery, years = NULL, by = c("year", "quarter", "site_type", "segment") )
fishery |
A |
years |
Years (default: all). |
by |
Grouping columns. |
A long data frame with kg per group and species.
The guides are installed with the package (folder guide), whether or not
the vignettes were built. casimir_guide() writes a guide to dir as a
stand-alone R Markdown file that trainers can edit and knit.
casimir_guide( guide = c("how-it-works", "user-guide", "teaching", "all"), dir = ".", render = FALSE, overwrite = FALSE )casimir_guide( guide = c("how-it-works", "user-guide", "teaching", "all"), dir = ".", render = FALSE, overwrite = FALSE )
guide |
|
dir |
Folder to write to (created). |
render |
Also render to HTML (needs 'rmarkdown' and pandoc). |
overwrite |
Replace existing files. |
The paths of the files written, invisibly.
## Not run: casimir_guide("teaching", dir = "course") ## End(Not run)## Not run: casimir_guide("teaching", dir = "course") ## End(Not run)
The texts shown when hovering over the (i) symbols in the app.
casimir_tip(id = NULL)casimir_tip(id = NULL)
id |
Tip id; |
A character string, or a named list.
casimir_tip("psu")casimir_tip("psu")
Sums the stratum estimates (site type x quarter x segment) over the groups
not in by, with variances added over strata (strata are sampled
independently; segments within a site type and quarter share the visits,
so sums over segments are approximate).
catch_by(x, by = NULL, fishery = NULL, level = 0.95)catch_by(x, by = NULL, fishery = NULL, level = 0.95)
x |
A |
by |
Grouping columns among |
fishery |
Optional |
level |
Confidence level. |
A data frame with year, the groups, species, estimate, se,
cv, lower, upper (kg), and the truth if fishery is given.
Compare an estimate with the truth
check_estimate(estimate, fishery)check_estimate(estimate, fishery)
estimate |
A |
fishery |
The |
The total table with true (kg), rel_error and covered.
Compare designs on the same fishery
compare_designs(fishery, designs, ...)compare_designs(fishery, designs, ...)
fishery |
A |
designs |
A named list of |
... |
Passed to |
A data frame of the summaries, with columns design and cost.
Builds designs that spend a similar budget on more site-days with fewer landings each, or fewer site-days with more landings each.
days_vs_boats(budget_m = 16, n = 1, steps = 4)days_vs_boats(budget_m = 16, n = 1, steps = 4)
budget_m |
PSUs per site type and quarter for the "days" end. |
n |
Landings per segment and visit at the "days" end. |
steps |
Number of designs. |
A named list of cas_design objects.
Roughly the slide-6 sampling intensity: 13 weeks x 3 sites per stratum
with about 20 site-days marked, i.e. m = 20 per site type and quarter,
spread over the weeks.
deck_design(...)deck_design(...)
... |
Passed to |
Design cost
design_cost(design, sample = NULL, fishery = NULL)design_cost(design, sample = NULL, fishery = NULL)
design |
A |
sample |
Optional |
fishery |
Needed for the planned cost (site types, segments). |
Cost in enumerator-hours.
Selects site-days in each site type x quarter, then landings of each segment at each visit, subsamples them and splits the subsample by species. Returns the field forms:
draw_sample( fishery, design = cas_design(), year = max(fishery$years), artefacts = NULL, seed = 1, frame_year = NULL )draw_sample( fishery, design = cas_design(), year = max(fishery$years), artefacts = NULL, seed = 1, frame_year = NULL )
fishery |
A |
design |
A |
year |
Survey year. |
artefacts |
A |
seed |
Seed. |
frame_year |
Year of the frame survey used (default: the survey year
minus |
A (visit and segment): psu, date, site, site_type, quarter,
segment, N (landings counted), boats (boats of the segment based at
the site, counted at the visit);
B (landing): psu, trip, segment, vessel, hours, W (landed
kg), w (subsample kg);
C (species in the subsample, long): psu, trip, species, w_s.
A cas_sample with visits, A, B, C, the frame, the
psu_frame sizes M, and the design and artefacts.
Sums the stratum estimates of effort (landings, i.e. fishing trips) over
the groups not in by, like catch_by() does for catch.
effort_by(x, by = "segment", fishery = NULL, level = 0.95)effort_by(x, by = "segment", fishery = NULL, level = 0.95)
x |
A |
by |
Grouping columns among |
fishery |
Optional |
level |
Confidence level. |
A data frame with year, the groups, estimate, se, cv,
lower, upper (trips), and true, rel_error, covered if
fishery is given.
The estimators of the FAO training, one stage at a time:
estimate_catch(sample, route = c("psu", "effort"), level = 0.95)estimate_catch(sample, route = c("psu", "effort"), level = 0.95)
sample |
A |
route |
|
level |
Confidence level.
Effort, the number of landings (fishing trips), is estimated per stratum
as |
species s in landing j of PSU i: W_ijs = w_ijs / sum_s(w_ijs) * W_ij;
species s in PSU i (per segment): W_is = N_i / n_i * sum_j W_ijs;
species s in stratum h (site type x quarter x segment):
W_hs = M_h / m_h * sum_i W_is;
total: sum over strata.
The variance of step 3 is the ultimate-cluster estimator with the stage-1
finite population correction, M^2 (1 - m/M) s^2 / m, where s^2 is the
variance of the PSU totals; within adds the second-stage term
M/m sum_i N_i^2 (1 - n_i/N_i) s_i^2 / n_i.
route = "effort" estimates instead catch = effort x CPUE, with effort =
frame boats x fishing days x boat activity (landings counted per frame
boat and visit) and CPUE the mean step-1 catch per landing in the stratum.
A cas_estimate with landing (step 1, wide), psu (step 2,
long), stratum (step 3, long), total (by species: estimate, se,
cv, lower, upper) and effort (trips per stratum: estimate, var).
Writes the frame survey, the design and the field forms of a sample as csv files, with an optional answer key for trainers.
export_survey(sample, dir = "casimir_survey", answer_key = NULL)export_survey(sample, dir = "casimir_survey", answer_key = NULL)
sample |
A |
dir |
Folder to write to (created). |
answer_key |
The |
The paths written, invisibly, with attribute dir.
A census of boats by landing site and segment in a reference year (boats
active in that year), and of fishing days by quarter in the survey year.
An old frame (ref_year before the survey year) misses boats that entered
since and keeps boats that left, which biases the CPUE x effort route.
frame_survey(fishery, year = max(fishery$years), ref_year = year)frame_survey(fishery, year = max(fishery$years), ref_year = year)
fishery |
A |
year |
Survey year (default: last year). |
ref_year |
Year of the frame survey (default: |
A cas_frame: boats (site, site_type, segment, boats), days
(quarter, fishing days), sites, year, ref_year.
Spatial views of the simulated fishery on the island grid (cells of 2.5 km; the coast and the landing sites are on the left, depth increases offshore to the right):
plot_species_map(fishery, species = NULL, ncol = 5) plot_effort_map( fishery, year = max(fishery$years), by = "segment", value = c("trips", "hours"), ncol = NULL ) plot_catch_map( fishery, species = NULL, year = max(fishery$years), by = NULL, ncol = 5 )plot_species_map(fishery, species = NULL, ncol = 5) plot_effort_map( fishery, year = max(fishery$years), by = "segment", value = c("trips", "hours"), ncol = NULL ) plot_catch_map( fishery, species = NULL, year = max(fishery$years), by = NULL, ncol = 5 )
fishery |
A |
species |
Species to show (default: all). |
ncol |
Number of facet columns. |
year |
Year (default: the last). |
by |
Facet by |
value |
For effort: |
plot_species_map(): where each species lives (unfished density,
relative to its maximum), from depth, substrate and temperature;
plot_effort_map(): where the fleet fished in a year (trips, or hours
fished, per cell), by segment, quarter or tactic;
plot_catch_map(): where the catch of each species was taken (kg per
cell), optionally by segment or quarter.
Landing sites are drawn on every map, coloured by site type.
A ggplot.
Settings for cpueTargeting::simulate_logbook() with landing sites, after
Island X of the FAO training concept by Pedro Barros (Data Collection and
Estimation of Fisheries Quantities): 9 landing sites of 3 types (Harbour,
Beach, Jetty) and three segments (fishing-unit types) with different
ranges, home tactics and site preferences:
island_x( boats = c(canoe = 150, small_motor = 60, large_motor = 15), range = c(canoe = 5, small_motor = 12, large_motor = 25), trips = 16000, n_years = 10, start_year = 2016, entry = 0.4, exit = 0.1 )island_x( boats = c(canoe = 150, small_motor = 60, large_motor = 15), range = c(canoe = 5, small_motor = 12, large_motor = 25), trips = 16000, n_years = 10, start_year = 2016, entry = 0.4, exit = 0.1 )
boats |
Number of boats per segment. |
range |
Fishing range per segment, in cells. |
trips |
Trips in an average year (all segments). |
n_years |
Number of years simulated. |
start_year |
First calendar year. |
entry |
Share of boats entering the fleet after year 1. |
exit |
Share of boats leaving the fleet before the last year. |
| Segment | Boats | Range (cells, 2.5 km) | Home tactics | Based at |
| canoe | 150 | 5 | Reef | beaches, jetties |
| small_motor | 60 | 12 | Reef, Sediment | all site types |
| large_motor | 15 | 25 | Pelagic, Sediment | harbours, some jetties |
By default 10 years are simulated: 5 years of burn-in (depletion, fleet
change) and a 5-year sampling programme in the final years (see
run_survey()).
Fishing pressure is moderate: catchability q = 0.004, and the reef
species Dentex dentex, the most valuable and most exposed to the canoes,
has its catchability by tactic reduced to 30%. Stocks decline but do not
collapse (Dentex to about half of its unfished biomass by year 10).
Boats join and leave the fleet: a share entry of the boats enters after
year 1 and a share exit of the others leaves, spread evenly over the
years. Trips per year follow the expected number of active
boats, so a growing fleet fishes more, and an old frame survey (one that
counts the boats of an earlier year) misses the new boats.
A settings list for cas_fishery().
Table of planted artefacts (answer key)
planted_artefacts(x)planted_artefacts(x)
x |
A |
A data frame of the artefacts that are switched on.
Estimate with 95% interval and the true catch for each species, in one panel per block (segment, site type, quarter, or a combination).
plot_catch_by(x, by = "segment", fishery = NULL, species = NULL)plot_catch_by(x, by = "segment", fishery = NULL, species = NULL)
x |
A |
by |
Grouping columns for |
fishery |
Optional |
species |
Species to show (default: all). |
A ggplot.
Fishing weeks of one quarter (rows) by the landing sites of one site type (columns): the sampling frame, with the visited site-days marked.
plot_design_grid(sample, site_type = NULL, quarter = 1)plot_design_grid(sample, site_type = NULL, quarter = 1)
sample |
A |
site_type |
Site type. |
quarter |
Quarter. |
A ggplot.
Estimated effort (landings, i.e. fishing trips) by year with 95% intervals, one panel per segment, and the true effort.
plot_effort_series(x, fishery = NULL, by = "segment")plot_effort_series(x, fishery = NULL, by = "segment")
x |
A |
fishery |
Optional |
by |
|
A ggplot.
Estimates against the truth
plot_estimate(estimate, fishery = NULL)plot_estimate(estimate, fishery = NULL)
estimate |
A |
fishery |
The |
A ggplot of the estimated catch by species with confidence intervals (and the true catch as a black dot).
Bias, CV and coverage by species
plot_eval(x)plot_eval(x)
x |
A |
A ggplot with one panel per measure.
The census and the sample at each stage: site-days, landings and kg, with the raising factor between them.
plot_funnel(sample, fishery)plot_funnel(sample, fishery)
sample |
A |
fishery |
The |
A ggplot.
Fishing grounds of all landings in the survey year (grey) and of the landings sampled (coloured by segment), with the landing sites visited.
plot_sample_map(sample, fishery)plot_sample_map(sample, fishery)
sample |
A |
fishery |
The |
A ggplot.
Estimated catch by year with 95% intervals, one panel per species (or per
species and segment), and the true catch when fishery is given.
plot_series(x, fishery = NULL, by = NULL, species = NULL)plot_series(x, fishery = NULL, by = NULL, species = NULL)
x |
A |
fishery |
Optional |
by |
|
species |
Species to show (default: all). |
A ggplot.
Each replicate's estimate divided by the true catch, by species: the spread is the precision, the offset of the median from 1 the bias (house edge).
plot_spin(x)plot_spin(x)
x |
A |
A ggplot.
For each survey year: the middle 95% of the estimates over the replicates (band or error bar), their mean, and the true catch or effort.
plot_spin_series( x, what = c("catch", "effort"), species = NULL, type = c("band", "bars"), show_truth = TRUE )plot_spin_series( x, what = c("catch", "effort"), species = NULL, type = c("band", "bars"), show_truth = TRUE )
x |
A |
what |
|
species |
Species to show (catch; default all). |
type |
|
show_truth |
Draw the true catch or effort. |
A ggplot.
Draws R samples from the same fishery with the same design (and
artefacts), raises each, and compares the totals by species with the true
landings.
replicate_design( fishery, design = cas_design(), R = 100, artefacts = NULL, route = "psu", year = max(fishery$years), seed = 1, progress = NULL )replicate_design( fishery, design = cas_design(), R = 100, artefacts = NULL, route = "psu", year = max(fishery$years), seed = 1, progress = NULL )
fishery |
A |
design |
A |
R |
Number of replicates. |
artefacts |
A |
route |
Estimation route (see |
year |
Survey year. |
seed |
Seed; replicate r uses |
progress |
Optional function called with the replicate number. |
A cas_eval: reps (one row per replicate and species), summary
by species (true, mean estimate, relative bias, CV, coverage, RMSE),
cost, and the design.
Opens the casimir Shiny app: build the island, look at the frame survey, set the design, roll a sample, raise it to catch per species and spin the design many times, step by step in the left panel. The app uses the same functions as the package, so everything done in the app can be repeated in a script.
run_app(...)run_app(...)
... |
passed to |
called for its side effect (runs the app).
if (interactive()) run_app()if (interactive()) run_app()
Rolls and raises the same design in each survey year (by default the last five years of the fishery), giving a time series of catch estimates.
run_survey( fishery, design = cas_design(), years = utils::tail(fishery$years, 5), artefacts = NULL, frame = c("annual", "first"), route = "psu", seed = 1 )run_survey( fishery, design = cas_design(), years = utils::tail(fishery$years, 5), artefacts = NULL, frame = c("annual", "first"), route = "psu", seed = 1 )
fishery |
A |
design |
A |
years |
Survey years (default: the last 5). |
artefacts |
A |
frame |
|
route |
Estimation route (see |
seed |
Seed. |
A cas_survey: samples and estimates by year, years.
## Not run: fish <- cas_fishery(island_x(), seed = 1) sv <- run_survey(fish, cas_design()) plot_series(sv, fish) plot_series(sv, fish, by = "segment") ## End(Not run)## Not run: fish <- cas_fishery(island_x(), seed = 1) sv <- run_survey(fish, cas_design()) plot_series(sv, fish) plot_series(sv, fish, by = "segment") ## End(Not run)
Derives a reproducible seed from a master seed and a tag (e.g. "sample",
"rep12"), so that redrawing one stream (a new sample) does not change
another (the fishery), and designs compared on the same fishery share the
same draws where they overlap (common random numbers).
seed_for(seed, tag)seed_for(seed, tag)
seed |
Master seed (integer). |
tag |
Character tag of the stream. |
An integer seed.
Runs the programme of run_survey() R times: in each replicate the same
design is rolled and raised in every survey year. The result holds the
estimates of every replicate by stratum, so the spread of the estimates
can be compared with the true catch and effort year by year.
spin_series( fishery, design = cas_design(), years = utils::tail(fishery$years, 5), R = 100, artefacts = NULL, frame = c("annual", "first"), route = "psu", seed = 1, progress = NULL )spin_series( fishery, design = cas_design(), years = utils::tail(fishery$years, 5), R = 100, artefacts = NULL, frame = c("annual", "first"), route = "psu", seed = 1, progress = NULL )
fishery |
A |
design |
A |
years |
Survey years (default: the last 5). |
R |
Number of replicates. |
artefacts |
A |
frame |
|
route |
Estimation route (see |
seed |
Seed. |
progress |
Optional function called with the replicate number. |
A cas_spins with catch (one row per replicate, year, quarter,
site type, segment and species: estimate, se, true, in kg),
effort (the same without species: trips), and the settings.
## Not run: fish <- cas_fishery(island_x(), seed = 1) sp <- spin_series(fish, cas_design(), R = 50) plot_spin_series(sp) plot_spin_series(sp, what = "effort") head(spins_long(sp)) ## End(Not run)## Not run: fish <- cas_fishery(island_x(), seed = 1) sp <- spin_series(fish, cas_design(), R = 50) plot_spin_series(sp) plot_spin_series(sp, what = "effort") head(spins_long(sp)) ## End(Not run)
One row per replicate (iter), year, quarter, site type (area), segment
(fleet) and species, with the estimated and the true catch (kg); effort
rows (variable = "effort", trips) have species = NA. Works for a single
sample's estimate, a survey over years, or a spun programme.
spins_long(x, fishery = NULL, effort = TRUE)spins_long(x, fishery = NULL, effort = TRUE)
x |
A |
fishery |
The |
effort |
Include effort rows. |
A data frame: variable ("catch" kg, "effort" trips), iter,
year, quarter, site_type, segment, species, estimate, se,
true.
Summary of a spun programme by year
summary_spins(x, what = c("catch", "effort"), by = NULL)summary_spins(x, what = c("catch", "effort"), by = NULL)
x |
A |
what |
|
by |
Optional grouping ( |
A data frame by year (and group, species): true, mean
estimate, the 2.5% and 97.5% quantiles of the estimates (q025,
q975), relative bias, CV and coverage of the 95% intervals.