Skip to content

Commit

Permalink
update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWilliams-NOAA committed Oct 2, 2024
1 parent 43f78d2 commit 7dc2305
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 101 deletions.
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

export(F_to_mu)
export(baranov)
export(beverton_holt)
export(catch_at_age)
export(compute_bio)
export(compute_fleet_catch)
export(compute_ssb)
export(compute_total_catch)
export(compute_total_f)
export(cyclic.recruitment)
export(cyclic_recruitment)
export(discard_F)
export(extend_years)
export(findF_bisection)
Expand Down
24 changes: 12 additions & 12 deletions R/derived_quantities.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Compute Spawning Stock Biomass
#'
#'
#' Compute SSB from a matrix of NAA and a set of demographic
#' parameters.
#'
Expand All @@ -8,7 +8,7 @@
#'
#' @export compute_ssb
#'
#' @example

#'
compute_ssb <- function(naa, dem_params){
return(
Expand All @@ -17,7 +17,7 @@ compute_ssb <- function(naa, dem_params){
}

#' Compute Total Stock Biomass
#'
#'
#' Compute biomass from a matrix of NAA and a set of demographic
#' parameters. This computes the sum of male and female biomass
#' across all age classes. For mature female biomass, see
Expand All @@ -26,9 +26,9 @@ compute_ssb <- function(naa, dem_params){
#' @param naa numbers-at-age matrix [nyears, nages, nsexes, nregions]
#' @param dem_params demagraphic parameters list
#'
#' @export compute_bio
#' @export compute_bio
#'
#' @example

#'
compute_bio <- function(naa, dem_params){
return(
Expand All @@ -37,14 +37,14 @@ compute_bio <- function(naa, dem_params){
}

#' Compute Total Catch
#'
#'
#' Compute total catch volume (in mt) from a matrix of CAA.
#'
#' @param caa catch-at-age matrix [nyears, nages, nsexes, nregions, nfleets]
#'
#' @export compute_total_catch
#' @export compute_total_catch
#'
#' @example

#'
compute_total_catch <- function(caa){
return(
Expand All @@ -53,15 +53,15 @@ compute_total_catch <- function(caa){
}

#' Compute Catch by Fishing Fleet
#'
#'
#' Compute total catch volume (in mt) for each fishing
#' fleet from a matrix of CAA.
#'
#' @param caa catch-at-age matrix [nyears, nages, nsexes, nregions, nfleets]
#'
#' @export compute_fleet_catch
#'
#' @example

#'
compute_fleet_catch <- function(caa){
return(
Expand All @@ -70,7 +70,7 @@ compute_fleet_catch <- function(caa){
}

#' Compute Total Fishing Mortality
#'
#'
#' Compute total fishing mortality from a matrix of FAA.
#' Note that this is an approximation of total fishing mortality,
#' and assumes that selectivity = 1 for some age class.
Expand All @@ -79,7 +79,7 @@ compute_fleet_catch <- function(caa){
#'
#' @export compute_total_f
#'
#' @example

#'
compute_total_f <- function(faa){
return(
Expand Down
42 changes: 17 additions & 25 deletions R/plotting.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Plot demographic parameters
#'
#'
#' Generate standard plot for all demographic
#' parameter matrices. Output plots will include:
#' - weight-at-age
Expand All @@ -16,12 +16,11 @@
#' @param out_dir optional directory to save plots to
#' @param ... additional parameters to be passed to `ggsave`
#'
#' @export plot_demographic_parameters
#' @export
#'
#' @example
#'
plot_demographic_parameters <- function(dem_params, params=NA, show_plots=TRUE, out_dir=NA, ...){

waa_plot <- plot_waa(dem_params$waa)
mat_plot <- plot_mat(dem_params$mat)
mort_plot <- plot_mort(dem_params$mort, is_dmr=FALSE)
Expand Down Expand Up @@ -55,15 +54,14 @@ plot_demographic_parameters <- function(dem_params, params=NA, show_plots=TRUE,
}

#' Plot Weight-at-Age
#'
#'
#' Generates plot of weight-at-age across time, age, sex,
#' and region as appropriate.
#'
#' @param waa four-dimensional weight-at-age matrix
#'
#' @export plot_waa
#' @export
#'
#' @example
#'
plot_waa <- function(waa){

Expand All @@ -82,7 +80,7 @@ plot_waa <- function(waa){
coord_cartesian(expand=0)+
labs(x="Age", y="Weight", color="Sex", linetype="Time Block", title="Weight-at-Age")+
theme_bw()

if(dimensions$nregions > 1){
plot <- plot + facet_wrap(~region, scales="free_y")
}
Expand All @@ -92,16 +90,14 @@ plot_waa <- function(waa){
}

#' Plot Maturity-at-Age
#'
#'
#' Generates plot of maturity-at-age across time, age, sex,
#' and region as appropriate. Only the female maturity ogive
#' is plotted.
#'
#' @param mat four-dimensional maturity-at-age matrix
#'
#' @export plot_mat
#'
#' @example
#' @export
#'
plot_mat <- function(mat){

Expand Down Expand Up @@ -131,16 +127,14 @@ plot_mat <- function(mat){
}

#' Plot Mortality
#'
#'
#' Generates plot of mortality-at-age across time, age, sex,
#' and region as appropriate. This function can be used for
#' and region as appropriate. This function can be used for
#' both natural and discard mortality
#'
#' @param waa four-dimensional mortality-at-age matrix
#'
#' @export plot_mort
#'
#' @example
#' @export
#'
plot_mort <- function(mort, is_dmr=FALSE){

Expand Down Expand Up @@ -169,16 +163,14 @@ plot_mort <- function(mort, is_dmr=FALSE){
}

#' Plot Selectivity-at-Age and Retention-at-Age
#'
#'
#' Generates plot of selectivity-at-age or retention-at-age
#' across time, age, sex, region, and fleet as appropriate.
#'
#' @param selret five-dimensional selectivity-at-age or
#' retention-at-age matrix
#'
#' @export plot_selret
#'
#' @example
#' @export
#'
plot_selret <- function(selret, is_selectivity=TRUE){

Expand Down Expand Up @@ -342,15 +334,15 @@ plot_atage <- function(atage){
mutate(total_naa = sum(naa)) %>%
mutate(prop = naa/total_naa) %>%
mutate(avg_age = weighted.mean(age, prop))


plot <- ggplot(atage_df)+
geom_point(aes(x=time, y=age, size=prop, color=prop))+
geom_line(
data=atage_df %>% distinct(time, avg_age),
data=atage_df %>% distinct(time, avg_age),
aes(x=time, y=avg_age),
linewidth=2,
color='red',
linewidth=2,
color='red',
show.legend = FALSE
)+
coord_cartesian(expand=0.01)+
Expand Down
12 changes: 5 additions & 7 deletions R/project_multi.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Project Population Forward Multiple Years
#'
#'
#' A wrapper function around the base `project` function that handles
#' projecting forward multiple years given a removals timeseries and a
#' projecting forward multiple years given a removals timeseries and a
#' recruitment timeseries.
#'
#' @param init_naa numbers-at-age matrix in starting year ([1, nages, nsexes, nregions])
Expand All @@ -11,9 +11,7 @@
#' @param nyears number of projection yeas
#' @param model_option list of additional model options
#'
#' @export project_multi
#'
#' @example
#' @export
#'
project_multi <- function(init_naa, removals_timeseries, recruitment, dem_params, nyears, model_options){

Expand Down Expand Up @@ -50,7 +48,7 @@ project_multi <- function(init_naa, removals_timeseries, recruitment, dem_params

# full_recruitment <- array(NA, dim=c(nyears, 1, 1, nregions))
r <- apportion_recruitment(
rec_timeseries = recruitment,
rec_timeseries = recruitment,
apportionment = model_options$recruit_apportionment,
nyears = nyears,
nregions = nregions
Expand Down Expand Up @@ -87,7 +85,7 @@ project_multi <- function(init_naa, removals_timeseries, recruitment, dem_params
nregions = nregions,
list(naa=naa[y,,,,drop=FALSE], dem_params=dp.y)
)

out_vars <- project(
removals = removals_input,
dem_params=dp.y,
Expand Down
22 changes: 11 additions & 11 deletions R/recruitment.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Resample from historical recruitment timeseries
#'
#' Draw `n` random samples, with replacement, from the
#' Draw `n` random samples, with replacement, from the
#' historical recruitment timeseries.
#'
#' @param n number of samples to draw
Expand All @@ -9,10 +9,10 @@
#'
#' @return `n` random samples from the historical
#' recruitment timeseries.
#'
#' @export resample_recruitment
#'
#' @example
#' @export
#'
#' @examples
#' set.seed(1120)
#' n <- 100
#' hist.rec <- c(100, 200, 50, 100, 120)
Expand All @@ -37,19 +37,19 @@ mean_recruitment <- function(n, log.mu, sd, seed=NA){
#' Description
#'
#' @param n number of recruitment events
#' @param mu.rec vector of mean log-recruitment levels, one for each regime
#' @param sd.rec vector of recruitment variation, one for each regime
#' @param mu_rec vector of mean log-recruitment levels, one for each regime
#' @param sd_rec vector of recruitment variation, one for each regime
#' @param dur duration of regimes
#' @param start which regime to start in
#' @param seed a random seed (optional)
#'
#' @return `n` log-recruitment levels from two different regimes of
#' length `dur`.
#' @export cyclic.recruitment
#'
#' @example
cyclic.recruitment <- function(n, mu_rec, sd_rec, dur, start=1, seed=NA){
#' @export
#'

cyclic_recruitment <- function(n, mu_rec, sd_rec, dur, start=1, seed=NA){

if(!is.na(seed)){
set.seed(seed)
}
Expand Down
7 changes: 4 additions & 3 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,16 @@ extend_years <- function(dem_params, dimension, e, new.dimnames=NA){


#' Set Default Values for Model Options
#'
#'
#' Set up a fully formed model_options list object with all
#' required elements set to sensible default values.
#'
#' @param model_dimensions model dimensions list object
#'
#' @export setup_model_options
#' @export
#'
#' @example \dontrun{
#' @examples
#' \dontrun{
#' dimensions = list(nyears=60, nages=50, nsexes=2, nregions=1, nfleets=1)
#' model_options = setup_model_options(dimensions)
#' }
Expand Down
21 changes: 11 additions & 10 deletions R/validate_dem_params.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#' Validate Demographic Parameter Matrices
#'
#'
#' Helper function for validating that all required demographic
#' parameter matrices have been defined and that they are all of
#' the correct dimension.
#'
#' Required demographic parameters are: mort, waa, sexrat, sel,
#' parameter matrices have been defined and that they are all of
#' the correct dimension.
#'
#' Required demographic parameters are: mort, waa, sexrat, sel,
#' ret, dmr, and surv_sel
#'
#'
#' Expected dimensions are: [nyears, nages, nsexes, nregions, nfleets]
#'
#' @param dem_params a named list containing the demographic rate matrices.
Expand All @@ -16,10 +16,11 @@
#'
#' @return unmodified dem_params object; if dem_params list is invalid, will
#' return a descriptive error message
#' @export validate_dem_params
#' @export
#'
#' @examples
#' \dontrun{
#'
#' @example \dontrun{
#'
#' model_dims = list(nyears=64, nages=30, nsexes=2, nregions=1, nfleets=2)
#' dem_params = list(
#' mort = mortality_matrix,
Expand All @@ -31,7 +32,7 @@
#' surv_sel = survey_selectivity_matrix
#' )
#' dem_params = validate_dem_params(dem_params, model_dims)
#'
#'
#' }
#'
validate_dem_params <- function(dem_params, model_dims){
Expand Down
Loading

0 comments on commit 7dc2305

Please sign in to comment.