Skip to contents

Fit a calibration of in situ survey biomass on plot cover for Macrocystis pyrifera via Stan.

Usage

kb_fit_cover_calibration_macro(
  data,
  priors = NULL,
  ...,
  prior_only = FALSE,
  chains = 4L,
  niters = 1000L,
  nthin = 1L,
  cores = NULL,
  seed = NULL,
  progress = c("bar", "verbose", "none"),
  progress_dir = NULL
)

Arguments

data

A data frame of in situ survey biomass with plot cover, one row per survey (see kb_check_data_cover_calibration_macro() for the columns).

priors

A named list of prior objects (see kb_priors_cover_calibration_macro()), or NULL to use the defaults. Entries not supplied keep their defaults.

...

Additional arguments passed to rstan::sampling(), such as control.

prior_only

A flag specifying whether to sample from the priors only, for prior predictive checks.

chains

A whole number of MCMC chains.

niters

A whole number of saved post-warmup draws per chain (minimum 2; number of warmup draws defaults to match).

nthin

A whole number of the thinning interval.

cores

A whole number of cores used to sample chains in parallel, or NULL to use getOption("mc.cores") if set or chains if not. Capped at the number of available cores.

seed

A whole number passed to rstan::sampling() to make the fit reproducible, or NULL (the default, which uses a random seed). A preceding set.seed() also makes the fit reproducible.

progress

A string, one of "bar" (the default, a console progress bar), "verbose" (rstan's per-iteration output and diagnostic warnings), or "none" (silent).

progress_dir

A string giving an existing directory in which to record progress for kb_progress(), or NULL (the default) to record none.

Value

A kb_fit_cover_calibration_macro object (a kb_fit). Its data holds the fitted surveys, with biomass_kg_m2 the mean of each survey's draws and biomass_sd_kg_m2 their SD.

Details

data is the output of kb_predict_biomass_in_situ() with its defaults. Surveys without plot cover are not fitted, and a message gives their number. The model is described in Methods.

The fit warns when a survey is past the tide limit with more than 5% prior probability, and when every survey with no plants has a plot cover at or below that of every survey with plants, since presence_cover_slope is then informed only by its prior. If canopy is already tide-corrected, set canopy_tide_height_m = 0.

Model

kb_model_describe() prints the fitted model, with its priors, in notation or as a written description. The Methods article describes each model and its assumptions.

Sampling

Chains run in parallel by default, one core per chain, capped at the cores available. cores sets the number of cores; when it is NULL, getOption("mc.cores") is used if set. Set cores = 1 (or options(mc.cores = 1)) where cores are shared or limited, or inside another parallel process.

The sampler runs with adapt_delta = 0.95 by default, which reduces divergences at the cost of slightly longer runtime. Override it, or set any other sampler control, by passing a control list through ..., for example control = list(adapt_delta = 0.99); only the entries supplied are changed. See the control argument of rstan::stan() for the full set of entries.

By default, each of 4 chains runs 1000 warmup iterations and then saves 1000 draws with no thinning, giving 4000 draws in total. These match the defaults of rstan::stan() and Stan's general recommendations.

Check convergence with kb_converged(), glance(), or summary().

Examples

# A pre-fit example model is included with the package:
tidy(fit_cover_calibration_sim_macro)
#> # A tibble: 6 × 4
#>   term              estimate   lower  upper
#>   <chr>                <dbl>   <dbl>  <dbl>
#> 1 cover_slope         8.6    6.96    10.3  
#> 2 biomass_floor       0.326  0.105    0.601
#> 3 tide_height_slope   0.235  0.18     0.287
#> 4 cv_residual         0.22   0.165    0.293
#> 5 sd_site             0.0675 0.00334  0.258
#> 6 sd_year             0.0614 0.00254  0.284
if (FALSE) { # interactive()
biomass <- kb_predict_biomass_in_situ(
  fit_weight_sim_macro,
  data_size_census_sim_macro,
  data_survey_in_situ_sim_macro
)
fit <- kb_fit_cover_calibration_macro(biomass)
tidy(fit)
}