fit_stockseasonr: Fit model

View source: R/fit.R

fit_stockseasonrR Documentation

Fit model

Description

Fit model

Usage

fit_stockseasonr(
  abund_formula = NULL,
  comp_formula = NULL,
  abund_dat = NULL,
  comp_dat = NULL,
  abund_offset = NULL,
  abund_knots = NULL,
  comp_knots = NULL,
  pred_dat = NULL,
  model = c("negbin", "dirichlet", "integrated"),
  random_walk = FALSE,
  fit = TRUE,
  silent = TRUE,
  nlminb_loops = 1L,
  newton_loops = 0L
)

Arguments

abund_formula

Model formula for abundance component of model (fit with negative binomial distribution (nbinom2)). Random intercepts (IID or RW) are possible using lme4 syntax, e.g., + (1 | g) where g is a column of class factor representing groups. Penalized splines are possible via mgcv with s() (te not currently supported).

comp_formula

Model formula for composition component of model (fit with dirichlet distribution). Random intercepts (IID or RW) are possible using lme4 syntax, e.g., + (1 | g) where g is a column of class factor representing groups. Splines are possible via mgcv with s(), but unlike abundance component these are unpenalized so knots must be specified with k=.

abund_dat

Dataframe containing abundance response (e.g. catch) and covariates.

comp_dat

Dataframe containing composition response in long format (i.e column of class factor) and covariates.

abund_offset

A numeric vector representing the model offset. Usually a log transformed variable. Not included in any prediction.

abund_knots

Optional named list containing knot values to be used for basis construction of smoothing terms in abundance component of model. See mgcv::gam() and mgcv::gamm(). E.g., ⁠s(x, bs = 'cc', k = 4), knots = list(x = c(1, 2, 3, 4))⁠

comp_knots

As above, but for composition component of model.

pred_dat

Optional dataframe used to generate predictions. Note that in integrated model this applies to both abundance and composition components.

model

String specifying whether abundance (negbin), composition (dirichlet), or integrated model is fit.

random_walk

Logical specifying whether random intercepts are IID or random walk.

fit

Logical specifying whether model should be fit in TMB. If FALSE list of model inputs is returned.

silent

Silent or include optimization details? Helpful to set to FALSE for models that take a while to fit.

nlminb_loops

How many times to run stats::nlminb() optimization. Sometimes restarting the optimizer at the previous best values aids convergence. If the maximum gradient is still too large, try increasing this to 2.

newton_loops

Alternative Newton optimizer for stats::optimHess(). Sometimes aids convergence.

Value

List including model inputs as well as output from TMB::sdreport() (if fit = TRUE).

Examples

dum_pred <- expand.grid(month_n = seq(1, 12, by = 0.1),
                        region = unique(comp_ex$region))

# composition model with hierarchical random walk
m1 <- fit_stockseasonr(comp_formula = agg ~ 1 + region + 
                        s(month_n, bs = "tp", k = 4, m = 2) + 
                        (1 | year),
                       comp_dat = comp_ex,
                       pred_dat = dum_pred,
                       model = "dirichlet",
                       random_walk = TRUE,
                       fit = TRUE,
                       newton_loops = 1)
                                 
# group-specific integrated model with hierarchical random walk
m2 <- fit_stockseasonr(abund_formula = catch ~ 1 +
                       s(month_n, bs = "tp", k = 3, m = 2) +
                         region +
                         (1 | year),
                       abund_dat = catch_ex,
                       abund_offset = "offset",
                       comp_formula = agg ~ 1 + region + 
                         s(month_n, bs = "tp", k = 4, m = 2) + 
                         (1 | year),
                       comp_dat = comp_ex,
                       pred_dat = dum_pred,
                       model = "integrated",
                       random_walk = TRUE,
                       fit = TRUE,
                       silent = FALSE,
                       newton_loops = 1)
                                 

pbs-assess/stockseasonr documentation built on April 25, 2024, 12:15 p.m.