Package 'multiStockassessment'

Title: Fitting Multiple State-Space Assessment Models
Description: Fitting multiple SAM models.
Authors: Christoffer Moesgaard Albertsen [aut, cre]
Maintainer: Christoffer Moesgaard Albertsen <[email protected]>
License: GPL-2
Version: 0.4.0
Built: 2024-11-14 05:29:42 UTC
Source: https://github.com/calbertsen/multi_SAM

Help Index


Add transparency to a color

Description

Function to add transparency to a color

Usage

addTrans(name, alpha = 1)

Arguments

name

Name of color

alpha

Alpha level

Value

A hex color string

Author(s)

Christoffer Moesgaard Albertsen


Collect msam objects

Description

Collect a number of msam objects into an msamset

Usage

## S3 method for class 'msam'
c(...)

Arguments

...

msam objects

Value

An msamset


Catch plot

Description

Catch plot

Usage

## S3 method for class 'msam'
catchplot(fit, obs.show = TRUE, drop = 0, ...)

Arguments

fit

msam object

obs.show

Show observations in plot?

drop

Number of years to drop from the end

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


Catch Table

Description

Catch Table

Usage

## S3 method for class 'msam'
catchtable(fit, obs.show = FALSE, returnList = FALSE, ...)

Arguments

fit

msam object

obs.show

should observed catches be included?

returnList

If true, a list of matrices is returned

...

Other parameters

Details

Table of predicted catch in weight

Value

A matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


Extract parameter estimates of msam object

Description

Extract parameter estimates of msam object

Usage

## S3 method for class 'msam'
coef(object, ...)

Arguments

object

msam object

...

extra arguments not used

Value

a vector of parameter estimates

Author(s)

Christoffer Moesgaard Albertsen


Correlation and partial correlation plot between survival processes

Description

Correlation and partial correlation plot between survival processes

Usage

## S3 method for class 'msam'
corplot(object, ...)

Arguments

object

msam object

...

Other parameters not currently used

Value

A matrix of correlations (lower triangular) and partial correlations (upper triangular)

Author(s)

Christoffer Moesgaard Albertsen


Deterministic reference points

Description

Deterministic reference points

Usage

## S3 method for class 'msam'
deterministicReferencepoints(
  fit,
  referencepoints,
  catchType = "catch",
  nYears = 300,
  Fsequence = seq(0, 2, len = 50),
  aveYears = lapply(fit, function(x) max(x$data$years) + (-9:0)),
  selYears = lapply(fit, function(x) max(x$data$years)),
  biasCorrect = FALSE,
  newton.control = list(),
  ...
)

Arguments

fit
referencepoints
catchType
nYears
Fsequence
aveYears
selYears
biasCorrect
newton.control
...

Author(s)

Christoffer Moesgaard Albertsen


F-at-age Table

Description

F-at-age table

Usage

## S3 method for class 'msam'
faytable(fit, returnList = FALSE, ...)

Arguments

fit

msam object

returnList

If true, a list of matrices is returned

...

Other parameters

Details

Table of estimated age-wise fishing mortality

Value

A matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


Fbar plot for msam object

Description

Fbar plot for msam object

Usage

## S3 method for class 'msam'
fbarplot(
  fit,
  partial = FALSE,
  drop = 0,
  page = NULL,
  plot = TRUE,
  effectiveF = TRUE,
  add = FALSE,
  ex = numeric(0),
  ...
)

Arguments

fit

fitted msam object

partial

Should F for each age in Fbar range be added?

drop

Number of years to drop from the end

page

List of ages to be used per stock for partial = true. Defaults to all ages used to calculate Fbar.

...

plotting arguments

Author(s)

Christoffer Moesgaard Albertsen


Fbar Table

Description

Fbar Table

Usage

## S3 method for class 'msam'
fbartable(fit, ...)

Arguments

fit

msam object

...

Other parameters

Details

Table of estimated average fishing mortalities

Value

A matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


Fit plot

Description

Fit plot

Usage

## S3 method for class 'msam'
fitplot(
  fit,
  stock,
  log = TRUE,
  fleets = lapply(attr(fit, "m_data")$sam, function(x) unique(x$aux[, "fleet"])),
  ...
)

Arguments

fit

msam object

stock

Stock to plot for

log

Plot on log-scale?

fleets

Fleets to plot

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


F selectivity plot

Description

F selectivity plot

Usage

## S3 method for class 'msam'
fselectivityplot(
  fit,
  cexAge = 1,
  type = c("bar", "line"),
  col = .plotcols.crp(length(fit)),
  ...
)

Arguments

fit

msam object

cexAge

size of Age label

type

bar for separate bar plots, line for overlayed line plots

col

Line colors for line plot

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


Prepare allele matrix for PCA analysis

Description

Prepare allele matrix for PCA analysis

Usage

gen2PCA(x, alleleMeans)

Arguments

x

output from \linkread.gen

alleleMeans

Mean allele frequencies to impute. Is calculated if missing.

Value

allele data for PCA analysis

Author(s)

Christoffer Moesgaard Albertsen


Calculate gradient of a function

Description

Calculate gradient of a function

Usage

grad(
  func,
  x,
  h = abs(1e-04 * x) + 1e-04 * (abs(x) < sqrt(.Machine$double.eps/7e-07)),
  ...
)

Arguments

func

function

x

parameter values

...

passed to func

Value

gradient vector

Author(s)

Christoffer Moesgaard Albertsen


Holdout forecast for validation

Description

Holdout forecast for validation

Usage

holdout(fit, nYears, ...)

## S3 method for class 'msam'
holdout(fit, nYears, forecastYears = 1, ncores = 1, ...)

Arguments

fit

msam fit

nYears

Number of years to hold out

...

Arguments passed to modelforecast

Value

A model forecast

Author(s)

Christoffer Moesgaard Albertsen


Calculate jacobian of a function

Description

Calculate jacobian of a function

Usage

jacobian(
  func,
  x,
  h = abs(1e-04 * x) + 1e-04 * (abs(x) < sqrt(.Machine$double.eps/7e-07)),
  ...
)

Arguments

func

function

x

parameter values

...

passed to func

Value

jacobian matrix

Author(s)

Christoffer Moesgaard Albertsen


Jitter runs

Description

Jitter runs

Usage

## S3 method for class 'msam'
jit(
  fit,
  nojit = 10,
  par = NULL,
  sd = 0.25,
  ncores = parallel::detectCores() - 1,
  silent = TRUE
)

Arguments

fit

a fitted model object as returned from sam.fit

nojit

a list of vectors. Each element in the list specifies a run where the fleets mentioned are omitted

par

initial values to jitter around. The defaule ones are returned from the defpar function

sd

the standard deviation used to jitter the initial values (most parameters are on a log scale, so similar to cv)

ncores

the number of cores to attemp to use

Details

...

Value

A "samset" object, which is basically a list of sam fits


Life expectancy plot

Description

Life expectancy plot

Usage

## S3 method for class 'msam'
lifeexpectancyplot(
  fit,
  atRecruit = TRUE,
  col = .plotcols.crp(length(fit)),
  ylimAdd = max(sapply(fit, function(x) x$conf$maxAge)),
  ...
)

Arguments

fit

msam object

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


SSB Table

Description

Life expectancy Table

Usage

## S3 method for class 'msam'
lifeexpectancytable(fit, atRecruit = TRUE, ...)

Arguments

fit

msam object

...

Other parameters

Details

Table of estimated life expectancy.

Value

A matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


Forecast of a multiStockassessment

Description

Forecast of a multiStockassessment

Usage

## S3 method for class 'msam'
modelforecast(
  fit,
  constraints = NULL,
  fscale = NULL,
  catchval = NULL,
  fval = NULL,
  nextssb = NULL,
  landval = NULL,
  nosim = 1000,
  year.base = unlist(min(sapply(fit, function(x) max(x$data$years)))),
  ave.years = lapply(fit, function(x) max(x$data$years) + (-9:0)),
  overwriteBioModel = FALSE,
  rec.years = lapply(fit, function(x) numeric(0)),
  label = NULL,
  overwriteSelYears = NULL,
  deterministicF = FALSE,
  processNoiseF = FALSE,
  fixedFdeviation = FALSE,
  useFHessian = FALSE,
  resampleFirst = !is.null(nosim) && nosim > 0,
  resampleParameters = FALSE,
  useModelLastN = TRUE,
  fixFirstN = FALSE,
  customSel = NULL,
  lagR = FALSE,
  splitLD = FALSE,
  addTSB = FALSE,
  biasCorrect = FALSE,
  returnAllYears = FALSE,
  returnObj = FALSE,
  progress = nosim > 0 && ncores == 1,
  estimate = median,
  silent = TRUE,
  newton_config = NULL,
  custom_pl = NULL,
  useNonLinearityCorrection = (nosim > 0 && !deterministicF),
  ncores = 1,
  ...
)

Arguments

fit
fscale
catchval
fval
nextssb
landval
nosim
year.base
ave.years
rec.years
label
overwriteSelYears
deterministicF
processNoiseF
customSel
lagR
splitLD
addTSB
biasCorrect
returnAllYears

Value

msamforecast


Model table

Description

Model table

Usage

## S3 method for class 'msam'
modeltable(fits, ...)

Arguments

fits

msam object

...

extra arguments

Value

A matrix of model information

Author(s)

Christoffer Moesgaard Albertsen


Model table

Description

Model table

Usage

## S3 method for class 'msamset'
modeltable(fits, ...)

Arguments

fits

msamset object

...

extra arguments

Value

A matrix of model information

Author(s)

Christoffer Moesgaard Albertsen


Fit multiple SAM models with correlations

Description

Fit multiple SAM models with correlated survival processes.

Usage

multisam.fit(
  x,
  formula = ~-1,
  corStructure = suggestCorStructure(x, nAgeClose = 0),
  usePartialCors = TRUE,
  newtonsteps = 0,
  rm.unidentified = FALSE,
  nlminb.control = list(trace = 1, eval.max = 20000, iter.max = 20000),
  lower = NULL,
  upper = NULL,
  starting = NULL,
  community_formula = ~-1,
  community_type = 1,
  shared_data = NULL,
  shared_keys = character(0),
  shared_selectivity = 0,
  shared_seasonality = 0,
  shared_stockrecruitment = FALSE,
  shared_oneFScalePars = FALSE,
  shared_initN = FALSE,
  shared_fleetParameters = c(),
  shared_proportionalHazard = NULL,
  shared_phmap = NULL,
  skip_stock_observations = FALSE,
  stockAreas = matrix(1, 1, length(x)),
  genetics_data = prepareGenetics(),
  genetics_dirichlet = FALSE,
  genetics_spatialAge = TRUE,
  genetics_independentStocks = TRUE,
  initN = 0,
  initF = FALSE,
  parlist = NULL,
  run = TRUE,
  symbolicAnalysis = FALSE,
  fullDerived = FALSE,
  mohn = FALSE,
  ...
)

Arguments

x

samset from the stockassessment package

formula

formula for covariance matrix covariates (See Details)

corStructure

symmetric boolean matrix. True if a (partial) correlation in survival should be fixed to zero between the corresponding age/stock combination

usePartialCors

if TRUE corStructure describes the partial correlations. If FALSE corStructure describes correlations.

newtonsteps

As for stockassessment::sam.fit

lower

As for stockassessment::sam.fit

upper

As for stockassessment::sam.fit

...

Additional arguments passed to TMB::MakeADFun

Details

Function to fit a multi-stock SAM model (Albertsen et al., 2018).

Value

A list of class msam and samset

Author(s)

Christoffer Moesgaard Albertsen

References

Albertsen, C. M., Nielsen, A. and Thygesen, U. H. (2018) Connecting single-stock assessment models through correlated survival. ICES Journal of Marine Science, 75(1), 235-244. doi: 10.1093/icesjms/fsx114

Examples

if(require(stockassessment)){
  data(nscodData)
  data(nscodConf)
  data(nscodParameters)
  fit <- sam.fit(nscodData, nscodConf, nscodParameters)
  fits <- c(fit)
  obj <- multisam.fit(fits)
}

N Table

Description

N Table

Usage

## S3 method for class 'msam'
ntable(fit, returnList = FALSE, ...)

Arguments

fit

msam object

returnList

If true, a list of matrices is returned

...

Other parameters

Details

Table of estimated numbers-at-age

Value

A matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


Parameter plot

Description

Parameter plot

Usage

## S3 method for class 'msam'
parplot(fit, cor.report.limit = 0.95, ...)

Arguments

fit

msam object

cor.report.limit

Not used

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


Parameter table

Description

Parameter table

Usage

## S3 method for class 'msam'
partable(fit, ...)

Arguments

fit

msam object

...

Other parameters

Details

Table of estimated parameters

Value

A matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


Function to actually do the plotting

Description

Function to actually do the plotting

Usage

## S3 method for class 'msam'
plotit(
  fit,
  what,
  x = lapply(attr(fit, "m_data")$sam, function(x) x$years),
  ylab = what,
  xlab = "Years",
  ex = numeric(0),
  trans = function(x) x,
  add = FALSE,
  ci = TRUE,
  cicol = gray(0.5, alpha = 0.5),
  addCI = NA,
  drop = 0,
  unnamed.basename = "current",
  xlim = NULL,
  ciAlpha = 0.3,
  col = .plotcols.crp(length(fit) + 1),
  extraLabel = NULL,
  addTotal = FALSE,
  onlyTotal = FALSE,
  legend.pos = "bottom",
  stocks = seq_len(length(fit) + addTotal),
  ...
)

## S3 method for class 'msamforecast'
plotit(
  fit,
  what,
  x = fit$data$years,
  ylab = what,
  xlab = "Years",
  ex = numeric(0),
  trans = exp,
  add = FALSE,
  ci = TRUE,
  cicol = gray(0.5, alpha = 0.5),
  addCI = NA,
  drop = 0,
  unnamed.basename = "current",
  xlim = NULL,
  addTotal = FALSE,
  onlyTotal = FALSE,
  ...
)

Arguments

fit

msam fit

what

variable to plot

x

x-axis values

ylab

y-axis label

xlab

x-axis label

ex

extra years to add

trans

function to transform variable to plot

add

If false a new plot is created. If true everything is added to the previous plot-

ci

Add confidence intervals?

cicol

Color of confidence intervals

addCI

Not used

drop

Number of years to drop from the end

unnamed.basename

not used

xlim

x-axis limits. If null, the range of x is used.

ciAlpha

Alpha channel value of confidence interval color

col

line colors

extraLabel

Not used

...

Other arguments


Print result from multisam.fit

Description

Print output from multisam.fit

Usage

## S3 method for class 'msam'
print(x, ...)

Arguments

x

A msam object

...

Other parameters passes to logLik


Print msam coef object

Description

Print msam coef object

Usage

## S3 method for class 'msamcoef'
print(x, ...)

Arguments

x

msamcoef object

...

Not used

Author(s)

Christoffer Moesgaard Albertsen


Read genepop data files

Description

Read genepop data files

Usage

read.gen(
  f,
  pop.names,
  sort.loci = FALSE,
  sort.individuals = FALSE,
  NAlleleKeep = NA
)

Arguments

f

file name

pop.names

population names. If missing, the ID of the last individual is used

sort.loci

Should loci be sorted by names?

sort.individuals

Should individuals be sorted by id?

Value

an allele array

Author(s)

Christoffer Moesgaard Albertsen


Recruitment plot

Description

Recruitment plot

Usage

## S3 method for class 'msam'
recplot(fit, ...)

Arguments

fit

msam object

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


Recruitment Table

Description

Recruitment Table

Usage

## S3 method for class 'msam'
rectable(fit, ...)

Arguments

fit

msam object

...

Other parameters

Details

Table of estimated recruitment to the fisheries.

Value

A matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


Simulate from a msam object

Description

Simulate from a msam object

Usage

## S3 method for class 'msam'
simulate(
  object,
  nsim = 1,
  seed = NULL,
  full.data = TRUE,
  ready.to.fit = FALSE,
  ...
)

Arguments

object

msam object result from multisam.fit

nsim

Number of simulations

seed

random number seed

full.data

should a full data set for sam.fit be returned?

...

Other arguments not used

Value

a list of lists.

Author(s)

Christoffer Moesgaard Albertsen


Stock-recruitment plot

Description

Stock-recruitment plot

Usage

## S3 method for class 'msam'
srplot(
  fit,
  textcol = "red",
  add = FALSE,
  col = .plotcols.crp(length(fit)),
  ...
)

Arguments

fit

msam object

textcol

Text color

add

Should the figure be added to a current plot?

col

Line colors

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


SSB plot

Description

SSB plot

Usage

## S3 method for class 'msam'
ssbplot(fit, ...)

Arguments

fit

msam object

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


SSB Table

Description

SSB Table

Usage

## S3 method for class 'msam'
ssbtable(fit, ...)

Arguments

fit

msam object

...

Other parameters

Details

Table of estimated spawning stock biomasses.

Value

A matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


Suggest correlation structure

Description

Construct valid (band) correlation structures for multisam.fit

Usage

suggestCorStructure(
  x,
  nAgeClose = 1,
  noConnection = FALSE,
  onlyCloseAge = TRUE,
  noCorInArea = TRUE,
  onlyCorInArea = FALSE,
  noCorBetween = matrix(0, 0, 2)
)

Arguments

x

a samset

nAgeClose

Number of close ages to correlate

noConnection

If TRUE, no correlations

onlyCloseAge

If TRUE, age classes no less than nAgeClose are un-correlated

noCorInArea

If TRUE, age classes in same area/stock are un-correlated

onlyCorInArea

If TRUE, only age classes in same area/stock are correlated

noCorBetween

matrix of area/stock numbers that should not be correlated

Value

A boolean matrix

Author(s)

Christoffer Moesgaard Albertsen


Summary of msam object

Description

Summary of msam object

Usage

## S3 method for class 'msam'
summary(object, returnList = FALSE, digits = c(0, 0, 3), ...)

Arguments

object

msam object

returnList

If true, a list of table is returned

digits

digits for rounding output. Vector of length 3 corresponding to recruitment, ssb, and fbar.

...

not used

Value

a summary table

Author(s)

Christoffer Moesgaard Albertsen


Table helper

Description

Table helper

Usage

tableit(
  fit,
  what,
  x = lapply(attr(fit, "m_data")$sam, function(x) x$years),
  trans = function(x) x,
  returnList = FALSE,
  ...
)

## S3 method for class 'msam'
tableit(
  fit,
  what,
  x = lapply(attr(fit, "m_data")$sam, function(x) x$years),
  trans = function(x) x,
  returnList = FALSE,
  addTotal = FALSE,
  ...
)

Arguments

fit

msam object

what

quoted name of what to extract

x

rownames of table

trans

function to be applied

returnList

If true, a list of matrices is returned

...

extra arguments not used

Value

matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


TSB plot

Description

TSB plot

Usage

## S3 method for class 'msam'
tsbplot(fit, ...)

Arguments

fit

msam object

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


TSB Table

Description

TSB Table

Usage

## S3 method for class 'msam'
tsbtable(fit, ...)

Arguments

fit

msam object

...

Other parameters

Details

Table of estimated total stock biomasses.

Value

A matrix of estimates and confidence intervals

Author(s)

Christoffer Moesgaard Albertsen


Life Years lost to fishing plot

Description

Life Years lost to fishing plot

Usage

## S3 method for class 'msam'
yearslostplot(fit, cause = c("Fishing", "Other", "LifeExpectancy"), ...)

Arguments

fit

msam object

...

extra arguments for plotting

Author(s)

Christoffer Moesgaard Albertsen


Yield per recruit calculation

Description

Yield per recruit calculation

Usage

## S3 method for class 'msam'
ypr(
  fit,
  Flimit = 2,
  Fdelta = 0.01,
  aveYears = lapply(attr(fit, "m_data")$sam, function(x) min(15, length(x$years))),
  ageLimit = 100,
  ...
)

Arguments

fit

msam object

Flimit

Upper limit for Fbar

Fdelta

increments on the Fbar axis

aveYears

A list/vector of same length as the number of stocks with number of years to average over

ageLimit

Oldest age used

...

not used

Value

A list of samypr objects

Author(s)

Christoffer Moesgaard Albertsen