Skip to content

Commit bf87157

Browse files
committed
Change params in dynamics() into a list (now matches projections())
1 parent d072a45 commit bf87157

6 files changed

+76
-71
lines changed

R/20_dynamics.R

+24-14
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,41 @@
55
#' @details
66
#' The population model is a single-sex age-structured model in which the number of calves or pups born each year is density dependent, with the extent of density dependence a function of the number of mature adults \eqn{\tildeN}, the fecundity (pregnancy rate) at pre-exploitation equilibrium \eqn{f_0}, the maximum theoretical fecundity rate fmax, the degree of compensation \eqn{z}, and the abundance of individuals aged 1+ \eqn{N_{y+1}^{1+}} relative to carrying capacity \eqn{K^{1+}}. This function can be used alone but is intended to be used with \code{Projections()} to generate multiple simulations. NOTE: Either \code{ConstantCatch} or \code{ConstantF} can be specified, but not both.
77
#'
8-
#' @param S0 Calf/pup survival, a numeric value between 0 and 1
9-
#' @param S1plus Survival for animals age 1 year and older, a numeric value between 0 and 1
10-
#' @param K1plus The pre-exploitation population size of individuals aged 1 and older. If this value is unavailable, it can be approximated by using the initial depletion and the estimate of current abundance
11-
#' @param AgeMat Age at maturity in years (assumed to be age at first parturition - 1).
8+
#' @param lh.params A list containing life history parameters: \cr\cr
9+
#' \code{S0} Calf/pup survival, a numeric value between 0 and 1 \cr\cr
10+
#' \code{S1plus} Survival for animals age 1 year and older, a numeric value between 0 and 1 \cr\cr
11+
#' \code{K1plus} The pre-exploitation population size of individuals aged 1 and older. If this value is unavailable, it can be approximated by using the initial depletion and the estimate of current abundance \cr\cr
12+
#' \code{AgeMat} Age at maturity in years (assumed to be age at first parturition - 1) \cr\cr
13+
#' \code{nages} "Maximum" age, treated as the plus group age. The plus group age can be set equal to the age at maturity +2 years without losing accuracy. Must be greater than \code{AgeMat}.\cr\cr
14+
#' \code{z} The degree of compensation. The default value is \code{z = 2.39}.\cr\cr
15+
#' \code{lambdaMax} Maximum steady rate of increase (population growth rate)
1216
#' @param InitDepl Starting depletion level
1317
#' @param ConstantCatch Total bycatch each year, expressed as a vector of length \code{nyears}
1418
#' @param ConstantF vector (length = \code{nyears}) rate of bycatch each year
15-
#' @param z The degree of compensation. The default value is \code{z = 2.39}.
1619
#' @param nyears Number of years to project
17-
#' @param nages "Maximum" age, treated as the plus group age. The plus group age can be set equal to the age at maturity +2 years without losing accuracy. Must be greater than AgeMat.
18-
#' @param lambdaMax Maximum steady rate of increase (population growth rate)
1920
#'
2021
#' @return A list containing a matrix \code{N} of numbers at age (dimensions \code{nyears} (rows) x \code{nages} (columns)) and one vector \code{TotalPop} (a vector of length \code{nyears}), containing the number of age 1+ individuals in the population.
21-
22-
# Note, nages = Plus Group Age, and Plus Group Age can = AgeMat+2 without losing accuracy (per AEP 11/30/18)
2322
#'
2423
#' @examples
2524
#' # Generate a time series of abundance for a bowhead whale
26-
#' dynamics(S0 = 0.944, S1plus = 0.99, K1plus = 9000, AgeMat = 17,
25+
#' dynamics(lh.params = list(S0 = 0.944, S1plus = 0.99, K1plus = 9000, AgeMat = 17,nages = 25,z = 2.39, lambdaMax = 1.04),
2726
#' InitDepl = 0.6, ConstantCatch = NA, ConstantF = rep(0.01, times = 100),
28-
#' z = 2.39, nyears = 100, nages = 25, lambdaMax = 1.04)
27+
#' nyears = 100)
2928
#' @export
30-
dynamics <- function(S0, S1plus, K1plus, AgeMat, InitDepl,
31-
ConstantCatch = NA, ConstantF = NA, z,
32-
nyears, nages, lambdaMax) {
29+
dynamics <- function(lh.params, InitDepl,
30+
ConstantCatch = NA, ConstantF = NA,
31+
nyears) {
32+
33+
#S0, S1plus, K1plus, AgeMat, nages, z, lambdaMax
34+
# Life history params
35+
S0 <- lh.params$S0
36+
S1plus <- lh.params$S1plus
37+
K1plus <- lh.params$K1plus
38+
AgeMat <- lh.params$AgeMat
39+
nages <- lh.params$nages
40+
z <- lh.params$z
41+
lambdaMax <- lh.params$lambdaMax
42+
3343
# Checks
3444
if (length(ConstantCatch) > 1 & length(ConstantF) > 1) {
3545
stop("Cannot have both constant F and constant catch- choose one and set the other to NA!")

R/21_projections.R

+25-19
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22
#'
33
#' @description Generates several projections, stochasticity is in the number of catches from year to year
44
#' @importFrom stats rlnorm
5-
#' @param NOut number of simulations
5+
#' @param NOut Number of simulations
66
#' @param ConstantBycatch Mean and CV of number of animals killed as bycatch per year (assumed lognormal)
77
#' @param ConstantRateBycatch Mean and CV of bycatch rate (assumed normal)
8-
#' @param InitDepl initial depletion. If obs_CV>0, this is the mean depletion.
9-
#' @param lh.params - life history parameters as a list. The list must include S0, S1plus, K1plus, AgeMat, nages, z, and lambdaMax
10-
#' @param nyears number of years to do projections
11-
#' @param obs_CV observation CV. Default to 1 for simple projections
12-
#' @return list of outputs from simulations: \code{params} contains parameter values for each trajectory as a matrix; \code{trajectories} contains simulation outputs as a matrix; \code{fishing.rates} contain the bycatch rates for each year in each simulation as a matrix; \code{InitDepl} returns the initial depletion for the projections; \code{ConstantBycatch} provides Catch (total individuals killed in bycatch events per year) and CV of Catch (if the user has specified bycatch as a constant number); \code{ConstantRateBycatch} contains Bycatch Rate (additional mortality from bycatch each year) and CV of ByCatch rate. Other parameters are the same as in the \code{dynamics()} function.
8+
#' @param InitDepl Initial depletion. If obs_CV>0, this is the mean depletion.
9+
#' @param lh.params Life history parameters as a list. The list must include S0, S1plus, K1plus, AgeMat, nages, z, and lambdaMax.
10+
#' @param nyears Number of years to do projections
11+
#' @param obs_CV Observation CV. Default to 1 for simple projections
12+
#' @return A list of outputs from simulations: \cr\cr
13+
#' * \code{params} contains parameter values for each trajectory as a matrix; \cr
14+
#' * \code{trajectories} contains simulation outputs as a matrix; \cr
15+
#' * \code{fishing.rates} contain the bycatch rates for each year in each simulation as a matrix; \cr
16+
#' * \code{InitDepl} returns the initial depletion for the projections; \cr
17+
#' * \code{ConstantBycatch} provides Catch (total individuals killed in bycatch events per year) and CV of Catch (if the user has specified bycatch as a constant number); \cr
18+
#' * \code{ConstantRateBycatch} contains Bycatch Rate (additional mortality from bycatch each year) and CV of ByCatch rate. Other parameters are the same as in the \code{dynamics()} function.
1319
#'
1420
#' @examples
1521
#' projections(
@@ -33,7 +39,7 @@ projections <- function(NOut,
3339
obs_CV = 0) {
3440
trajectories <- rep(0, times = nyears)
3541
fishing.rates <- rep(0, times = nyears)
36-
params <- 0 # S0, S1plus, fmax, K1plus, z # also need: nyears = 50, nages = 15
42+
params <- 0
3743

3844
# Life history params
3945
S0 <- lh.params$S0
@@ -45,23 +51,23 @@ projections <- function(NOut,
4551
lambdaMax <- lh.params$lambdaMax
4652

4753
# Checks for parameter values
48-
if (S0 >= 1) {
54+
if (lh.params$S0 >= 1) {
4955
warning("Calf/pup survival must be between 0 and 1")
5056
}
51-
if (S1plus >= 1) {
57+
if (lh.params$S1plus >= 1) {
5258
warning("Adult survival must be between 0 and 1")
5359
}
54-
if (AgeMat > nages) {
60+
if (lh.params$AgeMat > nages) {
5561
warning("Age at maturity must be less than plus group age")
5662
}
63+
if (lh.params$lambdaMax < 1) {
64+
warning("LambdaMax should be greater than 1.
65+
Typical values are lambdaMax = 1.04 for cetaceans and lambdaMax = 1.12 for pinnipeds.")
66+
}
5767
if (!is.na(ConstantBycatch$Catch) & !is.na(ConstantRateBycatch$Rate)) {
5868
warning("You cannot provide both bycatch as a whole number and as a rate.
5969
Please specify either ConstantBycatch or ConstantRateBycatch.")
6070
}
61-
if (lambdaMax < 1) {
62-
warning("LambdaMax should be greater than 1.
63-
Typical values are lambdaMax = 1.04 for cetaceans and lambdaMax = 1.12 for pinnipeds.")
64-
}
6571

6672
set.seed(123)
6773

@@ -79,9 +85,9 @@ projections <- function(NOut,
7985
sdlog.catches <- sqrt(log(ConstantBycatch$CV^2 + 1)) # should be ~cv when sd is low (<0.5)
8086
catch.vec <- stats::rlnorm(n = nyears, meanlog = log(ConstantBycatch$Catch), sdlog = sdlog.catches)
8187
traj <- dynamics(
82-
S0 = S0, S1plus = S1plus, K1plus = K1plus, AgeMat = AgeMat, InitDepl = InitVal / K1plus,
88+
lh.params = lh.params, InitDepl = InitVal / lh.params$K1plus,
8389
ConstantCatch = catch.vec,
84-
z = z, nyears = nyears, nages = nages, lambdaMax = lambdaMax
90+
nyears = nyears
8591
)$TotalPop # TotalPop is 1+ component
8692
param.vec <- unlist(lh.params)
8793
if (NTries < NOut) {
@@ -107,10 +113,10 @@ projections <- function(NOut,
107113
}
108114
fishing.rates <- rbind(fishing.rates, f.vec)
109115

110-
traj <- dynamics(
111-
S0 = S0, S1plus = S1plus, K1plus = K1plus, AgeMat = AgeMat, InitDepl = InitVal / K1plus,
116+
traj <- dynamics(lh.params = lh.params,
117+
InitDepl = InitVal / K1plus,
112118
ConstantF = f.vec,
113-
z = z, nyears = nyears, nages = nages, lambdaMax = lambdaMax
119+
nyears = nyears
114120
)$TotalPop # 1+ component of population
115121

116122
param.vec <- unlist(lh.params)

R/28_extract_df.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#' Extract results from `dynamics()` function as a dataframe
1+
#' Extract results from `projections()` function as a dataframe
22
#'
3-
#' @description #unnest results from dynamics() and turn them into a dataframe that you can bind together
3+
#' @description Unnest results from projections() and turn them into a dataframe that you can bind together
44
#' @param x Outputs from a call to projections(), which are a named list
55
#'
66
#' @return a dataframe with population trajectories, initial depletion, and bycatch levels that can be printed as a table.

man/dynamics.Rd

+11-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/extract_df.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/projections.Rd

+12-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)