Skip to contents

Fit a size-distribution model for Nereocystis luetkeana via Stan.

Usage

kb_fit_size_nereo(
  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 a size sample, one row per measured plant (see kb_check_data_size_sample_nereo() for the required columns).

priors

A named list of prior objects (see kb_priors_size_nereo()), 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_size_nereo object (a kb_fit).

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

Site and year interaction

The site:year effect lets a site depart from the year-to-year pattern shared by all sites. It is separable from the site and year effects only when a site has data in more than one year and a year has more than one site; otherwise the fit warns. Estimates for the site-years in the data are then unaffected, but other predictions depend on the sd_site_year prior (see kb_sensitivity()).

Examples

# A pre-fit example model is included with the package:
tidy(fit_size_sim_nereo)
#> # A tibble: 5 × 4
#>   term         estimate   lower  upper
#>   <chr>           <dbl>   <dbl>  <dbl>
#> 1 intercept      3.22   3.06    3.37  
#> 2 shape          2.78   2.64    2.93  
#> 3 sd_site        0.183  0.109   0.347 
#> 4 sd_year        0.039  0.00233 0.145 
#> 5 sd_site_year   0.0477 0.00601 0.0956
if (FALSE) { # interactive()
fit <- kb_fit_size_nereo(data_size_sample_sim_nereo)
tidy(fit)
}