R/utils-get-age-comp-limits.R

Defines functions get_age_comp_limits

Documented in get_age_comp_limits

#' Get the start and end year of the age comp data, and maximum
#' proportion overall with its year and age
#'
#' @param model A model as returnded by [load_ss_files()]
#' @param type 1 for Fishery and 2 for Survey
#'
#' @return A vector of 5 elements as described above
#' @export
get_age_comp_limits <- function(model, type = 1){

  dat <- model$dat$agecomp %>%
    set_names(tolower(names(.))) |>
    filter(fltsvy == type) |>
    select(yr, matches("^a\\d+$")) %>%
    setNames(gsub("a", "", names(.)))

  subdat <- dat |>
    select(-yr)
  max_row_col <- which(subdat == max(subdat), arr.ind = TRUE)
  max_prop_yr <- dat[max_row_col[1], ]$yr
  max_prop_age <- names(subdat)[max_row_col[2]]
  ret_vec <- c(as.integer(min(dat$yr)),
               as.integer(max(dat$yr)),
               max(subdat),
               max_prop_yr,
               max_prop_age)

  names(ret_vec) <- c("start_yr",
                      "end_yr",
                      "max_prop",
                      "max_prop_yr",
                      "max_prop_age")

  ret_vec
}
pacific-hake/hake-assessment documentation built on Nov. 8, 2024, 1:16 p.m.