Skip to content

Commit

Permalink
Fix tab_brood. NOAA GOA_ESA data request filled.
Browse files Browse the repository at this point in the history
Using this function for a data request an noticed some errors. Fixed those but still need to work on this repository. Use cautiously.
  • Loading branch information
adamreimer committed Sep 9, 2024
1 parent a59adc1 commit 975d40b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
28 changes: 28 additions & 0 deletions data_requests/GOA_ESA_broodtable.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generate a brood table for a NOAA data request associated with the GOA ESA petition.

# Author: Adam Reimer
# Version: 2024-09-09

# Packages
packs <- c("tidyverse", "coda", "writexl")
lapply(packs, require, character.only = TRUE)

# Source functions
function_files <- list.files(path=".\\functions")
lapply(function_files, function(x) source(paste0(".\\functions\\", x)))

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# Read data ---------------------------------------------------------------
post <- readRDS(".\\output\\post_1977on")

# * Format data ----------------------------------------------------------

# Analysis ----------------------------------------------------------------

# Results -----------------------------------------------------------------
tbl_brood <- table_brood(stats_dat = get_summary(post$samples))
knitr::kable(tbl_brood, escape = FALSE, align = "r")
write_xlsx(list(
"Anchor" = tbl_brood),
".\\data_requests\\GOA_ESA_brood.xlsx")
8 changes: 4 additions & 4 deletions functions/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ table_age <- function(post_dat, node, firstyr = 1977){
#' @return A tibble
#'
#' @examples
#' table_brood(get_summary(post), 1)
#' table_brood(get_summary(post))
#'
#' @export
table_brood <- function(stats_dat,firstyr = 1977){
Expand All @@ -84,8 +84,8 @@ table_brood <- function(stats_dat,firstyr = 1977){
tibble::rownames_to_column() %>%
dplyr::filter(grepl(paste0("^N.ta\\[\\d+,\\d\\]"), rowname)) %>%
dplyr::select(rowname = "rowname", mean = "Mean") %>%
dplyr::mutate(age_n = 2 + as.numeric(gsub("N.ta\\[\\d+,(\\d)\\]", "\\1", rowname)),
year = firstyr + as.numeric(gsub("N.ta\\[(\\d+),\\d\\]", "\\1", rowname)) - age_n,
dplyr::mutate(age_n = 3 + as.numeric(gsub("N.ta\\[\\d+,(\\d)\\]", "\\1", rowname)),
year = (firstyr - 1) + as.numeric(gsub("N.ta\\[(\\d+),\\d\\]", "\\1", rowname)) - age_n,
age_c = paste0("age-", age_n)) %>%
dplyr::select(year, age_c, mean) %>%
#tidyr::spread(age_c, mean)
Expand All @@ -97,7 +97,7 @@ table_brood <- function(stats_dat,firstyr = 1977){
dplyr::select(rowname = "rowname", mean = "Mean") %>%
dplyr::mutate(name = stringr::str_sub(rowname, 1, stringr::str_locate(rowname, "\\[")[, 1] - 1),
index = as.numeric(gsub(".\\[(\\d+)\\]", "\\1", rowname)),
year = (name == "S") * (firstyr + index) + (name == "R") * (firstyr - 7 + index)) %>%
year = (name == "S") * ((firstyr - 1) + index) + (name == "R") * ((firstyr - 1) - 7 + index)) %>%
dplyr::select(year, mean, name) %>%
tidyr::spread(name, mean) %>%
dplyr::select(year, S, R) %>%
Expand Down

0 comments on commit 975d40b

Please sign in to comment.