-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #424 from Public-Health-Scotland/charlie
Working open open cases
- Loading branch information
Showing
11 changed files
with
275 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
###########################. | ||
### D&G UCPN Update Fix ### | ||
###########################. | ||
|
||
# Author: Charlie Smith | ||
# Date: 2024-09-13 | ||
|
||
# Issue: D&G PT moving to Morse from early October 2024, meaning that PT will adopt a UCPN. | ||
# This will mean that pre-existing records' UCPNs will no longer match. Phil will | ||
# provide the old-style UCPN in the UPI field. | ||
|
||
# Solution: for submission from 2024-10-24 onwards, ensure old UCPN (in UPI field) | ||
# for D&G pathways IF pwathway has info from prior 2024-10-24. | ||
|
||
|
||
# Create a test dataset to test function on: | ||
# ~ | ||
|
||
df_test_data <- as.data.frame( | ||
|
||
header_ref_date <- c("", "", "", ""), | ||
|
||
hb_name <- c("NHS Dumfries and Galloway", "NHS Dumfries and Galloway", "NHS Dumfries and Galloway", "NHS Fife"), | ||
|
||
dataset_type <- c("PT", "PT", "PT", "PT"), | ||
|
||
ucpn <- c("12345", "12345", "62891", "62891"), | ||
|
||
upi <- c("2639123", "12345", "62891", "62891"), | ||
|
||
ref_rec_date <- c("", "", "", "") | ||
|
||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
#################################. | ||
### Check publication figures ### | ||
#################################. | ||
|
||
# Author: Charlie Smith | ||
# Date: 2024-7-24 | ||
|
||
check_publication_figs <- function(){ | ||
|
||
# Referrals | ||
df_refs <- read_parquet( | ||
paste0("//PHI_conf/MentalHealth5/CAPTND/CAPTND_shorewise/output/analysis_", | ||
data_analysis_latest_date, "/shorewise_publication/data/referrals/table_referrals_quarterly.parquet")) | ||
|
||
nc <- ncol(df_refs) | ||
|
||
df_refs <- df_refs |> | ||
select(c(1, 2, nc)) |> | ||
filter(`Health board` %in% c("NHSScotland", "NHS Scotland")) | ||
|
||
nc <- ncol(df_refs) | ||
|
||
# Accepted refs | ||
df_acc <- read_parquet( | ||
paste0("//PHI_conf/MentalHealth5/CAPTND/CAPTND_shorewise/output/analysis_", | ||
data_analysis_latest_date, "/shorewise_publication/data/non_acceptance/table_acc_rate.parquet")) |> | ||
select(c(1, 2, Total)) |> | ||
filter(`Health board` %in% c("NHSScotland", "NHS Scotland")) | ||
|
||
|
||
# check refs == total in accepted refs | ||
df_check1 <- left_join(df_refs, df_acc, by = c("dataset_type", "Health board")) |> | ||
mutate(match = , Total)) | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
|
||
############################. | ||
### Calculate open cases ### | ||
############################. | ||
|
||
# Author: Charlie Smith | ||
# Date: 2024-09-19 | ||
|
||
|
||
summarise_open_cases <- function(){ | ||
|
||
dir.create(open_dir) | ||
measure_label <- "open_cases_" | ||
|
||
# single row per individual | ||
df_single_row <- read_parquet(paste0(root_dir,'/swift_glob_completed_rtt.parquet')) |> | ||
lazy_dt() |> | ||
filter(!!sym(referral_month_o) %in% date_range & # apply date range filter | ||
!!sym(rtt_eval_o) == "seen - active") |> # the same as open cases? | ||
group_by(!!!syms(data_keys)) |> | ||
slice(1) |> | ||
ungroup() |> | ||
add_sex_description() |> | ||
tidy_age_group_order() |> | ||
as.data.frame() | ||
|
||
|
||
# overall ----------------------------------------------------------------- | ||
|
||
# by hb | ||
df_all_hb <- df_single_row |> | ||
group_by(!!sym(dataset_type_o), !!sym(hb_name_o)) |> | ||
summarise(count = n(), .groups = "drop") |> | ||
group_by(!!sym(dataset_type_o)) %>% | ||
bind_rows(summarise(., | ||
across(where(is.numeric), sum), | ||
across(!!sym(hb_name_o), ~"NHS Scotland"), | ||
.groups = "drop")) |> | ||
#add_proportion_ds_hb() |> | ||
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, "all_hb")) | ||
|
||
# by sex | ||
df_all_sex <- df_single_row |> | ||
group_by(!!sym(dataset_type_o), !!sym(hb_name_o), !!sym(sex_reported_o)) |> | ||
summarise(count = n(), .groups = "drop") |> | ||
group_by(!!sym(dataset_type_o), !!sym(sex_reported_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("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, "all_hb_sex")) | ||
|
||
# by age | ||
df_all_age <- df_single_row |> | ||
group_by(!!sym(dataset_type_o), !!sym(hb_name_o), !!sym(age_group_o)) |> | ||
summarise(count = n(), .groups = "drop") |> | ||
group_by(!!sym(dataset_type_o), #age_at_ref_rec, | ||
!!sym(age_group_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("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, "all_hb_age")) | ||
|
||
# by simd | ||
df_all_simd <- df_single_row |> | ||
group_by(!!sym(dataset_type_o), !!sym(hb_name_o), !!sym(simd_quintile_o)) |> | ||
summarise(count = n(), .groups = "drop") |> | ||
group_by(!!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("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, "all_hb_simd")) | ||
|
||
|
||
|
||
|
||
|
||
# by month ---------------------------------------------------------------- | ||
|
||
# by hb and month | ||
df_month_hb <- df_single_row |> | ||
group_by(!!sym(referral_month_o), !!sym(dataset_type_o), !!sym(hb_name_o)) |> | ||
summarise(count = n(), .groups = "drop") |> | ||
group_by(!!sym(referral_month_o), !!sym(dataset_type_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")) |> | ||
|
||
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) |> | ||
save_as_parquet(path = paste0(open_dir, measure_label, "quarter_hb")) | ||
|
||
|
||
# by hb, month, and sex | ||
df_month_hb_sex <- df_single_row |> | ||
group_by(!!sym(referral_month_o), !!sym(dataset_type_o), !!sym(hb_name_o), | ||
!!sym(sex_reported_o)) |> | ||
summarise(count = n(), .groups = "drop") |> | ||
group_by(!!sym(referral_month_o), !!sym(dataset_type_o), !!sym(sex_reported_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_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) |> | ||
save_as_parquet(path = paste0(open_dir, measure_label, "quarter_hb_sex")) | ||
|
||
|
||
# by hb, month, and age | ||
df_month_hb_age <- df_single_row |> | ||
group_by(!!sym(referral_month_o), !!sym(dataset_type_o), !!sym(hb_name_o), #age_at_ref_rec, | ||
!!sym(age_group_o)) |> | ||
summarise(count = n(), .groups = "drop") |> | ||
group_by(!!sym(referral_month_o), !!sym(dataset_type_o), #age_at_ref_rec, | ||
!!sym(age_group_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"#,"age_at_ref_rec" | ||
)) |> | ||
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_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) |> | ||
save_as_parquet(path = paste0(open_dir, measure_label, "quarter_hb_age")) | ||
|
||
|
||
# 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")) | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters