fit_vb: Fit a von Bertalanffy growth model

View source: R/growth.R

fit_vbR Documentation

Fit a von Bertalanffy growth model

Description

For use with data for a single species.

Usage

fit_vb(
  dat,
  sex = c("female", "male", "all"),
  method = c("tmb", "mpd", "mcmc"),
  downsample = Inf,
  chains = 4L,
  iter = 1000L,
  cores = parallel::detectCores(),
  allow_slow_mcmc = FALSE,
  est_method = median,
  min_samples = 50L,
  too_high_quantile = 1,
  uniform_priors = FALSE,
  ageing_method_codes = NULL,
  usability_codes = c(0, 1, 2, 6),
  check_convergence_tmb = TRUE,
  tmb_inits = list(k = 0.5, linf = 40, log_sigma = log(0.1), t0 = -1),
  ...
)

Arguments

dat

Input data frame. Should be from gfdata::get_survey_samples() or gfdata::get_commercial_samples().

sex

Either "male" or "female".

method

"mpd" for the mode of the posterior distribution (with rstan::optimizing()) or "mcmc" for full MCMC sampling with Stan (with rstan::sampling()). "tmb" for a TMB model.

downsample

If not Inf this represents a number of fish specimens to sample prior to model fitting. Can be useful for large data sets that you want to fit with MCMC for testing purposes.

chains

Number of Stan chains.

iter

Number of Stan sampling iterations.

cores

Number of cores for Stan.

allow_slow_mcmc

Logical. If TRUE then the function will let you fit with MCMC to any number of fish. Defaults to FALSE to avoid accidentally fitting a model to a giant data set (stop if number of fish > 50,000).

est_method

If MCMC this defines how to summarize the posterior. Should be a function such as mean or median.

min_samples

The minimum number of fish before a model will be fit.

too_high_quantile

A quantile above which to discard weights and lengths. Can be useful for outliers. Defaults to including all data.

uniform_priors

Logical. If true then uniform priors will be used.

ageing_method_codes

A numeric vector of ageing method codes to filter on. Defaults to NULL, which brings in all valid ageing codes. See gfdata::get_age_methods().

usability_codes

An optional vector of usability codes. All usability codes not in this vector will be omitted. Set to NULL to include all samples.

check_convergence_tmb

Logical.

tmb_inits

A named list of initial parameter values for the TMB model.

...

Any other arguments to pass on to rstan::sampling() or rstan::optimizing().

Examples

## Not run: 
# with `rstan::optimizing()` for the mode of the posterior density:
model_f <- fit_vb(pop_samples, sex = "female")
model_m <- fit_vb(pop_samples, sex = "male")
plot_vb(model_f, model_m)
model_f$model
model_f$predictions

# You can also fit both sexes combined if you want.
# Just note that you need to specify the colours explicitly in the plot.
model_all <- fit_vb(pop_samples, sex = "all")
plot_vb(object_all = model_all, col = c("All" = "black"))

# with MCMC via Stan (slower):
x <- fit_vb(pop_samples, method = "mcmc",
  chains = 1, iter = 800, seed = 123) # just for a fast example
x$pars
x$predictions
x$data
x$model
posterior <- rstan::extract(x$model)
hist(posterior$linf)

# If less than `min_samples`, fit_vb() returns an empty object that
# plot_vb() will correctly parse and produce an empty plot:
obj <- fit_vb(pop_samples[1:2,])
plot_vb(obj, obj)

## End(Not run)

pbs-assess/gfplot documentation built on April 3, 2024, 2:10 p.m.