Skip to content

Commit

Permalink
r4ss colname change: Gender to sex
Browse files Browse the repository at this point in the history
  • Loading branch information
k-doering-NOAA committed Aug 5, 2024
1 parent 2389804 commit 1dfb163
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions R/initOM.R
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ rm_sample_struct_hist <- function(sample_struct_hist, dat) {
compare_obj = sample_struct_hist,
name_in_obj = "lencomp",
colnames = c(
"Yr", "Seas", "FltSvy", "Gender",
"Yr", "Seas", "FltSvy", "sex",
"Part"
)
)
Expand All @@ -549,7 +549,7 @@ rm_sample_struct_hist <- function(sample_struct_hist, dat) {
compare_obj = sample_struct_hist,
name_in_obj = "agecomp",
colnames = c(
"Yr", "Seas", "FltSvy", "Gender",
"Yr", "Seas", "FltSvy", "sex",
"Part", "Ageerr", "Lbin_lo",
"Lbin_hi"
)
Expand All @@ -564,7 +564,7 @@ rm_sample_struct_hist <- function(sample_struct_hist, dat) {
return_obj = dat,
compare_obj = sample_struct_hist,
name_in_obj = "MeanSize_at_Age_obs",
colnames = c("Yr", "Seas", "FltSvy", "Gender", "Part", "AgeErr", "N_")
colnames = c("Yr", "Seas", "FltSvy", "sex", "Part", "AgeErr", "N_")
)
dat
}
Expand Down
12 changes: 6 additions & 6 deletions R/manipulate_EM.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ get_EM_dat <- function(OM_dat, EM_dat, do_checks = TRUE) {
lcomps <- lapply(dat, function(x) {
tmp <- combine_cols(
x, "lencomp",
c("Yr", "Seas", "FltSvy", "Gender", "Part")
c("Yr", "Seas", "FltSvy", "sex", "Part")
)
})
matches_l <- which(lcomps[[1]][, "combo"] %in% lcomps[[2]][, "combo"])
Expand All @@ -89,7 +89,7 @@ get_EM_dat <- function(OM_dat, EM_dat, do_checks = TRUE) {
acomps <- lapply(dat, function(x) {
tmp <- combine_cols(
x, "agecomp",
c("Yr", "Seas", "FltSvy", "Gender", "Part", "Lbin_lo", "Lbin_hi")
c("Yr", "Seas", "FltSvy", "sex", "Part", "Lbin_lo", "Lbin_hi")
)
})
matches_a <- which(acomps[[1]][, "combo"] %in% acomps[[2]][, "combo"])
Expand All @@ -111,7 +111,7 @@ get_EM_dat <- function(OM_dat, EM_dat, do_checks = TRUE) {
size_at_age <- lapply(dat, function(x) {
tmp <- combine_cols(
x, "MeanSize_at_Age_obs",
c("Yr", "Seas", "FltSvy", "Gender", "Part", "Ageerr")
c("Yr", "Seas", "FltSvy", "sex", "Part", "Ageerr")
)
})
matches_size_at_age <- which(size_at_age[[1]][, "combo"] %in% size_at_age[[2]][, "combo"])
Expand Down Expand Up @@ -231,14 +231,14 @@ add_new_dat <- function(OM_dat,
by_val <- switch(df_name,
"catch" = c("year", "seas", "fleet"),
"CPUE" = c("year", "seas", "index"),
"lencomp" = c("Yr", "Seas", "FltSvy", "Gender", "Part"),
"lencomp" = c("Yr", "Seas", "FltSvy", "sex", "Part"),
"agecomp" = c(
"Yr", "Seas", "FltSvy", "Gender", "Part", "Ageerr",
"Yr", "Seas", "FltSvy", "sex", "Part", "Ageerr",
"Lbin_lo", "Lbin_hi"
),
"meanbodywt" = c("Year", "Seas", "Fleet", "Partition", "Type"),
"MeanSize_at_Age_obs" = c(
"Yr", "Seas", "FltSvy", "Gender", "Part",
"Yr", "Seas", "FltSvy", "sex", "Part",
"AgeErr"
)
)
Expand Down
24 changes: 12 additions & 12 deletions R/red_noise_funcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ calc_comp_var <- function(data_obs, data_exp, bins, fleets = NULL, years = NULL,

# Check if select genders are specified if not select all genders in the input composition data frame
if (!is.null(genders)) {
genders <- genders[is.element(genders, unique(data_obs[["Gender"]]))]
genders <- genders[is.element(genders, unique(data_obs[["sex"]]))]
} else {
genders <- unique(data_obs[["Gender"]])
genders <- unique(data_obs[["sex"]])
}

# Check if select fleets are specified if not select all fleets in the input composition data frame
Expand All @@ -50,8 +50,8 @@ calc_comp_var <- function(data_obs, data_exp, bins, fleets = NULL, years = NULL,

# Subset the input observed and selected data to retain only the year, season, gender, and fleet
# elements specified above
data_obs <- data_obs[is.element(data_obs[["Yr"]], years) & is.element(data_obs[["FltSvy"]], fleets) & is.element(data_obs[["Seas"]], seasons) & is.element(data_obs[["Gender"]], genders) & data_obs[["Yr"]] > 0, ]
data_exp <- data_exp[is.element(data_exp[["Yr"]], years) & is.element(data_exp[["FltSvy"]], fleets) & is.element(data_exp[["Seas"]], seasons) & is.element(data_exp[["Gender"]], genders) & data_exp[["Yr"]] > 0, ]
data_obs <- data_obs[is.element(data_obs[["Yr"]], years) & is.element(data_obs[["FltSvy"]], fleets) & is.element(data_obs[["Seas"]], seasons) & is.element(data_obs[["sex"]], genders) & data_obs[["Yr"]] > 0, ]
data_exp <- data_exp[is.element(data_exp[["Yr"]], years) & is.element(data_exp[["FltSvy"]], fleets) & is.element(data_exp[["Seas"]], seasons) & is.element(data_exp[["sex"]], genders) & data_exp[["Yr"]] > 0, ]

# If merge fleets was set as true change all fleets to 0 so that data will be aggregated across them all
if (merge_fleets == TRUE) {
Expand All @@ -63,8 +63,8 @@ calc_comp_var <- function(data_obs, data_exp, bins, fleets = NULL, years = NULL,
# If merge genders was set as true change all genders to 0 so that data will be aggregated across them all
if (merge_genders == TRUE) {
genders <- 0
data_obs[["Gender"]] <- 0
data_exp[["Gender"]] <- 0
data_obs[["sex"]] <- 0
data_exp[["sex"]] <- 0
}

# If merge seasons was set as true change all seasons to 0 so that data will be aggregated across them all
Expand All @@ -90,8 +90,8 @@ calc_comp_var <- function(data_obs, data_exp, bins, fleets = NULL, years = NULL,
for (l in 1:length(seasons)) {
Comp_uncert[[2]][[i]][[j]][[l]] <- list()
# For each fleet, gender, and season combination subset the data for analysis
sub_dat_obs <- data_obs[data_obs[["FltSvy"]] == fleets[i] & data_obs[["Gender"]] == genders[j] & data_obs[["Seas"]] == seasons[l], ((length(data_obs[1, ]) - length(bins) + 1):length(data_obs[1, ]))]
sub_dat_exp <- data_exp[data_exp[["FltSvy"]] == fleets[i] & data_exp[["Gender"]] == genders[j] & data_obs[["Seas"]] == seasons[l], ((length(data_exp[1, ]) - length(bins) + 1):length(data_exp[1, ]))]
sub_dat_obs <- data_obs[data_obs[["FltSvy"]] == fleets[i] & data_obs[["sex"]] == genders[j] & data_obs[["Seas"]] == seasons[l], ((length(data_obs[1, ]) - length(bins) + 1):length(data_obs[1, ]))]
sub_dat_exp <- data_exp[data_exp[["FltSvy"]] == fleets[i] & data_exp[["sex"]] == genders[j] & data_obs[["Seas"]] == seasons[l], ((length(data_exp[1, ]) - length(bins) + 1):length(data_exp[1, ]))]

# Calculate the total observed and expected samples to allow scaling and unlisting of matrix data
total_obs <- apply(sub_dat_obs, 1, sum)
Expand Down Expand Up @@ -227,9 +227,9 @@ Sim_comp <- function(Comp_uncert, data_exp, bins, years = NULL, seasons = NULL,
}

if (!is.null(genders)) {
genders <- genders[is.element(genders, unique(data_exp[["Gender"]]))]
genders <- genders[is.element(genders, unique(data_exp[["sex"]]))]
} else {
genders <- unique(data_exp[["Gender"]])
genders <- unique(data_exp[["sex"]])
}

if (!is.null(fleets)) {
Expand All @@ -240,7 +240,7 @@ Sim_comp <- function(Comp_uncert, data_exp, bins, years = NULL, seasons = NULL,

# Subset the expected data to only include the selected fleet, year, season, gender range and the replicate this as the
# observed data object to be filled with new random observations
data_exp <- data_exp[is.element(data_exp[["Yr"]], years) & is.element(data_exp[["FltSvy"]], fleets) & is.element(data_exp[["Seas"]], seasons) & is.element(data_exp[["Gender"]], genders) & data_exp[["Yr"]] > 0, ]
data_exp <- data_exp[is.element(data_exp[["Yr"]], years) & is.element(data_exp[["FltSvy"]], fleets) & is.element(data_exp[["Seas"]], seasons) & is.element(data_exp[["sex"]], genders) & data_exp[["Yr"]] > 0, ]
data_obs <- data_exp

# offset assumes that the observed composition bins are represented buy the final columns of the data frame
Expand All @@ -252,7 +252,7 @@ Sim_comp <- function(Comp_uncert, data_exp, bins, years = NULL, seasons = NULL,
# For each of fleet, season, and gender select the corresponding reference link to the uncertainty list object
fleet_ref <- which(Comp_uncert[[1]][[1]] == data_obs[["FltSvy"]][i])
season_ref <- which(Comp_uncert[[1]][[2]] == data_obs[["Seas"]][i])
gender_ref <- which(Comp_uncert[[1]][[3]] == data_obs[["Gender"]][i])
gender_ref <- which(Comp_uncert[[1]][[3]] == data_obs[["sex"]][i])
# Extract the variance and bias information for the referenced fleet, gender, season
sub_var <- Comp_uncert[[2]][[fleet_ref]][[gender_ref]][[season_ref]][[2]]
sub_bias <- Comp_uncert[[2]][[fleet_ref]][[gender_ref]][[season_ref]][[3]]
Expand Down
6 changes: 3 additions & 3 deletions R/sample_struct.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ convert_to_r4ss_names <- function(sample_struct,
# currently SSMSE only allows repeating the same sample sizes.
"year", "month", "fleet", "sex", "part", "ageerr", "N_"
# Tags releases
# "Area", "Yr", "Season", "Gender", "Age", "Nrelease",
# "Area", "Yr", "Season", "sex", "Age", "Nrelease",
# Morph comp
),
sample_struct_name = c(
Expand Down Expand Up @@ -191,7 +191,7 @@ create_sample_struct <- function(dat, nyrs, rm_NAs = FALSE) {
}
if (name %in% c("lencomp", "agecomp", "MeanSize_at_Age_obs")) {
# Sex
sex_col <- grep("Sex|Gender", colnames(df),
sex_col <- grep("Sex|sex", colnames(df),
ignore.case = TRUE,
value = TRUE
)
Expand Down Expand Up @@ -417,7 +417,7 @@ get_full_sample_struct <- function(sample_struct,
)
x[["Sex"]] <- NA
for (i in unique(x[["FltSvy"]])) {
tmp_sx <- unique(tmp_dat[tmp_dat[[flt_colname]] == i, "Gender"])
tmp_sx <- unique(tmp_dat[tmp_dat[[flt_colname]] == i, "sex"])
if (length(tmp_sx) == 1) {
x[x[["FltSvy"]] == i, "Sex"] <- tmp_sx
} else {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-extendOM.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ extend_vals <- list(
),
lencomp = data.frame(
Yr = 101:103, Seas = 1, FltSvy = 1,
Gender = 0, Part = 0,
sex = 0, Part = 0,
Nsamp = c(25, 50, 100)
),
agecomp = data.frame(
Yr = 101:104, Seas = 1, FltSvy = 2,
Gender = 0, Part = 0, Ageerr = 1,
sex = 0, Part = 0, Ageerr = 1,
Lbin_lo = -1, Lbin_hi = -1,
Nsamp = c(25, 50, 100, 150)
)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-initOM.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ test_that("create_OM can modify model", {
new_dat[["CPUE"]][, c("year", "seas", "index")],
unique(new_dat[["CPUE"]][, c("year", "seas", "index")])
)
cols_lencomp <- c("Yr", "Seas", "FltSvy", "Gender", "Part")
cols_lencomp <- c("Yr", "Seas", "FltSvy", "sex", "Part")
expect_equal(
new_dat[["lencomp"]][, cols_lencomp],
unique(new_dat[["lencomp"]][, cols_lencomp])
)
cols_agecomp <- c(
"Yr", "Seas", "FltSvy", "Gender", "Part", "Ageerr", "Lbin_lo",
"Yr", "Seas", "FltSvy", "sex", "Part", "Ageerr", "Lbin_lo",
"Lbin_hi"
)
expect_equal(
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test-sample_struct.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ test_that("assumptions about r4ss colnames are true.", {
CPUE = data.frame(year = c(102, 105), seas = 7, index = 2, se_log = 0.2),
lencomp = data.frame(
Yr = c(102, 105), Seas = 1, FltSvy = 1,
Gender = 0, Part = 0, Nsamp = 125
sex = 0, Part = 0, Nsamp = 125
),
agecomp = data.frame(
Yr = c(102, 105), Seas = 1, FltSvy = 2,
Gender = 0, Part = 0, Ageerr = 1,
sex = 0, Part = 0, Ageerr = 1,
Lbin_lo = -1, Lbin_hi = -1, Nsamp = 500
),
meanbodywt = data.frame(
Expand All @@ -44,7 +44,7 @@ test_that("assumptions about r4ss colnames are true.", {
Yr = c(1971, 1995),
Seas = 7,
FltSvy = c(1, 1, 2, 2),
Gender = 3,
sex = 3,
Part = 0,
AgeErr = 1
)
Expand Down Expand Up @@ -73,11 +73,11 @@ test_that("convert_to_r4ss_names works", {
expect_equal(names(r4ss_sample_struct[["CPUE"]]), c("year", "seas", "index", "se_log"))
expect_equal(names(r4ss_sample_struct[["lencomp"]]), c(
"Yr", "Seas", "FltSvy",
"Gender", "Part", "Nsamp"
"sex", "Part", "Nsamp"
))
expect_equal(names(r4ss_sample_struct[["agecomp"]]), c(
"Yr", "Seas", "FltSvy",
"Gender", "Part", "Ageerr",
"sex", "Part", "Ageerr",
"Lbin_lo", "Lbin_hi", "Nsamp"
))
})
Expand Down

0 comments on commit 1dfb163

Please sign in to comment.