generated from ncl-icb-analytics/ncl_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonth_version.R
420 lines (295 loc) · 13.1 KB
/
month_version.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
library(readxl)
library(NCLRtemplates)
library(Hmisc)
library(AER)
library(lme4)
library(tidyverse)
library(broom)
library(broom.mixed)
# Wilson score binomial ci function
prop_ci <- function(o, n, ci=0.95){
z <- qnorm(ci + ((1-ci)/2))
p <- o/n
q <- 1-p
plower <- ((2*o + z^2) - z * sqrt(z^2 + (4*o*q))) / (2*(n+z^2))
pupper <- ((2*o + z^2) + z * sqrt(z^2 + (4*o*q))) / (2*(n+z^2))
return(c(o/n,plower, pupper))
}
# overdispersion test
od_test<-function(model, ...){
sum(residuals(model, type="pearson")^2) / df.residual(model)
}
#read in
AE_balanced_scorecard_mn<-
read_excel("data/AE BS by month and 3-mth rolling.xlsx"
, col_types = c("date", "numeric", "text",
"numeric", "numeric", "text", "numeric",
"numeric")
, na = "NULL"
)
# See below for diagnostic script, but missing August A&E data despite having population data
# Removing to repeat below
AE_balanced_scorecard_mn <-
AE_balanced_scorecard_mn %>%
filter((year(month) == 2023 & month(month)==7))
#filter(!(year(month) == 2023 & month(month)==8))
AE_year_mn <-
AE_balanced_scorecard_mn %>%
group_by(Quintile) %>%
summarise(PERSONS = sum(PERSONS, na.rm = TRUE),
AE_ATTENDS = sum(AE_ATTENDS, na.rm = TRUE),
) %>%
mutate(prop = AE_ATTENDS/ PERSONS,
#binconf(AE_ATTENDS, PERSONS),
lcl = binconf(AE_ATTENDS, PERSONS)[,2],
ucl = binconf(AE_ATTENDS, PERSONS)[,3]
)
AE_year_mn %>%
ggplot(aes(Quintile, prop))+
geom_point()+
# Why is this drawing in the wrong orientation? Answer: they aren't it's the long hats and almost invisibly small bars
geom_errorbar(aes(ymax=ucl, ymin=lcl), col="red") +
theme_nclicb()
##### Create a couple of features
AE_balanced_scorecard_mn$deprived <- ifelse(AE_balanced_scorecard_mn$Quintile <2, 1,0)
AE_balanced_scorecard_mn$age_cat <- factor(AE_balanced_scorecard_mn$ageband)
AE_balanced_scorecard_mn$GP_Borough_Name <- factor(AE_balanced_scorecard_mn$GP_Borough_Name)
levels(AE_balanced_scorecard_mn$age_cat)
levels(AE_balanced_scorecard_mn$age_cat)
AE_balanced_scorecard_mn %>%
group_by(age_cat) %>%
summarise(sum(PERSONS),
sum(AE_ATTENDS))
# Date range
min(AE_balanced_scorecard_mn$month)
max(AE_balanced_scorecard_mn$month)
AE_balanced_scorecard_mn %>%
group_by(month) %>%
count()
#### Simple Poisson model first ####
model1_mn <- glm(AE_ATTENDS ~ age_cat + gender + deprived
+ offset(log(PERSONS))
, data=AE_balanced_scorecard_mn
, family="poisson", na.action = na.omit)
summary(model1_mn)
od_test(model1_mn)
##### Random-intercept for clustering at borough ####
# This is confounded with quantiles though, as there is a geographical component here.
# Wider problem is quantiles in general though, as a composite indicator.
model2_mn2 <- glmer(AE_ATTENDS ~ (1|GP_Borough_Name) + age_cat + factor(gender) + deprived #
# + nsk(month_no, knots = c(4,8))+
+offset(log(PERSONS))
, data=AE_balanced_scorecard_mn, family="poisson", na.action = na.omit
, control=glmerControl(optimizer="bobyqa",optCtrl=list(maxfun=2e8)))
summary(model2_mn2)
#model2
modelfit.all <- lme4::allFit(model2_mn2)
ss <- summary(modelfit.all)
od_test(model2_mn2)
tidy(model2_mn2, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE) %>%
filter(term == "deprived")
# Diagnosd the key issue here which is that we can't use the random intercept with this parameterisation
# , as both the PERSONS variable is based of GP populations, and the random intercept is GP. This means they are
# correlated and model becomes nearly unidentifiable. Better to pursue with NB2 structure without explict random effect
# because we are using PERSONS as a denomincator here in the offset.
#### Negative Binomial ####
library(MASS)
library(splines2)
model3_mn <- glm.nb(AE_ATTENDS ~ age_cat + gender + deprived + offset(log(PERSONS)), data=AE_balanced_scorecard_mn, na.action = na.omit)
model3_mn_s <- glm.nb(AE_ATTENDS ~ age_cat + factor(gender) + deprived
#+ nsk(month_no, knots = c(4,8))
+ offset(log(PERSONS))
, data=AE_balanced_scorecard_mn, na.action = na.omit)
summary(model3_mn)
summary(model3_mn_s)
anova(model3_mn_s)
od_test(model3_mn)
AIC(model2_mn2)
AIC(model3_mn_s)
od_test(model3_mn_s)
exp(coef(model3_mn))
exp(confint(model3_mn))
tidy(model3_mn, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE) %>%
filter(term == "deprived")
tidy(model3_mn_s, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE) %>%
filter(term == "deprived")
# Incidence rate ratio: deprived = 1.3536108
# The A&E admission rate is 1.35 (1.34 - 1.37) times higher, by rolling 12-month periods ending 01/01/2022 - 01/07/2023
library(sjPlot)
plot_model(model3_mn, ci.lvl = 0.95, line.size = 2)
library(glmmTMB)
model3_mn_s_rint <- glmmTMB(AE_ATTENDS ~ (1|GP_Borough_Name) + age_cat + gender + deprived +
#nsk(month_no, knots = c(4,8))+
offset(log(PERSONS))
, data=AE_balanced_scorecard_mn, na.action = na.omit)
summary(model3_mn_s)
library(cAIC4)
cAIC(model2_mn2)
cAIC(model3_mn)
cAIC(model3_mn_s)
cAIC(model3_mn_s_rint)
# This wont' converge and calculated here due to the correlation between offset and random intercept.
od_test(model3_mn)
od_test(model3_mn_s)
od_test(model3_mn_s_rint)
#### Now per borough ###
mod_out1_mn <-
AE_balanced_scorecard_mn %>%
nest_by(GP_Borough_Name) %>%
mutate(mod = list(glm.nb(AE_ATTENDS ~ age_cat + gender + deprived +
nsk(month_no, knots = c(4,8)) + offset(log(PERSONS))
, data=data))) %>%
mutate(ci = list(confint(mod))) %>%
reframe(tidy(mod, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE)) %>%
mutate(adjustment = "Age, Sex, Seasonality")
mod_out1_mn %>%
filter(term == "deprived")
# What's going on here? Iteration limit reached, nothing for Enfield. Is there something missing in data?
a <- AE_balanced_scorecard_mn %>%
filter(GP_Borough_Name == "NHS Islington CCG")
b<- a %>%
group_by(month, age_cat, gender, deprived) %>%
summarise(ct = sum(AE_ATTENDS)) %>%
pivot_wider(names_from = c(age_cat, gender, deprived) , values_from = ct )
AE_balanced_scorecard_mn %>%
group_by(month, GP_Borough_Name) %>%
summarise(ATTENDS = sum(AE_ATTENDS),
PERSONS = sum(PERSONS)) %>%
filter(year(month) == 2023 & month(month) == 8)
# So it appears in this sample like there's no AE data from August, but we have denominator.
# Looping back up to top to add this a s filter.
mod_out2_mn <-
AE_balanced_scorecard_mn %>%
nest_by(GP_Borough_Name) %>%
mutate(mod = list(glm.nb(AE_ATTENDS ~ deprived +
nsk(month_no, knots = c(4,8))+ offset(log(PERSONS))
, data=data))) %>%
mutate(ci = list(confint(mod))) %>%
reframe(tidy(mod, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE)) %>%
mutate(adjustment = "Seasonality")
mod_out2_mn %>%
filter(term == "deprived")
mod_out3_mn <-
AE_balanced_scorecard_mn %>%
nest_by(GP_Borough_Name) %>%
mutate(mod = list(glm.nb(AE_ATTENDS ~ deprived +
offset(log(PERSONS))
, data=data))) %>%
mutate(ci = list(confint(mod))) %>%
reframe(tidy(mod, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE)) %>%
mutate(adjustment = "None")
mod_out3_mn %>%
filter(term == "deprived")
mod_out1_mn %>%
bind_rows(mod_out2_mn, mod_out3_mn) %>%
filter(term == "deprived") %>%
ggplot(aes(x=GP_Borough_Name, y=estimate, colour=adjustment, fill = adjustment))+
geom_point(position = position_dodge(width = 0.8))+
geom_errorbar(aes(ymin=conf.low, ymax=conf.high, group=adjustment), position = position_dodge(width = 0.8))+
labs(y="Incidence Rate Ratio (IRR)", x="GP Borough Name",
colour = "Standardisation applied", fill ="Standardisation applied",
title = "Relative A&E attendance rate for deprived populations against others",
subtitle = "IMD Quntile 1 vs. 2-5, where 1 = rate is the same, >1 = deprived group is higher") +
scale_x_discrete(guide = guide_axis(n.dodge = 2))+
scale_y_continuous(limits=c(1, 1.6))+
scale_fill_ncl()+
scale_colour_ncl()+
theme_nclics()+
theme(text = element_text(size=16))
ggsave("./output/ae_attend_IRR.png", width = (758*2), height = (471*2), units = "px")
####### Same deal, but applying the quintiles 1 & 2 instead of just 1. ####
AE_balanced_scorecard_mn$deprived2 <- ifelse(AE_balanced_scorecard_mn$Quintile <3, 1,0)
model1_mn2 <- glm(AE_ATTENDS ~ age_cat + gender + deprived2 + offset(log(PERSONS))
, data=AE_balanced_scorecard_mn, family="poisson", na.action = na.omit)
summary(model1_mn2)
od_test(model1_mn2)
tidy(model1_mn2, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE) %>%
filter(term == "deprived2")
##### Random-intercept for clustering at borough ####
# This is confounded with quantiles though, as there is a geographical component here.
# Wider problem is quantiles in general though, as a composite indicator.
model2_mn2 <- glmer(AE_ATTENDS ~ (1|GP_Borough_Name) + age_cat + gender + deprived2 +
nsk(month_no, knots = c(4,8))+ offset(log(PERSONS))
, data=AE_balanced_scorecard_mn, family="poisson", na.action = na.omit
, control=glmerControl(optimizer="bobyqa",optCtrl=list(maxfun=2e5)))
summary(model2_mn2)
model2
od_test(model2_mn2)
tidy(model2_mn2, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE) %>%
filter(term == "deprived2")
#### Negative Binomial ####
library(MASS)
library(splines2)
model3_mn2 <- glm.nb(AE_ATTENDS ~ age_cat + gender + deprived2 + offset(log(PERSONS)), data=AE_balanced_scorecard_mn, na.action = na.omit)
model3_mn223 <- glm.nb(AE_ATTENDS ~ age_cat + gender + deprived2 + GP_Borough_Name +
nsk(month_no, knots = c(4,8)) +
offset(log(PERSONS))
, data=AE_balanced_scorecard_mn, na.action = na.omit)
summary(model3_mn2)
summary(model3_mn223)
anova(model3_mn22)
od_test(model3_mn2)
AIC(model3_mn)
AIC(model3_mn223)
od_test(model3_mn2)
exp(coef(model3_mn))
exp(confint(model3_mn))
tidy(model3_mn2, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE) %>%
filter(term == "deprived2")
tidy(model3_mn22, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE) %>%
filter(term == "deprived2")
# Incidence rate ratio: deprived = 1.3536108
# The A&E admission rate is 1.35 (1.34 - 1.37) times higher, by rolling 12-month periods ending 01/01/2022 - 01/07/2023
library(sjPlot)
plot_model(model3_mn, ci.lvl = 0.95, line.size = 2)
#### Now per borough ###
mod_out1_mn2 <-
AE_balanced_scorecard_mn %>%
nest_by(GP_Borough_Name) %>%
mutate(mod = list(glm.nb(AE_ATTENDS ~ age_cat + gender + deprived2 +
nsk(month_no, knots = c(4,8)) + offset(log(PERSONS))
, data=data))) %>%
mutate(ci = list(confint(mod))) %>%
reframe(tidy(mod, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE)) %>%
mutate(adjustment = "Age, Sex, Seasonality")
mod_out1_mn2 %>%
filter(term == "deprived2")
mod_out2_mn2 <-
AE_balanced_scorecard_mn %>%
nest_by(GP_Borough_Name) %>%
mutate(mod = list(glm.nb(AE_ATTENDS ~ deprived2 +
nsk(month_no, knots = c(4,8))+ offset(log(PERSONS))
, data=data))) %>%
mutate(ci = list(confint(mod))) %>%
reframe(tidy(mod, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE)) %>%
mutate(adjustment = "Seasonality")
mod_out2_mn2 %>%
filter(term == "deprived2")
mod_out3_mn2 <-
AE_balanced_scorecard_mn %>%
nest_by(GP_Borough_Name) %>%
mutate(mod = list(glm.nb(AE_ATTENDS ~ deprived2 +
offset(log(PERSONS))
, data=data))) %>%
mutate(ci = list(confint(mod))) %>%
reframe(tidy(mod, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE)) %>%
mutate(adjustment = "None")
mod_out3_mn2 %>%
filter(term == "deprived2")
mod_out1_mn2 %>%
bind_rows(mod_out2_mn2, mod_out3_mn2) %>%
filter(term == "deprived2") %>%
ggplot(aes(x=GP_Borough_Name, y=estimate, colour=adjustment, fill = adjustment))+
geom_point(position = position_dodge(width = 0.8))+
geom_errorbar(aes(ymin=conf.low, ymax=conf.high, group=adjustment), position = position_dodge(width = 0.8))+
labs(y="Incidence Rate Ratio (IRR)", x="GP Borough Name",
colour = "Standardisation applied", fill ="Standardisation applied",
title = "Relative A&E attendance rate for deprived populations against others",
subtitle = "IMD Quntile 1-2 vs. 3-5, where 1 = rate is the same, >1 = deprived group is higher") +
scale_x_discrete(guide = guide_axis(n.dodge = 2))+
scale_y_continuous(limits=c(1, 1.6))+
scale_fill_ncl(reverse = FALSE)+
scale_colour_ncl(reverse = FALSE)+
theme_nclics()+
theme(text = element_text(size=16))
ggsave("./output/ae_attend_IRR_IMD12.png", width = (758*2), height = (471*2), units = "px")