calcMCMCModelFit: calcMCMCModelFit

View source: R/FUNCTION_calcMCMCModelFit.R

calcMCMCModelFitR Documentation

calcMCMCModelFit

Description

This function calculates Ricker Model parameters for spawner-recruit data with an MCMC using the jags() function from the R2jags package. For details such as model form and variable definitions, refer to this wiki page. Note: This requires installing JAGS from here. Also note that these are designed as quick fits to support initial exploration of Bayesian posteriors, to support a pre-screeing of assumptions before setting up a proper model fit. Some standard diagnostics can be part of the output, but the function does NOT do any quality control for you. Also, the default priors and inits may not make sense for your data. There are many tutorials available online for linear model fits and associated diagnostics using R2jags (e.g. here,here, and here).

Usage

calcMCMCModelFit(
  sr_obj,
  sr.scale = 10^6,
  model.type = "Basic",
  model.file = "BUILT_IN_MODEL_Ricker_BUGS.txt",
  min.obs = 15,
  mcmc.settings = list(n.chains = 2, n.burnin = 20000, n.thin = 60, n.samples = 50000),
  mcmc.inits = "default",
  mcmc.priors = list(p.alpha = 0, tau_alpha = 1e-04, p.beta = 1, tau_beta = 0.1,
    max.scalar = 3, shape.tau_R = 0.001, lambda_tau_R = 0.01, shape.tauw = 0.01,
    lambda_tauw = 0.001),
  mcmc.output = "short",
  mcmc.out.path = "MCMC_Out",
  mcmc.out.label = "MCMC",
  mcmc.seed = "default",
  tracing = FALSE
)

Arguments

sr_obj

a data frame with Spn,Rec (actual numbers, not thousands or millions) for the MCMC and logRpS for the deterministic fit (Data for 1 Stock!). Other variables can be there but are not used (RpS, Qual, ExpF etc)

sr.scale

an integer value used to rescale the Spn and Rec variables in sr_obj, prior to the MCMC fit, default = 10^6 (i.e. convert to millions). NOTE: If sr.scale is different from 1, then the benchmark estimates are scaled back, but the MCMC estimates of alpha and beta will be in different units then the alpha and beta estimates from the deterministic fit.

model.type

one of "Basic", "Kalman", or "AR1". This needs to match the model structure in the model.file argument. If Kalman or AR1, it will check for gaps in the time series, and return NA results if there are any.

model.file

a txt file with JAGS code (default is "MODEL_Ricker_BUGS.txt" )

min.obs

min number of S-R pairs needed to fit a model

mcmc.settings

a list with n.chains (2), n.burnin (20000), n.thin (60), and n.samples (50000). Default values in brackets.

mcmc.inits

a list of lists with inits for each chain. Default is "list(list(tau_R=3, S.max=1),list(tau_R=7, S.max=2))"

mcmc.priors

a list with p.alpha, p.beta, tau_alpha,tau_beta (if model.file = "MODEL_Ricker_BUGS.txt" )

mcmc.output

one of "short" (only return summary stats for key parameters in a list object), "post" (also save posterior distribution samples to folder), or "all" (also produce pdf files with standard diagnostic plots). This is passed on to the internal call of doRJAGS.

mcmc.out.path

text string specifying folder. if mcmc.output is "post" or "all", the generated files will be stored to this folder

mcmc.out.label

label use in the output files if mcmc.output is "post" or "all"

mcmc.seed

either "default" or an integer giving the random seed for starting MCMC (R2Jags default is 123)

tracing

if TRUE, diagnostic details for intermediate objects will be printed to the screen for debugging

Examples

ricker.fit <- calcMCMCModelFit(SR_Sample[SR_Sample$Stock == "Stock1",],min.obs = 10)
print(ricker.fit)

SOLV-Code/RapidRicker documentation built on Jan. 14, 2025, 9:36 a.m.