Install and load the library.

Start by loading the libraries, after installing them if needed. Note that BycatchEstimator will open all the other libraries it needs, including tidyverse. For more guidance, see here: https://ebabcock.github.io/BycatchEstimator/articles/InstallationGuide.html

# devtools::install_github("ebabcock/BycatchEstimator")
library(BycatchEstimator)
library(MuMIn)

Look at format of the input data.

For bycatch estimation, there are two data sources, the observer data, which has one row per sample unit (sets or trips) and the logbook data. For CPUE standardization, only the observer data is needed.

The package includes some simulated longline observer data from LLSIM, and a toy example data set. This is the toy data:

summary(obsdatExample)
dim(obsdatExample)

This is trip by trip observer data from a benthic longline fishery. The variables we need are Catch, which is bycatch of a grouper species, sampled.sets, which is the unit of effort, and the variables Year, EW and season which will define the stratification for the design-based estimate, and serve as predictor variables for the model-base estimates. CPUE would be in terms of Catch/sampled.sets.

The corresponding logbook data is here:

summary(logdatExample)
dim(logdatExample)

The logbook data must include all the effort for which bycatch estimates are needed (i.e.the whole fleet). This logbook data contains one row per trip, the same as the observer data. The logbook data could also be aggregated, with a column giving the sample size per row (here trips is 1 for all rows). The logbook data must include effort in the same units as the observer data (in this case sets), and the same predictor variables (in this case Year, EW, and season).

Bycatch setup

The first step in bycatch estimation is setup the input file and review and verify the data. Notice that returned value from bycatchSetup is assigned as an object that will be used in a subsequent step for model fitting. This step also produces output that is saved to the working directory for the user to review. This step also generates the design-based estimates, as requested by the user, but does not make any figures. The inputs to the bycatchSetup function are explained in the help file. To see what the function does:

?bycatchSetup

The help file explains all the inputs. Read through them to see what options there are. The User's Guide goes into much more detail.

For now, let's run the example from the help file. First set you working directory to where you want the outputs to be, for example:

# Commented out so it will not run, because every computer has a different path. 
# setwd("~/Box/bycatch project (ebabcock@miami.edu)/LLSim/Papers and talks")

Now copy and paste the example from the bycatchSetup help file and run it.

setupObj<-bycatchSetup(
modelTry = c("Delta-Lognormal","TMBnbinom2"),
obsdat = obsdatExample,
logdat = logdatExample,
yearVar = "Year",
obsEffort = "sampled.sets",
logEffort = "sets",
logUnsampledEffort = NULL,
includeObsCatch  = FALSE,
matchColumn = NA,
factorNames = c("Year","season"),
randomEffects = NULL,
randomEffects2 = NULL,
EstimateIndex = TRUE,
EstimateBycatch = TRUE,
logNum = NA,
sampleUnit = "trips",
complexModel = formula(y~(Year+season)^2),
simpleModel = formula(y~Year),
indexModel = formula(y~Year),
designMethods = c("Ratio", "Delta"),
designVars = c("Year","season"),
designPooling = TRUE,
poolTypes=c("adjacent","all"),
pooledVar=c(NA,NA),
adjacentNum=c(1,NA),
minStrataUnit = 1,
baseDir = getwd(),
runName = "SimulatedExample",
runDescription = "Example with simulated data",
common = "Simulated species",
sp = "Genus species",
obsCatch = "Catch",
catchUnit = "number",
catchType = "dead discard"
)

The tool has printed some output files in a the directory you specified with baseDir and a subdirectory named runName.

The pdf file called "dataSummary.pdf" shows a summary of the available data. At this point, if the fishery and/or the observer coverage is low, check to make sure that you have at least some positive observations in each year. If any year has no positive observations, the delta models will not work. This example has only 1 positive observation in each of the last two years, so it will not crash, but we can expect some models to perform badly. The last two columns of the dataSummary table are a ratio estimator that is unstratified (except by year) as a quick reality check on the expected magnitude of the total bycatch.

The bycatchSetup function also generates the design-based estimators. The available estimator types are a stratified ratio estimator, and a stratified design-based delta-lognormal estimator. These results are in the .csv files with "design" in the name. There is also a file with "Pooling" in the name, that gives some information about how much pooling was done. In this example, pooling was only requested if there was less than one sample unit (trip) and only to adjacent years and all season, which was not enough to eliminate the zero estiamtes in some strata. We will return to this later.

Bycatch fit

The final step is to fit all the bycatch estimation models using the bycatchFit function.

?bycatchFit

This function has a few more specifications regarding model selection, such as which information criterion to use, and whether there are true values to plot against for validation (e.g. in a simulation study). This function will using the information criteria to find the best set of predictor variables for each kind of model listed in modelTry. It can also do cross validation if desired. It prints out the model based estimates, diagnostics, and selection criteria. Run the example in the help file

bycatchFit(
 setupObj = setupObj,
 selectCriteria = "BIC",
 DoCrossValidation = TRUE,
 DredgeCrossValidation = FALSE,
 ResidualTest = FALSE,
 CIval = 0.05,
 VarCalc = "Simulate",
 useParallel = TRUE,
 nSims = 1000,
 baseDir = getwd(),
 plotValidation = FALSE,
 trueVals = NULL,
 trueCols = NULL
)

There will now be a pdf file in the output directory that includes all the figures and plots. In addition, all the model outputs are printed to .csv files for later use.



natureanalytics-ca/BycatchEstimator documentation built on June 2, 2025, 12:38 p.m.