diff --git a/DESCRIPTION b/DESCRIPTION index f27e81b..7a48056 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: reskit Title: Nice selection box of goodies for hobnobbing with NHP model results -Version: 1.0.2 +Version: 1.1.0 Authors@R: c(person( "Fran", "Barton", diff --git a/R/change_factor_data.R b/R/change_factor_data.R index 8cd9b9d..49f5ffb 100644 --- a/R/change_factor_data.R +++ b/R/change_factor_data.R @@ -141,6 +141,7 @@ prepare_principal_cf_data <- function( tpma_lookup, include_baseline ) { + tpma_lookup <- dplyr::select(tpma_lookup, c("strategy", "tpma_label")) bsline_filtered <- dplyr::filter(dat, .data[["change_factor"]] != "baseline") dat_prepared <- if (include_baseline) dat else bsline_filtered dat_prepared |> diff --git a/R/demo_results_tables.R b/R/demo_results_tables.R new file mode 100644 index 0000000..756a7c2 --- /dev/null +++ b/R/demo_results_tables.R @@ -0,0 +1,370 @@ +#' Function to create a "default" table with synthetic values +#' +#' The table contains synthetic (randomly created) values for a selection of +#' PODs and measures, across 2 "sites", as if created by 64 model runs +#' @param seed integer, for reproducibility of outputs, a seed value to be used +#' @keywords internal +create_demo_default_tbl <- function(seed) { + init_tbl1 <- create_base_tbl_with_sitetret("site1") + init_tbl2 <- create_base_tbl_with_sitetret("site2") + baseline_tbl1 <- add_baseline_values(init_tbl1, c(5e3L, 85e3L), 1e3L, seed) + baseline_tbl2 <- add_baseline_values(init_tbl2, c(3e3L, 45e3L), 1e3L, seed) + initial_means <- get_full_initial_means(0.8, 0.9, 1) + horizon_tbl1 <- add_horizon_values(baseline_tbl1, initial_means, seed) + horizon_tbl2 <- add_horizon_values(baseline_tbl2, initial_means, seed) + dplyr::bind_rows(horizon_tbl1, horizon_tbl2) |> + dplyr::arrange(dplyr::pick(c("pod", "sitetret", "measure", "model_run"))) +} + + +#' Function to create a "tretspef + LoS group" table with synthetic values +#' The table contains synthetic (randomly created) values for a selection of +#' PODs, tretspefs, LoS groups and measures, across 2 "sites", as if created +#' by 64 model runs +#' @inheritParams create_demo_default_tbl +#' @keywords internal +create_demo_tretspef_losgroup_tbl <- function(seed) { + tretspef_codes <- sort(sample(get_tretspef_lookup()[["code"]], 25)) |> + withr::with_seed(seed = seed) + los_groups <- paste0( + c("0", "1", "2", "3", "4-7", "8-14", "15-21", "22+"), + " days" + ) |> + sub("^1 days$", "1 day", x = _) + init_tbl1 <- create_ip_base_tbl() |> + dplyr::mutate( + sitetret = "site1", + tretspef = list(tretspef_codes), + los_group = list(los_groups), + .after = "pod" + ) |> + tidyr::unnest_longer("tretspef") |> + tidyr::unnest_longer("los_group") |> + tidyr::expand( + tidyr::nesting(pod, measure, sitetret), + tretspef, + los_group + ) |> + dplyr::relocate("measure", .after = dplyr::last_col()) + init_tbl2 <- dplyr::mutate(init_tbl1, sitetret = "site2") + baseline_tbl1 <- add_baseline_values(init_tbl1, c(100L, 5e3L), 100L, seed) + baseline_tbl2 <- add_baseline_values(init_tbl2, c(100L, 2e3L), 100L, seed) + horizon_tbl1 <- add_horizon_values(baseline_tbl1, 0.85, seed) + horizon_tbl2 <- add_horizon_values(baseline_tbl2, 0.85, seed) + dplyr::bind_rows(horizon_tbl1, horizon_tbl2) |> + dplyr::arrange(dplyr::pick(c( + "pod", + "sitetret", + "tretspef", + "los_group", + "measure", + "model_run" + ))) +} + + +#' Function to create a "tretspef + age group" table with synthetic values +#' The table contains synthetic (randomly created) values for a selection of +#' PODs, tretspefs, age groups and measures, across 2 "sites", as if created +#' by 64 model runs +#' @inheritParams create_demo_default_tbl +#' @keywords internal +create_demo_sex_agegroup_tbl <- function(seed) { + # fmt: skip + age_groups <- c( + "0", "1-4", "10-15", "16-17", "18-34", "35-49", "5-9", + "50-64", "65-74", "75-84", "85+" + ) + init_tbl1 <- create_base_tbl_with_sitetret("site1") |> + dplyr::mutate( + sex = list(seq(2)), + age_group = list(age_groups), + .after = "sitetret" + ) |> + tidyr::unnest_longer("sex") |> + tidyr::unnest_longer("age_group") |> + tidyr::expand(tidyr::nesting(pod, measure, sitetret), sex, age_group) |> + dplyr::relocate("measure", .after = dplyr::last_col()) + init_tbl2 <- dplyr::mutate(init_tbl1, sitetret = "site2") + baseline_tbl1 <- add_baseline_values(init_tbl1, c(2e3L, 5e4L), 100L, seed) + baseline_tbl2 <- add_baseline_values(init_tbl2, c(1e3L, 2e4L), 100L, seed) + multip <- length(seq(2)) * length(age_groups) + initial_values <- rep(get_full_initial_means(0.9, 0.85, 0.8), each = multip) + horizon_tbl1 <- add_horizon_values(baseline_tbl1, initial_values, seed) + horizon_tbl2 <- add_horizon_values(baseline_tbl2, initial_values, seed) + dplyr::bind_rows(horizon_tbl1, horizon_tbl2) |> + dplyr::arrange(dplyr::pick(c( + "pod", + "sitetret", + "sex", + "age_group", + "measure", + "model_run" + ))) +} + + +#' Function to create a "sex + tretspef_grouped" table with synthetic values +#' The table contains synthetic (randomly created) values for 2 sexes, a +#' selection of tretspef group codes, and measures, across 2 "sites", as if +#' created by 64 model runs +#' @inheritParams create_demo_default_tbl +#' @keywords internal +create_demo_sex_tretspef_tbl <- function(seed) { + # fmt: skip + tretspef_groups <- c( + "100", "101", "110", "120", "130", "140", "150", "160", "170", + "300", "301", "320", "330", "340", "400", "410", "430", "Other" + ) + init_tbl1 <- list(create_ip_base_tbl(), create_op_base_tbl()) |> + purrr::list_rbind() |> + dplyr::mutate( + sitetret = "site1", + sex = list(seq(2)), + tretspef_grouped = list(tretspef_groups), + .after = "pod" + ) |> + tidyr::unnest_longer("sex") |> + tidyr::unnest_longer("tretspef_grouped") + init_tbl2 <- dplyr::mutate(init_tbl1, sitetret = "site2") + baseline_tbl1 <- add_baseline_values(init_tbl1, c(500L, 4.5e3L), 100L, seed) + baseline_tbl2 <- add_baseline_values(init_tbl2, c(100L, 2.2e3L), 100L, seed) + multip <- length(seq(2)) * length(tretspef_groups) + initial_values <- rep(get_ipop_initial_means(0.8, 0.85), each = multip) + horizon_tbl1 <- add_horizon_values(baseline_tbl1, initial_values, seed) + horizon_tbl2 <- add_horizon_values(baseline_tbl2, initial_values, seed) + dplyr::bind_rows(horizon_tbl1, horizon_tbl2) |> + dplyr::arrange(dplyr::pick(c( + "pod", + "sitetret", + "sex", + "tretspef_grouped", + "measure", + "model_run" + ))) +} + + +#' Function to create a "step_counts" table with synthetic values +#' The table contains synthetic (randomly created) values for a selection of +#' 64 model runs, providing random baselines, random sizes of mitigated +#' activity across a subset of TPMAs, and random values for demographic growth +#' @inheritParams create_demo_default_tbl +#' @keywords internal +create_demo_stepcounts_tbl <- function(seed) { + tpma_lookup <- get_tpma_label_lookup(TRUE) + # for this demo table, we can just include a subset of all strategies + n_strategies <- 40 + strategies <- dplyr::slice_sample(tpma_lookup, n = n_strategies) |> + withr::with_seed(seed = seed) |> + dplyr::select(!"tpma_label") + core_cols <- c("sitetret", "pod", "measure") + init_tbl1 <- create_base_tbl_with_sitetret("site1") |> + dplyr::mutate(dplyr::across("measure", \(x) { + dplyr::if_else(x %in% c("walk-in", "ambulance"), "arrivals", x) + })) |> + dplyr::distinct() |> + dplyr::filter_out(.data[["measure"]] == "procedures") |> + get_activity_type_from_pod() |> + dplyr::inner_join( + strategies, + by = "activity_type", + relationship = "many-to-many" + ) |> + dplyr::filter_out( + dplyr::when_any( + .data[["measure"]] == "admissions" & + .data[["change_factor"]] == "efficiencies", + .data[["measure"]] == "tele_attendances" & + grepl("^convert_to_tele", .data[["strategy"]]) + ) + ) |> + tidyr::nest(.by = c("activity_type", tidyselect::all_of(core_cols))) + init_tbl2 <- dplyr::mutate(init_tbl1, sitetret = "site2") + init_tbl <- dplyr::bind_rows(init_tbl1, init_tbl2) + demo_data <- create_demo_default_tbl(seed) |> + dplyr::mutate(dplyr::across("measure", \(x) { + dplyr::if_else(x %in% c("walk-in", "ambulance"), "arrivals", x) + })) |> + dplyr::summarise(dplyr::across("value", sum), .by = !"value") + baseline_data <- demo_data |> + dplyr::filter(.data[["model_run"]] == 0L) |> + dplyr::select(!"model_run") |> + dplyr::rename(baseline = "value") + horizon_data <- demo_data |> + dplyr::filter(.data[["model_run"]] > 0L) |> + dplyr::rename(horizon = "value") + baseline_tbl <- init_tbl |> + dplyr::left_join(baseline_data, core_cols) |> + dplyr::left_join(horizon_data, core_cols) |> + dplyr::mutate(mitigation = .data[["baseline"]] - .data[["horizon"]]) + dgf_values <- withr::with_seed(seed, rnorm(nrow(baseline_tbl), 1, 0.1)) + dgf_values <- dgf_values * (baseline_tbl[["baseline"]] * 0.01) + impact_tbl <- baseline_tbl |> + dplyr::mutate( + demographic_adjustment = dgf_values, + impact_data = purrr::map2( + .data[["data"]], + .data[["mitigation"]], + \(x, y) create_sample_impacts(x, y, seed) + ) + ) + cfs <- c("baseline", "demographic_adjustment", "model_interaction_term") + id_cols <- c("activity_type", core_cols, "model_run", "change_factor") + impact_tbl |> + dplyr::select(!c("data")) |> + tidyr::nest( + horizon_data = c( + "model_run", + "horizon", + "mitigation", + "demographic_adjustment" + ) + ) |> + dplyr::mutate(dplyr::across("horizon_data", \(x) { + purrr::map2(x, .data[["impact_data"]], \(x, y) { + dplyr::inner_join(x, y, "model_run") + }) + })) |> + dplyr::select(!"impact_data") |> + tidyr::unnest("horizon_data") |> + dplyr::mutate( + total_mitigation = sum(.data[["value"]]), + model_interaction_term = .data[["mitigation"]] - + (.data[["total_mitigation"]] + .data[["demographic_adjustment"]]), + .by = tidyselect::all_of(c(core_cols, "model_run")) + ) |> + dplyr::select(!c("horizon", "mitigation", "total_mitigation")) |> + tidyr::pivot_wider(names_from = "strategy") |> + tidyr::pivot_longer( + !tidyselect::all_of(id_cols), + names_to = "strategy", + values_drop_na = TRUE + ) |> + dplyr::mutate( + dplyr::across("change_factor", \(x) { + dplyr::if_else(.data[["strategy"]] %in% cfs, .data[["strategy"]], x) + }), + dplyr::across("strategy", \(x) dplyr::if_else(x %in% cfs, "-", x)) + ) +} + + +#' Helper function +#' @keywords internal +create_sample_impacts <- function(dat, mitigation, seed, picks = 64) { + n_rows <- nrow(dat) + n_zeros <- round(0.2 * n_rows * picks) + n_picks <- (n_rows * picks) - n_zeros + mitigation_mean <- mitigation / n_rows + mitigation_dist <- withr::with_seed(seed, rnorm(n_picks, 1, 0.1)) + mitigation_dist <- mitigation_dist * mitigation_mean + + impacts <- abs(sample(c(rep(0, n_zeros), mitigation_dist), n_rows * picks)) |> + withr::with_seed(seed = seed) + impacts_tbl <- tibble::tibble( + rn = rep(seq(n_rows), each = picks), + model_run = rep(seq(picks), n_rows), + value = impacts + ) + dat |> + dplyr::mutate(rn = dplyr::row_number()) |> + dplyr::left_join(impacts_tbl, "rn") |> + dplyr::select(!"rn") +} + + +#' Helper function +#' @param site A string giving the name/code of an imaginary site +#' @keywords internal +create_base_tbl_with_sitetret <- function(site) { + list(create_ae_base_tbl(), create_ip_base_tbl(), create_op_base_tbl()) |> + purrr::list_rbind() |> + dplyr::mutate(sitetret = {{ site }}, .after = "pod") +} + + +#' Helper function +#' @keywords internal +add_baseline_values <- function(base_tbl, baseline_range, step = 100L, seed) { + stopifnot(length(baseline_range) == 2) + baseline_range <- as.integer(baseline_range) + step <- as.integer(step) + poss_baselines <- seq(min(baseline_range), max(baseline_range), step) + baseline_values <- sample(poss_baselines, nrow(base_tbl), TRUE) |> + withr::with_seed(seed = seed) + dplyr::mutate(base_tbl, model_run = 0L, value = baseline_values) +} + + +#' Helper function +#' @keywords internal +add_horizon_values <- function(tbl_with_baseline, initial_means, seed) { + distrib_values <- rnorm(nrow(tbl_with_baseline), 1, 0.1) |> + withr::with_seed(seed = seed) + mitigations <- distrib_values * initial_means + ghv_partial <- purrr::partial(generate_horizon_values, seed = seed) + horizons_tbl <- tbl_with_baseline |> + dplyr::select(!"model_run") |> + dplyr::mutate( + dplyr::across("value", \(x) purrr::map2(x, mitigations, ghv_partial)) + ) |> + tidyr::unnest_longer("value", indices_to = "model_run") |> + dplyr::mutate(dplyr::across("value", \(x) as.integer(round(x)))) + dplyr::bind_rows(tbl_with_baseline, horizons_tbl) +} + +#' Helper function +#' @keywords internal +generate_horizon_values <- function(baseline, mitigation, seed, picks = 64) { + distr <- withr::with_seed(seed, rnorm(picks, 1, 0.1)) + as.integer(round(distr * mitigation * baseline)) +} + + +#' Helper function +#' @keywords internal +create_ae_base_tbl <- function() { + ae_pods <- paste0("aae_type-0", seq(5)) + ae_measures <- c("ambulance", "walk-in") + tidyr::expand_grid(pod = ae_pods, measure = ae_measures) +} + +#' Helper function +#' @keywords internal +create_ip_base_tbl <- function() { + ip_pods <- c( + paste0("ip_", c("elective", "maternity", "non-elective"), "_admission"), + paste0("ip_regular_", c("night", "day"), "_attender") + ) + ip_measures <- c("admissions", "beddays", "procedures") + tidyr::expand_grid(pod = ip_pods, measure = ip_measures) +} + +#' Helper function +#' @keywords internal +create_op_base_tbl <- function() { + op_pods <- paste0("op_", c("first", "follow-up", "procedure")) + op_measures <- paste0(c("", "tele_"), "attendances") + tidyr::expand_grid(pod = op_pods, measure = op_measures) +} + +#' Helper function +#' @keywords internal +get_full_initial_means <- function(ae_mean, ip_mean, op_mean) { + c( + rep(ae_mean, nrow(create_ae_base_tbl())), + rep(ip_mean, nrow(create_ip_base_tbl())), + rep(op_mean, nrow(create_op_base_tbl())) + ) +} + + +#' Helper function +#' @keywords internal +get_ipop_initial_means <- function(ip_mean, op_mean) { + c( + rep(ip_mean, nrow(create_ip_base_tbl())), + rep(op_mean, nrow(create_op_base_tbl())) + ) +} diff --git a/R/tables.R b/R/tables.R index 823e0a9..88d30b9 100644 --- a/R/tables.R +++ b/R/tables.R @@ -28,7 +28,7 @@ make_principal_los_table <- function(principal_los_data) { } -#' Create a gt table with data from compile_detailed_activity_data() +#' Create a gt table with data from [compile_detailed_activity_data] #' #' @param detailed_activity_data A suitable tibble #' @param final_year string. The horizon year eg "2042/43" @@ -50,7 +50,7 @@ make_detailed_activity_table <- function(detailed_activity_data, final_year) { } -#' Create a gt table with data from compile_distribution_summary_data() +#' Create a gt table with data from [compile_distribution_summary_data] #' #' @param distr_summary_data A suitable tibble #' @returns A gt table diff --git a/man/add_baseline_values.Rd b/man/add_baseline_values.Rd new file mode 100644 index 0000000..0a8a404 --- /dev/null +++ b/man/add_baseline_values.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{add_baseline_values} +\alias{add_baseline_values} +\title{Helper function} +\usage{ +add_baseline_values(base_tbl, baseline_range, step = 100L, seed) +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/add_horizon_values.Rd b/man/add_horizon_values.Rd new file mode 100644 index 0000000..ef8b4e9 --- /dev/null +++ b/man/add_horizon_values.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{add_horizon_values} +\alias{add_horizon_values} +\title{Helper function} +\usage{ +add_horizon_values(tbl_with_baseline, initial_means, seed) +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/create_ae_base_tbl.Rd b/man/create_ae_base_tbl.Rd new file mode 100644 index 0000000..718f41f --- /dev/null +++ b/man/create_ae_base_tbl.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_ae_base_tbl} +\alias{create_ae_base_tbl} +\title{Helper function} +\usage{ +create_ae_base_tbl() +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/create_base_tbl_with_sitetret.Rd b/man/create_base_tbl_with_sitetret.Rd new file mode 100644 index 0000000..b310303 --- /dev/null +++ b/man/create_base_tbl_with_sitetret.Rd @@ -0,0 +1,15 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_base_tbl_with_sitetret} +\alias{create_base_tbl_with_sitetret} +\title{Helper function} +\usage{ +create_base_tbl_with_sitetret(site) +} +\arguments{ +\item{site}{A string giving the name/code of an imaginary site} +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/create_demo_default_tbl.Rd b/man/create_demo_default_tbl.Rd new file mode 100644 index 0000000..b0d0533 --- /dev/null +++ b/man/create_demo_default_tbl.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_demo_default_tbl} +\alias{create_demo_default_tbl} +\title{Function to create a "default" table with synthetic values} +\usage{ +create_demo_default_tbl(seed) +} +\arguments{ +\item{seed}{integer, for reproducibility of outputs, a seed value to be used} +} +\description{ +The table contains synthetic (randomly created) values for a selection of +PODs and measures, across 2 "sites", as if created by 64 model runs +} +\keyword{internal} diff --git a/man/create_demo_sex_agegroup_tbl.Rd b/man/create_demo_sex_agegroup_tbl.Rd new file mode 100644 index 0000000..37c4afd --- /dev/null +++ b/man/create_demo_sex_agegroup_tbl.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_demo_sex_agegroup_tbl} +\alias{create_demo_sex_agegroup_tbl} +\title{Function to create a "tretspef + age group" table with synthetic values +The table contains synthetic (randomly created) values for a selection of +PODs, tretspefs, age groups and measures, across 2 "sites", as if created +by 64 model runs} +\usage{ +create_demo_sex_agegroup_tbl(seed) +} +\arguments{ +\item{seed}{integer, for reproducibility of outputs, a seed value to be used} +} +\description{ +Function to create a "tretspef + age group" table with synthetic values +The table contains synthetic (randomly created) values for a selection of +PODs, tretspefs, age groups and measures, across 2 "sites", as if created +by 64 model runs +} +\keyword{internal} diff --git a/man/create_demo_sex_tretspef_tbl.Rd b/man/create_demo_sex_tretspef_tbl.Rd new file mode 100644 index 0000000..6128283 --- /dev/null +++ b/man/create_demo_sex_tretspef_tbl.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_demo_sex_tretspef_tbl} +\alias{create_demo_sex_tretspef_tbl} +\title{Function to create a "sex + tretspef_grouped" table with synthetic values +The table contains synthetic (randomly created) values for 2 sexes, a +selection of tretspef group codes, and measures, across 2 "sites", as if +created by 64 model runs} +\usage{ +create_demo_sex_tretspef_tbl(seed) +} +\arguments{ +\item{seed}{integer, for reproducibility of outputs, a seed value to be used} +} +\description{ +Function to create a "sex + tretspef_grouped" table with synthetic values +The table contains synthetic (randomly created) values for 2 sexes, a +selection of tretspef group codes, and measures, across 2 "sites", as if +created by 64 model runs +} +\keyword{internal} diff --git a/man/create_demo_stepcounts_tbl.Rd b/man/create_demo_stepcounts_tbl.Rd new file mode 100644 index 0000000..2ac9fea --- /dev/null +++ b/man/create_demo_stepcounts_tbl.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_demo_stepcounts_tbl} +\alias{create_demo_stepcounts_tbl} +\title{Function to create a "step_counts" table with synthetic values +The table contains synthetic (randomly created) values for a selection of +64 model runs, providing random baselines, random sizes of mitigated +activity across a subset of TPMAs, and random values for demographic growth} +\usage{ +create_demo_stepcounts_tbl(seed) +} +\arguments{ +\item{seed}{integer, for reproducibility of outputs, a seed value to be used} +} +\description{ +Function to create a "step_counts" table with synthetic values +The table contains synthetic (randomly created) values for a selection of +64 model runs, providing random baselines, random sizes of mitigated +activity across a subset of TPMAs, and random values for demographic growth +} +\keyword{internal} diff --git a/man/create_demo_tretspef_losgroup_tbl.Rd b/man/create_demo_tretspef_losgroup_tbl.Rd new file mode 100644 index 0000000..6b16622 --- /dev/null +++ b/man/create_demo_tretspef_losgroup_tbl.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_demo_tretspef_losgroup_tbl} +\alias{create_demo_tretspef_losgroup_tbl} +\title{Function to create a "tretspef + LoS group" table with synthetic values +The table contains synthetic (randomly created) values for a selection of +PODs, tretspefs, LoS groups and measures, across 2 "sites", as if created +by 64 model runs} +\usage{ +create_demo_tretspef_losgroup_tbl(seed) +} +\arguments{ +\item{seed}{integer, for reproducibility of outputs, a seed value to be used} +} +\description{ +Function to create a "tretspef + LoS group" table with synthetic values +The table contains synthetic (randomly created) values for a selection of +PODs, tretspefs, LoS groups and measures, across 2 "sites", as if created +by 64 model runs +} +\keyword{internal} diff --git a/man/create_ip_base_tbl.Rd b/man/create_ip_base_tbl.Rd new file mode 100644 index 0000000..f7d4916 --- /dev/null +++ b/man/create_ip_base_tbl.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_ip_base_tbl} +\alias{create_ip_base_tbl} +\title{Helper function} +\usage{ +create_ip_base_tbl() +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/create_op_base_tbl.Rd b/man/create_op_base_tbl.Rd new file mode 100644 index 0000000..5281bc7 --- /dev/null +++ b/man/create_op_base_tbl.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_op_base_tbl} +\alias{create_op_base_tbl} +\title{Helper function} +\usage{ +create_op_base_tbl() +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/create_sample_impacts.Rd b/man/create_sample_impacts.Rd new file mode 100644 index 0000000..5d0ab6a --- /dev/null +++ b/man/create_sample_impacts.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{create_sample_impacts} +\alias{create_sample_impacts} +\title{Helper function} +\usage{ +create_sample_impacts(dat, mitigation, seed, picks = 64) +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/generate_horizon_values.Rd b/man/generate_horizon_values.Rd new file mode 100644 index 0000000..58ed41c --- /dev/null +++ b/man/generate_horizon_values.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{generate_horizon_values} +\alias{generate_horizon_values} +\title{Helper function} +\usage{ +generate_horizon_values(baseline, mitigation, seed, picks = 64) +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/get_full_initial_means.Rd b/man/get_full_initial_means.Rd new file mode 100644 index 0000000..dcbf305 --- /dev/null +++ b/man/get_full_initial_means.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{get_full_initial_means} +\alias{get_full_initial_means} +\title{Helper function} +\usage{ +get_full_initial_means(ae_mean, ip_mean, op_mean) +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/get_ipop_initial_means.Rd b/man/get_ipop_initial_means.Rd new file mode 100644 index 0000000..5f2ef9d --- /dev/null +++ b/man/get_ipop_initial_means.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/demo_results_tables.R +\name{get_ipop_initial_means} +\alias{get_ipop_initial_means} +\title{Helper function} +\usage{ +get_ipop_initial_means(ip_mean, op_mean) +} +\description{ +Helper function +} +\keyword{internal} diff --git a/man/make_detailed_activity_table.Rd b/man/make_detailed_activity_table.Rd index 50bee6a..518fafc 100644 --- a/man/make_detailed_activity_table.Rd +++ b/man/make_detailed_activity_table.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/tables.R \name{make_detailed_activity_table} \alias{make_detailed_activity_table} -\title{Create a gt table with data from compile_detailed_activity_data()} +\title{Create a gt table with data from \link{compile_detailed_activity_data}} \usage{ make_detailed_activity_table(detailed_activity_data, final_year) } @@ -15,5 +15,5 @@ make_detailed_activity_table(detailed_activity_data, final_year) A gt table } \description{ -Create a gt table with data from compile_detailed_activity_data() +Create a gt table with data from \link{compile_detailed_activity_data} } diff --git a/man/make_distribution_summary_table.Rd b/man/make_distribution_summary_table.Rd index a03e3ff..b47f369 100644 --- a/man/make_distribution_summary_table.Rd +++ b/man/make_distribution_summary_table.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/tables.R \name{make_distribution_summary_table} \alias{make_distribution_summary_table} -\title{Create a gt table with data from compile_distribution_summary_data()} +\title{Create a gt table with data from \link{compile_distribution_summary_data}} \usage{ make_distribution_summary_table(distr_summary_data) } @@ -13,5 +13,5 @@ make_distribution_summary_table(distr_summary_data) A gt table } \description{ -Create a gt table with data from compile_distribution_summary_data() +Create a gt table with data from \link{compile_distribution_summary_data} } diff --git a/tests/testthat/test-change_factor_data.R b/tests/testthat/test-change_factor_data.R deleted file mode 100644 index 242ccf5..0000000 --- a/tests/testthat/test-change_factor_data.R +++ /dev/null @@ -1,172 +0,0 @@ -test_that("compile_change_factor_data does what we need", { - skip_on_ci() - results <- readr::read_rds(here::here("test_results.rds")) - step_counts_tbl <- results[["step_counts"]] |> - dplyr::select(1:8) - expect_shape(step_counts_tbl, nrow = 70656L) - col_names <- c( - "activity_type", - "sitetret", - "pod", - "change_factor", - "strategy", - "measure", - "model_run", - "value" - ) - expect_named(step_counts_tbl, col_names) - - # test with baseline included - include_baseline <- TRUE - keep_measures <- c("admissions", "beddays") - - bsline_filtered <- step_counts_tbl |> - dplyr::filter(.data[["change_factor"]] != "baseline") |> - expect_no_error() - dat_prepared <- if (include_baseline) step_counts_tbl else bsline_filtered - expect_identical(dat_prepared, step_counts_tbl) - - out1 <- dat_prepared |> - dplyr::filter( - dplyr::if_any("measure", \(x) x %in% {{ keep_measures }}) & - dplyr::if_any("value", \(x) x != 0) - ) - expect_shape(out1, nrow = 25908L) - - out2 <- out1 |> - dplyr::mutate( - dplyr::across("pod", \(x) sub("^aae.*$", "aae", x)), - dplyr::across("measure", uppercase_init) - ) |> - inner_join_for_labels(get_principal_pods()) |> - relabel_pods() |> - dplyr::left_join(get_tpma_label_lookup(), "strategy") |> - dplyr::select(!"strategy") |> - dplyr::mutate( - dplyr::across("tpma_label", \(x) dplyr::if_else(is.na(x), "-", x)) - ) |> - expect_no_error() - expect_named( - out2, - c( - "activity_type", - "sitetret", - "pod", - "pod_label", - "activity_type_label", - "change_factor", - "measure", - "model_run", - "value", - "tpma_label" - ) - ) - expect_shape(out2, nrow = 25908L) - - out3 <- out2 |> - dplyr::summarise( - dplyr::across("value", mean), - .by = change_factor_sort_vars() - ) |> - expect_no_error() - expect_shape(out3, dim = c(110L, 7L)) - - # test with baseline excluded - include_baseline <- FALSE - dat_prepared <- if (include_baseline) step_counts_tbl else bsline_filtered - expect_identical(dat_prepared, bsline_filtered) - - out1 <- dat_prepared |> - dplyr::filter( - dplyr::if_any("measure", \(x) x %in% {{ keep_measures }}) & - dplyr::if_any("value", \(x) x != 0) - ) - expect_shape(out1, nrow = 21812L) - - out2 <- out1 |> - dplyr::mutate( - dplyr::across("pod", \(x) sub("^aae.*$", "aae", x)), - dplyr::across("measure", uppercase_init) - ) |> - inner_join_for_labels(get_principal_pods()) |> - relabel_pods() |> - dplyr::left_join(get_tpma_label_lookup(), "strategy") |> - dplyr::select(!"strategy") |> - dplyr::mutate( - dplyr::across("tpma_label", \(x) dplyr::if_else(is.na(x), "-", x)) - ) |> - expect_no_error() - expect_named( - out2, - c( - "activity_type", - "sitetret", - "pod", - "pod_label", - "activity_type_label", - "change_factor", - "measure", - "model_run", - "value", - "tpma_label" - ) - ) - expect_shape(out2, nrow = 21812L) - - out3 <- out2 |> - dplyr::summarise( - dplyr::across("value", mean), - .by = change_factor_sort_vars() - ) |> - expect_no_error() - expect_shape(out3, dim = c(94L, 7L)) -}) - - -test_that("main compile function does what is expected", { - skip_on_ci() - results <- readr::read_rds(here::here("test_results.rds")) - step_counts_tbl <- results[["step_counts"]] |> - dplyr::select(seq(8)) - - activity_types <- as.character(unique(step_counts_tbl[["activity_type"]])) - pods <- unique(step_counts_tbl[["pod"]]) - measures <- unique(step_counts_tbl[["measure"]]) - - set.seed(21879) - activity_type <- sample(activity_types, 1) - measure <- sample(measures, 1) - expect_identical(activity_type, "ip") - expect_identical(measure, "admissions") - - table_data <- step_counts_tbl |> - filter_principal_data(measure, activity_type) |> - filter_to_selected_sites(sites = NULL) |> - summarise_for_all_sites() |> - dplyr::summarise(dplyr::across("value", sum), .by = "change_factor") |> - expect_no_error() - expect_shape(table_data, dim = c(8, 2)) - - table_data_as_factor <- table_data |> - dplyr::mutate( - dplyr::across("change_factor", \(x) { - forcats::fct_reorder(x, .data[["value"]], .desc = TRUE) - }), - # baseline may now not be the first factor level, move it back to start - dplyr::across("change_factor", \(x) forcats::fct_relevel(x, "baseline")) - ) |> - dplyr::arrange(dplyr::pick("change_factor")) |> - expect_no_error() - expect_identical( - levels(table_data_as_factor[["change_factor"]])[[1]], - "baseline" - ) - expect_identical( - levels(table_data_as_factor[["change_factor"]])[[2]], - "demographic_adjustment" - ) - expect_identical( - levels(table_data_as_factor[["change_factor"]])[[8]], - "health_status_adjustment" - ) -}) diff --git a/tests/testthat/test-get_principal_pods.R b/tests/testthat/test-get_principal_pods.R deleted file mode 100644 index 1572fa7..0000000 --- a/tests/testthat/test-get_principal_pods.R +++ /dev/null @@ -1,9 +0,0 @@ -test_that("test to specify expected dims of pods lookup", { - gpp_out <- get_principal_pods() - expect_named(gpp_out, c("activity_type_label", "pod", "pod_label")) - expect_identical(nrow(gpp_out), 10L) - expect_identical( - sort(unique(as.character(gpp_out[["activity_type_label"]]))), - c("A&E", "Inpatient", "Outpatient") - ) -}) diff --git a/tests/testthat/test-principal_los_data.R b/tests/testthat/test-principal_los_data.R deleted file mode 100644 index 5782ce4..0000000 --- a/tests/testthat/test-principal_los_data.R +++ /dev/null @@ -1,79 +0,0 @@ -test_that("compile_main_data does what we need", { - skip_on_ci() - results <- readr::read_rds(here::here("test_results.rds")) - los_tbl <- results[["tretspef+los_group"]] |> - dplyr::select(seq(7)) - expect_shape(los_tbl, nrow = 290667L) - col_names <- c( - "pod", - "sitetret", - "tretspef", - "los_group", - "measure", - "model_run", - "value" - ) - expect_named(los_tbl, col_names) - - measure <- "beddays" - - out1 <- los_tbl |> - dplyr::filter( - dplyr::if_any("measure", \(x) x == {{ measure }}) - ) - expect_shape(out1, nrow = 105884) - - out2 <- out1 |> - inner_join_for_labels(get_principal_pods()) - - col_names2 <- c( - "pod", - "pod_label", - "activity_type_label", - "sitetret", - "tretspef", - "los_group", - "measure", - "model_run", - "value" - ) - - expect_named(out2, col_names2) - - out3 <- out2 |> - relabel_pods() |> - dplyr::select(!c("activity_type_label", "tretspef")) |> - dplyr::summarise( - dplyr::across("value", sum), - .by = tidyselect::all_of(default_group_cols("los_group")) - ) |> - calculate_principal_stats(default_group_cols("los_group")) |> - dplyr::filter(dplyr::if_any("stat", \(x) x == "mean")) |> - dplyr::select(!"stat") - - col_names3 <- c( - "pod_label", - "sitetret", - "los_group", - "baseline", - "principal" - ) - expect_named(out3, col_names3) - expect_shape(out3, nrow = 29) - - out4 <- out3 |> - filter_to_selected_sites(sites = NULL) |> - summarise_for_all_sites() |> - add_change_cols() - - col_names4 <- c( - "pod_label", - "los_group", - "baseline", - "principal", - "change", - "change_pct" - ) - expect_named(out4, col_names4) - expect_shape(out4, dim = c(26, 6)) -}) diff --git a/tests/testthat/test-principal_pod_data.R b/tests/testthat/test-principal_pod_data.R deleted file mode 100644 index 34abdeb..0000000 --- a/tests/testthat/test-principal_pod_data.R +++ /dev/null @@ -1,76 +0,0 @@ -test_that("compile_principal_pod_data does what we need", { - skip_on_ci() - results <- readr::read_rds(here::here("test_results.rds")) - default_tbl <- results[["default"]] |> - dplyr::select(1:5) - expect_shape(default_tbl, nrow = 10794L) - col_names <- c("pod", "sitetret", "measure", "model_run", "value") - expect_named(default_tbl, col_names) - - out1 <- default_tbl |> - filter_to_main_measures() |> - exclude_op_teleatt_procedures() |> - dplyr::mutate(dplyr::across("pod", \(x) sub("^aae.*$", "aae", x))) |> - inner_join_for_labels(get_principal_pods()) |> - relabel_pods() |> - relabel_ip_activity_types() - expect_shape(out1, nrow = nrow(dplyr::distinct(out1))) - - col_names1 <- c( - "pod", - "pod_label", - "activity_type_label", - "sitetret", - "measure", - "model_run", - "value" - ) - - expect_named(out1, col_names1) - - final_activity_types <- c( - "A&E", - "Inpatient Admissions", - "Inpatient Bed Days", - "Outpatient" - ) - expect_identical( - sort(unique(as.character(out1[["activity_type_label"]]))), - final_activity_types - ) - - out2 <- out1 |> - dplyr::summarise( - dplyr::across("value", sum), - .by = tidyselect::all_of(default_group_cols("activity_type_label")) - ) |> - calculate_principal_stats(default_group_cols("activity_type_label")) |> - dplyr::filter(dplyr::if_any("stat", \(x) x == "mean")) |> - dplyr::select(!"stat") - - col_names2 <- c( - "pod_label", - "sitetret", - "activity_type_label", - "baseline", - "principal" - ) - expect_named(out2, col_names2) - expect_shape(out2, nrow = 31) - - out3 <- out2 |> - filter_to_selected_sites(sites = NULL) |> - summarise_for_all_sites() |> - add_change_cols() - - col_names3 <- c( - "pod_label", - "activity_type_label", - "baseline", - "principal", - "change", - "change_pct" - ) - expect_named(out3, col_names3) - expect_shape(out3, dim = c(16, 6)) -}) diff --git a/tests/testthat/test-read_results_parquet_files.R b/tests/testthat/test-read_results_parquet_files.R deleted file mode 100644 index e120fcb..0000000 --- a/tests/testthat/test-read_results_parquet_files.R +++ /dev/null @@ -1,142 +0,0 @@ -test_that("get model runs (data_dirs) overview", { - skip_on_ci() - res <- get_results_container() - expect_s3_class(res, "blob_container") - root_dir <- Sys.getenv("AZ_RESULTS_DIRECTORY", NA) - expect_false(is.na(root_dir)) - version <- "v3.6" - scheme <- "RCF" - scenario <- "test" - - scenario_path <- file.path(root_dir, version, scheme, scenario) - expect_true(AzureStor::blob_dir_exists(res, scenario_path)) - - data_dirs <- AzureStor::list_blobs(res, scenario_path, recursive = FALSE) |> - dplyr::pull("name") - expect_length(data_dirs, 2L) # currently 2 model runs in this scenario dir - - all(purrr::map_lgl(data_dirs, \(x) AzureStor::blob_dir_exists(res, x))) |> - expect_true() -}) - - -test_that("read parquet files overview", { - skip_on_ci() - res <- get_results_container() - expect_s3_class(res, "blob_container") - root_dir <- Sys.getenv("AZ_RESULTS_DIRECTORY", NA) - expect_false(is.na(root_dir)) - version <- "v3.6" - scheme <- "RCF" - scenario <- "test" - scenario_path <- file.path(root_dir, version, scheme, scenario) - - # for this test block we will just use one model run directory - data_dir <- AzureStor::list_blobs(res, scenario_path, recursive = FALSE) |> - dplyr::pull("name") |> - max() - - get_parquet_paths <- function(container, path) { - AzureStor::list_blobs(container, path, recursive = FALSE) |> - dplyr::pull("name") |> - gregv("\\.parquet$") - } - parquet_paths <- get_parquet_paths(res, data_dir) - - get_parquet_names <- \(x) sub("^(.*/)([[:graph:]]+)(\\.parquet)$", "\\2", x) - parquet_names <- get_parquet_names(parquet_paths) - - tbl_names <- c( - "acuity", - "age", - "attendance_category", - "avoided_activity", - "default", - "sex+age_group", - "sex+tretspef", - "step_counts", - "tretspef_raw+los_group", - "tretspef_raw" - ) - expect_equal(parquet_names, tbl_names) - - parquet_data <- parquet_paths |> - purrr::map(\(x) azkit::read_azure_parquet(res, x, info = FALSE)) |> - rlang::set_names(parquet_names) - expect_true(all(purrr::map_lgl(parquet_data, \(x) inherits(x, "tbl_df")))) -}) - - -test_that("table selection works", { - tbl_names <- c( - "acuity", - "age", - "attendance_category", - "avoided_activity", - "default", - "sex+age_group", - "sex+tretspef", - "step_counts", - "tretspef_raw+los_group", - "tretspef_raw" - ) - - tables <- c("default", "acuity") - - check_args <- function(tables = NULL) { - rlang::arg_match(tables, values = tbl_names, multiple = TRUE) - } - expect_equal(check_args(tables), tables) - - tables_wrong <- c("acuity", "default", "random") - absent <- setdiff(tables_wrong, tbl_names) - msg <- azkit::cv_error_msg("Table{?s} {absent} {?is/are} not present") - azkit::check_vec(tables_wrong, \(x) x %in% tbl_names, msg) |> - expect_error("random is not present") - - test_vec <- rlang::set_names(letters[seq(3)], tables_wrong) - filt_vec <- test_vec[tables] # vector reordered by order of names - expect_equal(unname(filt_vec), c("b", "a")) - filt_vec <- test_vec[tables[c(2, 1)]] - expect_equal(unname(filt_vec), c("a", "b")) - - test_named <- purrr::map(test_vec, toupper) # map retains names - expect_named(test_named) -}) - - -test_that("bughunting", { - skip_on_ci() - - res <- get_results_container() - expect_s3_class(res, "blob_container") - root_dir <- Sys.getenv("AZ_RESULTS_DIRECTORY", NA) - expect_false(is.na(root_dir)) - - version <- "v4.0" - expect_true(rlang::is_string(version)) - scheme <- "RRK" - expect_true(rlang::is_string(scheme)) - - check_grdp_inputs(res, root_dir, version, scheme, NULL, NULL) |> - expect_true() - - orig_path <- file.path(root_dir, version, scheme) - folder_name <- AzureStor::list_blobs(res, orig_path, recursive = FALSE) |> - dplyr::filter(dplyr::if_any("isdir")) |> - dplyr::pull("name") - expect_true(rlang::is_scalar_character(folder_name)) - azkit::check_scalar_type(folder_name, "character", "fail") |> - expect_no_error() - azkit::check_scalar_type(folder_name, "character", "fail") |> - expect_equal(folder_name) # folder_name should be passed through - - exp_path <- file.path(root_dir, version, scheme, "test-2324-2") - - scen_path <- check_single_subdir(orig_path, "scenario", NULL, res) - expect_equal(scen_path, paste0(exp_path, "/")) # file.path doesn't add final / - - expect_no_message(check_single_subdir(scen_path, "dttm_stamp", NULL, res)) - check_single_subdir(scen_path, "dttm_stamp", "max", res) |> - expect_message("Using latest model run") -})