Skip to content

Commit 7ef6a71

Browse files
Update docs, add example WPP data
1 parent 3c1e9fa commit 7ef6a71

12 files changed

Lines changed: 147 additions & 38 deletions

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
^tools$
1616
^scratch\.R$
1717
^scratch$
18+
^data-raw$

DESCRIPTION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ Imports:
4040
readr,
4141
rlang,
4242
tidyr
43+
Depends:
44+
R (>= 3.5)
45+
LazyData: true

NAMESPACE

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
export(basic_burden_sanity)
44
export(check_demography_alignment)
5-
export(check_template_alignment)
6-
export(const_data_colnames)
5+
export(file_dict_colnames)
76
export(impact_check)
87
export(plot_age_patterns)
98
export(plot_compare_demography)
@@ -15,4 +14,6 @@ export(theme_vimc)
1514
export(transfrom_coverage_fvps)
1615
export(validate_complete_incoming_files)
1716
export(validate_file_dict_template)
17+
export(validate_template_alignment)
18+
importFrom(dplyr,.data)
1819
importFrom(ggplot2,ggplot)

R/burden_diagnositics.R

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,25 @@ validate_complete_incoming_files <- function(
207207
#'
208208
#' @description
209209
#'
210-
#' @param burden_set
210+
#' @param burden_set A `<data.frame>` of modeller-provided burden-set data.
211211
#'
212-
#' @param template
212+
#' @param template A `<data.frame>` of the burden template as provided to
213+
#' modelling groups by VIMC.
213214
#'
214-
#' @return
215+
#' @return A named list of checks carried out on `burden_set` to comapre it
216+
#' against `template`, with information on missing and extra data.
215217
#'
216218
#' @examples
217219
#'
218220
#' @keywords diagnostics
219221
#'
220222
#' @export
221-
check_template_alignment <- function(burden_set, template) {
222-
# TODO: figure out what the args are expected to be: dfs? lists, vecs?
223-
expected <- names(template)
224-
provided <- names(burden_set)
223+
validate_template_alignment <- function(burden_set, template) {
224+
checkmate::assert_data_frame(burden_set)
225+
checkmate::assert_data_frame(template)
226+
227+
expected <- colnames(template)
228+
provided <- colnames(burden_set)
225229

226230
missing_cols_in_burden <- setdiff(expected, provided)
227231
extra_cols_in_burden <- setdiff(provided, expected)
@@ -248,8 +252,8 @@ check_template_alignment <- function(burden_set, template) {
248252

249253
# TODO: if these are data.frames, this might not be the best way to check
250254
# for differences
251-
missing_grid_in_burden <- setdiff(template_grid, burden_grid)
252-
extra_grid_in_burden <- setdiff(burden_grid, template_grid)
255+
missing_grid_in_burden <- dplyr::setdiff(template_grid, burden_grid)
256+
extra_grid_in_burden <- dplyr::setdiff(burden_grid, template_grid)
253257
burden_grid_matches_template <- all(
254258
c(
255259
nrow(missing_grid_in_burden),
@@ -283,30 +287,35 @@ check_template_alignment <- function(burden_set, template) {
283287
#' @keywords diagnostics
284288
#'
285289
#' @export
286-
check_demography_alignment <- function(burden_set, wpp, gender = "both") {
290+
check_demography_alignment <- function(
291+
burden_set,
292+
wpp,
293+
gender = c("Both", "Male", "Female")
294+
) {
287295
# TODO: input checks
296+
checkmate::assert_data_frame(burden_set)
297+
checkmate::assert_data_frame(wpp)
298+
299+
gender <- rlang::arg_match(gender)
288300

289-
# TODO: check if these can be made constants
290301
cols_to_select <- c("country", "year", "age", "cohort_size")
291302
provided <- dplyr::select(
292303
burden_set,
293304
{{ cols_to_select }}
294305
)
295306
provided <- dplyr::mutate(
296-
provided = cohort_size # check if this can be made a string const
307+
provided,
308+
provided = cohort_size
297309
)
298310

299311
# TODO: explain what expected is
300312
# TODO: replace with a right-join?
301313
expected <- dplyr::filter(
302314
wpp,
303-
country %in%
304-
provided$country &
305-
year %in% provided$year &
306-
age %in% provided$age,
307315
gender == {{ gender }}
308316
)
309317

318+
# in case there are many extra cols
310319
cols_to_select <- c("country", "year", "age", "value")
311320
expected <- dplyr::select(
312321
expected,
@@ -315,7 +324,7 @@ check_demography_alignment <- function(burden_set, wpp, gender = "both") {
315324
expected <- dplyr::rename(
316325
expected,
317326
expected = value
318-
) # TODO: prefer not to use NSE
327+
)
319328

320329
# return left join
321330
alignment <- dplyr::left_join(

R/eg_wpp.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#' Example of UN-WPP time-series data
2+
#'
3+
#' An example of the population estimate data used by VIMC.
4+
#'
5+
#' @format ## `eg_wpp`
6+
#' A data frame with 65,448 rows and 5 columns:
7+
#' \describe{
8+
#' \item{country}{Country name; this is a placeholder name.}
9+
#' \item{year}{Year}
10+
#' \item{age}{Age}
11+
#' \item{gender}{Sex given as three categories, "Male", "Female", or "Both"}.
12+
#' \item{value}{Population size}
13+
#' }
14+
#' @source Derived from data originally prepared by the United Nations as part
15+
#' of the World Population Prospects: <https://population.un.org/wpp/>.
16+
"eg_wpp"

data-raw/eg_wpp.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## code to prepare `eg_wpp` dataset goes here
2+
3+
# this code creates a template file that conforms to current VIMC-used
4+
# population estimate format from the UN-WPP https://population.un.org/wpp/
5+
6+
library(tidyr)
7+
8+
country <- "RFP"
9+
10+
gender <- c("Male", "Female", "Both") # taken from existing reports
11+
12+
year_start <- 1885
13+
year_end <- 2100
14+
year <- seq(year_start, year_end)
15+
16+
value <- 10e6 # assuming a constant, medium-size pop. value
17+
18+
age_min <- 0
19+
age_max <- 100
20+
age <- seq(age_min, age_max)
21+
22+
eg_wpp <- crossing(
23+
country = country,
24+
year = year,
25+
age = age,
26+
gender = gender,
27+
value = value
28+
)
29+
30+
# NOTE that this table has more entries than seen in reports
31+
# as historical estimates are not available for all age groups
32+
33+
usethis::use_data(eg_wpp, overwrite = TRUE)

data/eg_wpp.rda

1.9 KB
Binary file not shown.

man/check_template_alignment.Rd

Lines changed: 0 additions & 17 deletions
This file was deleted.

man/constants.Rd

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/eg_wpp.Rd

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)