From bef3746861f41210c070ee1a66fe07dada420277 Mon Sep 17 00:00:00 2001 From: Nathan Vaughan Date: Mon, 10 Jun 2024 08:20:11 -0400 Subject: [PATCH] Intitial sampling Fixed bugs in initial sampling to ensure that bootstrapped values are used rather than exact OM data. Also added catch and discard sampling. --- R/initOM.R | 2 +- R/manipulate_EM.R | 24 +++++++++++++++++++++--- R/runSSMSE.R | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/R/initOM.R b/R/initOM.R index 5ec7b050..b0c0f239 100644 --- a/R/initOM.R +++ b/R/initOM.R @@ -471,7 +471,7 @@ run_OM <- function(OM_dir, if (is.null(seed)) { seed <- stats::runif(1, 1, 9999999) } - + start <- r4ss::SS_readstarter(file.path(OM_dir, "starter.ss"), verbose = FALSE ) diff --git a/R/manipulate_EM.R b/R/manipulate_EM.R index c2c035fb..8c54147c 100644 --- a/R/manipulate_EM.R +++ b/R/manipulate_EM.R @@ -66,6 +66,23 @@ get_EM_dat <- function(OM_dat, EM_dat, do_checks = TRUE) { check_OM_dat(OM_dat, EM_dat) } dat <- list(OM_dat = OM_dat, EM_dat = EM_dat) + + Catches <- lapply(dat, function(x) { + tmp <- combine_cols(x, "catch", c("year", "seas", "fleet")) + }) + # match 1 way: match each EM obs with an OM obs. extract only these OM obs. + matches <- which(Catches[[1]][, "combo"] %in% Catches[[2]][, "combo"]) + # extract only the rows of interest and get rid of the "combo" column + new_dat[["catch"]] <- Catches[[1]][matches, -ncol(Catches[[1]])] + + Discards <- lapply(dat, function(x) { + tmp <- combine_cols(x, "discard_data", c("Yr", "Seas", "Flt")) + }) + # match 1 way: match each EM obs with an OM obs. extract only these OM obs. + matches <- which(Discards[[1]][, "combo"] %in% Discards[[2]][, "combo"]) + # extract only the rows of interest and get rid of the "combo" column + new_dat[["discard_data"]] <- Discards[[1]][matches, -ncol(Discards[[1]])] + CPUEs <- lapply(dat, function(x) { tmp <- combine_cols(x, "CPUE", c("year", "seas", "index")) }) @@ -73,6 +90,7 @@ get_EM_dat <- function(OM_dat, EM_dat, do_checks = TRUE) { matches <- which(CPUEs[[1]][, "combo"] %in% CPUEs[[2]][, "combo"]) # extract only the rows of interest and get rid of the "combo" column new_dat[["CPUE"]] <- CPUEs[[1]][matches, -ncol(CPUEs[[1]])] + # add in lcomps if (OM_dat[["use_lencomp"]] == 1) { lcomps <- lapply(dat, function(x) { @@ -85,7 +103,7 @@ get_EM_dat <- function(OM_dat, EM_dat, do_checks = TRUE) { new_dat[["lencomp"]] <- lcomps[[1]][matches_l, -ncol(lcomps[[1]])] } # add in age comps - if (!is.null(dat[["agecomp"]])) { + if (!is.null(OM_dat[["agecomp"]])) { acomps <- lapply(dat, function(x) { tmp <- combine_cols( x, "agecomp", @@ -97,7 +115,7 @@ get_EM_dat <- function(OM_dat, EM_dat, do_checks = TRUE) { } # TODO: check this for other types of data, esp. mean size at age, k # and mean size. - if (!is.null(dat[["meanbodywt"]])) { + if (!is.null(OM_dat[["meanbodywt"]])) { meansize <- lapply(dat, function(x) { tmp <- combine_cols( x, "meanbodywt", @@ -107,7 +125,7 @@ get_EM_dat <- function(OM_dat, EM_dat, do_checks = TRUE) { matches_meansize <- which(meansize[[1]][, "combo"] %in% meansize[[2]][, "combo"]) new_dat[["meanbodywt"]] <- meansize[[1]][matches_meansize, -ncol(meansize[[1]])] } - if (!is.null(dat[["MeanSize_at_Age_obs"]])) { + if (!is.null(OM_dat[["MeanSize_at_Age_obs"]])) { size_at_age <- lapply(dat, function(x) { tmp <- combine_cols( x, "MeanSize_at_Age_obs", diff --git a/R/runSSMSE.R b/R/runSSMSE.R index 9f96f77e..21566495 100644 --- a/R/runSSMSE.R +++ b/R/runSSMSE.R @@ -611,6 +611,7 @@ run_SSMSE_iter <- function(out_dir = NULL, verbose = verbose, init_run = TRUE, seed = (iter_seed[["iter"]][1] + 12345) ) } + if (use_SS_boot == FALSE) { stop( "Currently, only sampling can be done using the bootstrapping ", @@ -648,6 +649,7 @@ run_SSMSE_iter <- function(out_dir = NULL, seed = (iter_seed[["iter"]][1] + 123456), sample_struct = sample_struct # add for bias ) + message( "Finished getting catch (years ", min(new_catch_list[["catch"]][, "year"]), " to ", max(new_catch_list[["catch"]][, "year"]),