Package 'casimir'

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

Help Index


Planted artefacts

Description

Field problems a trainer can switch on. Each is recorded in the answer key (planted_artefacts()).

Usage

artefacts(
  high_roller = 0,
  short_watch = NULL,
  easy_sites = NULL,
  dropped_zeros = FALSE,
  top_of_box = 0,
  old_frame = 0
)

Arguments

high_roller

0-1: strength of size-biased selection of landings (the enumerator picks the big boats); selection probability is proportional to W^(3 * high_roller).

short_watch

Hour at which the enumerator leaves the site (landings after it are neither counted nor sampled); NULL = whole day.

easy_sites

Sites the enumerator visits (others are never sampled but still counted in M); NULL = all.

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 price^(2 * top_of_box)).

old_frame

Years between the frame survey and the survey (0 = current).

Value

A cas_artefacts list.


Survey design

Description

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.

Usage

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"
)

Arguments

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

"srs" (simple random) or "systematic" (spread over the quarter in date order, as the week x site grid of the deck).

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.

Value

A cas_design.


True effort (number of landings) by stratum

Description

The answer key for effort: the number of landings (fishing trips) by year, quarter, site type and segment, from the full census of landings.

Usage

cas_effort_truth(
  fishery,
  years = NULL,
  by = c("year", "quarter", "site_type", "segment")
)

Arguments

fishery

A cas_fishery.

years

Years (default: all).

by

Grouping columns.

Value

A data frame with trips per group.


Simulate the fishery (the population of landings)

Description

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).

Usage

cas_fishery(
  settings = island_x(),
  seed = 1,
  cache = getOption("casimir.cache", FALSE)
)

Arguments

settings

Settings from island_x() (must include landing).

seed

Seed.

cache

Keep built islands on disk (in tools::R_user_dir("casimir", "cache")) and reuse them for the same settings and seed. Off by default; the app switches it on (options(casimir.cache = TRUE)).

Value

An object of class cas_fishery: landings, sites, vessels, species, segments, years, env, plots, settings, seed.

Examples

## Not run: 
fish <- cas_fishery(island_x(), seed = 1)
fish
plot(fish)

## End(Not run)

True landings by species and stratum

Description

The answer key: true landed weight (kg) by year, quarter, site type, segment and species, from the full census of landings.

Usage

cas_truth(
  fishery,
  years = NULL,
  by = c("year", "quarter", "site_type", "segment")
)

Arguments

fishery

A cas_fishery.

years

Years (default: all).

by

Grouping columns.

Value

A long data frame with kg per group and species.


Copy, and optionally render, a casimir guide

Description

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.

Usage

casimir_guide(
  guide = c("how-it-works", "user-guide", "teaching", "all"),
  dir = ".",
  render = FALSE,
  overwrite = FALSE
)

Arguments

guide

"how-it-works", "user-guide", "teaching" or "all".

dir

Folder to write to (created).

render

Also render to HTML (needs 'rmarkdown' and pandoc).

overwrite

Replace existing files.

Value

The paths of the files written, invisibly.

Examples

## Not run: 
casimir_guide("teaching", dir = "course")

## End(Not run)

Short explanations of settings

Description

The texts shown when hovering over the (i) symbols in the app.

Usage

casimir_tip(id = NULL)

Arguments

id

Tip id; NULL returns all tips as a named list.

Value

A character string, or a named list.

Examples

casimir_tip("psu")

Catch by species and segment (or other groups)

Description

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).

Usage

catch_by(x, by = NULL, fishery = NULL, level = 0.95)

Arguments

x

A cas_estimate or a cas_survey.

by

Grouping columns among "segment", "site_type", "quarter"; NULL for species totals.

fishery

Optional cas_fishery to add the true catch (true, rel_error, covered).

level

Confidence level.

Value

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

Description

Compare an estimate with the truth

Usage

check_estimate(estimate, fishery)

Arguments

estimate

A cas_estimate.

fishery

The cas_fishery it was sampled from.

Value

The total table with true (kg), rel_error and covered.


Compare designs on the same fishery

Description

Compare designs on the same fishery

Usage

compare_designs(fishery, designs, ...)

Arguments

fishery

A cas_fishery.

designs

A named list of cas_design objects.

...

Passed to replicate_design() (R, artefacts, route, seed).

Value

A data frame of the summaries, with columns design and cost.


Days or boats? Designs of about equal cost

Description

Builds designs that spend a similar budget on more site-days with fewer landings each, or fewer site-days with more landings each.

Usage

days_vs_boats(budget_m = 16, n = 1, steps = 4)

Arguments

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.

Value

A named list of cas_design objects.


Design of the training deck

Description

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.

Usage

deck_design(...)

Arguments

...

Passed to cas_design().


Design cost

Description

Design cost

Usage

design_cost(design, sample = NULL, fishery = NULL)

Arguments

design

A cas_design.

sample

Optional cas_sample (realised cost); otherwise planned.

fishery

Needed for the planned cost (site types, segments).

Value

Cost in enumerator-hours.


Draw a survey sample (Roll)

Description

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:

Usage

draw_sample(
  fishery,
  design = cas_design(),
  year = max(fishery$years),
  artefacts = NULL,
  seed = 1,
  frame_year = NULL
)

Arguments

fishery

A cas_fishery.

design

A cas_design.

year

Survey year.

artefacts

A cas_artefacts, or NULL.

seed

Seed.

frame_year

Year of the frame survey used (default: the survey year minus artefacts$old_frame).

Details

  • 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.

Value

A cas_sample with visits, A, B, C, the frame, the psu_frame sizes M, and the design and artefacts.


Effort by segment (or other groups)

Description

Sums the stratum estimates of effort (landings, i.e. fishing trips) over the groups not in by, like catch_by() does for catch.

Usage

effort_by(x, by = "segment", fishery = NULL, level = 0.95)

Arguments

x

A cas_estimate or a cas_survey.

by

Grouping columns among "segment", "site_type", "quarter"; NULL for the total.

fishery

Optional cas_fishery to add the true effort.

level

Confidence level.

Value

A data frame with year, the groups, estimate, se, cv, lower, upper (trips), and true, rel_error, covered if fishery is given.


Raise a sample to catch per species (Raise)

Description

The estimators of the FAO training, one stage at a time:

Usage

estimate_catch(sample, route = c("psu", "effort"), level = 0.95)

Arguments

sample

A cas_sample.

route

"psu" (raising by PSUs) or "effort" (CPUE x effort).

level

Confidence level. Effort, the number of landings (fishing trips), is estimated per stratum as ⁠M_h / m_h * sum_i N_i⁠ from the landings counted on Form A (route "psu"), or as frame boats x days x activity (route "effort").

Details

  1. species s in landing j of PSU i: W_ijs = w_ijs / sum_s(w_ijs) * W_ij;

  2. species s in PSU i (per segment): ⁠W_is = N_i / n_i * sum_j W_ijs⁠;

  3. species s in stratum h (site type x quarter x segment): ⁠W_hs = M_h / m_h * sum_i W_is⁠;

  4. 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.

Value

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).


Export a survey (Cash out)

Description

Writes the frame survey, the design and the field forms of a sample as csv files, with an optional answer key for trainers.

Usage

export_survey(sample, dir = "casimir_survey", answer_key = NULL)

Arguments

sample

A cas_sample.

dir

Folder to write to (created).

answer_key

The cas_fishery the sample was drawn from, to add an ⁠answer_key/⁠ folder (true catch by stratum, estimate vs truth, planted artefacts); NULL for none.

Value

The paths written, invisibly, with attribute dir.


Frame survey

Description

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.

Usage

frame_survey(fishery, year = max(fishery$years), ref_year = year)

Arguments

fishery

A cas_fishery.

year

Survey year (default: last year).

ref_year

Year of the frame survey (default: year).

Value

A cas_frame: boats (site, site_type, segment, boats), days (quarter, fishing days), sites, year, ref_year.


Maps of the island: species, effort and catch

Description

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):

Usage

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
)

Arguments

fishery

A cas_fishery.

species

Species to show (default: all).

ncol

Number of facet columns.

year

Year (default: the last).

by

Facet by "segment", "quarter" or "tactic"; NULL for none.

value

For effort: "trips" or "hours".

Details

  • 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.

Value

A ggplot.


Island X: the default training fishery

Description

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:

Usage

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
)

Arguments

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.

Details

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.

Value

A settings list for cas_fishery().


Table of planted artefacts (answer key)

Description

Table of planted artefacts (answer key)

Usage

planted_artefacts(x)

Arguments

x

A cas_artefacts (or a cas_sample).

Value

A data frame of the artefacts that are switched on.


Estimated against true catch, blocked by segment or stratum

Description

Estimate with 95% interval and the true catch for each species, in one panel per block (segment, site type, quarter, or a combination).

Usage

plot_catch_by(x, by = "segment", fishery = NULL, species = NULL)

Arguments

x

A cas_estimate.

by

Grouping columns for catch_by(), e.g. "segment" or c("site_type", "segment"); NULL for species totals.

fishery

Optional cas_fishery for the truth.

species

Species to show (default: all).

Value

A ggplot.


Week x site grid of a stratum (deck slide 6)

Description

Fishing weeks of one quarter (rows) by the landing sites of one site type (columns): the sampling frame, with the visited site-days marked.

Usage

plot_design_grid(sample, site_type = NULL, quarter = 1)

Arguments

sample

A cas_sample.

site_type

Site type.

quarter

Quarter.

Value

A ggplot.


Effort series against the truth

Description

Estimated effort (landings, i.e. fishing trips) by year with 95% intervals, one panel per segment, and the true effort.

Usage

plot_effort_series(x, fishery = NULL, by = "segment")

Arguments

x

A cas_survey.

fishery

Optional cas_fishery for the true effort.

by

"segment" (default), "site_type" or NULL (total).

Value

A ggplot.


Estimates against the truth

Description

Estimates against the truth

Usage

plot_estimate(estimate, fishery = NULL)

Arguments

estimate

A cas_estimate.

fishery

The cas_fishery (for the truth); NULL hides it.

Value

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

Description

Bias, CV and coverage by species

Usage

plot_eval(x)

Arguments

x

A cas_eval, or a data frame from compare_designs().

Value

A ggplot with one panel per measure.


How much of the fishery the sample saw

Description

The census and the sample at each stage: site-days, landings and kg, with the raising factor between them.

Usage

plot_funnel(sample, fishery)

Arguments

sample

A cas_sample.

fishery

The cas_fishery (for the number of landings and kg in the census).

Value

A ggplot.


Where the sampled boats fished

Description

Fishing grounds of all landings in the survey year (grey) and of the landings sampled (coloured by segment), with the landing sites visited.

Usage

plot_sample_map(sample, fishery)

Arguments

sample

A cas_sample.

fishery

The cas_fishery.

Value

A ggplot.


Catch series against the truth

Description

Estimated catch by year with 95% intervals, one panel per species (or per species and segment), and the true catch when fishery is given.

Usage

plot_series(x, fishery = NULL, by = NULL, species = NULL)

Arguments

x

A cas_survey.

fishery

Optional cas_fishery for the true catch.

by

NULL (species totals) or "segment" / "site_type".

species

Species to show (default: all).

Value

A ggplot.


The spin: distribution of the estimates around the truth

Description

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).

Usage

plot_spin(x)

Arguments

x

A cas_eval, or a named list of them (one per design).

Value

A ggplot.


Spun programme against the truth, year by year

Description

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.

Usage

plot_spin_series(
  x,
  what = c("catch", "effort"),
  species = NULL,
  type = c("band", "bars"),
  show_truth = TRUE
)

Arguments

x

A cas_spins.

what

"catch" (one panel per species) or "effort" (one panel per segment).

species

Species to show (catch; default all).

type

"band" (estimates as a band, truth as a line) or "bars" (paired bars of the mean estimate and the truth, with the 95% range of the estimates).

show_truth

Draw the true catch or effort.

Value

A ggplot.


Repeat a design many times (Spin x1000)

Description

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.

Usage

replicate_design(
  fishery,
  design = cas_design(),
  R = 100,
  artefacts = NULL,
  route = "psu",
  year = max(fishery$years),
  seed = 1,
  progress = NULL
)

Arguments

fishery

A cas_fishery.

design

A cas_design.

R

Number of replicates.

artefacts

A cas_artefacts, or NULL.

route

Estimation route (see estimate_catch()).

year

Survey year.

seed

Seed; replicate r uses seed_for(seed, paste0("rep", r)).

progress

Optional function called with the replicate number.

Value

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.


Start the casimir app

Description

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.

Usage

run_app(...)

Arguments

...

passed to shiny::runApp(), e.g. launch.browser = TRUE or port = 3838.

Value

called for its side effect (runs the app).

Examples

if (interactive()) run_app()

Run a sampling programme over several years

Description

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.

Usage

run_survey(
  fishery,
  design = cas_design(),
  years = utils::tail(fishery$years, 5),
  artefacts = NULL,
  frame = c("annual", "first"),
  route = "psu",
  seed = 1
)

Arguments

fishery

A cas_fishery.

design

A cas_design.

years

Survey years (default: the last 5).

artefacts

A cas_artefacts, or NULL.

frame

"annual" (a new frame survey every year) or "first" (the frame survey of the first survey year is used throughout, so it goes out of date as boats enter and leave).

route

Estimation route (see estimate_catch()).

seed

Seed.

Value

A cas_survey: samples and estimates by year, years.

Examples

## 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)

Seed for one stream of random numbers

Description

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).

Usage

seed_for(seed, tag)

Arguments

seed

Master seed (integer).

tag

Character tag of the stream.

Value

An integer seed.


Spin a sampling programme many times (Spin x1000 over the years)

Description

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.

Usage

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
)

Arguments

fishery

A cas_fishery.

design

A cas_design.

years

Survey years (default: the last 5).

R

Number of replicates.

artefacts

A cas_artefacts, or NULL.

frame

"annual" (a new frame survey every year) or "first" (the frame survey of the first survey year is used throughout, so it goes out of date as boats enter and leave).

route

Estimation route (see estimate_catch()).

seed

Seed.

progress

Optional function called with the replicate number.

Value

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.

Examples

## 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)

Estimates against the truth as one long table

Description

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.

Usage

spins_long(x, fishery = NULL, effort = TRUE)

Arguments

x

A cas_estimate, cas_survey or cas_spins.

fishery

The cas_fishery (for the truth; not needed for cas_spins).

effort

Include effort rows.

Value

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

Description

Summary of a spun programme by year

Usage

summary_spins(x, what = c("catch", "effort"), by = NULL)

Arguments

x

A cas_spins.

what

"catch" (by species) or "effort".

by

Optional grouping ("segment", "site_type", "quarter").

Value

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.