Skip to contents

Builds one version of the data per posterior draw of the calibration exported by sheepdog (calibration_draws.parquet, the probability that a prediction with a given score is a true detection, at each site and species), for fp_occu_mc(). In each version an occasion is a detection when at least one of its predictions is judged true under that draw.

Usage

fp_draws_data(
  x,
  rule = c("sample", "threshold"),
  p_min = 0.95,
  n_draws = NULL,
  seed = NULL,
  occasions = x$occasions,
  sites = x$sites,
  site = "site"
)

Arguments

x

An export read by fp_read_sheepdog(), with calibration draws. Read it with species set to the target species, or to several species for the multispecies model (see Value).

rule

How a prediction is judged true under a draw with probability p of being true: "sample" (default) draws it true with probability p, so that no threshold is involved; "threshold" takes it as true when p >= p_min, as sheepdog::history_draws() does.

p_min

Threshold of rule = "threshold".

n_draws

Optional number of calibration draws to use (the first ones, in the order of their identifiers); all by default.

seed

Optional seed of rule = "sample". The random numbers of the session continue afterwards as if the call had drawn none.

occasions

The occasions, one row per site and occasion_id of the export, with the column site names (the sites of the model, such as site-nights) and any detection covariates. Defaults to x$occasions. Every version carries a copy of it, so keep only the columns the model needs when there are many occasions and draws.

sites

The sites table of fp_data(), with the column site names. Defaults to x$sites.

site

Column of occasions and sites that identifies the sites of the model.

Value

A list of fp_data() objects, one per calibration draw, with the attributes draws (the draw identifiers) and rule. When the detections hold several species, each version has every occasion once per species, with the column species (the short codes of sheepdog), as fp_data(species = "species"); the species are those with predictions.

Details

The rules. "sample" judges each prediction on its own, so an occasion with many low-scoring predictions is likely to hold at least one judged true; it assumes that the predictions of an occasion are true or false independently, given the calibration. "threshold" keeps only the predictions the calibration is nearly sure of, so it gives fewer detections: on the curiango data (custom classifier, minute occasions), about 8% of the minutes with "sample" against about 1% with "threshold".

When the calibration has keys beyond site and species (schema$calibration$keys, such as "month"), a prediction takes the value of its occasion: each such key is a column of the occasions of the export and of calibration_draws.parquet. In an export of schema version 1.0, without a month column, the month is taken from the recording date of the occasion.

A prediction takes the calibration value of the grid point nearest its score, as in sheepdog: grid index round((score - grid_min) / grid_step) + 1, within the grid. A prediction without calibration draws (no draws for its site, species and keys, or a key missing for its occasion) cannot be scored: its occasion is set to NA (not surveyed) in every version, with a warning, rather than counted as a non-detection.

Histories drawn by sheepdog. An export of schema 1.2 may hold the histories sheepdog drew with sheepdog::occasion_draws() (occasion_draws.parquet), by the same rules. They are read instead of drawn again when they were made for the target species with the same rule, the same seed ("sample", which then needs seed) or p_min ("threshold"), at least the draws asked for, and every prediction has its occasion in occasions; the values are the same.

Calibrations with continuous covariates. When the calibration has continuous covariates (schema$calibration$covariates, schema 1.3, such as a standardised temperature of the night), the probability that a prediction is true depends on its occasion, not only on its score, and sheepdog draws the histories when it exports; the export holds no calibration draws. The histories are then read as they are: rule, p_min and seed left at their defaults take the values of the export, and other values, or more draws than were drawn, are an error (export again). Occasions sheepdog could not score (without a value of a covariate) are treated as not surveyed, with a warning, and occasions not in occasions are left out.

The recordings annotated by ear (x$verified) are the verified occasions of every version: they do not depend on the calibration. The reviewed clips (x$validation) are not used here; they are the data of the calibration.

Histories built this way hold few false positives, so a false-positive process fitted to them (the default of fp_occu()) is informed mostly by its priors; compare with fp = NULL.

Experimental

This function is experimental (new in 2.0.0, part of the integration with 'sheepdog'): its arguments and results may change in a minor version. See crywolf-api.

Examples

dir <- system.file("extdata", "sheepdog-export", package = "crywolf")
x <- fp_read_sheepdog(dir, species = "phy_cuv")
#> Warning: 320 reviewed clip(s) drawn with sampling_mode = "top" were dropped: they were chosen for their high scores and would make the false-positive rate look lower than it is. Use keep_top = TRUE to keep them.
# the calibration of this export has a continuous covariate, so the
# histories are those sheepdog drew
versions <- fp_draws_data(x, n_draws = 5)
versions[[1]]
#> <fp_data>
#>   sites:                  4
#>   automated occasions:    354 (naive occupancy 1.00)
#>   verified sites:         0 (0 occasions; verified detections at 0 sites)
#>   sites known occupied:   0
#>   observation covariates: occasion_id, source_audio, recording_date, recording_hour, night, start, duration, temp_z 
# \donttest{
# the export has no verified data, so the model without false positives
if (instantiate::stan_cmdstan_exists()) {
  mc <- fp_occu_mc(versions, occ = ~ 1, fp = NULL, chains = 2,
                   threads_per_chain = 1)
  summary(mc)
}
#> Warning: No verified data: the false-positive rate is identified only by the priors. Treat the estimates with caution.
#> Running MCMC with 2 parallel chains, with 1 thread(s) per chain...
#> 
#> Chain 1 finished in 0.4 seconds.
#> Chain 2 finished in 0.4 seconds.
#> 
#> Both chains finished successfully.
#> Mean chain execution time: 0.4 seconds.
#> Total execution time: 0.5 seconds.
#> 
#> Warning: No verified data: the false-positive rate is identified only by the priors. Treat the estimates with caution.
#> Running MCMC with 2 parallel chains, with 1 thread(s) per chain...
#> 
#> Chain 1 finished in 0.4 seconds.
#> Chain 2 finished in 0.4 seconds.
#> 
#> Both chains finished successfully.
#> Mean chain execution time: 0.4 seconds.
#> Total execution time: 0.5 seconds.
#> 
#> Warning: No verified data: the false-positive rate is identified only by the priors. Treat the estimates with caution.
#> Running MCMC with 2 parallel chains, with 1 thread(s) per chain...
#> 
#> Chain 1 finished in 0.4 seconds.
#> Chain 2 finished in 0.4 seconds.
#> 
#> Both chains finished successfully.
#> Mean chain execution time: 0.4 seconds.
#> Total execution time: 0.5 seconds.
#> 
#> Warning: No verified data: the false-positive rate is identified only by the priors. Treat the estimates with caution.
#> Running MCMC with 2 parallel chains, with 1 thread(s) per chain...
#> 
#> Chain 1 finished in 0.3 seconds.
#> Chain 2 finished in 0.4 seconds.
#> 
#> Both chains finished successfully.
#> Mean chain execution time: 0.4 seconds.
#> Total execution time: 0.5 seconds.
#> 
#> Warning: No verified data: the false-positive rate is identified only by the priors. Treat the estimates with caution.
#> Running MCMC with 2 parallel chains, with 1 thread(s) per chain...
#> 
#> Chain 1 finished in 0.4 seconds.
#> Chain 2 finished in 0.4 seconds.
#> 
#> Both chains finished successfully.
#> Mean chain execution time: 0.4 seconds.
#> Total execution time: 0.5 seconds.
#> 
#> Posterior pooled over 5 fits (95% equal-tailed intervals)
#> 
#>         variable  mean     sd  lower upper sd_within sd_between share_between
#>  occ_(Intercept) 1.725 1.0284 -0.181 3.890    1.0283    0.02599      0.000639
#>  det_(Intercept) 0.680 0.1148  0.457 0.904    0.1132    0.01945      0.028712
#>          p11_ref 0.663 0.0255  0.612 0.712    0.0252    0.00431      0.028536
#>  max_rhat min_ess_bulk
#>      1.00         1732
#>      1.01         1661
#>      1.01         1661
#> 
#>   Per fit: 0 divergent transitions in all, largest R-hat 1.007, smallest bulk ESS 1661
# }