Adding functions to produce impact, coverage and fvp plots #15
Adding functions to produce impact, coverage and fvp plots #15zegibney wants to merge 2 commits intovimc:developfrom
Conversation
…ots central impact estimates, coverage and fvps. Associated documentation also added from attempts at roxygen!
|
Hi @zegibney - I'm taking a look at the code now. Just a few preliminary points:
|
pratikunterwegs
left a comment
There was a problem hiding this comment.
I've just added some comments here. We should probably think of a testing strategy as well, but that's a bit bound up with the overall issue of how to provide data for testing as in PR #14.
| @@ -0,0 +1,240 @@ | |||
| #' Plot central impact estimates by cohort and year. | |||
| #' TODO: need to add importFrom ... to avoid package issues with testing? | |||
There was a problem hiding this comment.
The way @importFrom is used is to import functions from packages that are used repeatedly, really only to avoid writing out the explicit namespacing each time (i.e., ggplot2::fn()). Tests might fail if you neither import nor explicitly namespace a function, but the tag isn't otherwise linked to test failures.
| #' plot_impact( | ||
| #' data = impact_data, | ||
| #' burden_type = "cases", | ||
| #' title = "Cases averted", | ||
| #' view = "year" | ||
| #' ) |
There was a problem hiding this comment.
This example will fail as impact_data hasn't been defined. Each @example block is standalone, so you'll need to create impact_data here.
| data, | ||
| burden_type, | ||
| title, | ||
| view |
There was a problem hiding this comment.
The convention for a small number of fixed argument options is to display them in the function signature (i.e., this bit).
So prefer f = function(arg = c("option_1", "option_2")). This way when users pull up the help page they can immediately see the valid options.
This should be paired with some argument checking which you already have. checkmate::assert_choice() is fine; in {vimcheck} I've gone with rlang::arg_match() whose error messages are a bit more informative.
| view | ||
| ){ | ||
| checkmate::assert_tibble(data, min.rows = 1L, min.cols = 1L) | ||
| checkmate::assert_character(burden_type, len = 1) |
There was a problem hiding this comment.
Prefer using rlang::arg_match() here too. If keeping assert_choice(), this assert_character() isn't needed. assert_character(..., len = 1) is better replaced with assert_string() (in other use cases, can simply be removed here).
| ) | ||
|
|
||
| Impact <- | ||
| data %>% |
There was a problem hiding this comment.
So I would recommend against using pipes in functions, because any errors that occur can only be traced to the top of the pipeline (here, L. 44), and not to the line where they actually are. It's tedious, but it's better to replace each step in the pipeline with an explicit assignment step.
| #' \item{fvps}{A plot of fully vaccinated persons over time.} | ||
| #' } | ||
| #' @examples | ||
| #' plots <- plot_coverage_fvps(fvps) |
There was a problem hiding this comment.
Need to define the data fvps.
| title, | ||
| view | ||
| ){ | ||
| checkmate::assert_tibble(data, min.rows = 1L, min.cols = 1L) |
There was a problem hiding this comment.
Might be good to check that data has at least as many cols as are used in this fn; would also be good to add checks on the specific column names. See e.g. fns in R/fn_plotting_prep_bur_diag.R which use checkmate::assert_names() for this.
| plot_coverage_fvps <- function(fvps){ | ||
| checkmate::assert_tibble(fvps, min.rows = 1L, min.cols = 1L) |
There was a problem hiding this comment.
Pretty much the same general suggestions as for the preceding fn.
| dplyr::select( | ||
| .data$country, | ||
| .data$vaccine_delivery, | ||
| .data$year, | ||
| .data$coverage_adjusted) %>% |
There was a problem hiding this comment.
I'm not sure how advisable this syntax is. I would define the cols to select as a character vector and pass them to select using all_of() or {{ }}.
| .data$vaccine_delivery, | ||
| .data$year, | ||
| .data$coverage_adjusted) %>% | ||
| dplyr::rename(coverage = .data$coverage_adjusted) |
There was a problem hiding this comment.
Here it might be safer to pass a named vector giving the renaming map. See 2nd example in rename docs: https://dplyr.tidyverse.org/reference/rename.html
This PR adds additional functions from [https://github.com/vimc/montagu-reports/tree/main/src/rapid-model-run-impact] a key report for the VIMC Science & Policy team.
Functions added generate central estimate impact plots and coverage and fvp plots per year, country and view.
First attempt at ensuring functions are package-ready/ utilising roxygen etc. so any comment welcome.
TODO's in script
fn_plotting_impact.Rwith specific questions. I understand testing may need to be stronger/ the next step