Skip to content

Commit

Permalink
Adds fits to length on R side
Browse files Browse the repository at this point in the history
Still need to check for when lengths are present and deal
with those errors
  • Loading branch information
kellijohnson-NOAA committed Dec 3, 2024
1 parent 4851eeb commit 2b7567a
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 3 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export(EWAAgrowth)
export(FIMSFrame)
export(Fleet)
export(Index)
export(LengthComp)
export(LogisticMaturity)
export(LogisticSelectivity)
export(Parameter)
Expand Down Expand Up @@ -38,9 +39,11 @@ export(log_error)
export(log_info)
export(log_warning)
export(lognormal)
export(m_age_to_length_conversion)
export(m_agecomp)
export(m_index)
export(m_landings)
export(m_lengthcomp)
export(m_weight_at_age)
export(multinomial)
export(run_gtest)
Expand All @@ -54,6 +57,7 @@ exportMethods(Summary)
exportMethods(m_agecomp)
exportMethods(m_index)
exportMethods(m_landings)
exportMethods(m_lengthcomp)
import(methods)
import(stats)
importFrom(Rcpp,sourceCpp)
Expand Down
3 changes: 2 additions & 1 deletion R/distribution_formulas.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ get_expected_name <- function(family, data_type) {
data_type == "index" &&
grepl("lognormal|gaussian", family_string) &&
link_string == "identity" ~ "expected_index",
grepl("comp", data_type) ~ "proportion_catch_numbers_at_age"
grepl("agecomp", data_type) ~ "proportion_catch_numbers_at_age",
grepl("lengthcomp", data_type) ~ "proportion_catch_numbers_at_length",
)
# Check combination of entries was okay and led to valid name
if (is.na(expected_name)) {
Expand Down
100 changes: 99 additions & 1 deletion R/fimsframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ setClass(
n_years = "integer",
ages = "numeric",
n_ages = "integer",
lengths = "numeric",
n_lengths = "integer",
weight_at_age = "data.frame",
start_year = "integer",
end_year = "integer"
Expand Down Expand Up @@ -96,6 +98,22 @@ setMethod(
function(x) FIMSFrame(x)@n_ages
)

setGeneric("get_lengths", function(x) standardGeneric("get_lengths"))
setMethod("get_lengths", "FIMSFrame", function(x) x@lengths)
setMethod(
"get_lengths",
"data.frame",
function(x) FIMSFrame(x)@lengths
)

setGeneric("n_lengths", function(x) standardGeneric("n_lengths"))
setMethod("n_lengths", "FIMSFrame", function(x) x@n_lengths)
setMethod(
"n_lengths",
"data.frame",
function(x) FIMSFrame(x)@n_lengths
)

setGeneric("weight_at_age", function(x) standardGeneric("weight_at_age"))
setMethod("weight_at_age", "FIMSFrame", function(x) x@weight_at_age)
setMethod(
Expand Down Expand Up @@ -203,6 +221,72 @@ setMethod(
}
)

#' Get the length-composition data to be used in the model
#'
#' @param x The object containing the length-composition data.
#' @param fleet_name The name of the fleet for the length-composition data.
#' @export
setGeneric(
"m_lengthcomp",
function(x, fleet_name) standardGeneric("m_lengthcomp")
)
# Should we add name as an argument here?

#' Get the length-composition data data to be used in the model
#'
#' @param x The FIMSFrame containing length-composition data.
#' @param fleet_name The name of the fleet for the length-composition data.
#' @export
setMethod(
"m_lengthcomp",
"FIMSFrame",
function(x, fleet_name) {
dplyr::filter(
.data = x@data,
.data[["type"]] == "length",
.data[["name"]] == fleet_name
) |>
dplyr::pull(.data[["value"]])
}
)

#' Get the age-to-length-conversion data to be used in the model
#'
#' @param x The object containing the age-to-length-conversion data (i.e.,
#' proportion of age "a" that are length "l").
#' @param fleet_name A string specifying the name of the fleet that you want
#' data for.
#' @export
setGeneric(
"m_age_to_length_conversion",
function(x, fleet_name) standardGeneric("m_age_to_length_conversion")
)

#' Get the age-to-length-conversion matrix to be used in the model
#'
#' @param x The object containing the age-to-length-conversion data (i.e.,
#' proportion of age "a" that are length "l").
#' @param fleet_name A string specifying the name of the fleet that you want
#' data for.
setMethod(
"m_age_to_length_conversion",
"FIMSFrame",
function(x, fleet_name) {
if ("length" %in% colnames(x@data)) {
dplyr::filter(
.data = as.data.frame(x@data),
.data[["type"]] == "age-to-length-conversion",
.data[["name"]] == fleet_name
) |>
dplyr::group_by(.data[["age"]], .data[["length"]]) |>
dplyr::summarize(
mean_value = mean(as.numeric(.data[["value"]]), na.rm = TRUE)
) |>
dplyr::pull(as.numeric(.data[["mean_value"]]))
}
}
)

# Note: don't include setters, because for right now, we don't want users to be
# setting ages, fleets, etc. However, we could allow it in the future, if there
# is away to update the object based on changing the fleets?
Expand Down Expand Up @@ -394,8 +478,20 @@ FIMSFrame <- function(data) {
# Make empty NA data frames in the format needed to pass to FIMS
# Get the range of ages displayed in the data to use to specify population
# simulation range
ages <- min(data[["age"]], na.rm = TRUE):max(data[["age"]], na.rm = TRUE)
if ("age" %in% colnames(data)) {
ages <- min(data[["age"]], na.rm = TRUE):max(data[["age"]], na.rm = TRUE)
} else {
ages <- numeric()
}
n_ages <- length(ages)
if ("length" %in% colnames(data)) {
lengths <- min(data[["length"]], na.rm = TRUE):
max(data[["length"]], na.rm = TRUE)
} else {
lengths <- numeric()
}
n_lengths <- length(lengths)

weight_at_age <- dplyr::filter(
data,
.data[["type"]] == "weight-at-age"
Expand All @@ -410,6 +506,8 @@ FIMSFrame <- function(data) {
end_year = end_year,
ages = ages,
n_ages = n_ages,
lengths = lengths,
n_lengths = n_lengths,
weight_at_age = weight_at_age
)
return(out)
Expand Down
13 changes: 12 additions & 1 deletion R/initialize_modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ initialize_module <- function(parameters, data, module_name) {
"log_Fmort"
))
}

if (!"age-to-length-conversion" %in% fleet_types) {
module_fields <- setdiff(module_fields, c(
"age_length_conversion_matrix",
# Right now we can also remove nlengths because the default is 0
"nlengths",
"proportion_catch_numbers_at_length"
))
}
}


Expand All @@ -110,7 +119,8 @@ initialize_module <- function(parameters, data, module_name) {
# index and agecomp distributions. No input values are required.

non_standard_field <- c(
"ages", "nages", "proportion_female", "estimate_prop_female",
"ages", "nages", "nlengths",
"proportion_female", "estimate_prop_female",
"nyears", "nseasons", "nfleets", "estimate_log_devs", "weights",
"is_survey", "estimate_q", "random_q"
)
Expand All @@ -121,6 +131,7 @@ initialize_module <- function(parameters, data, module_name) {
field,
"ages" = ages(data),
"nages" = n_ages(data),
"nlengths" = n_lengths(data),
"proportion_female" = numeric(0),
"estimate_prop_female" = TRUE,
"nyears" = n_years(data),
Expand Down
18 changes: 18 additions & 0 deletions man/m_age_to_length_conversion-FIMSFrame-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/m_age_to_length_conversion.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/m_lengthcomp-FIMSFrame-method.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/m_lengthcomp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2b7567a

Please sign in to comment.