Skip to content

Commit

Permalink
add placeholder decision table
Browse files Browse the repository at this point in the history
  • Loading branch information
iantaylor-NOAA committed Jul 21, 2023
1 parent 0e2e621 commit d9b3dc1
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 0 deletions.
135 changes: 135 additions & 0 deletions R/table_decision.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#' Format a decision table
#'
#' Format a decision table for its inclusion in a document.
#'
#' @param ... Each element provided in the call to this function that is not
#' assigned to a particular input argument should be a vector of file paths
#' to the models you want to read for a given column of your decision table.
#' For example, if a single column has results from three models and
#' if your decision table has a low and high state of nature, then
#' there should be two vectors passed via `...` and each vector would contain
#' paths to three different models.
#' The order of the vectors will be the column order from left to right.
#' The order of the models within a vector will be the row order.
#' @param years A vector of years you want catches for.
#' @template format
#' @param caption A character string that
#' will be passed to the `caption` parameter of [kableExtra::kbl].
#' The default value is `NULL`.
#' @param label A character string without underscores that
#' will be passed to the `label` parameter of [kableExtra::kbl].
#' The default value is `NULL`.
#' @param digits A vector of digits for catch, spawning output, and fraction,
#' unfished. It gets passed to `r4ss::SS_decision_table_stuff()`.
#' @export
#' @author Kelli F. Johnson, Ian G. Taylor, Chantel R. Wetzel
#' @examples
#' table_decision(
#' list(mod.low.A, mod.base.A, mod.high.A),
#' list(mod.low.B, mod.base.B, mod.high.B),
#' list(mod.low.C, mod.base.C, mod.high.C)
#' )
table_decision <- function(
...,
years = 2023:2034,
format = c("latex", "html"),
caption = formals(kableExtra::kbl)$caption,
label = formals(kableExtra::kbl)$label,
digits = c(0, 2, 3)) {
mods <- list(...)
# make sure that the format input is good
# chooses first option by default
format <- match.arg(format)

# hardwired to prevent users from adding too-long inputs
rowgroup <- c("A", "B", "C")

# process output
results <- purrr::modify_depth(
mods,
.depth = 2,
.f = r4ss::SS_decision_table_stuff,
yrs = years, digits = digits
) %>%
purrr::modify_depth(1, dplyr::bind_cols) %>%
dplyr::bind_rows(.id = "Mgmt") %>%
dplyr::mutate(
Mgmt = rowgroup[as.numeric(Mgmt)],
# Catch = pmax(na.rm = TRUE, !!!rlang::syms(grep(value = TRUE, "catch", names(.))))
) %>%
dplyr::rename(Year = "yr...1") %>%
dplyr::select_if(!grepl("yr\\.+", colnames(.))) # %>%

# get the catch columns and warn if they aren't all equal
# (simpler than trying to automatically italicize values associated
# with mismatched catch which had issues for lingcod)
catch_cols <- results %>% dplyr::select(dplyr::starts_with("catch"))
if (max(abs(apply(catch_cols, 1, sd))) > 0.01) {
warning("Catch differs among columns, perhaps due to a crashed model without enough biomass.")
}

# clean up column names and remove extra catch columns
# first rename the first catch column
results <- results %>%
dplyr::rename(Catch = "catch...2") %>%
dplyr::select(-dplyr::starts_with("catch", ignore.case = FALSE))

# remove repeated lables in Mgmt column
results <- results %>%
dplyr::mutate(Mgmt = ifelse(duplicated(Mgmt), "", Mgmt))

# # add horizontal lines between groups (only works in latex)
# # only works if Mgmt column has single value at the top of each group
# results <- results %>%
# kableExtra::row_spec(row = which(results$Mgmt != "")[-1] - 1,
# hline_after = TRUE)

# add color to the depletion column
results <- results %>%
dplyr::mutate_at(
.vars = dplyr::vars(grep(value = TRUE, "^dep", colnames(.))),
~ kableExtra::cell_spec(
format = format,
x = .,
color = "white",
background = kableExtra::spec_color(
.,
begin = 0, end = 1,
option = "D",
scale_from = c(0, 1),
direction = -1
)
)
)

# add column names
rownames(results) <- NULL
colnames(results) <- c(
"Mgmt", "Year", "Catch", "Low Spawn", "Low Frac",
"Base Spawn", "Base Frac", "High Spawn", "High Frac"
)

results %>%
kableExtra::kbl(
format = format,
escape = FALSE,
booktabs = TRUE,
linesep = rep(c(rep("", length(years) - 1), "\\addlinespace"), 2),
align = c("l", "l", "r", rep(c("r", "r"), 3)),
caption = caption,
label = label
) %>%
kableExtra::column_spec(c(1), bold = TRUE) %>% # first column bold
# kableExtra::column_spec(c(1, 3, 3+2*3, border_right = TRUE) %>% # vertical lines (not really needed)
kableExtra::column_spec(3,
color = "white", # white text
background = kableExtra::spec_color(results[["Catch"]], # background coloring for catch columns
begin = 0.3,
end = 0.7,
option = "E",
direction = -1
)
) %>%
kableExtra::column_spec(4:NCOL(results), width = "3.5em") %>%
kableExtra::kable_classic(full_width = FALSE)
}
46 changes: 46 additions & 0 deletions Rscripts/forecast_notes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
if (FALSE) {
SSexecutivesummary(mod.34.10, forecast_ofl = c(3763, 3563))
save_loc <- file.path(mod.34.10$inputs$dir, "tex_tables")
dir.create(save_loc)
sa4ss::es_table_tex(
dir = mod.34.10$inputs$dir,
save_loc = save_loc
)

sa4ss::es_table_tex(
# dir = mod.34.10$inputs$dir,
dir = "models/2023.a034.010_fixed_forecast_catch/tables/",
csv_name = "table_label_projection_modified.csv",
digits = 0,
save_loc = save_loc
)



# Row-group names for first column; kableExtra::linebreak allows for hard returns
rowgroupnames <- kableExtra::linebreak(
x = c(
# "Recent\n avg.\n catch",
# "ACL\n$P^*$=0.40",
"ACL $P^*$=0.45"
),
align = "l"
)
} # end if (FALSE) to help with sourcing the commands below


### make decision table
#devtools::load_all();
caption <- "Decision table with 10-year projections. 'Mgmt' refers to the three management scenarios (A) the default harvest control rule $P^* = 0.45$, (B) [TODO: fill in something here], and (C) [TODO: fill in something here]. In each case the 2023 and 2024 catches are fixed at the ACLs which have been set for that year with estimated fleet allocation provided by the GMT. The alternative states of nature ('Low', 'Base', and 'High') are provided in the columns, with Spawning Output ('Spawn', in trillions of eggs) and Fraction of unfished ('Frac') provided for each state. The colors of catch and fraction unfished are relative with lighter colors representing lower values."

tab <- table_decision(
caption = caption,
label = "es-decision",
list(mod.34.10, mod.34.10, mod.34.10),
list(mod.34.10, mod.34.10, mod.34.10),
list(mod.34.10, mod.34.10, mod.34.10)
)
writeLines(tab, "documents/tex_tables/decision_table.tex")

# kableExtra::save_kable(file = "documents/tex_tables/decision_table.tex")

4 changes: 4 additions & 0 deletions documents/01executive.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ Main life history parameters, such as natural mortality and stock-recruit curve

Not required in draft assessment undergoing review.

\clearpage
\input{tex_tables/decision_table.tex}

\clearpage
## Scientific uncertainty{-}

The model estimated uncertainty around the `r endyr+1` spawning output for is $\sigma$ = `r round(mod_base$Pstar_sigma, 2)`. The uncertainty around the OFL is $\sigma$ = `r round(mod_base$OFL_sigma, 2)`. These values are lower than for many West Coast groundfish stocks for several reasons: large `r spp` sample sizes of length and age data from fisheries and surveys, high frequency of occurrence in the \gls{s-wcgbt} thanks to `r spp` primarily residing in trawlable habitat within the scope of the survey, and strong contrast in the data caused by fishing down the stock to a low level followed by rapid rebuilding. Nevertheless, these $\sigma$ values surely underestimate the overall uncertainty as they do not incorporate the model structural uncertainty and do not account for any time-varying dynamics other than recruitment.
Expand Down
49 changes: 49 additions & 0 deletions documents/tex_tables/decision_table.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
\begin{table}

\caption{\label{tab:es-decision}Decision table with 10-year projections. 'Mgmt' refers to the three management scenarios (A) the default harvest control rule $P^* = 0.45$, (B) [TODO: fill in something here], and (C) [TODO: fill in something here]. In each case the 2023 and 2024 catches are fixed at the ACLs which have been set for that year with estimated fleet allocation provided by the GMT. The alternative states of nature ('Low', 'Base', and 'High') are provided in the columns, with Spawning Output ('Spawn', in trillions of eggs) and Fraction of unfished ('Frac') provided for each state. The colors of catch and fraction unfished are relative with lighter colors representing lower values.}
\centering
\begin{tabular}[t]{>{}ll>{}r>{\raggedleft\arraybackslash}p{3.5em}>{\raggedleft\arraybackslash}p{3.5em}>{\raggedleft\arraybackslash}p{3.5em}>{\raggedleft\arraybackslash}p{3.5em}>{\raggedleft\arraybackslash}p{3.5em}>{\raggedleft\arraybackslash}p{3.5em}}
\toprule
Mgmt & Year & Catch & Low Spawn & Low Frac & Base Spawn & Base Frac & High Spawn & High Frac\\
\midrule
\textbf{A} & 2023 & \cellcolor[HTML]{4E576C}{\textcolor{white}{3485}} & 7.69 & \cellcolor[HTML]{34B679}{\textcolor{white}{0.336}} & 7.69 & \cellcolor[HTML]{34B679}{\textcolor{white}{0.336}} & 7.69 & \cellcolor[HTML]{34B679}{\textcolor{white}{0.336}}\\
\textbf{} & 2024 & \cellcolor[HTML]{5D616E}{\textcolor{white}{3285}} & 6.70 & \cellcolor[HTML]{48C16E}{\textcolor{white}{0.292}} & 6.70 & \cellcolor[HTML]{48C16E}{\textcolor{white}{0.292}} & 6.70 & \vphantom{2} \cellcolor[HTML]{48C16E}{\textcolor{white}{0.292}}\\
\textbf{} & 2025 & \cellcolor[HTML]{A89E75}{\textcolor{white}{2277}} & 5.85 & \cellcolor[HTML]{5AC864}{\textcolor{white}{0.255}} & 5.85 & \cellcolor[HTML]{5AC864}{\textcolor{white}{0.255}} & 5.85 & \vphantom{2} \cellcolor[HTML]{5AC864}{\textcolor{white}{0.255}}\\
\textbf{} & 2026 & \cellcolor[HTML]{AFA473}{\textcolor{white}{2196}} & 5.59 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.59 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.59 & \vphantom{2} \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}}\\
\textbf{} & 2027 & \cellcolor[HTML]{B0A473}{\textcolor{white}{2186}} & 5.52 & \cellcolor[HTML]{63CB5F}{\textcolor{white}{0.241}} & 5.52 & \cellcolor[HTML]{63CB5F}{\textcolor{white}{0.241}} & 5.52 & \vphantom{2} \cellcolor[HTML]{63CB5F}{\textcolor{white}{0.241}}\\
\textbf{} & 2028 & \cellcolor[HTML]{ACA174}{\textcolor{white}{2229}} & 5.58 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.58 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.58 & \vphantom{2} \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}}\\
\textbf{} & 2029 & \cellcolor[HTML]{A79D75}{\textcolor{white}{2288}} & 5.72 & \cellcolor[HTML]{5CC863}{\textcolor{white}{0.25}} & 5.72 & \cellcolor[HTML]{5CC863}{\textcolor{white}{0.25}} & 5.72 & \vphantom{2} \cellcolor[HTML]{5CC863}{\textcolor{white}{0.25}}\\
\textbf{} & 2030 & \cellcolor[HTML]{A29A76}{\textcolor{white}{2342}} & 5.88 & \cellcolor[HTML]{58C765}{\textcolor{white}{0.257}} & 5.88 & \cellcolor[HTML]{58C765}{\textcolor{white}{0.257}} & 5.88 & \vphantom{2} \cellcolor[HTML]{58C765}{\textcolor{white}{0.257}}\\
\textbf{} & 2031 & \cellcolor[HTML]{9F9777}{\textcolor{white}{2385}} & 6.03 & \cellcolor[HTML]{56C667}{\textcolor{white}{0.263}} & 6.03 & \cellcolor[HTML]{56C667}{\textcolor{white}{0.263}} & 6.03 & \vphantom{2} \cellcolor[HTML]{56C667}{\textcolor{white}{0.263}}\\
\textbf{} & 2032 & \cellcolor[HTML]{9C9677}{\textcolor{white}{2417}} & 6.17 & \cellcolor[HTML]{52C569}{\textcolor{white}{0.269}} & 6.17 & \cellcolor[HTML]{52C569}{\textcolor{white}{0.269}} & 6.17 & \vphantom{2} \cellcolor[HTML]{52C569}{\textcolor{white}{0.269}}\\
\textbf{} & 2033 & \cellcolor[HTML]{9A9377}{\textcolor{white}{2444}} & 6.28 & \cellcolor[HTML]{50C46A}{\textcolor{white}{0.274}} & 6.28 & \cellcolor[HTML]{50C46A}{\textcolor{white}{0.274}} & 6.28 & \vphantom{2} \cellcolor[HTML]{50C46A}{\textcolor{white}{0.274}}\\
\textbf{} & 2034 & \cellcolor[HTML]{999278}{\textcolor{white}{2462}} & 6.38 & \cellcolor[HTML]{4EC36B}{\textcolor{white}{0.279}} & 6.38 & \cellcolor[HTML]{4EC36B}{\textcolor{white}{0.279}} & 6.38 & \vphantom{2} \cellcolor[HTML]{4EC36B}{\textcolor{white}{0.279}}\\
\addlinespace
\textbf{B} & 2023 & \cellcolor[HTML]{4E576C}{\textcolor{white}{3485}} & 7.69 & \cellcolor[HTML]{34B679}{\textcolor{white}{0.336}} & 7.69 & \cellcolor[HTML]{34B679}{\textcolor{white}{0.336}} & 7.69 & \cellcolor[HTML]{34B679}{\textcolor{white}{0.336}}\\
\textbf{} & 2024 & \cellcolor[HTML]{5D616E}{\textcolor{white}{3285}} & 6.70 & \cellcolor[HTML]{48C16E}{\textcolor{white}{0.292}} & 6.70 & \cellcolor[HTML]{48C16E}{\textcolor{white}{0.292}} & 6.70 & \vphantom{1} \cellcolor[HTML]{48C16E}{\textcolor{white}{0.292}}\\
\textbf{} & 2025 & \cellcolor[HTML]{A89E75}{\textcolor{white}{2277}} & 5.85 & \cellcolor[HTML]{5AC864}{\textcolor{white}{0.255}} & 5.85 & \cellcolor[HTML]{5AC864}{\textcolor{white}{0.255}} & 5.85 & \vphantom{1} \cellcolor[HTML]{5AC864}{\textcolor{white}{0.255}}\\
\textbf{} & 2026 & \cellcolor[HTML]{AFA473}{\textcolor{white}{2196}} & 5.59 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.59 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.59 & \vphantom{1} \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}}\\
\textbf{} & 2027 & \cellcolor[HTML]{B0A473}{\textcolor{white}{2186}} & 5.52 & \cellcolor[HTML]{63CB5F}{\textcolor{white}{0.241}} & 5.52 & \cellcolor[HTML]{63CB5F}{\textcolor{white}{0.241}} & 5.52 & \vphantom{1} \cellcolor[HTML]{63CB5F}{\textcolor{white}{0.241}}\\
\textbf{} & 2028 & \cellcolor[HTML]{ACA174}{\textcolor{white}{2229}} & 5.58 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.58 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.58 & \vphantom{1} \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}}\\
\textbf{} & 2029 & \cellcolor[HTML]{A79D75}{\textcolor{white}{2288}} & 5.72 & \cellcolor[HTML]{5CC863}{\textcolor{white}{0.25}} & 5.72 & \cellcolor[HTML]{5CC863}{\textcolor{white}{0.25}} & 5.72 & \vphantom{1} \cellcolor[HTML]{5CC863}{\textcolor{white}{0.25}}\\
\textbf{} & 2030 & \cellcolor[HTML]{A29A76}{\textcolor{white}{2342}} & 5.88 & \cellcolor[HTML]{58C765}{\textcolor{white}{0.257}} & 5.88 & \cellcolor[HTML]{58C765}{\textcolor{white}{0.257}} & 5.88 & \vphantom{1} \cellcolor[HTML]{58C765}{\textcolor{white}{0.257}}\\
\textbf{} & 2031 & \cellcolor[HTML]{9F9777}{\textcolor{white}{2385}} & 6.03 & \cellcolor[HTML]{56C667}{\textcolor{white}{0.263}} & 6.03 & \cellcolor[HTML]{56C667}{\textcolor{white}{0.263}} & 6.03 & \vphantom{1} \cellcolor[HTML]{56C667}{\textcolor{white}{0.263}}\\
\textbf{} & 2032 & \cellcolor[HTML]{9C9677}{\textcolor{white}{2417}} & 6.17 & \cellcolor[HTML]{52C569}{\textcolor{white}{0.269}} & 6.17 & \cellcolor[HTML]{52C569}{\textcolor{white}{0.269}} & 6.17 & \vphantom{1} \cellcolor[HTML]{52C569}{\textcolor{white}{0.269}}\\
\textbf{} & 2033 & \cellcolor[HTML]{9A9377}{\textcolor{white}{2444}} & 6.28 & \cellcolor[HTML]{50C46A}{\textcolor{white}{0.274}} & 6.28 & \cellcolor[HTML]{50C46A}{\textcolor{white}{0.274}} & 6.28 & \vphantom{1} \cellcolor[HTML]{50C46A}{\textcolor{white}{0.274}}\\
\textbf{} & 2034 & \cellcolor[HTML]{999278}{\textcolor{white}{2462}} & 6.38 & \cellcolor[HTML]{4EC36B}{\textcolor{white}{0.279}} & 6.38 & \cellcolor[HTML]{4EC36B}{\textcolor{white}{0.279}} & 6.38 & \vphantom{1} \cellcolor[HTML]{4EC36B}{\textcolor{white}{0.279}}\\
\addlinespace
\textbf{C} & 2023 & \cellcolor[HTML]{4E576C}{\textcolor{white}{3485}} & 7.69 & \cellcolor[HTML]{34B679}{\textcolor{white}{0.336}} & 7.69 & \cellcolor[HTML]{34B679}{\textcolor{white}{0.336}} & 7.69 & \cellcolor[HTML]{34B679}{\textcolor{white}{0.336}}\\
\textbf{} & 2024 & \cellcolor[HTML]{5D616E}{\textcolor{white}{3285}} & 6.70 & \cellcolor[HTML]{48C16E}{\textcolor{white}{0.292}} & 6.70 & \cellcolor[HTML]{48C16E}{\textcolor{white}{0.292}} & 6.70 & \cellcolor[HTML]{48C16E}{\textcolor{white}{0.292}}\\
\textbf{} & 2025 & \cellcolor[HTML]{A89E75}{\textcolor{white}{2277}} & 5.85 & \cellcolor[HTML]{5AC864}{\textcolor{white}{0.255}} & 5.85 & \cellcolor[HTML]{5AC864}{\textcolor{white}{0.255}} & 5.85 & \cellcolor[HTML]{5AC864}{\textcolor{white}{0.255}}\\
\textbf{} & 2026 & \cellcolor[HTML]{AFA473}{\textcolor{white}{2196}} & 5.59 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.59 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.59 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}}\\
\textbf{} & 2027 & \cellcolor[HTML]{B0A473}{\textcolor{white}{2186}} & 5.52 & \cellcolor[HTML]{63CB5F}{\textcolor{white}{0.241}} & 5.52 & \cellcolor[HTML]{63CB5F}{\textcolor{white}{0.241}} & 5.52 & \cellcolor[HTML]{63CB5F}{\textcolor{white}{0.241}}\\
\textbf{} & 2028 & \cellcolor[HTML]{ACA174}{\textcolor{white}{2229}} & 5.58 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.58 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}} & 5.58 & \cellcolor[HTML]{60CA60}{\textcolor{white}{0.244}}\\
\textbf{} & 2029 & \cellcolor[HTML]{A79D75}{\textcolor{white}{2288}} & 5.72 & \cellcolor[HTML]{5CC863}{\textcolor{white}{0.25}} & 5.72 & \cellcolor[HTML]{5CC863}{\textcolor{white}{0.25}} & 5.72 & \cellcolor[HTML]{5CC863}{\textcolor{white}{0.25}}\\
\textbf{} & 2030 & \cellcolor[HTML]{A29A76}{\textcolor{white}{2342}} & 5.88 & \cellcolor[HTML]{58C765}{\textcolor{white}{0.257}} & 5.88 & \cellcolor[HTML]{58C765}{\textcolor{white}{0.257}} & 5.88 & \cellcolor[HTML]{58C765}{\textcolor{white}{0.257}}\\
\textbf{} & 2031 & \cellcolor[HTML]{9F9777}{\textcolor{white}{2385}} & 6.03 & \cellcolor[HTML]{56C667}{\textcolor{white}{0.263}} & 6.03 & \cellcolor[HTML]{56C667}{\textcolor{white}{0.263}} & 6.03 & \cellcolor[HTML]{56C667}{\textcolor{white}{0.263}}\\
\textbf{} & 2032 & \cellcolor[HTML]{9C9677}{\textcolor{white}{2417}} & 6.17 & \cellcolor[HTML]{52C569}{\textcolor{white}{0.269}} & 6.17 & \cellcolor[HTML]{52C569}{\textcolor{white}{0.269}} & 6.17 & \cellcolor[HTML]{52C569}{\textcolor{white}{0.269}}\\
\textbf{} & 2033 & \cellcolor[HTML]{9A9377}{\textcolor{white}{2444}} & 6.28 & \cellcolor[HTML]{50C46A}{\textcolor{white}{0.274}} & 6.28 & \cellcolor[HTML]{50C46A}{\textcolor{white}{0.274}} & 6.28 & \cellcolor[HTML]{50C46A}{\textcolor{white}{0.274}}\\
\textbf{} & 2034 & \cellcolor[HTML]{999278}{\textcolor{white}{2462}} & 6.38 & \cellcolor[HTML]{4EC36B}{\textcolor{white}{0.279}} & 6.38 & \cellcolor[HTML]{4EC36B}{\textcolor{white}{0.279}} & 6.38 & \cellcolor[HTML]{4EC36B}{\textcolor{white}{0.279}}\\
\bottomrule
\end{tabular}
\end{table}

0 comments on commit d9b3dc1

Please sign in to comment.