-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path3d. Ratio.R
More file actions
179 lines (143 loc) · 7.13 KB
/
Copy path3d. Ratio.R
File metadata and controls
179 lines (143 loc) · 7.13 KB
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
## Ration of clock starts and completions
library(ggplot2)
library(data.table)
library(ggpubr)
source('2. Produce descriptive statistics.R')
## lockdown period for charts:
lockdown <- annotate('rect',
xmin = as.Date(c('2020-03-26', '2020-11-05', '2021-01-06')),
xmax = as.Date(c('2020-06-23', '2020-12-02', '2021-03-08')),
ymin = 0, ymax = Inf, alpha = 0.4)
## chart function to plot new clokc starts only
plot_ratio <- function(ccg_code = 'ENGLAND',
specialty,
quantiles = c(0.95, 0.50),
chart_title = ''){
new <- list()
adm <- list()
non <- list()
j <- 1
for (i in all_months){
new[[j]] <- dashboard_stats_ccg(monthyear=i,
ccg_code=ccg_code,
specialty=specialty,
quantiles=quantiles,
type='newRTT',
independent=0)
new[[j+1]] <- dashboard_stats_ccg(monthyear=i,
ccg_code=ccg_code,
specialty=specialty,
quantiles=quantiles,
type='newRTT',
independent=1)
adm[[j]] <- dashboard_stats_ccg(monthyear=i,
ccg_code=ccg_code,
specialty=specialty,
quantiles=quantiles,
type='completeadmitted',
independent=0)
adm[[j+1]] <- dashboard_stats_ccg(monthyear=i,
ccg_code=ccg_code,
specialty=specialty,
quantiles=quantiles,
type='completeadmitted',
independent=1)
non[[j]] <- dashboard_stats_ccg(monthyear=i,
ccg_code=ccg_code,
specialty=specialty,
quantiles=quantiles,
type='completenonadmitted',
independent=0)
non[[j+1]] <- dashboard_stats_ccg(monthyear=i,
ccg_code=ccg_code,
specialty=specialty,
quantiles=quantiles,
type='completenonadmitted',
independent=1)
j <- j + 2
}
result_new <- rbindlist(new) %>% .[, .(monthyear,
ccg,
ccg_name,
specialty,
type,
independent,
total.patients)]
result_fin <- rbind(rbindlist(adm), rbindlist(non)) %>% .[, .(monthyear,
ccg,
ccg_name,
specialty,
type,
independent,
total.patients)]
fin <- result_fin[, sum(total.patients), by = .(monthyear,
ccg,
ccg_name,
specialty,
independent)]
result_adm <- rbindlist(adm) %>% .[, .(monthyear,
ccg,
ccg_name,
specialty,
independent,
total.patients)]
result <- merge(result_new, fin, by = c('monthyear', 'ccg', 'ccg_name', 'specialty', 'independent'))
result <- merge(result, result_adm, by = c('monthyear', 'ccg', 'ccg_name', 'specialty', 'independent'))
comb <- result[, lapply(.SD, sum), by = .(monthyear,
ccg,
ccg_name,
specialty),
.SDcols = c('total.patients.x', 'V1', 'total.patients.y')]
comb$independent <- 'Total'
comb$type <- 'newRTT'
result <- rbind(result, comb)
result$date <- as.Date(paste0('01-',
substr(result$monthyear, 1, 3),
'-',
substr(result$monthyear, 4, 5)),
format = '%d-%b-%y')
result[, prop_all := total.patients.x / V1]
result[, Provider := fifelse(independent == 'IS', 'IS', fifelse(independent == 'Non-IS', 'NHS', 'Total'))]
p <- ggplot(result, aes(date, prop_all, color = Provider)) +
geom_line(aes(size = Provider)) +
scale_size_manual(values = c(IS = 1, NHS = 1, Total = 2)) +
theme_minimal() +
# scale_y_continuous(limits = c(0, max(result$prop_adm))) +
ggtitle(paste0('Ratio of new RTTs to completed pathways - ', specialty)) +
ylab('Pathways started for each one finished') +
geom_hline(yintercept = 1, color = 'grey', linetype = 'dashed') +
theme(plot.title = element_text(size = 10),
axis.title = element_text(size = 8)) +
lockdown +
scale_color_manual(values = c(IS = '#F8766D', NHS = '#619CFF', Total = 'black'))
result[, prop_adm := total.patients.x / total.patients.y]
#result[, prop_non := total.patients.x / (V1 - total.patients.y)]
q <- ggplot(result, aes(date, prop_adm, color = Provider)) +
geom_line(aes(size = Provider)) +
scale_size_manual(values = c(IS = 1, NHS = 1, Total = 2)) +
theme_minimal() +
# scale_y_continuous(limits = c(0, max(result$prop_adm))) +
ggtitle(paste0('Ratio of new RTTs to admitted pathways - ', specialty)) +
ylab('Pathways started for each one admitted') +
geom_hline(yintercept = 1, color = 'grey', linetype = 'dashed') +
theme(plot.title = element_text(size = 10),
axis.title = element_text(size = 8)) +
lockdown +
scale_color_manual(values = c(IS = '#F8766D', NHS = '#619CFF', Total = 'black'))
plot <- ggarrange(p, q, common.legend = TRUE, legend = 'bottom', align = 'hv')
return(plot)
}
n <- 1
## save new RTT charts
for (j in all_specialties){
plot_ratio(specialty = j)
ggsave(paste0('Charts/Ratio/Chart_', j, '.png'), plot = last_plot())
print(paste0('Saved ', n, ' of ', length(all_specialties)))
n <- n + 1
}
# move all plots to s3
s3sync(path = 'Charts/',
bucket = IHT_bucket,
prefix = 'RTT waiting times data/Charts/',
direction = 'upload')
unlink('Charts', recursive = TRUE)