pfmc-assessments / sa4ss

Generate a stock assessment document from Stock Synthesis output
https://pfmc-assessments.github.io/sa4ss/
Other
12 stars 7 forks source link

Develop shared functions for making tables #78

Open iantaylor-NOAA opened 1 year ago

iantaylor-NOAA commented 1 year ago

Problem

U.S. West Coast groundfish assessment reports all use the same Terms of Reference which require the same tables. However, there have not been any shared functions developed to create most of the tables. Meeting Section 508 accessibility requirements will be easier with some changes to our tables and these changes will be easier if we share the effort.

Proposal

Write some new generalized functions for making standard tables. There are a bunch of functions that @kellijohnson-NOAA wrote for the 2021 lingcod assessments in https://github.com/pfmc-assessments/lingcod/tree/main/R that could be a starting place (just look for files with the name R/table_*). The other 2021 assessment repositories also have table-making code. Another useful reference is this spreadsheet of tables created by @chantelwetzel-noaa https://docs.google.com/spreadsheets/d/1NSFVKXfOLz0xQ_q7HsvxUmWY0778Os8O1kOwqf4kCAw/, which was the origin of Appendix I in our Terms of Reference.

I don't know if these functions would be better added to {sa4ss} or a separate repository, and we don't need to have functions for 100% of the tables, but I think some shared functions would save a lot of time and angst and result in better and more consistent assessments.

I'm assigning myself and @chantelwetzel-noaa who just volunteered to look at the exec summary table functions (which is an example of shared functions that we are already using). @kellijohnson-NOAA, please comment on where you think stuff like this should go and if you have time or interest in helping with the code, that's welcome too.

Note that there's an r4ss issue related to this need as well, https://github.com/r4ss/r4ss/issues/8, but it's been in place since 2014 without much progress, and the Rmarkdown/LaTeX-needs of U.S. west coast groundfish assessments make me think that something the pfmc-assessments organization is the better place to house these functions.

List of functions that could be added [edited at various times to add more detail and additional functions]

chantelwetzel-noaa commented 1 year ago

Thank you for starting this discussion @iantaylor-NOAA. Currently, sa4ss has some function that help users create tex tables via the es_table_tex() function which can work on a csv file where there is a column for file location to create the tex table and table caption text by calling the formatting table_format() function. This approach allows users to only make a master csv file for all the various csv file located across a users computer rather than having to move each file into a document table folder. I suspect the function @kellijohnson-NOAA created for lingcod has some nicer bells and whistles for table formatting but I suspect we could merge the two existing files into a single one for users.

kellijohnson-NOAA commented 1 year ago

I think it would be great to store these in {sa4ss}. Then we could load the simple model from {r4ss} as a default for testing and for examples with the template.

iantaylor-NOAA commented 1 year ago

I started working on this in https://github.com/pfmc-assessments/sa4ss/tree/table_functions_78 . So far it's just the table of parameters, modified slightly from the lingcod version to fill in the blank cells based on guidance in #87.

Based on input from @kellijohnson-NOAA about long tables split across pages causing problems for accessibility, I added the a wrapper function table_pars_split() which splits the table created by table_pars() into one table per page, where the following commands in 52tables.Rmd produced 16 tables as shown in the snippets below.

```{r tables-model, results = "asis"}
cat("\\begin{landscape}")
table_pars_split(mod_base)
cat("\\end{landscape}")
```

It's on the user to figure out how many tables get produced and add text like Parameters values are shown in Tables \ref{tab:table-pars-base-1}-\ref{tab:table-pars-base-16}.

Here's the top of the first table (useful to create this early in the process to see that there are some unnecessary priors on fixed parameters): image Here's the last table: image

@kellijohnson-NOAA, I don't know how quickly I'll be able to add more functions. Do you want this branch to stay separate until we have lots of table functions in it or should I create pull requests along the way as useful pieces get added?

kellijohnson-NOAA commented 1 year ago

@iantaylor-NOAA this is great stuff, 🙏 thank you for working on this. I would like to merge it in next week when I am back from leave rather than waiting for more tables. Really quick, I see two things that we might want to change.

iantaylor-NOAA commented 1 year ago

Thanks for the input @kellijohnson-NOAA (though better to truly be on leave next time).

I don't know why sprintf("(%8.3f, %8.3f)", Min, Max) was adding a space, but I fixed it via %>% stringr::str_replace("\\(\\s+", "(") and then changed my mind and applied signif() instead (via a new signif_string() function added to utils.R).

The resulting tables looks like this, which seems very readable to my eyes at least: image

iantaylor-NOAA commented 1 year ago

As I've been adding tables for the petrale report, I've been adding to a checklist at the top of this issue of which table-making functions might be useful to re-use either from lingcod or those I've modified for petrale.

For example, I just modified the table_compweight() function written by @kellijohnson-NOAA for lingcod to add some additional columns and show CAAL as distinct from Age comps. I think the information on the mean and total sample sizes is helpful to understand the influence of these data on the model.

The function is in https://github.com/pfmc-assessments/petrale/blob/main/R/table_compweight.R and gets called in 52tables.Rmd with a command as below to create the following table:

table_compweight(mod_base, 
  caption_extra = paste("The WCGBTS age comps are conditioned on length,",
   "so there are more observations with fewer samples per observation.")
)

image

brianlangseth-NOAA commented 1 year ago

Ive updated @iantaylor-NOAA function here and here to allow output as a data.frame for instances where table manipulation is helpful and we dont have to edit tex tables!

iantaylor-NOAA commented 1 year ago

I simplified the lingcod decision table function and added it to the petrale repo in https://github.com/pfmc-assessments/petrale/commit/d9b3dc1ac658bb838d8eb16497c68f7741a5cf28 along with the script to call it and the entry in the exec summary. The result is pasted below. I'm sure the code could be improved in many ways, but this should work for any set of 9 models populating a 3x3 decision table. It should work with just 1 or 2 catch streams as well, but is would break if you don't have 3 states of nature. image