Created
September 25, 2022 22:22
-
-
Save johnmackintosh/1d7b1763ef3cf77d5a2f6357638606b7 to your computer and use it in GitHub Desktop.
as per Chris Beeley caseload question from NHSR demo and how-to
This file contains hidden or 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
teams <- c("Apple", "Banana", "Clementine") | |
dates <- seq(as.Date("2017-01-01"), as.Date("2021-01-01"), "days") | |
# 5 columns- client id, referral id, team_desc, referral_date, discharge_date | |
test_frame <- purrr::map_dfr(1 : 100, function(x){ | |
rnum <- sample(1 : 5, 1) | |
team_name <- sample(teams, rnum, replace = TRUE) | |
ran_date <- sample(dates, rnum) | |
tibble::tibble("client_id" = rep(x, rnum), | |
"referral_id" = 1 : rnum, | |
"team_desc" = team_name, | |
"referral_date" = ran_date, | |
"discharge_date" = ran_date + sample(7 : 365, 1)) | |
}) | |
test_frame[sample(nrow(test_frame), 100), "discharge_date"] = NA | |
library(data.table) | |
library(patientcounter) | |
setDT(test_frame) | |
test_frame[,`:=`(unique_id = paste0(test_frame$client_id, | |
"_", test_frame$referral_id), | |
referral_date = as.POSIXct(referral_date), | |
discharge_date = as.POSIXct(discharge_date)) | |
] | |
discharged <- test_frame[!is.na(discharge_date)] | |
current <- test_frame[is.na(discharge_date)] | |
res_dis <- interval_census(discharged, | |
identifier = "unique_id", | |
admit = "referral_date", | |
discharge = "discharge_date", | |
time_unit = "1 day", | |
results = "patient") | |
res_cur <- interval_census(current, | |
identifier = "unique_id", | |
admit = "referral_date", | |
discharge = "discharge_date", | |
time_unit = "1 day", | |
results = "patient") | |
combined <- rbindlist(l =list(res_cur,res_dis)) | |
# aggregate results by date and team | |
combined[, .N, .(base_date, team_desc)] | |
# alternatively, grouped by team | |
res_dis_g <- interval_census(discharged, | |
identifier = "unique_id", | |
admit = "referral_date", | |
discharge = "discharge_date", | |
time_unit = "1 day", | |
group_var = "team_desc", | |
results = "group", | |
uniques = FALSE) | |
res_cur_g <- interval_census(current, | |
identifier = "unique_id", | |
admit = "referral_date", | |
discharge = "discharge_date", | |
time_unit = "1 day", | |
group_var = "team_desc", | |
results = "group", | |
uniques = FALSE) | |
combined2 <- rbindlist(l =list(res_cur_g, res_dis_g)) | |
combined2[,.(total = sum(N)), .(base_date, team_desc)] | |
# go for totals from the off | |
res_dis_t <- interval_census(discharged, | |
identifier = "unique_id", | |
admit = "referral_date", | |
discharge = "discharge_date", | |
time_unit = "1 day", | |
group_var = "team_desc", | |
results = "total", | |
uniques = FALSE) | |
res_cur_t <- interval_census(current, | |
identifier = "unique_id", | |
admit = "referral_date", | |
discharge = "discharge_date", | |
time_unit = "1 day", | |
group_var = "team_desc", | |
results = "total", | |
uniques = FALSE) | |
combined3 <- rbindlist(l =list(res_cur_t, res_dis_t)) | |
# checks | |
combined[base_date == '2022-09-25'] # 100 rows | |
combined[base_date == '2022-09-25', .N, team_desc] # tally by team (33 + 33 + 34) | |
combined2[base_date == '2022-09-25'] # N = 33 + 33 + 34 = 100 | |
combined3[base_date == '2022-09-25'] # N = 100 | |
Author
johnmackintosh
commented
Sep 25, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment