library(data.table)
library(ggplot2)
library(ggrepel)
# very old KPIs
very_old_kpis = fread("fig5b.csv")
# old KPIs
old_kpis_raw = fread("combined_output_Jan2022.csv")
# newer KPIs
newer_kpis_raw = fread("combined_output_May2022.csv")
# combo raw KPIs
combo_raw_kpis = rbind(old_kpis_raw, newer_kpis_raw)
Aggregate raw KPIs by week
combo_agg_kpis = combo_raw_kpis[, .(
patients_eligible = uniqueN(patient_id),
patients_flagged_by_tool = uniqueN(ifelse(
review %in% c("(1) High extreme lows", "(2) High lows", "(3) Large drop in TIR", "(4) Low TIR"),
patient_id, NA), na.rm=T)
), by = 'load_time']
combo_agg_kpis[,date := as.Date(as.character(cut(load_time, "week")))]
combo_agg_kpis[, load_time := NULL]
combo_agg_kpis_long = melt(combo_agg_kpis, id.vars="date")
Load myChart message data
message_sends_epic = fread('text/keyword_df_export.csv')
message_sends_epic[,`:=`(
dt = as.Date(ds, "%m/%d/%y"),
ds = NULL,
msg_id = NULL
)]
message_sends_epic[,start_of_week_dt := as.character(cut(dt, "week"))]
messages_agg = message_sends_epic[
has_an_average_blood_glucose_of==TRUE,
.(patients_contacted_mychart = uniqueN(mrn)),
by = 'start_of_week_dt']
messages_agg[, date := as.Date(start_of_week_dt)]
messages_agg[, start_of_week_dt := NULL]
messages_agg_long = melt(messages_agg, id.vars = c('date'))
Combine old and new KPIs
very_old_kpis[, date := as.Date(date)]
combo_kpis = rbind(combo_agg_kpis_long, very_old_kpis, messages_agg_long)
combo_kpis[variable == "patients_eligible", variable := "patients_shown"]
combo_kpis[variable == "patients_flagged_by_tool", variable := "patients_flagged_for_review"]
combo_kpis[variable == "patients_contacted", variable := "patients_contacted_redcap"]
combo_kpis[, label := ifelse(date == max(date), as.character(variable), NA_character_), by='variable']
ggplot(combo_kpis, aes(x=date, y=value, color=variable)) + geom_line() + geom_point() + theme_bw() +
geom_label_repel(aes(label = label), nudge_x = 100, nudge_y = -10, na.rm = TRUE) +
theme(legend.position = "none") + ylab("No. Patients") + xlab("Date (start of week)")
Export wide table
fwrite(dcast(combo_kpis, date ~ variable), "combo_kpis.csv")