Skip to content

Commit

Permalink
Merge pull request #426 from Public-Health-Scotland/charlie
Browse files Browse the repository at this point in the history
Prepping monthly publication data for MMI use
  • Loading branch information
charlie-smith authored Sep 23, 2024
2 parents 55accd7 + 6927220 commit 5a3d124
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion 01_control/control_publication.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ compile_non_acceptance_action_summary()

compile_appointments_summary()
compile_basic_opti_summary()

compile_open_cases_summary()


# 5 - Create tables/charts for publication --------------------------------
Expand Down
12 changes: 12 additions & 0 deletions 07_publication/script/chapters/3_set_constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,21 @@ df_ds_hb_name <- cross_join(as.data.frame(vec_dataset_type),
as.data.frame(hb_vector)) |>
rename(dataset_type = vec_dataset_type,
hb_name = hb_vector) |>
mutate(hb_name = factor(hb_name, levels = hb_vector))
filter(!(#dataset_type == "CAMHS" &
hb_name == "NHS 24")) # remove invalid combo

# with time columns
df_time <- data.frame(month = date_range) |>
append_quarter_ending(date_col = "month")

df_months <- df_time |> select(month)
df_quarts <- df_time |> select(quarter_ending)

df_qt_ds_hb <- df_ds_hb_name |> cross_join(df_quarts)
df_month_ds_hb <- df_ds_hb_name |> cross_join(df_months)



# 5. Plotting constants -------------------------------------------------------

Expand Down
12 changes: 8 additions & 4 deletions 07_publication/script/functions/archive/non_acceptance_outcome.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ summarise_non_acceptance_reason <- function(df){ # should be REASON
across(hb_name, ~"NHS Scotland"),
.groups = "drop")) |>
full_join(df_ds_hb_name, by = c("dataset_type", "hb_name")) |>
mutate(hb_name = factor(hb_name, hb_vector)) |>
arrange(dataset_type, hb_name) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name")) |>
right_join(df_month_ds_hb, by = c("referral_month" = "month", "dataset_type", "hb_name")) |>
mutate(hb_name = factor(hb_name, hb_vector)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "month_hb")) |>

append_quarter_ending(date_col = "referral_month") |>
Expand All @@ -131,9 +132,10 @@ summarise_non_acceptance_reason <- function(df){ # should be REASON
across(hb_name, ~"NHS Scotland"),
.groups = "drop")) |>
full_join(df_ds_hb_name, by = c("dataset_type", "hb_name")) |>
mutate(hb_name = factor(hb_name, hb_vector)) |>
arrange(dataset_type, hb_name) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "sex_reported", "hb_name")) |>
right_join(df_month_ds_hb, by = c("referral_month" = "month", "dataset_type", "hb_name")) |>
mutate(hb_name = factor(hb_name, hb_vector)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "month_hb_sex")) |>

append_quarter_ending(date_col = "referral_month") |>
Expand All @@ -155,10 +157,11 @@ summarise_non_acceptance_reason <- function(df){ # should be REASON
across(hb_name, ~"NHS Scotland"),
.groups = "drop")) |>
full_join(df_ds_hb_name, by = c("dataset_type", "hb_name")) |>
mutate(hb_name = factor(hb_name, hb_vector)) |>
arrange(dataset_type, hb_name) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", #"age_at_ref_rec",
"hb_name")) |>
right_join(df_month_ds_hb, by = c("referral_month" = "month", "dataset_type", "hb_name")) |>
mutate(hb_name = factor(hb_name, hb_vector)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "month_hb_age")) |>

append_quarter_ending(date_col = "referral_month") |>
Expand All @@ -180,9 +183,10 @@ summarise_non_acceptance_reason <- function(df){ # should be REASON
across(hb_name, ~"NHS Scotland"),
.groups = "drop")) |>
full_join(df_ds_hb_name, by = c("dataset_type", "hb_name")) |>
mutate(hb_name = factor(hb_name, hb_vector)) |>
arrange(dataset_type, hb_name) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "simd2020_quintile", "hb_name")) |>
right_join(df_month_ds_hb, by = c("referral_month" = "month", "dataset_type", "hb_name")) |>
mutate(hb_name = factor(hb_name, hb_vector)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "month_hb_simd")) |>

append_quarter_ending(date_col = "referral_month") |>
Expand Down
22 changes: 11 additions & 11 deletions 07_publication/script/functions/non_acceptance.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ summarise_non_acceptance <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb() |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "all_hb"))


Expand All @@ -68,7 +68,7 @@ summarise_non_acceptance <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("dataset_type", "hb_name", "sex_reported")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "all_hb_sex"))

# by age
Expand All @@ -85,7 +85,7 @@ summarise_non_acceptance <- function(df){
add_proportion_ds_hb(vec_group = c("dataset_type", "hb_name", #"age_at_ref_rec",
"age_group")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "all_hb_age"))

# by simd
Expand All @@ -99,7 +99,7 @@ summarise_non_acceptance <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("dataset_type", "hb_name", "simd2020_quintile")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "all_hb_simd"))


Expand All @@ -117,13 +117,13 @@ summarise_non_acceptance <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "month_hb")) |>

append_quarter_ending(date_col = "referral_month") |>
summarise_by_quarter(vec_group = c("quarter_ending", "dataset_type", "hb_name", "ref_acc_desc")) |>
add_proportion_ds_hb(vec_group = c("quarter_ending", "dataset_type", "hb_name")) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "quarter_hb"))


Expand All @@ -138,13 +138,13 @@ summarise_non_acceptance <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name", "sex_reported")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "month_hb_sex")) |>

append_quarter_ending(date_col = "referral_month") |>
summarise_by_quarter(vec_group = c("quarter_ending", "dataset_type", "hb_name", "sex_reported", "ref_acc_desc")) |>
add_proportion_ds_hb(vec_group = c("quarter_ending", "dataset_type", "hb_name", "sex_reported")) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "quarter_hb_sex"))


Expand All @@ -162,15 +162,15 @@ summarise_non_acceptance <- function(df){
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name"#, "age_at_ref_rec"
)) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "month_hb_age")) |>

append_quarter_ending(date_col = "referral_month") |>
summarise_by_quarter(vec_group = c("quarter_ending", "dataset_type", "hb_name", #"age_at_ref_rec",
"age_group", "ref_acc_desc")) |>
add_proportion_ds_hb(vec_group = c("quarter_ending", "dataset_type", "hb_name"#, "age_at_ref_rec"
)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "quarter_hb_age"))


Expand All @@ -187,7 +187,7 @@ summarise_non_acceptance <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name", "simd2020_quintile")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(non_acc_dir, measure_label, "month_hb_simd")) |>

append_quarter_ending(date_col = "referral_month") |>
Expand Down
24 changes: 4 additions & 20 deletions 07_publication/script/functions/summarise_open_cases.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ summarise_open_cases <- function(){
.groups = "drop")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = level_order_hb)) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
right_join(df_month_ds_hb, by = c("referral_month" = "month", "dataset_type", "hb_name")) |>
save_as_parquet(path = paste0(open_dir, measure_label, "month_hb")) |>
append_quarter_ending(date_col = "referral_month") |>
group_by(quarter_ending, !!!syms(c(dataset_type_o, hb_name_o))) |>
Expand All @@ -143,6 +144,7 @@ summarise_open_cases <- function(){
.groups = "drop")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = level_order_hb)) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o), !!sym(sex_reported_o)) |>
right_join(df_month_ds_hb, by = c("referral_month" = "month", "dataset_type", "hb_name")) |>
save_as_parquet(path = paste0(open_dir, measure_label, "month_hb_sex")) |>
append_quarter_ending(date_col = "referral_month") |>
ungroup() |>
Expand All @@ -169,6 +171,7 @@ summarise_open_cases <- function(){
.groups = "drop")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = level_order_hb)) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o), !!sym(age_group_o)) |>
right_join(df_month_ds_hb, by = c("referral_month" = "month", "dataset_type", "hb_name")) |>
save_as_parquet(path = paste0(open_dir, measure_label, "month_hb_age")) |>
append_quarter_ending(date_col = "referral_month") |>
ungroup() |>
Expand All @@ -182,26 +185,6 @@ summarise_open_cases <- function(){


# by hb, month, and simd
df_month_hb_simd <- df_single_row |>
group_by(!!sym(referral_month_o), !!sym(dataset_type_o), !!sym(hb_name_o),
!!sym(simd_quintile_o)) |>
summarise(count = n(), .groups = "drop") |>
group_by(!!sym(referral_month_o), !!sym(dataset_type_o), !!sym(simd_quintile_o)) %>%
bind_rows(summarise(.,
across(where(is.numeric), sum),
across(!!sym(hb_name_o), ~"NHS Scotland"),
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = level_order_hb)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
save_as_parquet(path = paste0(open_dir, measure_label, "month_hb_simd")) |>

append_quarter_ending(date_col = "referral_month") |>
summarise_by_quarter(vec_group = c("quarter_ending", "dataset_type", "hb_name", "simd2020_quintile")) |>
add_proportion_ds_hb(vec_group = c("quarter_ending", "dataset_type", "hb_name")) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
save_as_parquet(path = paste0(open_dir, measure_label, "quarter_hb_simd"))

df_month_hb_simd <- df_single_row_monthly |>
group_by(!!sym(referral_month_o), !!sym(dataset_type_o), !!sym(hb_name_o), #age_at_ref_rec,
!!sym(simd_quintile_o)) |>
Expand All @@ -217,6 +200,7 @@ summarise_open_cases <- function(){
.groups = "drop")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = level_order_hb)) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o), !!sym(simd_quintile_o)) |>
right_join(df_month_ds_hb, by = c("referral_month" = "month", "dataset_type", "hb_name")) |>
save_as_parquet(path = paste0(open_dir, measure_label, "month_hb_simd")) |>
append_quarter_ending(date_col = "referral_month") |>
ungroup() |>
Expand Down
24 changes: 12 additions & 12 deletions 07_publication/script/functions/summarise_referrals.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ summarise_referrals <- function(df){
.groups = "drop")) |>
#add_proportion_ds_hb() |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "all_hb"))

# by sex
Expand All @@ -53,7 +53,7 @@ summarise_referrals <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("dataset_type", "hb_name")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "all_hb_sex"))

# by age
Expand All @@ -68,7 +68,7 @@ summarise_referrals <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("dataset_type", "hb_name")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "all_hb_age"))

# by simd
Expand All @@ -82,7 +82,7 @@ summarise_referrals <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("dataset_type", "hb_name")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "all_hb_simd"))


Expand All @@ -102,13 +102,13 @@ summarise_referrals <- function(df){
.groups = "drop")) |>
#add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "month_hb")) |>

append_quarter_ending(date_col = "referral_month") |>
summarise_by_quarter(vec_group = c("quarter_ending", "dataset_type", "hb_name")) |>
#add_proportion_ds_hb(vec_group = c("quarter_ending", "dataset_type", "hb_name")) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "quarter_hb"))


Expand All @@ -124,13 +124,13 @@ summarise_referrals <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "month_hb_sex")) |>

append_quarter_ending(date_col = "referral_month") |>
summarise_by_quarter(vec_group = c("quarter_ending", "dataset_type", "hb_name", "sex_reported")) |>
add_proportion_ds_hb(vec_group = c("quarter_ending", "dataset_type", "hb_name")) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "quarter_hb_sex"))


Expand All @@ -148,15 +148,15 @@ summarise_referrals <- function(df){
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name"#,"age_at_ref_rec"
)) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "month_hb_age")) |>

append_quarter_ending(date_col = "referral_month") |>
summarise_by_quarter(vec_group = c("quarter_ending", "dataset_type", "hb_name", #"age_at_ref_rec",
"age_group")) |>
add_proportion_ds_hb(vec_group = c("quarter_ending", "dataset_type","hb_name"#, "age_at_ref_rec"
)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "quarter_hb_age"))


Expand All @@ -172,13 +172,13 @@ summarise_referrals <- function(df){
.groups = "drop")) |>
add_proportion_ds_hb(vec_group = c("referral_month", "dataset_type", "hb_name")) |>
mutate(!!sym(hb_name_o) := factor(!!sym(hb_name_o), levels = hb_vector)) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "month_hb_simd")) |>

append_quarter_ending(date_col = "referral_month") |>
summarise_by_quarter(vec_group = c("quarter_ending", "dataset_type", "hb_name", "simd2020_quintile")) |>
add_proportion_ds_hb(vec_group = c("quarter_ending", "dataset_type", "hb_name")) |>
arrange(!!dataset_type_o, !!hb_name_o) |>
arrange(!!sym(dataset_type_o), !!sym(hb_name_o)) |>
save_as_parquet(path = paste0(ref_dir, measure_label, "quarter_hb_simd"))

}

0 comments on commit 5a3d124

Please sign in to comment.