-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalysis_pipeline_v1.1.0_afar2A_PD.R
320 lines (279 loc) · 16.4 KB
/
Analysis_pipeline_v1.1.0_afar2A_PD.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#libraries
library(readxl)
library(plyr)
library(dplyr)
library(ggplot2)
library(tidyr)
library(stringr)
library(reshape2)
library(viridis)
library(ggrepel)
library(scales)
library(treemapify)
library(RColorBrewer)
###@Julius I needed to make some changes here, you can revert back to your version
date <- Sys.Date() #User should make sure this matches with date in names of csv files to be read in
usefuldescription <- "Afar" #User should make sure this matches with names of csv files to be read in
#usefuldescription <- scenarios$Geography_dontedit[1]
#scenarios <- read_xlsx("config/model_inputs_snnpr2.xlsx",sheet="Scenarios")
#read in summary statistics csv files generated from post-processing steps in "Run_simulations.R"
Mean_ServiceCat <- read.csv(paste("results/Mean_ServiceCat_",usefuldescription,"_",date,".csv",sep=""))
Stats_TotClin <- read.csv(paste("results/Stats_TotClin_",usefuldescription,"_",date,".csv",sep=""))
Mean_ClinCat <- read.csv(paste("results/Mean_ClinCat_",usefuldescription,"_",date,".csv",sep=""))
Mean_Total <- read.csv(paste("results/Mean_Total_",usefuldescription,"_",date,".csv",sep=""))
ByRun_ClinMonth <- read.csv(paste("results/ByRun_ClinMonth_",usefuldescription,"_",date,".csv",sep=""))
Stats_ClinMonth <- read.csv(paste("results/Stats_ClinMonth_",usefuldescription,"_",date,".csv",sep=""))
Mean_Alloc <- read.csv(paste("results/Mean_Alloc_",usefuldescription,"_",date,".csv",sep=""))
#Addition Pierre
############################################################################################################################################
#set color palettes
#ServiceCategory Colors
sc_tritanopia_colors <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf", "#aec7e8", "#ffbb78", "#98df8a", "#c5b0d5", "#ff9896", "#ff9896", "#9edae5")
scats <- c("Pregnancy","Sexual health","Sick child","Immunization","Nutrition","Tuberculosis","Malaria","First Aid","NCDs","Mental health","NTDs","HIV","Family planning","FI", "Adult Outpatient")
names(sc_tritanopia_colors) <- scats
sc_fillScale <- scale_fill_manual(name = "ServiceCat",values = sc_tritanopia_colors)
sc_colorScale <- scale_color_manual(name = "ServiceCat",values = sc_tritanopia_colors)
#ServiceLabel Colors
sl_tritanopia_colors <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf", "#aec7e8", "#ffbb78", "#98df8a", "#c5b0d5", "#ff9896", "#ff9896", "#9edae5")
slabs <- c("Pregnancy","Sexual health","IMNCI","RI","Nutri","Tuberculosis","Malaria","First Aid","NCDs","Mental health","NTDs","HIV","FP","FI", "Adult Outpatient")
names(sl_tritanopia_colors) <- slabs
sl_fillScale <- scale_fill_manual(name = "ServiceLabel",values = sl_tritanopia_colors)
#ClinicalCategory Colors
cc_tritanopia_colors <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf", "#aec7e8")
ccats <- c("Non-health","Supportive","Clinical - Acute","Clinical - Chronic","Clinical - Preventative","Non-clinical","Clinical - Promotive","Surveillance","Overhead")
names(cc_tritanopia_colors) <- ccats
cc_fillScale <- scale_fill_manual(name = "Category",values = cc_tritanopia_colors)
cc_colorScale <- scale_color_manual(name = "Category",values = cc_tritanopia_colors)
#set hours per week for secondary axis
hrsperweek <- ServiceCat_Clinical$HrsPerWeek[1]
############################################################################################################################################
# time allocation by Clinical Category
temp_clin <- Mean_ClinCat %>%
filter(Year >= 2023 & Year <= 2035) %>%
dplyr::mutate(Category = case_when(
ClinicalOrNon != "Clinical" ~ ClinicalOrNon,
ClinicalOrNon == "Clinical" ~ paste("Clinical -", ClinicalCat))) %>%
dplyr::mutate(Alpha = case_when(
ClinicalOrNon == "Clinical" ~ 0.3,
ClinicalOrNon != "Clinical" ~ 1)) %>%
dplyr::mutate(Scenario_label = paste(Scenario_ID, format(BaselinePop, big.mark = ","),"Starting Pop", sep=" "))
temp_clin$Category <- factor(temp_clin$Category,ordered=TRUE,levels=unique(temp_clin$Category))
temp_total <- Mean_Total %>%
filter(Year >= 2023 & Year <= 2035) %>%
dplyr::mutate(Scenario_label = paste(Scenario_ID, format(BaselinePop, big.mark = ","),"Starting Pop", sep=" "))
ylabel <- "Hours per Week per Catchment Pop"
maxyval <- max(Mean_Total$CI95/Mean_Total$WeeksPerYr)*1.05
###Original
ggplot()+
geom_bar(data = temp_clin, aes(x=Year,y=MeanHrs/WeeksPerYr,fill=Category),stat="identity",alpha=.9)+
geom_line(data = temp_total, aes(x=Year,y=MeanHrs/WeeksPerYr),linewidth=1.2)+
geom_point(data = temp_total, aes(x=Year,y=MeanHrs/WeeksPerYr))+
geom_errorbar(data =temp_total, aes(x=Year,ymin=CI05/WeeksPerYr, ymax=CI95/WeeksPerYr), colour="black", width=.3)+
ylim(0,maxyval)+
theme_bw()+
scale_x_continuous(breaks = seq(2021,2035))+
theme(legend.title=element_blank(),axis.text.x = element_text(angle=-90, vjust = .5, hjust=1))+
scale_fill_viridis_d()+
facet_wrap(~Scenario_label)+
ylab(ylabel) + xlab("") + labs(title = paste("Time Allocation by Clinical Category"))
###New Pierre
ggplot()+
geom_bar(data = temp_clin, aes(x=Year,y=MeanHrs/WeeksPerYr,fill=Category),stat="identity",alpha=.9)+
geom_line(data = temp_total, aes(x=Year,y=MeanHrs/WeeksPerYr),linewidth=1.2)+
geom_point(data = temp_total, aes(x=Year,y=MeanHrs/WeeksPerYr))+
geom_errorbar(data =temp_total, aes(x=Year,ymin=CI05/WeeksPerYr, ymax=CI95/WeeksPerYr), colour="black", width=.3)+
ylim(0,maxyval)+
theme_bw()+
scale_x_continuous(breaks = seq(2021,2035))+
scale_y_continuous(sec.axis = sec_axis(~ . / hrsperweek, name = "# HCWs"))+
theme(legend.title=element_blank(),axis.text.x = element_text(angle=-90, vjust = .5, hjust=1))+
cc_fillScale+
facet_wrap(~Scenario_label)+
ylab(ylabel) + xlab("") + labs(title = paste("Time Allocation by Clinical Category"))
####################################################################################################
# time allocation by Service Category, Bar Plot
#Colour code for different categories
my_palette <- viridis(13, option = "D", direction = -1, begin = 0, end = 1, alpha = 1)
ServiceCat_Clinical <- Mean_ServiceCat %>%
subset(ClinicalOrNon=="Clinical") %>%
filter(Year >= 2021 & Year <= 2035) %>%
dplyr::mutate(Scenario_label = paste(Scenario_ID, format(BaselinePop, big.mark = ","),"Starting Pop", sep=" ")) %>%
group_by(Scenario_ID, Year) %>%
dplyr::mutate(TotalHrs=sum(MeanHrs))
temp_TotClin <- Stats_TotClin %>%
filter(Year >= 2021 & Year <= 2035) %>%
dplyr::mutate(Scenario_label = paste(Scenario_ID, format(BaselinePop, big.mark = ","),"Starting Pop", sep=" "))
ymax <- max(temp_TotClin$CI95/temp_TotClin$WeeksPerYr)*1.05
###Original
ggplot() +
theme_bw()+
geom_bar(data=ServiceCat_Clinical,aes(x=Year,y=MeanHrs/WeeksPerYr,fill=ServiceCat),stat="identity",alpha=.9)+
geom_line(data=temp_TotClin,aes(x=Year,y=CI50/WeeksPerYr),linewidth=1.2)+
geom_point(data=temp_TotClin,aes(x=Year,y=CI50/WeeksPerYr))+
geom_errorbar(data=temp_TotClin,aes(x=Year,ymin=CI05/WeeksPerYr, ymax=CI95/WeeksPerYr), colour="black", width=.3)+
ylim(0, ymax) +
facet_wrap(~Scenario_label) +
scale_x_continuous(breaks = c(2021,2025, 2030, 2035))+
theme(legend.title=element_blank(),legend.position = c(0.02, 1), legend.justification = c(0.02, 1),
legend.key.size=unit(0.3, 'cm'), legend.direction="vertical", legend.background = element_rect(fill = 'transparent'))+
scale_fill_manual(values = my_palette)+
labs(x="Year", y="Hours per Week per Catchment Pop") + labs(title = paste("Time Allocation by Service Category"))
###New Pierre
ggplot() +
theme_bw()+
geom_bar(data=ServiceCat_Clinical,aes(x=Year,y=MeanHrs/WeeksPerYr,fill=ServiceCat),stat="identity",alpha=.9)+
geom_line(data=temp_TotClin,aes(x=Year,y=CI50/WeeksPerYr),linewidth=1.2)+
geom_point(data=temp_TotClin,aes(x=Year,y=CI50/WeeksPerYr))+
geom_errorbar(data=temp_TotClin,aes(x=Year,ymin=CI05/WeeksPerYr, ymax=CI95/WeeksPerYr), colour="black", width=.3)+
ylim(0, ymax) +
facet_wrap(~Scenario_label) +
scale_x_continuous(breaks = c(2021,2025, 2030, 2035))+
scale_y_continuous(sec.axis = sec_axis(~ . / hrsperweek, name = "# HCWs"))+
theme(legend.title=element_blank(),legend.position = c(0.02, 1), legend.justification = c(0.02, 1),
legend.key.size=unit(0.3, 'cm'), legend.direction="vertical", legend.background = element_rect(fill = 'transparent'))+
sc_fillScale +
labs(x="Year", y="Hours per Week per Catchment Pop") + labs(title = paste("Time Allocation by Service Category"))
####################################################################################################
# time allocation by Service Category, Tile Plot
unique(Mean_ServiceCat$ServiceCat)
temp_ServiceCat <- Mean_ServiceCat %>%
filter(Year == 2035) %>%
filter(ClinicalOrNon == "Clinical") %>%
dplyr::mutate(ServiceLabel = case_when(
ServiceCat == "Family planning" ~ "FP",
ServiceCat == "Immunization" ~ "RI",
ServiceCat == "Nutrition" ~ "Nutri",
ServiceCat == "Sick child" ~ "IMNCI",
T ~ ServiceCat))
###Original
ggplot(temp_ServiceCat,aes(area=MeanHrs,fill=ServiceLabel,label=ServiceLabel,subgroup=ServiceLabel))+
geom_treemap()+geom_treemap_text(color="black",place="center",size=16)+
geom_treemap_subgroup_border(color="black",size=2.5)+
facet_wrap(~Scenario_ID) +
theme_bw()+theme(legend.position = "none")+
scale_fill_viridis_d()
###New Pierre
ggplot(temp_ServiceCat,aes(area=MeanHrs,fill=ServiceLabel,label=ServiceLabel,subgroup=ServiceLabel))+
geom_treemap()+geom_treemap_text(color="black",place="center",size=16)+
geom_treemap_subgroup_border(color="black",size=2.5)+
facet_wrap(~Scenario_ID) +
theme_bw()+theme(legend.position = "none")+
sl_fillScale
####################################################################################################
###@Julius - Assuming this is not being used
# time allocation by Cadre
unique(Mean_Alloc$RoleDescription)
Cadre_labelled <- Mean_Alloc %>%
filter(CI50!=0 & Year >= 2023 & Year <= 2035) %>%
group_by(Scenario_ID, Year) %>%
dplyr::mutate(sum_CI50 = sum(CI50), sum_CI05 = sum(CI05), sum_CI95 = sum(CI95))
ggplot(data=Cadre_labelled)+
geom_bar(aes(x=Year,y=CI50/WeeksPerYr,fill=RoleDescription),stat="identity",alpha=.9)+
geom_line(aes(x=Year,y=sum_CI50/WeeksPerYr),linewidth=1.2)+
geom_point(aes(x=Year,y=sum_CI50/WeeksPerYr))+
geom_errorbar(aes(x=Year,ymin=sum_CI05/WeeksPerYr, ymax=sum_CI95/WeeksPerYr), colour="black", width=.3)+
theme_bw()+
scale_x_continuous(breaks = c(2021,2025, 2030, 2035))+
theme(legend.title=element_blank(),legend.position = c(0.02, 0.99), legend.justification = c(0.02, 0.99),
legend.key.size=unit(0.3, 'cm'), legend.direction="vertical", legend.background = element_rect(fill = 'transparent'))+
scale_fill_brewer(palette = "Paired", direction = -1)+
facet_wrap(~Scenario_ID)+
labs(x="Year", y="Hours per Week per 5,000 Pop", fill = "Cadre", title = "Time allocation by Cadre")
####################################################################################################
###@Julius - Assuming this is not being used
# FTE calculation by Cadre
ymax = max(ceiling(Cadre_labelled$CI50/Cadre_labelled$WeeksPerYr/(Cadre_labelled$HrsPerWeek*Cadre_labelled$MaxUtilization))) + 1
Cadre_labelled$ScenarioLabel = paste(Cadre_labelled$Scenario_ID,":", Cadre_labelled$MaxUtilization, "MaxUtil")
unique(Cadre_labelled$ScenarioLabel)
ggplot(data=Cadre_labelled)+
geom_bar(aes(x=Year,y=ceiling(CI50/WeeksPerYr/(HrsPerWeek*MaxUtilization))),stat="identity",position="stack")+
theme_bw()+
scale_x_continuous(breaks = c(2021,2025,2030,2035))+
ylim(0,ymax)+
facet_grid(~ScenarioLabel)+
labs(x="Year",y="Total required staff count",title="Required staff count (total)")
ggplot(data=Cadre_labelled)+
geom_bar(aes(x=Year,y=ceiling(CI50/WeeksPerYr/(HrsPerWeek*MaxUtilization)),fill=RoleDescription),stat="identity",position="stack")+
theme_bw()+
scale_x_continuous(breaks = c(2021,2025,2030,2035))+
ylim(0,ymax)+
facet_grid(~ScenarioLabel)+
labs(x="Year",y="Minimum staff count",fill="Cadre",title="Minimum staff count by cadre")
ggplot(data=Cadre_labelled)+
geom_bar(aes(x=Year,y=ceiling(CI50/WeeksPerYr/(HrsPerWeek*MaxUtilization)),fill=RoleDescription),stat="identity")+
theme_bw()+
scale_x_continuous(breaks = c(2021,2025,2030,2035))+
ylim(0,ymax)+
facet_grid(RoleDescription~ScenarioLabel)+
labs(x="Year",y="Minimum staff count",fill="Cadre",title="Minimum staff count by cadre")
####################################################################################################
# service mix change over time, Line Plot
#Keep HIV
ServiceCat_Clinical <- Mean_ServiceCat %>%
subset(ClinicalOrNon=="Clinical" & ServiceCat!="") %>%
filter(Year >= 2021 & Year <= 2035) %>%
group_by(Scenario_ID, ServiceCat) %>%
dplyr::mutate(MeanHrs_Start = dplyr::first(MeanHrs), RatioTo1 = MeanHrs/MeanHrs_Start) %>%
dplyr::mutate(RatioLastYr = case_when(
Year == max(Year) ~ RatioTo1)) %>%
dplyr::mutate(RatioLabel = case_when(
Year == max(Year) ~ paste(ServiceCat, round(RatioTo1,1), sep = ",")))
ServiceCat_Clinical$ServiceCat = as.factor(ServiceCat_Clinical$ServiceCat)
yplotmax = max(ServiceCat_Clinical$RatioTo1)*1.02
yplotmin = min(ServiceCat_Clinical$RatioTo1)*0.98
###Original
ggplot(ServiceCat_Clinical,aes(x=Year,y=RatioTo1,group=ServiceCat) )+
geom_line(aes(color=ServiceCat), linewidth=1.1) +
geom_hline(yintercept = 1,color="black",linetype="dashed") +
theme_bw() +
scale_color_discrete()+
geom_text_repel(aes(x=max(Year)+.2,y=RatioLastYr,label=RatioLabel),color="darkgrey", max.overlaps =200, size=3.5,hjust=0)+
# geom_text(aes(x=max(Year)+.2,y=RatioLastYr,label=RatioLabel),color="darkgrey",size=3.5, hjust=0, nudge_x = 0.5) +
facet_wrap(~Scenario_ID) +
scale_x_continuous(breaks = seq(2021,2035),limits=c(2021,max(ServiceCat_Clinical$Year)+6)) +
scale_y_continuous(limits = c(yplotmin,yplotmax)) +
theme(legend.title = element_blank(), legend.position="bottom",axis.text.x = element_text(angle=-90, vjust = .5, hjust=1)) +
labs(x = "", y = "Ratio to Baseline Year")
###New Pierre
ggplot(ServiceCat_Clinical,aes(x=Year,y=RatioTo1,group=ServiceCat) )+
geom_line(aes(color=ServiceCat), linewidth=1.1) +
geom_hline(yintercept = 1,color="black",linetype="dashed") +
theme_bw() +
sc_colorScale+
geom_text_repel(aes(x=max(Year)+.2,y=RatioLastYr,label=RatioLabel),color="darkgrey", max.overlaps =200, size=3.5,hjust=0)+
# geom_text(aes(x=max(Year)+.2,y=RatioLastYr,label=RatioLabel),color="darkgrey",size=3.5, hjust=0, nudge_x = 0.5) +
facet_wrap(~Scenario_ID) +
scale_x_continuous(breaks = seq(2021,2035),limits=c(2021,max(ServiceCat_Clinical$Year)+6)) +
scale_y_continuous(limits = c(yplotmin,yplotmax)) +
theme(legend.title = element_blank(), legend.position="bottom",axis.text.x = element_text(angle=-90, vjust = .5, hjust=1)) +
labs(x = "", y = "Ratio to Baseline Year")
####################################################################################################
# seasonality plot with uncertainty bounds
Monthly_NonClinical <- Mean_ClinCat %>%
subset(ClinicalOrNon != "Clinical") %>%
group_by(Scenario_ID, Year) %>%
dplyr::summarize(NonClinical_Monthly = sum(MeanHrs)/12)
RatioToAvg_ByMonth <- ByRun_ClinMonth %>%
subset(Year == 2035) %>%
left_join(Monthly_NonClinical, by = c("Scenario_ID", "Year")) %>%
mutate(NonClinical_Monthly = replace_na(NonClinical_Monthly,0)) %>%
group_by(Scenario_ID, Trial_num, Year) %>%
dplyr::mutate(MeanMonthHrs = (mean(TotHrs)+NonClinical_Monthly), RatioToMean = (TotHrs+NonClinical_Monthly)/(MeanMonthHrs)) %>%
ungroup() %>%
group_by(Scenario_ID, Month) %>%
dplyr::summarize(RatioToMean_p05 = quantile(RatioToMean, 0.05),
RatioToMean_p25 = quantile(RatioToMean, 0.25),
RatioToMean_p50 = quantile(RatioToMean, 0.50),
RatioToMean_p75 = quantile(RatioToMean, 0.75),
RatioToMean_p95 = quantile(RatioToMean, 0.95))
ggplot(data=RatioToAvg_ByMonth)+
theme_bw()+
geom_ribbon(aes(x = Month, ymin = RatioToMean_p05, ymax = RatioToMean_p95), fill = "#80B1D3", alpha = 0.25)+
# geom_line(aes(x = Month, y=RatioToMean_p50),linewidth=1)+
geom_smooth(aes(x = Month, y=RatioToMean_p50), method ="loess", fill = "transparent", span=0.5, alpha = 0.25)+
geom_hline(yintercept = 1, color = "blue", linetype="dashed")+
scale_color_manual("#80B1D3")+
ylim(0.85, 1.15) +
scale_x_continuous(breaks = seq(1, 12))+
facet_wrap(~Scenario_ID)+
labs(x = "Month", y="Ratio of workload for the month to annual average")