Skip to content

Commit

Permalink
fix: adapt to new r4ss column names
Browse files Browse the repository at this point in the history
Error was caused by change r4ss/r4ss#512 which
changed FltSvy to fleet and Yr to year. This change affected petrale,
opakapaka, and sardine.
  • Loading branch information
iantaylor-NOAA authored and kellijohnson-NOAA committed Oct 18, 2024
1 parent a573df3 commit 27ebeb3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
4 changes: 2 additions & 2 deletions content/NWFSC-petrale.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ source("R/get_ss3_data.R")
# by assigning to fleet -1, these get filtered by get_ss3_data()
# only include age comps with fleet = -4 or 1:
ss3dat$agecomp <- ss3dat$agecomp |> dplyr::filter(FltSvy %in% c(-4, 1))
ss3dat$agecomp$FltSvy <- abs(ss3dat$agecomp$FltSvy)
ss3dat$agecomp <- ss3dat$agecomp |> dplyr::filter(fleet %in% c(-4, 1))
ss3dat$agecomp$fleet <- abs(ss3dat$agecomp$fleet)
# convert SS3 data into FIMS format using function defined in the R directory
mydat <- get_ss3_data(ss3dat, fleets = c(1,4), ages = ages)
Expand Down
7 changes: 6 additions & 1 deletion content/PIFS-opakapaka.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ load(file.path(getwd(), "data_files", "opaka_model.RDS"))
## Should now have ss3dat, ss3ctl, and ss3rep in environment
## RDS file can also be found at
#githubURL <- "https://github.com/MOshima-PIFSC/Opaka-FIMS-Case-Study/blob/main/Model/FIMS-em/1/em/opaka_model.RDS"
# replace ss3dat and ss3ctl loaded from RDS file with new versions
# using current r4ss to read ss_new files from github
opaka_input <- r4ss::SS_read("https://raw.githubusercontent.com/MOshima-PIFSC/Opaka-FIMS-Case-Study/refs/heads/main/Model/FIMS-em/1/em/", ss_new = TRUE)
ss3dat <- opaka_input$dat
ss3ctl <- opaka_input$ctl
## Function written by Ian Taylor to get SS3 data into FIMSFrame format
## Function written by Ian Taylor and Meg Oshima to get SS3 data into FIMSFrame format
source("./R/get_ss3_data.R")
# Define the dimensions ----
Expand Down
24 changes: 12 additions & 12 deletions content/R/get_ss3_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,39 +102,39 @@ get_ss3_data <- function(dat, fleets, ages) {
# further processing
age_info <-
dat$agecomp |>
dplyr::filter(FltSvy %in% fleets) |> # filter by requested fleets
dplyr::mutate(FltSvy = abs(FltSvy)) |> # convert any negative fleet to positive
dplyr::select(!dplyr::starts_with("m", ignore.case = FALSE)) |> # exclude male comps
dplyr::filter(fleet %in% fleets) |> # filter by requested fleets
dplyr::mutate(fleet = abs(fleet)) |> # convert any negative fleet to positive
dplyr::select(!dplyr::matches("^m[0-9]")) |> # exclude male comps
tidyr::pivot_longer( # convert columns f1...f17 to values in a new "age" colum of a longer table
cols = dplyr::starts_with(c("f", "a"), ignore.case = FALSE),
cols = dplyr::matches("^f[0-9]") | dplyr::matches("^a[0-9]"), # 2-sex model uses f1, f2, ...; 1-sex model uses a1, a2, ...
names_to = "age",
values_to = "value"
) |>
dplyr::mutate(age = as.numeric(substring(age, first = 2))) |> # convert "f17" to 17
dplyr::select(Yr, FltSvy, Nsamp, age, value)
dplyr::select(year, fleet, Nsamp, age, value)

# add -999 for missing years
# create empty data frame for all combinations of year, fleet, and age
age_info_empty <- tidyr::expand_grid(
Yr = years,
FltSvy = fleets,
year = years,
fleet = fleets,
age = ages
) |> dplyr::mutate(Nsamp = 1, value = -999 - 0.001)
# combine the two data frames and remove redundant rows
# NOTE: this removes some age comp data because there
# were years with multiple observations from the same fleet
# due to multiple ageing error matrices
age_info <- rbind(age_info, age_info_empty) |>
dplyr::distinct(Yr, FltSvy, age, .keep_all = TRUE) |>
dplyr::arrange(FltSvy, Yr, age)
dplyr::distinct(year, fleet, age, .keep_all = TRUE) |>
dplyr::arrange(fleet, year, age)

# finish converting age comps to FIMSFrame format
agecomps <- data.frame(
type = "age",
name = paste0("fleet", abs(age_info$FltSvy)), # abs to include fleet == -4
name = paste0("fleet", abs(age_info$fleet)), # abs to include fleet == -4
age = age_info$age,
datestart = paste0(age_info$Yr, "-01-01"),
dateend = paste0(age_info$Yr, "-12-31"),
datestart = paste0(age_info$year, "-01-01"),
dateend = paste0(age_info$year, "-12-31"),
value = age_info$value + 0.001, # add constant to avoid 0 values
unit = "",
# Q: should uncertainty here be the total sample size across bins, or the samples within the bin?
Expand Down
4 changes: 2 additions & 2 deletions content/SWFSC-sardine.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,15 @@ ewaa_growth <- methods::new(EWAAgrowth)
ewaa_growth$ages <- ages
# NOTE: getting weight-at-age vector from
# petrale_output$wtatage |>
# dplyr::filter(Sex == 1 & Fleet == -1 & Yr == 1876) |>
# dplyr::filter(sex == 1 & fleet == -1 & year == 1876) |>
# dplyr::select(paste(0:40)) |>
# round(4)
# ewaa_growth$weights <- c(0.019490,0.077760,0.108865,
# 0.133855,0.154360,0.174905,0.184200,
# 0.196460,0.214155)
ewaa_growth$weights <- wtatage %>% filter(Fleet == 1, Yr == 2010) %>% select(as.character(0:10)) %>% t %>%
ewaa_growth$weights <- wtatage %>% filter(fleet == 1, year == 2010) %>% select(as.character(0:10)) %>% t %>%
as.vector
# maturity
Expand Down

0 comments on commit 27ebeb3

Please sign in to comment.