From 5ebf47f5002dc24c339dafe58194ecb875a567ab Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Thu, 12 Nov 2026 17:49:33 -0500 Subject: [PATCH 01/20] feat: read LIKELIHOOD table from Report.sso into stockplotr data object currently only works for r4ss formatted inputs --- R/conout_ss3.R | 57 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/R/conout_ss3.R b/R/conout_ss3.R index fb3f528b..b3300545 100644 --- a/R/conout_ss3.R +++ b/R/conout_ss3.R @@ -192,7 +192,7 @@ conout_ss3 <- function( # ntentionally empty std2 <- c() cha <- c() - info <- c() + info <- c("likelihoods_used", "likelihoods_laplace", "likelihoods_by_fleet") } # Loop for all identified parameters to extract for plotting and use @@ -994,24 +994,43 @@ conout_ss3 <- function( # next #### info #### } else if (parm_sel %in% info) { - # if (parm_sel == "LIKELIHOOD") { - # df1 <- extract[-1, ] - # # make row the header names for first df - # colnames(df1) <- df1[1, ] - # # Remove first row containing column names - # df2 <- df1[-1, ] |> - # dplyr::rename(label = Component) |> - # tidyr::pivot_longer( - # cols = -label, - # names_to = "type", - # values_to = "likelihood" - # ) - # # match to out_new - # df2[setdiff(tolower(names(out_new)), tolower(names(df2)))] <- NA - # # Add to out list - # out_list[[parm_sel]] <- df2 - # } else - if (parm_sel == "DEFINITIONS") { + if (parm_sel %in% c("likelihoods_used", "likelihoods_laplace")) { + df1 <- extract#[-1, ] + # make row the header names for first df + # colnames(df1) <- df1[1, ] + # Remove first row containing column names + df2 <- df1 |> + tibble::rownames_to_column("Component") |> + dplyr::rename(label = Component) |> + tidyr::pivot_longer( + cols = -label, + names_to = "type", + values_to = "likelihood" + ) |> + dplyr::mutate(module_name="likelihoods") + # match to out_new + df2[setdiff(tolower(names(out_new)), tolower(names(df2)))] <- NA + + out_list[[parm_sel]] <- df2 + } else if(parm_sel == "likelihoods_by_fleet"){ + df1 <- extract + df2 <- df1 |> + # dplyr::filter(!grepl("use|skip", Label)) |> + dplyr::rename(label=Label) |> + dplyr::select(-ALL) |> + tidyr::pivot_longer( + cols = -label, + names_to = "fleet", + values_to = "likelihood" + ) |> + tidyr::separate(col=label, into=c("label", "type"), sep="_(?=lambda|like|N)", remove=FALSE, extra="merge") |> + dplyr::mutate(module_name="likelihoods") + + df2[setdiff(tolower(names(out_new)), tolower(names(df2)))] <- NA + + out_list[[parm_sel]] <- df2 + } #else + else if (parm_sel == "DEFINITIONS") { # Extract start year and end year for iding era start_year <- as.numeric(extract[which(apply(extract, 1, function(row) any(row == "Start_year:"))), 2]) end_year <- as.numeric(extract[which(apply(extract, 1, function(row) any(row == "End_year:"))), 2]) From 3528732df25283106220268d24d502d1657904e5 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 10:38:42 -0500 Subject: [PATCH 02/20] feat: plot_likelihoods Create bar chart of likelihood components --- R/plot_likelihoods.R | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 R/plot_likelihoods.R diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R new file mode 100644 index 00000000..b30c324c --- /dev/null +++ b/R/plot_likelihoods.R @@ -0,0 +1,89 @@ + +plot_likelihoods <- function( + dat, + by_fleet=FALSE, + summarise_fleets=FALSE, + facet = NULL, + group = NULL, + make_rda = FALSE, + figures_dir = getwd(), + interactive = TRUE, + ... +){ + check_data(dat) + + likelihood_type <- "values" + if(by_fleet){ + cli::cli_alert_info("Plotting likelihood components by fleet.") + likelihood_type <- "like" + facet <- "fleet" + } + + prepared_data <- filter_data( + dat = dat, + label_name = "", + geom=geom, + era=NULL, + group=group, + facet=facet, + module="likelihoods", + scale_amount=1, + interactive=TRUE + ) + + if(!by_fleet){ + prepared_data <- prepared_data |> + dplyr::filter(is.na(fleet)) + }else{ + prepared_data <- prepared_data |> + dplyr::filter(!is.na(fleet)) + } + + processing <- process_data( + dat = prepared_data |> dplyr::filter(type==likelihood_type), + group = group, + facet = facet + ) + + plot_data <- processing[[1]] |> dplyr::filter(!is.na(estimate), estimate!=0) + + if(by_fleet && summarise_fleets){ + plot_data <- plot_data |> + dplyr::group_by(model, group_var, label) |> + dplyr::summarise( + fleet="All", + estimate = sum(estimate) + ) |> dplyr::bind_rows(plot_data) + } + + group <- processing[[2]] + # if (!is.null(processing[[3]])) facet <- processing[[3]] + + plt <- ggplot2::ggplot(plot_data)+ + ggplot2::geom_col( + ggplot2::aes(y=label, x=estimate, fill=label) + )+ + ggplot2::geom_vline(xintercept=0, linetype="dashed")+ + ggplot2::scale_x_continuous( + limits= ~ c(-max(abs(.x)), max(abs(.x))) + )+ + ggplot2::guides(fill="none")+ + ggplot2::labs(y="Likelihood Component", x="Likelihood Contribution")+ + theme_noaa() + + # Check if facet(s) are desired + if (!is.null(facet) & length(facet) > 0) { + facet <- paste("~", paste(facet, collapse = " + ")) + facet_formula <- stats::reformulate(facet) + + plt <- plt + ggplot2::facet_wrap(facet_formula, labeller = function(labels) { + # Clean column names (e.g., "growth_pattern" -> "Growth Pattern") + names(labels) <- tools::toTitleCase(gsub("_", " ", names(labels))) + + # Combine into "Variable: Value" format + lapply(names(labels), function(var) paste0(var, ": ", labels[[var]])) + }) + } + + return(plt) +} \ No newline at end of file From 71475739ee1369df4deb1c6cdaa7c6d7d695e6da Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 10:39:57 -0500 Subject: [PATCH 03/20] refactor: map likelihood values to "estimate" rather than "likelihood" column This is inline with how other models are converted --- R/conout_ss3.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/conout_ss3.R b/R/conout_ss3.R index b3300545..82c59cf9 100644 --- a/R/conout_ss3.R +++ b/R/conout_ss3.R @@ -1005,7 +1005,7 @@ conout_ss3 <- function( tidyr::pivot_longer( cols = -label, names_to = "type", - values_to = "likelihood" + values_to = "estimate" ) |> dplyr::mutate(module_name="likelihoods") # match to out_new @@ -1021,7 +1021,7 @@ conout_ss3 <- function( tidyr::pivot_longer( cols = -label, names_to = "fleet", - values_to = "likelihood" + values_to = "estimate" ) |> tidyr::separate(col=label, into=c("label", "type"), sep="_(?=lambda|like|N)", remove=FALSE, extra="merge") |> dplyr::mutate(module_name="likelihoods") From 5bac73f8893d45487610f583c621adfb495956aa Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 11:42:55 -0500 Subject: [PATCH 04/20] docs: :memo: roxygen for plot_likelihoods --- R/plot_likelihoods.R | 38 ++++++++++++++++++++- man/plot_likelihoods.Rd | 75 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 man/plot_likelihoods.Rd diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index b30c324c..c7ca3977 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -1,4 +1,40 @@ - +#' Plot likelihood components +#' +#' Plot likelihood components as a horizontal bar chart. +#' +#' @inheritParams plot_spawning_biomass +#' +#' @param by_fleet Logical, whether to plot fleet specific likelihood +#' components or not +#' +#' Default: FALSE +#' +#' @param summarise_fleets Logical, whether to aggregate fleet specificic +#' likelihood components into an additional "total" likelihood wheb plotting +#' fleet specific likelihoods. Only applied when `by_fleet=TRUE`. +#' +#' Default:FALSE +#' +#' @return A plot showing the value of likelihood components, optionally +#' across fleets. +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' plot_likelhoods( +#' dat = stockplotr::example_data, +#' ) +#' plot_likelhoods( +#' dat = stockplotr::example_data, +#' by_fleet = TRUE +#' ) +#' plot_likelhoods( +#' dat = stockplotr::example_data, +#' by_fleet = TRUE, +#' summarise_fleets = TRUE +#' ) +#' } plot_likelihoods <- function( dat, by_fleet=FALSE, diff --git a/man/plot_likelihoods.Rd b/man/plot_likelihoods.Rd new file mode 100644 index 00000000..cfd31898 --- /dev/null +++ b/man/plot_likelihoods.Rd @@ -0,0 +1,75 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_likelihoods.R +\name{plot_likelihoods} +\alias{plot_likelihoods} +\title{Plot likelihood components} +\usage{ +plot_likelihoods( + dat, + by_fleet = FALSE, + summarise_fleets = FALSE, + make_rda = FALSE, + figures_dir = getwd(), + interactive = TRUE, + ... +) +} +\arguments{ +\item{dat}{Data frame or list. A tibble or named list of tibbles (input as \code{list()}) +returned from \link[stockplotr]{convert_output}. + +If inputting a list of tibbles, the first tibble's reference point defined +in \code{ref_line} is used to plot a reference line or calculate relative spawning biomass.} + +\item{by_fleet}{Logical, whether to plot fleet specific likelihood +components or not + +Default: FALSE} + +\item{summarise_fleets}{Logical, whether to aggregate fleet specificic +likelihood components into an additional "total" likelihood wheb plotting +fleet specific likelihoods. Only applied when \code{by_fleet=TRUE}. + +Default:FALSE} + +\item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and +make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, +the rda will be exported to the folder indicated in the argument "figures_dir". + +Default: \code{FALSE}.} + +\item{figures_dir}{Path. Path to the "figures" folder. + +Default: \code{getwd()} + +The folder is created within the path if it does not exist.} + +\item{interactive}{Logical. TRUE/FALSE; indicate whether the environment is interactive. + +Default: \code{FALSE}} + +\item{...}{Arguments called from \link[ggplot2]{geom_line} or \link[ggplot2]{geom_point}} +} +\value{ +A plot showing the value of likelihood components, optionally +across fleets. +} +\description{ +Plot likelihood components as a horizontal bar chart. +} +\examples{ +\dontrun{ +plot_likelhoods( + dat = stockplotr::example_data, +) +plot_likelhoods( + dat = stockplotr::example_data, + by_fleet = TRUE +) +plot_likelhoods( + dat = stockplotr::example_data, + by_fleet = TRUE, + summarise_fleets = TRUE +) +} +} From 8f35880db0bb05a869c4be9884bd0a3a5f7abdf7 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 11:44:26 -0500 Subject: [PATCH 05/20] build: add plot_likelihoods to NAMESPACE --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index c7bad17d..bc973be2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,7 @@ export(plot_fishing_mortality) export(plot_index) export(plot_joint_residuals) export(plot_landings) +export(plot_likelihoods) export(plot_natural_mortality) export(plot_obsvpred) export(plot_recruitment) From 57f8d2a9614c1fbe697f2733de6ab1a87e54c3df Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 11:45:02 -0500 Subject: [PATCH 06/20] refactor:: remove extra parameters from plot_likelihoods --- R/plot_likelihoods.R | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index c7ca3977..67d7309d 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -39,8 +39,6 @@ plot_likelihoods <- function( dat, by_fleet=FALSE, summarise_fleets=FALSE, - facet = NULL, - group = NULL, make_rda = FALSE, figures_dir = getwd(), interactive = TRUE, @@ -87,7 +85,7 @@ plot_likelihoods <- function( plot_data <- plot_data |> dplyr::group_by(model, group_var, label) |> dplyr::summarise( - fleet="All", + fleet="Total", estimate = sum(estimate) ) |> dplyr::bind_rows(plot_data) } From 4c554cf75dc8eba6269a091e0d0c2cf26296ea78 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Wed, 18 Nov 2026 11:22:34 -0500 Subject: [PATCH 07/20] refactor: add back in necessary function pars for filter_data --- R/plot_likelihoods.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index 67d7309d..8f114e98 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -13,7 +13,7 @@ #' likelihood components into an additional "total" likelihood wheb plotting #' fleet specific likelihoods. Only applied when `by_fleet=TRUE`. #' -#' Default:FALSE +#' Default: FALSE #' #' @return A plot showing the value of likelihood components, optionally #' across fleets. @@ -42,6 +42,9 @@ plot_likelihoods <- function( make_rda = FALSE, figures_dir = getwd(), interactive = TRUE, + geom="bar", + group=NULL, + facet=NULL, ... ){ check_data(dat) From a9ec0227563d7e5a0e4301e5b460c84cb0bc89c5 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Wed, 18 Nov 2026 11:23:36 -0500 Subject: [PATCH 08/20] fix: place "Total" likelihood components as first facet Order fleets in the plot data as "Total" (if calculated), followed by the fleets in the order they appear in the data. --- R/plot_likelihoods.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index 8f114e98..076ac7bb 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -90,7 +90,10 @@ plot_likelihoods <- function( dplyr::summarise( fleet="Total", estimate = sum(estimate) - ) |> dplyr::bind_rows(plot_data) + ) |> dplyr::bind_rows(plot_data) |> + dplyr::mutate( + fleet = factor(fleet, levels=c("Total", unique(dat$fleet))) + ) } group <- processing[[2]] From e8f16b079061a0241045e78156cd4ed1e70bf47e Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Thu, 19 Nov 2026 16:09:49 -0500 Subject: [PATCH 09/20] feat: group likelihood components by model when multiple models are present when present, likelihood components for multiple models are plotted side-by-side, as a grouped barchart, allowing for comparison of likelihood components across models --- R/plot_likelihoods.R | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index 076ac7bb..db5fe63c 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -45,6 +45,7 @@ plot_likelihoods <- function( geom="bar", group=NULL, facet=NULL, + model=NULL, ... ){ check_data(dat) @@ -63,10 +64,11 @@ plot_likelihoods <- function( era=NULL, group=group, facet=facet, + model=model, module="likelihoods", scale_amount=1, interactive=TRUE - ) + ) |> dplyr::filter(!grepl("laplace_obj_fun", label)) if(!by_fleet){ prepared_data <- prepared_data |> @@ -74,6 +76,14 @@ plot_likelihoods <- function( }else{ prepared_data <- prepared_data |> dplyr::filter(!is.na(fleet)) + + prepared_data <- prepared_data |> dplyr::group_by(fleet, type, model) |> + dplyr::summarise( + estimate = sum(estimate) + ) |> + dplyr::mutate(label="total") |> + dplyr::bind_rows(prepared_data) + } processing <- process_data( @@ -101,14 +111,31 @@ plot_likelihoods <- function( plt <- ggplot2::ggplot(plot_data)+ ggplot2::geom_col( - ggplot2::aes(y=label, x=estimate, fill=label) + ggplot2::aes( + y=label, + x=estimate, + fill=if(length(unique(plot_data$model))>1){ + .data[["model"]] + }else{ + .data[["label"]] + } + ), + position="dodge", stat="identity" )+ ggplot2::geom_vline(xintercept=0, linetype="dashed")+ ggplot2::scale_x_continuous( limits= ~ c(-max(abs(.x)), max(abs(.x))) )+ - ggplot2::guides(fill="none")+ - ggplot2::labs(y="Likelihood Component", x="Likelihood Contribution")+ + ggplot2::guides()+ + ggplot2::labs( + y="Likelihood Component", + x="Likelihood Contribution", + fill = if(length(unique(plot_data$model))>1){ + "Model" + }else{ + "Component" + } + )+ theme_noaa() # Check if facet(s) are desired @@ -125,5 +152,5 @@ plot_likelihoods <- function( }) } - return(plt) + plt } \ No newline at end of file From 16c18ff0e36cd1d50debf8bff42102b66f6c0ba5 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Thu, 12 Nov 2026 17:49:33 -0500 Subject: [PATCH 10/20] feat: read LIKELIHOOD table from Report.sso into stockplotr data object currently only works for r4ss formatted inputs --- R/conout_ss3.R | 57 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/R/conout_ss3.R b/R/conout_ss3.R index fb3f528b..b3300545 100644 --- a/R/conout_ss3.R +++ b/R/conout_ss3.R @@ -192,7 +192,7 @@ conout_ss3 <- function( # ntentionally empty std2 <- c() cha <- c() - info <- c() + info <- c("likelihoods_used", "likelihoods_laplace", "likelihoods_by_fleet") } # Loop for all identified parameters to extract for plotting and use @@ -994,24 +994,43 @@ conout_ss3 <- function( # next #### info #### } else if (parm_sel %in% info) { - # if (parm_sel == "LIKELIHOOD") { - # df1 <- extract[-1, ] - # # make row the header names for first df - # colnames(df1) <- df1[1, ] - # # Remove first row containing column names - # df2 <- df1[-1, ] |> - # dplyr::rename(label = Component) |> - # tidyr::pivot_longer( - # cols = -label, - # names_to = "type", - # values_to = "likelihood" - # ) - # # match to out_new - # df2[setdiff(tolower(names(out_new)), tolower(names(df2)))] <- NA - # # Add to out list - # out_list[[parm_sel]] <- df2 - # } else - if (parm_sel == "DEFINITIONS") { + if (parm_sel %in% c("likelihoods_used", "likelihoods_laplace")) { + df1 <- extract#[-1, ] + # make row the header names for first df + # colnames(df1) <- df1[1, ] + # Remove first row containing column names + df2 <- df1 |> + tibble::rownames_to_column("Component") |> + dplyr::rename(label = Component) |> + tidyr::pivot_longer( + cols = -label, + names_to = "type", + values_to = "likelihood" + ) |> + dplyr::mutate(module_name="likelihoods") + # match to out_new + df2[setdiff(tolower(names(out_new)), tolower(names(df2)))] <- NA + + out_list[[parm_sel]] <- df2 + } else if(parm_sel == "likelihoods_by_fleet"){ + df1 <- extract + df2 <- df1 |> + # dplyr::filter(!grepl("use|skip", Label)) |> + dplyr::rename(label=Label) |> + dplyr::select(-ALL) |> + tidyr::pivot_longer( + cols = -label, + names_to = "fleet", + values_to = "likelihood" + ) |> + tidyr::separate(col=label, into=c("label", "type"), sep="_(?=lambda|like|N)", remove=FALSE, extra="merge") |> + dplyr::mutate(module_name="likelihoods") + + df2[setdiff(tolower(names(out_new)), tolower(names(df2)))] <- NA + + out_list[[parm_sel]] <- df2 + } #else + else if (parm_sel == "DEFINITIONS") { # Extract start year and end year for iding era start_year <- as.numeric(extract[which(apply(extract, 1, function(row) any(row == "Start_year:"))), 2]) end_year <- as.numeric(extract[which(apply(extract, 1, function(row) any(row == "End_year:"))), 2]) From 017de0f46640618b23288c51b6eae955f6996474 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 10:38:42 -0500 Subject: [PATCH 11/20] feat: plot_likelihoods Create bar chart of likelihood components --- R/plot_likelihoods.R | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 R/plot_likelihoods.R diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R new file mode 100644 index 00000000..b30c324c --- /dev/null +++ b/R/plot_likelihoods.R @@ -0,0 +1,89 @@ + +plot_likelihoods <- function( + dat, + by_fleet=FALSE, + summarise_fleets=FALSE, + facet = NULL, + group = NULL, + make_rda = FALSE, + figures_dir = getwd(), + interactive = TRUE, + ... +){ + check_data(dat) + + likelihood_type <- "values" + if(by_fleet){ + cli::cli_alert_info("Plotting likelihood components by fleet.") + likelihood_type <- "like" + facet <- "fleet" + } + + prepared_data <- filter_data( + dat = dat, + label_name = "", + geom=geom, + era=NULL, + group=group, + facet=facet, + module="likelihoods", + scale_amount=1, + interactive=TRUE + ) + + if(!by_fleet){ + prepared_data <- prepared_data |> + dplyr::filter(is.na(fleet)) + }else{ + prepared_data <- prepared_data |> + dplyr::filter(!is.na(fleet)) + } + + processing <- process_data( + dat = prepared_data |> dplyr::filter(type==likelihood_type), + group = group, + facet = facet + ) + + plot_data <- processing[[1]] |> dplyr::filter(!is.na(estimate), estimate!=0) + + if(by_fleet && summarise_fleets){ + plot_data <- plot_data |> + dplyr::group_by(model, group_var, label) |> + dplyr::summarise( + fleet="All", + estimate = sum(estimate) + ) |> dplyr::bind_rows(plot_data) + } + + group <- processing[[2]] + # if (!is.null(processing[[3]])) facet <- processing[[3]] + + plt <- ggplot2::ggplot(plot_data)+ + ggplot2::geom_col( + ggplot2::aes(y=label, x=estimate, fill=label) + )+ + ggplot2::geom_vline(xintercept=0, linetype="dashed")+ + ggplot2::scale_x_continuous( + limits= ~ c(-max(abs(.x)), max(abs(.x))) + )+ + ggplot2::guides(fill="none")+ + ggplot2::labs(y="Likelihood Component", x="Likelihood Contribution")+ + theme_noaa() + + # Check if facet(s) are desired + if (!is.null(facet) & length(facet) > 0) { + facet <- paste("~", paste(facet, collapse = " + ")) + facet_formula <- stats::reformulate(facet) + + plt <- plt + ggplot2::facet_wrap(facet_formula, labeller = function(labels) { + # Clean column names (e.g., "growth_pattern" -> "Growth Pattern") + names(labels) <- tools::toTitleCase(gsub("_", " ", names(labels))) + + # Combine into "Variable: Value" format + lapply(names(labels), function(var) paste0(var, ": ", labels[[var]])) + }) + } + + return(plt) +} \ No newline at end of file From ba45f65d15e7bc3a98ad15aecb72b52604a47d52 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 10:39:57 -0500 Subject: [PATCH 12/20] refactor: map likelihood values to "estimate" rather than "likelihood" column This is inline with how other models are converted --- R/conout_ss3.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/conout_ss3.R b/R/conout_ss3.R index b3300545..82c59cf9 100644 --- a/R/conout_ss3.R +++ b/R/conout_ss3.R @@ -1005,7 +1005,7 @@ conout_ss3 <- function( tidyr::pivot_longer( cols = -label, names_to = "type", - values_to = "likelihood" + values_to = "estimate" ) |> dplyr::mutate(module_name="likelihoods") # match to out_new @@ -1021,7 +1021,7 @@ conout_ss3 <- function( tidyr::pivot_longer( cols = -label, names_to = "fleet", - values_to = "likelihood" + values_to = "estimate" ) |> tidyr::separate(col=label, into=c("label", "type"), sep="_(?=lambda|like|N)", remove=FALSE, extra="merge") |> dplyr::mutate(module_name="likelihoods") From 3b0073da940a12eb6afb5f7fd67229e7a7580ac1 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 11:42:55 -0500 Subject: [PATCH 13/20] docs: :memo: roxygen for plot_likelihoods --- R/plot_likelihoods.R | 38 ++++++++++++++++++++- man/plot_likelihoods.Rd | 75 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 man/plot_likelihoods.Rd diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index b30c324c..c7ca3977 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -1,4 +1,40 @@ - +#' Plot likelihood components +#' +#' Plot likelihood components as a horizontal bar chart. +#' +#' @inheritParams plot_spawning_biomass +#' +#' @param by_fleet Logical, whether to plot fleet specific likelihood +#' components or not +#' +#' Default: FALSE +#' +#' @param summarise_fleets Logical, whether to aggregate fleet specificic +#' likelihood components into an additional "total" likelihood wheb plotting +#' fleet specific likelihoods. Only applied when `by_fleet=TRUE`. +#' +#' Default:FALSE +#' +#' @return A plot showing the value of likelihood components, optionally +#' across fleets. +#' +#' @export +#' +#' @examples +#' \dontrun{ +#' plot_likelhoods( +#' dat = stockplotr::example_data, +#' ) +#' plot_likelhoods( +#' dat = stockplotr::example_data, +#' by_fleet = TRUE +#' ) +#' plot_likelhoods( +#' dat = stockplotr::example_data, +#' by_fleet = TRUE, +#' summarise_fleets = TRUE +#' ) +#' } plot_likelihoods <- function( dat, by_fleet=FALSE, diff --git a/man/plot_likelihoods.Rd b/man/plot_likelihoods.Rd new file mode 100644 index 00000000..cfd31898 --- /dev/null +++ b/man/plot_likelihoods.Rd @@ -0,0 +1,75 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot_likelihoods.R +\name{plot_likelihoods} +\alias{plot_likelihoods} +\title{Plot likelihood components} +\usage{ +plot_likelihoods( + dat, + by_fleet = FALSE, + summarise_fleets = FALSE, + make_rda = FALSE, + figures_dir = getwd(), + interactive = TRUE, + ... +) +} +\arguments{ +\item{dat}{Data frame or list. A tibble or named list of tibbles (input as \code{list()}) +returned from \link[stockplotr]{convert_output}. + +If inputting a list of tibbles, the first tibble's reference point defined +in \code{ref_line} is used to plot a reference line or calculate relative spawning biomass.} + +\item{by_fleet}{Logical, whether to plot fleet specific likelihood +components or not + +Default: FALSE} + +\item{summarise_fleets}{Logical, whether to aggregate fleet specificic +likelihood components into an additional "total" likelihood wheb plotting +fleet specific likelihoods. Only applied when \code{by_fleet=TRUE}. + +Default:FALSE} + +\item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and +make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, +the rda will be exported to the folder indicated in the argument "figures_dir". + +Default: \code{FALSE}.} + +\item{figures_dir}{Path. Path to the "figures" folder. + +Default: \code{getwd()} + +The folder is created within the path if it does not exist.} + +\item{interactive}{Logical. TRUE/FALSE; indicate whether the environment is interactive. + +Default: \code{FALSE}} + +\item{...}{Arguments called from \link[ggplot2]{geom_line} or \link[ggplot2]{geom_point}} +} +\value{ +A plot showing the value of likelihood components, optionally +across fleets. +} +\description{ +Plot likelihood components as a horizontal bar chart. +} +\examples{ +\dontrun{ +plot_likelhoods( + dat = stockplotr::example_data, +) +plot_likelhoods( + dat = stockplotr::example_data, + by_fleet = TRUE +) +plot_likelhoods( + dat = stockplotr::example_data, + by_fleet = TRUE, + summarise_fleets = TRUE +) +} +} From be39167a20833f548acc22025f450f28b64d87fe Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 11:44:26 -0500 Subject: [PATCH 14/20] build: add plot_likelihoods to NAMESPACE --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index c7bad17d..bc973be2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -29,6 +29,7 @@ export(plot_fishing_mortality) export(plot_index) export(plot_joint_residuals) export(plot_landings) +export(plot_likelihoods) export(plot_natural_mortality) export(plot_obsvpred) export(plot_recruitment) From d4c57b59b53d303ee3a82b1d2c2d4183f04e9b64 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Mon, 16 Nov 2026 11:45:02 -0500 Subject: [PATCH 15/20] refactor:: remove extra parameters from plot_likelihoods --- R/plot_likelihoods.R | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index c7ca3977..67d7309d 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -39,8 +39,6 @@ plot_likelihoods <- function( dat, by_fleet=FALSE, summarise_fleets=FALSE, - facet = NULL, - group = NULL, make_rda = FALSE, figures_dir = getwd(), interactive = TRUE, @@ -87,7 +85,7 @@ plot_likelihoods <- function( plot_data <- plot_data |> dplyr::group_by(model, group_var, label) |> dplyr::summarise( - fleet="All", + fleet="Total", estimate = sum(estimate) ) |> dplyr::bind_rows(plot_data) } From 7b4562410077600eb9baf3f59d6091b7667bdd50 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Wed, 18 Nov 2026 11:22:34 -0500 Subject: [PATCH 16/20] refactor: add back in necessary function pars for filter_data --- R/plot_likelihoods.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index 67d7309d..8f114e98 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -13,7 +13,7 @@ #' likelihood components into an additional "total" likelihood wheb plotting #' fleet specific likelihoods. Only applied when `by_fleet=TRUE`. #' -#' Default:FALSE +#' Default: FALSE #' #' @return A plot showing the value of likelihood components, optionally #' across fleets. @@ -42,6 +42,9 @@ plot_likelihoods <- function( make_rda = FALSE, figures_dir = getwd(), interactive = TRUE, + geom="bar", + group=NULL, + facet=NULL, ... ){ check_data(dat) From 6882667f85572dffba74d57a82d1f3f67547e735 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Wed, 18 Nov 2026 11:23:36 -0500 Subject: [PATCH 17/20] fix: place "Total" likelihood components as first facet Order fleets in the plot data as "Total" (if calculated), followed by the fleets in the order they appear in the data. --- R/plot_likelihoods.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index 8f114e98..076ac7bb 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -90,7 +90,10 @@ plot_likelihoods <- function( dplyr::summarise( fleet="Total", estimate = sum(estimate) - ) |> dplyr::bind_rows(plot_data) + ) |> dplyr::bind_rows(plot_data) |> + dplyr::mutate( + fleet = factor(fleet, levels=c("Total", unique(dat$fleet))) + ) } group <- processing[[2]] From 46c7c991b2a09f37e86bc8de4e44db2e9dd7106e Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Thu, 19 Nov 2026 16:09:49 -0500 Subject: [PATCH 18/20] feat: group likelihood components by model when multiple models are present when present, likelihood components for multiple models are plotted side-by-side, as a grouped barchart, allowing for comparison of likelihood components across models --- R/plot_likelihoods.R | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index 076ac7bb..db5fe63c 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -45,6 +45,7 @@ plot_likelihoods <- function( geom="bar", group=NULL, facet=NULL, + model=NULL, ... ){ check_data(dat) @@ -63,10 +64,11 @@ plot_likelihoods <- function( era=NULL, group=group, facet=facet, + model=model, module="likelihoods", scale_amount=1, interactive=TRUE - ) + ) |> dplyr::filter(!grepl("laplace_obj_fun", label)) if(!by_fleet){ prepared_data <- prepared_data |> @@ -74,6 +76,14 @@ plot_likelihoods <- function( }else{ prepared_data <- prepared_data |> dplyr::filter(!is.na(fleet)) + + prepared_data <- prepared_data |> dplyr::group_by(fleet, type, model) |> + dplyr::summarise( + estimate = sum(estimate) + ) |> + dplyr::mutate(label="total") |> + dplyr::bind_rows(prepared_data) + } processing <- process_data( @@ -101,14 +111,31 @@ plot_likelihoods <- function( plt <- ggplot2::ggplot(plot_data)+ ggplot2::geom_col( - ggplot2::aes(y=label, x=estimate, fill=label) + ggplot2::aes( + y=label, + x=estimate, + fill=if(length(unique(plot_data$model))>1){ + .data[["model"]] + }else{ + .data[["label"]] + } + ), + position="dodge", stat="identity" )+ ggplot2::geom_vline(xintercept=0, linetype="dashed")+ ggplot2::scale_x_continuous( limits= ~ c(-max(abs(.x)), max(abs(.x))) )+ - ggplot2::guides(fill="none")+ - ggplot2::labs(y="Likelihood Component", x="Likelihood Contribution")+ + ggplot2::guides()+ + ggplot2::labs( + y="Likelihood Component", + x="Likelihood Contribution", + fill = if(length(unique(plot_data$model))>1){ + "Model" + }else{ + "Component" + } + )+ theme_noaa() # Check if facet(s) are desired @@ -125,5 +152,5 @@ plot_likelihoods <- function( }) } - return(plt) + plt } \ No newline at end of file From d35eed85836c6f2079806d978fe9da3403e9eb0e Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Wed, 25 Nov 2026 13:40:39 -0500 Subject: [PATCH 19/20] build: export plot_likelihoods --- R/plot_likelihoods.R | 4 ++-- man/plot_likelihoods.Rd | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index db5fe63c..4b868787 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -9,8 +9,8 @@ #' #' Default: FALSE #' -#' @param summarise_fleets Logical, whether to aggregate fleet specificic -#' likelihood components into an additional "total" likelihood wheb plotting +#' @param summarise_fleets Logical, whether to aggregate fleet specific +#' likelihood components into an additional "total" likelihood when plotting #' fleet specific likelihoods. Only applied when `by_fleet=TRUE`. #' #' Default: FALSE diff --git a/man/plot_likelihoods.Rd b/man/plot_likelihoods.Rd index cfd31898..ab17c5f5 100644 --- a/man/plot_likelihoods.Rd +++ b/man/plot_likelihoods.Rd @@ -11,6 +11,10 @@ plot_likelihoods( make_rda = FALSE, figures_dir = getwd(), interactive = TRUE, + geom = "bar", + group = NULL, + facet = NULL, + model = NULL, ... ) } @@ -26,11 +30,11 @@ components or not Default: FALSE} -\item{summarise_fleets}{Logical, whether to aggregate fleet specificic -likelihood components into an additional "total" likelihood wheb plotting +\item{summarise_fleets}{Logical, whether to aggregate fleet specific +likelihood components into an additional "total" likelihood when plotting fleet specific likelihoods. Only applied when \code{by_fleet=TRUE}. -Default:FALSE} +Default: FALSE} \item{make_rda}{Logical. TRUE/FALSE; indicate whether to save the object and make an automated caption and alternative text in the form of an \code{rda} object. If TRUE, @@ -48,6 +52,31 @@ The folder is created within the path if it does not exist.} Default: \code{FALSE}} +\item{geom}{String. Geom used for the plot. + +Default: "line". + +Options: "line", "point", or "area"} + +\item{group}{String. Single column that groups the data. + +Set group = "none" to summarize data over all indexing values. + +Default: NULL +Options: Including, but not limited to: "year", "area", "fleet", "sex", "none", NULL} + +\item{facet}{Character vector. Column name or names used for faceting. + +Default: NULL} + +\item{model}{Character vector. (Optional) Model name found in \code{dat$model}. +If selecting >1 model, place them in a vector like c("model1", "model2"). + +Default: NULL + +If the interactive and >1 model is found, user will select the +model in the console. @seealso \code{\link[=filter_data]{filter_data()}}} + \item{...}{Arguments called from \link[ggplot2]{geom_line} or \link[ggplot2]{geom_point}} } \value{ From 41f984aa0ab1130ed37d8a04d8ddc2823457c0b1 Mon Sep 17 00:00:00 2001 From: Joshua Zahner Date: Wed, 25 Nov 2026 16:49:47 -0500 Subject: [PATCH 20/20] docs: updated plot_likelihoods documentation --- R/plot_likelihoods.R | 13 ++- man/plot_likelihoods.Rd | 14 ++- tests/testthat/test-plot_likelihoods.R | 156 +++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 6 deletions(-) create mode 100644 tests/testthat/test-plot_likelihoods.R diff --git a/R/plot_likelihoods.R b/R/plot_likelihoods.R index 4b868787..02eac1a0 100644 --- a/R/plot_likelihoods.R +++ b/R/plot_likelihoods.R @@ -2,6 +2,9 @@ #' #' Plot likelihood components as a horizontal bar chart. #' +#' @details The current implementation only supports Stock Synthesis (SS3) models. +#' The `example_data` dataset is not currently compatible. +#' #' @inheritParams plot_spawning_biomass #' #' @param by_fleet Logical, whether to plot fleet specific likelihood @@ -22,15 +25,19 @@ #' #' @examples #' \dontrun{ +#' +#' # `example_data` not presently compatible +#' dat <- convert_output(r4ss::SSgetoutput(dir = "path/to/model")) +#' #' plot_likelhoods( -#' dat = stockplotr::example_data, +#' dat, #' ) #' plot_likelhoods( -#' dat = stockplotr::example_data, +#' dat, #' by_fleet = TRUE #' ) #' plot_likelhoods( -#' dat = stockplotr::example_data, +#' dat, #' by_fleet = TRUE, #' summarise_fleets = TRUE #' ) diff --git a/man/plot_likelihoods.Rd b/man/plot_likelihoods.Rd index ab17c5f5..ff7ecab5 100644 --- a/man/plot_likelihoods.Rd +++ b/man/plot_likelihoods.Rd @@ -86,17 +86,25 @@ across fleets. \description{ Plot likelihood components as a horizontal bar chart. } +\details{ +The current implementation only supports Stock Synthesis (SS3) models. +The \code{example_data} dataset is not currently compatible. +} \examples{ \dontrun{ + +# `example_data` not presently compatible +dat <- convert_output(r4ss::SSgetoutput(dir = "path/to/model")) + plot_likelhoods( - dat = stockplotr::example_data, + dat, ) plot_likelhoods( - dat = stockplotr::example_data, + dat, by_fleet = TRUE ) plot_likelhoods( - dat = stockplotr::example_data, + dat, by_fleet = TRUE, summarise_fleets = TRUE ) diff --git a/tests/testthat/test-plot_likelihoods.R b/tests/testthat/test-plot_likelihoods.R new file mode 100644 index 00000000..9887c387 --- /dev/null +++ b/tests/testthat/test-plot_likelihoods.R @@ -0,0 +1,156 @@ +ss3_likelihood_data <- function() { + skip_if_not_installed("r4ss") + models <- c("Simple", "Simple_with_Discard", "Simple_NoCPUE") + outputs <- lapply(stats::setNames(models, models), function(model) { + r4ss::SS_output( + test_path("fixtures", "ss3_models", "models", model), + verbose = FALSE + ) + }) + suppressMessages(convert_output(outputs)) +} + +test_that("plot_likelihoods plots total SS3 likelihoods for one to three models", { + dat <- ss3_likelihood_data() + models <- unique(dat$model) + + for (n_models in seq_along(models)) { + selected <- models[seq_len(n_models)] + p <- suppressMessages(plot_likelihoods( + dat, model = selected, interactive = FALSE + )) + expected <- dat |> + dplyr::mutate(label = tolower(label)) |> + dplyr::filter( + model %in% selected, + module_name == "likelihoods", + type == "values", + is.na(fleet), + !grepl("laplace_obj_fun", label), + !is.na(estimate), + estimate != 0 + ) |> + dplyr::select(model, label, estimate) |> + dplyr::arrange(model, label) + + expect_s3_class(p, "ggplot") + expect_equal( + p$data |> + dplyr::select(model, label, estimate) |> + dplyr::arrange(model, label), + expected + ) + expect_setequal(p$data$model, selected) + expect_equal(sum(p$data$label == "total"), n_models) + expect_true(any(p$data$estimate < 0)) + + built <- ggplot2::ggplot_build(p) + expect_equal(nrow(built$layout$layout), 1) + expect_equal(sort(built$data[[1]]$x), sort(expected$estimate)) + expect_setequal( + built$plot$scales$get_scales("fill")$get_limits(), + if (n_models > 1) selected else unique(expected$label) + ) + } +}) + +test_that("plot_likelihoods preserves SS3 fleet contributions and adds fleet totals", { + dat <- ss3_likelihood_data() + p <- suppressMessages(plot_likelihoods( + dat, model = unique(dat$model), by_fleet = TRUE, interactive = FALSE + )) + expected <- dat |> + dplyr::mutate(label = tolower(label)) |> + dplyr::filter( + module_name == "likelihoods", + type == "like", + !is.na(fleet), + !is.na(estimate), + estimate != 0 + ) |> + dplyr::select(model, fleet, label, estimate) |> + dplyr::arrange(model, fleet, label) + totals <- expected |> + dplyr::group_by(model, fleet) |> + dplyr::summarise(estimate = sum(estimate), .groups = "drop") + + expect_s3_class(p, "ggplot") + expect_equal( + p$data |> + dplyr::ungroup() |> + dplyr::filter(label != "total") |> + dplyr::select(model, fleet, label, estimate) |> + dplyr::arrange(model, fleet, label), + expected + ) + expect_equal( + p$data |> + dplyr::ungroup() |> + dplyr::filter(label == "total") |> + dplyr::select(model, fleet, estimate) |> + dplyr::arrange(model, fleet), + totals + ) + expect_false("Total" %in% p$data$fleet) + + built <- ggplot2::ggplot_build(p) + expect_setequal(built$layout$layout$fleet, expected$fleet) + expect_equal(nrow(built$layout$layout), dplyr::n_distinct(expected$fleet)) + expect_equal( + sort(built$data[[1]]$x), + sort(c(expected$estimate, totals$estimate)) + ) +}) + +test_that("plot_likelihoods summarises SS3 fleets separately for each model", { + dat <- ss3_likelihood_data() + p <- suppressMessages(plot_likelihoods( + dat, model = unique(dat$model), by_fleet = TRUE, + summarise_fleets = TRUE, interactive = FALSE + )) + source <- dat |> + dplyr::mutate(label = tolower(label)) |> + dplyr::filter( + module_name == "likelihoods", + type == "like", + !is.na(fleet), + !is.na(estimate), + estimate != 0 + ) + expected <- source |> + dplyr::group_by(model, label) |> + dplyr::summarise(estimate = sum(estimate), .groups = "drop") + totals <- source |> + dplyr::group_by(model) |> + dplyr::summarise(label = "total", estimate = sum(estimate), .groups = "drop") + expected <- dplyr::bind_rows(expected, totals) |> + dplyr::arrange(model, label) + + expect_s3_class(p, "ggplot") + expect_equal( + p$data |> + dplyr::ungroup() |> + dplyr::filter(fleet == "Total") |> + dplyr::select(model, label, estimate) |> + dplyr::arrange(model, label), + expected + ) + expect_equal( + p$data |> + dplyr::ungroup() |> + dplyr::filter(fleet != "Total", label != "total") |> + dplyr::mutate(fleet = as.character(fleet)) |> + dplyr::select(model, fleet, label, estimate) |> + dplyr::arrange(model, fleet, label), + source |> + dplyr::select(model, fleet, label, estimate) |> + dplyr::arrange(model, fleet, label) + ) + + built <- ggplot2::ggplot_build(p) + expect_equal(as.character(built$layout$layout$fleet[1]), "Total") + expect_setequal(as.character(built$layout$layout$fleet), c("Total", source$fleet)) + expect_equal(nrow(built$data[[1]]), nrow(p$data)) + expect_equal(sort(built$data[[1]]$x), sort(p$data$estimate)) +}) +