Skip to content

Commit

Permalink
style and docs: run devtools::document() and styler::style_pkg()
Browse files Browse the repository at this point in the history
  • Loading branch information
iantaylor-NOAA authored and kellijohnson-NOAA committed Jul 17, 2024
1 parent bf7034d commit bd06f5a
Show file tree
Hide file tree
Showing 41 changed files with 1,108 additions and 901 deletions.
81 changes: 24 additions & 57 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,25 @@ Package: PacFIN.Utilities
Title: Generate fishery composition data from PacFIN data for the NWFSC
Version: 0.2.9
Authors@R: c(
person(
given = c("Kelli", "F."),
family = "Johnson",
role = c("aut", "cre"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-5149-451X")
),
person(
given = c("Chantel", "R."),
family = "Wetzel",
role = "aut",
email = "[email protected]",
comment = c(ORCID = "0000-0002-7573-8240")
),
person(
given = c("Kathryn", "L."),
family = "Doering",
role = "ctb",
email = "[email protected]",
comment = c(ORCID = "0000-0002-0396-7044")
),
person(
given = c("Brian", "J."),
family = "Langseth",
role = "ctb",
email = "[email protected]",
comment = c(ORCID = "0000-0002-9901-6146")
),
person(
given = "Andi",
family = "Stephens",
role = "ctb",
email = "[email protected]"
),
person(
given = c("Ian", "G."),
family = "Taylor",
role = "ctb",
email = "[email protected]",
comment = c(ORCID = "0000-0002-4232-5669")
),
person(
given = c("John", "R."),
family = "Wallace",
role = "ctb",
email = "[email protected]",
comment = c(ORCID = "0000-0002-2333-1262")
)
)
Description: Manipulates data from the PacFIN database for stock synthesis.
person(c("Kelli", "F."), "Johnson", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-5149-451X")),
person(c("Chantel", "R."), "Wetzel", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0002-7573-8240")),
person(c("Kathryn", "L."), "Doering", , "[email protected]", role = "ctb",
comment = c(ORCID = "0000-0002-0396-7044")),
person(c("Brian", "J."), "Langseth", , "[email protected]", role = "ctb",
comment = c(ORCID = "0000-0002-9901-6146")),
person("Andi", "Stephens", , "[email protected]", role = "ctb"),
person(c("Ian", "G."), "Taylor", , "[email protected]", role = "ctb",
comment = c(ORCID = "0000-0002-4232-5669")),
person(c("John", "R."), "Wallace", , "[email protected]", role = "ctb",
comment = c(ORCID = "0000-0002-2333-1262"))
)
Description: Manipulates data from the PacFIN database for stock
synthesis.
License: MIT + file LICENSE
Depends:
R (>= 4.2)
License: MIT + file LICENSE
LazyData: true
Imports:
cli,
dplyr,
Expand All @@ -81,10 +46,12 @@ Suggests:
RODBC,
testthat (>= 3.0.0),
usethis
VignetteBuilder:
knitr
Remotes:
github::pfmc-assessments/nwfscSurvey
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Encoding: UTF-8
github::pfmc-assessments/nwfscSurvey
Config/testthat/edition: 3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
141 changes: 77 additions & 64 deletions R/EF1_Denominator.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
#' For a survey, tow- or haul-level data are typically available.
#' The sum of the weight in the sample is calculated different for each state
#' based on the data that are available.
#'
#'
#' Oregon provides information on the weight of females and males in the sample
#' via the `WEIGHT_OF_FEMALES` and `WEIGHT_OF_MALES` columns. These are often
#' model-based weights, which would be the only way they can get weights when
#' the fish were not weighed themselves. The weight of unsexed fish is calculated
#' internally by the code and added to the female and male weight.
#' **todo**: Let Oregon know that this calculation is being done and they may want
#' to provide UNK_WGT.
#'
#'
#' California sample weights were previously based on the column labeled
#' `SPECIES_WEIGHT`. Now, California data is parsed by PacFIN to furnish
#' species-specific cluster weights. Prior, cluster weights included the weight
Expand Down Expand Up @@ -59,7 +59,7 @@
#' * `Wt_Sampled_1`: the sum of sex-specific weights within the sample.
#' * `Wt_Sampled_2`: the species-specific sample weight only provided by
#' California in cluster weight.
#' * `LW_Calc_Wt`: individual weights predicted from the specified
#' * `LW_Calc_Wt`: individual weights predicted from the specified
#' length-weight relationships.
#' * `Wt_Sampled_3`: The sum of empirical weights, for those fish within a
#' sample where this information is available, and weights calculated from the
Expand All @@ -79,12 +79,13 @@ EF1_Denominator <- function(Pdata,
verbose = TRUE,
plot = FALSE,
col.weight = "weightkg") {

if (verbose) {
cat("\nIndividual weights will be generated from the following values:\n\n")
cat(" Females:", fa,fb, "\n",
"Males:", ma,mb, "\n",
"Unknowns and hermaphrodites:", ua,ub, "\n\n")
cat(
" Females:", fa, fb, "\n",
"Males:", ma, mb, "\n",
"Unknowns and hermaphrodites:", ua, ub, "\n\n"
)
} # End if verbose

stopifnotcolumn(Pdata, col.weight)
Expand All @@ -95,65 +96,74 @@ EF1_Denominator <- function(Pdata,
sex = Pdata$SEX,
pars = data.frame(
"A" = c("females" = fa, "males" = ma, "all" = ua),
"B" = c("females" = fb, "males" = mb, "all" = ub)),
"B" = c("females" = fb, "males" = mb, "all" = ub)
),
unit.out = "lb"
)
)

#### Calculate sample weight using FISH_WEIGHT in lbs
Pdata <- Pdata %>%
# Use weightkg if available and calculated from WL relationship when NA
# Note the change in units for weightkg from KG to LBS
dplyr::mutate(
bestweight = dplyr::case_when(
is.na(weightkg) ~ LW_Calc_Wt,
TRUE ~ weightkg * 2.20462)
# Use weightkg if available and calculated from WL relationship when NA
# Note the change in units for weightkg from KG to LBS
dplyr::mutate(
bestweight = dplyr::case_when(
is.na(weightkg) ~ LW_Calc_Wt,
TRUE ~ weightkg * 2.20462
)
) %>%
# Group by SAMPLE_NO so all subsequent calculations are done on subsets
# of the data, i.e., mean(bestweight) is mean of the bestweight in a
# specific sample
dplyr::group_by(SAMPLE_NO) %>%
dplyr::mutate(
bestweight = ifelse(
is.na(bestweight),
mean(bestweight),
bestweight)
# Group by SAMPLE_NO so all subsequent calculations are done on subsets
# of the data, i.e., mean(bestweight) is mean of the bestweight in a
# specific sample
dplyr::group_by(SAMPLE_NO) %>%
dplyr::mutate(
bestweight = ifelse(
is.na(bestweight),
mean(bestweight),
bestweight
)
) %>%
# Calculate sample weights and weight of unsexed fish per SAMPLE_NO
dplyr::mutate(
Wt_Sampled_3_L = sum(
na.rm = TRUE,
ifelse(is.na(length), NA, bestweight)
),
Wt_Sampled_3_A = sum(
na.rm = TRUE,
ifelse(is.na(Age), NA, bestweight)
),
UNK_WT = sum(ifelse(SEX == "U", bestweight, 0)),
UNK_NUM = sum(SEX == "U")
) %>%
# Back out the weight of fish that have no length or Age for each
# specific sample weight, if all are NA in sample, then set to 0.
dplyr::mutate(
Wt_Sampled_1_A = (-1 * sum(ifelse(is.na(Age), bestweight, 0)) +
FEMALES_WGT + MALES_WGT + UNK_WT) *
ifelse(all(is.na(Age)), 0, 1),
Wt_Sampled_1_L = (-1 * sum(ifelse(is.na(length), bestweight, 0)) +
FEMALES_WGT + MALES_WGT + UNK_WT) *
ifelse(all(is.na(length)), 0, 1)
) %>%
# Calculate sample weights and weight of unsexed fish per SAMPLE_NO
dplyr::mutate(
Wt_Sampled_3_L = sum(na.rm = TRUE,
ifelse(is.na(length), NA, bestweight)),
Wt_Sampled_3_A = sum(na.rm = TRUE,
ifelse(is.na(Age), NA, bestweight)),
UNK_WT = sum(ifelse(SEX == "U", bestweight, 0)),
UNK_NUM = sum(SEX == "U")
dplyr::ungroup() %>%
dplyr::group_by(SAMPLE_NO, CLUSTER_NO) %>%
# Do the same for CLUSTER_WGT
dplyr::mutate(
Wt_Sampled_2_A = (-1 * sum(ifelse(is.na(Age), bestweight, 0)) +
CLUSTER_WGT) * ifelse(all(is.na(Age)), 0, 1),
Wt_Sampled_2_L = (-1 * sum(ifelse(is.na(length), bestweight, 0)) +
CLUSTER_WGT) * ifelse(all(is.na(length)), 0, 1)
) %>%
# Back out the weight of fish that have no length or Age for each
# specific sample weight, if all are NA in sample, then set to 0.
dplyr::mutate(
Wt_Sampled_1_A = (-1 * sum(ifelse(is.na(Age), bestweight, 0)) +
FEMALES_WGT + MALES_WGT + UNK_WT) *
ifelse(all(is.na(Age)), 0, 1),
Wt_Sampled_1_L = (-1 * sum(ifelse(is.na(length), bestweight, 0)) +
FEMALES_WGT + MALES_WGT + UNK_WT) *
ifelse(all(is.na(length)), 0, 1)
) %>% dplyr::ungroup() %>% dplyr::group_by(SAMPLE_NO, CLUSTER_NO) %>%
# Do the same for CLUSTER_WGT
dplyr::mutate(
Wt_Sampled_2_A = (-1 * sum(ifelse(is.na(Age), bestweight, 0)) +
CLUSTER_WGT) * ifelse(all(is.na(Age)), 0, 1),
Wt_Sampled_2_L = (-1 * sum(ifelse(is.na(length), bestweight, 0)) +
CLUSTER_WGT) * ifelse(all(is.na(length)), 0, 1)
) %>%
# Bring the calculations back to the full scale of the data frame
dplyr::ungroup() %>%
# Coalesce sets things to downstream values, only if NA, i.e.,
# Wt_Sampled_[AL] is set by priority left to right 1, 2, 3
dplyr::mutate(
Wt_Sampled_A = dplyr::coalesce(Wt_Sampled_1_A, Wt_Sampled_2_A, Wt_Sampled_3_A),
Wt_Sampled_L = dplyr::coalesce(Wt_Sampled_1_L, Wt_Sampled_2_L, Wt_Sampled_3_L)
# Bring the calculations back to the full scale of the data frame
dplyr::ungroup() %>%
# Coalesce sets things to downstream values, only if NA, i.e.,
# Wt_Sampled_[AL] is set by priority left to right 1, 2, 3
dplyr::mutate(
Wt_Sampled_A = dplyr::coalesce(Wt_Sampled_1_A, Wt_Sampled_2_A, Wt_Sampled_3_A),
Wt_Sampled_L = dplyr::coalesce(Wt_Sampled_1_L, Wt_Sampled_2_L, Wt_Sampled_3_L)
) %>%
# Return a data frame rather than a tibble
data.frame
# Return a data frame rather than a tibble
data.frame()

#### Summary and boxplot
# todo: revamp the summary and plots
Expand All @@ -162,7 +172,7 @@ EF1_Denominator <- function(Pdata,
Pdata$Wt_Sampled_3_L, Pdata$Wt_Sampled_L
))

names(printemp) = c("M+F+U","Cluster","L-W","Final Wt_Sampled")
names(printemp) <- c("M+F+U", "Cluster", "L-W", "Final Wt_Sampled")

if (verbose) {
cat("\nSample weights\n\n")
Expand Down Expand Up @@ -200,13 +210,16 @@ EF1_Denominator <- function(Pdata,
args.legend = list(x = "topleft", bty = "n")
)
}
gg <- plotWL(Pdata[,"lengthcm"], Pdata[, "SEX"], Pdata[, "weightkg"],
Pdata[, "LW_Calc_Wt"] * 0.453592)
ggplot2::ggsave(gg, file = plot2,
width = 6, height = 6, units = "in")
gg <- plotWL(
Pdata[, "lengthcm"], Pdata[, "SEX"], Pdata[, "weightkg"],
Pdata[, "LW_Calc_Wt"] * 0.453592
)
ggplot2::ggsave(gg,
file = plot2,
width = 6, height = 6, units = "in"
)
}
if (nNA == 0 & verbose) cat("\nSample Wts found for all samples.\n\n")

return(Pdata)

} # End EF1_Denominator
27 changes: 14 additions & 13 deletions R/EF1_Numerator.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Calculate the numerator for the first level expansion factor
#'
#'
#' Calculate the numerator for the first-level expansion factor, where
#' the numerator is the species-specific landing weight for a given sample.
#' Thus, if two clusters were sampled from a single trip,
Expand All @@ -12,7 +12,7 @@
#' For Washington, `Pdata$RWT_LBS`, `Pdata$TOTAL_WGT`, `RWT_LBS`, or
#' `median(Pdata$TOTAL_WGT)`.
#' Then, if all else failed, per-year, state-specific medians.
#'
#'
#' Now, PacFIN works hard behind the scenes to provide species-specific landing
#' weights for each sampled fish. Therefore, we no longer rely on code to
#' calculate a fabricated landing weight. Species-specific landing weights are
Expand All @@ -33,29 +33,32 @@
#' @template plot
#' @author Andi Stephens

EF1_Numerator = function(Pdata,
verbose = TRUE,
plot = FALSE) {

EF1_Numerator <- function(Pdata,
verbose = TRUE,
plot = FALSE) {
Pdata$Trip_Sampled_Lbs <- dplyr::coalesce(
Pdata[["EXP_WT"]], Pdata[["RWT_LBS"]])
Pdata[["EXP_WT"]], Pdata[["RWT_LBS"]]
)

if (verbose){
if (verbose) {
cat("\nSampled pounds per trip:\n\n")
print(summary(Pdata$Trip_Sampled_Lbs))
}

if (plot != FALSE) {
numstate <- length(unique(Pdata$state))
if (is.character(plot)) grDevices::png(plot)
graphics::par(mgp = c(2.5, 0.5, 0), mfrow = c(numstate, 1), mar = rep(0, 4),
oma = c(4, 5, 3, 0.5))
graphics::par(
mgp = c(2.5, 0.5, 0), mfrow = c(numstate, 1), mar = rep(0, 4),
oma = c(4, 5, 3, 0.5)
)
for (st in unique(Pdata$state)) {
plotdata <- Pdata[Pdata[, "state"] == st & !is.na(Pdata[["Trip_Sampled_Lbs"]]), ]
if (all(is.na(plotdata$Trip_Sampled_Lbs))) next
graphics::boxplot(plotdata$Trip_Sampled_Lbs ~ plotdata$fishyr,
ylab = "", xlab = "", xaxt = "n",
at = unique(plotdata$fishyr), xlim = range(Pdata$fishyr))
at = unique(plotdata$fishyr), xlim = range(Pdata$fishyr)
)
graphics::legend("topleft", legend = st, bty = "n")
}
graphics::axis(1)
Expand All @@ -67,6 +70,4 @@ EF1_Numerator = function(Pdata,
}

return(Pdata)

} # End function EF1_Numerator

Loading

0 comments on commit bd06f5a

Please sign in to comment.