Skip to contents

Fit a carbon-fraction model for Macrocystis pyrifera via Stan.

Usage

kb_fit_carbon_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 carbon observations, one row per sample (see kb_check_data_carbon_macro() for the required columns).

priors

A named list of prior objects (see kb_priors_carbon_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_carbon_macro 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().

Examples

# A pre-fit example model is included with the package:
tidy(fit_carbon_sim_macro)
#> # A tibble: 2 × 4
#>   term      estimate   lower   upper
#>   <chr>        <dbl>   <dbl>   <dbl>
#> 1 intercept    -0.77  -0.796  -0.741
#> 2 precision   124    101     150    
if (FALSE) { # interactive()
fit <- kb_fit_carbon_macro(data_carbon_sim_macro)
tidy(fit)
}