-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp1_script.R
1587 lines (1384 loc) · 57.4 KB
/
wp1_script.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# WP1 - community services strategy
library(tidyverse)
library(janitor)
library(DT)
library(sf)
library(readxl)
library(patchwork)
library(GGally)
library(ggVennDiagram)
library(ggvenn)
library(plotly)
library(reshape2)
library(ggrepel)
# Project specifications ----
# **Package 1: Aggregate population:**
#
# Aggregate activity (frail, emergency elderly admissions, end of life and falls)
#
# * Count of patients and activity (spells and bed days)
# * Variation across country: totals and time series
# * Patient characteristics: Demographic breakdown (age, sex, ethnicity and deprivation)
# * ICB and provider comparisons
#
# ICB-specific outputs of the above
#
# **Package 2: Variation in activity by ICB:**
#
# * "Does this measure tell us whether the systems in place are good or bad at treating mitigable activity?"
# * Age and gender adjusted activity rates by ICB
# * Contextual population analysis of healthcare needs/disease prevalence - deprivation weighted
# * Regression analysis - controlling for population disease prevalence
# * Link to CDSD (Jac CSDS repository)
#
#
# **Package 3: Overlap:**
#
# Overlap in mitigation populations?
#
#
# **Package 4 - survival analysis:**
#
# Survival analysis 1: time to readmission
#
# * what patient groups are likely to be readmitted post-acute care
#
#
# Survival analysis 2: mortality
#
# * What services were people in contact with in the lead up to death (1 year)
# * Might people have been better treated if not in the secondary sector
# * Compare location of deaths
library(tidyverse)
library(janitor)
library(DT)
library(sf)
library(readxl)
library(patchwork)
# Functions/lookups ----
create_dt <- function(x) {
DT::datatable(
x
, extensions = "Buttons"
, options = list(
dom = "Blfrtip"
, buttons = c("copy", "csv")
, lengthMenu = list(
c(10, 25, 50, -1)
, c(10, 25, 50, "All"))))
}
ethnicity_lookup <- tribble(
~Code, ~Description,
"A", "White - British",
"B", "White - Irish",
"C", "White - Any other White background",
"D", "Mixed - White and Black Caribbean",
"E", "Mixed - White and Black African",
"F", "Mixed - White and Asian",
"G", "Mixed - Any other mixed background",
"H", "Asian or Asian British - Indian",
"J", "Asian or Asian British - Pakistani",
"K", "Asian or Asian British - Bangladeshi",
"L", "Asian or Asian British - Any other Asian background",
"M", "Black or Black British - Caribbean",
"N", "Black or Black British - African",
"P", "Black or Black British - Any other Black background",
"R", "Other Ethnic Groups - Chinese",
"S", "Other Ethnic Groups - Any other ethnic group",
"Z", "Not stated"
)
# Set SU theme ----
SU_colours <- c(
`orange` = grDevices::rgb(248, 191, 7, maxColorValue = 255), # "#f9bf07",
`charcoal` = grDevices::rgb(44, 40, 37, maxColorValue = 255), # "#2c2825",
`slate` = grDevices::rgb(104, 111, 115, maxColorValue = 255), # "#686f73",
`blue` = grDevices::rgb(88, 29, 193, maxColorValue = 255), # "#5881c1",
`red` = grDevices::rgb(236, 101, 85, maxColorValue = 255), # "#ec6555",
# additional accent colours from word doc template
`yellow` = grDevices::rgb(252, 229, 155, maxColorValue = 255),
`grey` = grDevices::rgb(163, 168, 172, maxColorValue = 255),
`white` = grDevices::rgb(255, 255, 255, maxColorValue = 255),
# light and dark ends from colour theme in word doc
`light orange` = grDevices::rgb(253, 242, 205, maxColorValue = 255),
`dark orange` = grDevices::rgb(124, 95, 3, maxColorValue = 255),
`light charcoal` = grDevices::rgb(235, 233, 231, maxColorValue = 255),
`dark charcoal` = "#000000", # black
`light slate` = grDevices::rgb(224, 226, 227, maxColorValue = 255),
`dark slate` = grDevices::rgb(51, 55, 57, maxColorValue = 255),
`light blue` = grDevices::rgb(221, 229, 242, maxColorValue = 255),
`dark blue` = grDevices::rgb(38, 61, 102, maxColorValue = 255),
`light red` = grDevices::rgb(251, 224, 220, maxColorValue = 255),
`dark red` = grDevices::rgb(144, 29, 16, maxColorValue = 255),
`light yellow` = grDevices::rgb(254, 249, 235, maxColorValue = 255),
`dark yellow` = grDevices::rgb(197, 152, 5, maxColorValue = 255),
`light grey` = grDevices::rgb(236, 237, 238, maxColorValue = 255),
`dark grey` = grDevices::rgb(79, 84, 88, maxColorValue = 255),
`light white` = grDevices::rgb(242, 242, 242, maxColorValue = 255),
`dark white` = grDevices::rgb(127, 127, 127, maxColorValue = 255),
`red2` = grDevices::rgb(215, 25, 28, maxColorValue = 255),
`orange2` = grDevices::rgb(253, 174, 97, maxColorValue = 255),
`yellow2` = grDevices::rgb(255, 255, 191, maxColorValue = 255),
`green2` = grDevices::rgb(171, 221, 164, maxColorValue = 255),
`blue2` = grDevices::rgb(43, 131, 186, maxColorValue = 255) # "#2b83ba"
)
SU_cols <- function(...) {
cols <- c(...)
if (is.null(cols)) {
return(SU_colours)
}
SU_colours[cols]
}
SU_palettes <- list(
`main` = SU_cols("orange", "charcoal", "slate", "blue", "red"),
`oranges` = SU_cols("light orange", "orange", "dark orange"),
`slates` = SU_cols("light slate", "slate", "dark slate"),
`mixed` = SU_cols("dark red", "orange", "yellow", "light blue", "slate"),
`oj_coal` = SU_cols("yellow", "orange", "red", "dark red", "dark charcoal"),
`oj_red` = SU_cols("yellow", "orange", "red", "dark red"),
`white_oj_coal` = SU_cols("white", "yellow", "orange", "red", "dark red", "dark charcoal"), # added since shared
`lyellow_oj_coal` = SU_cols("light yellow", "orange", "red", "dark red", "dark charcoal"), # added since shared
`wy_oj_coal` = SU_cols("white", "light yellow", "yellow", "orange", "red", "dark red", "charcoal", "dark charcoal"),
`red_coal` = SU_cols("red", "dark red", "charcoal", "dark charcoal"),
`blue_yellow_red` = SU_cols("red2", "orange2", "yellow2", "green2", "blue2"),
`red_yellow_blue` = SU_cols("blue2", "green2", "yellow2", "orange2", "red2")
)
SU_pal <- function(palette = "main", reverse = FALSE, ...) {
pal <- SU_palettes[[palette]]
if (reverse) pal <- rev(pal)
colorRampPalette(pal, ...)
}
scale_color_SU <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) {
pal <- SU_pal(palette = palette, reverse = reverse)
if (discrete) {
discrete_scale("colour", paste0("SU_", palette), palette = pal, ...)
} else {
scale_color_gradientn(colours = pal(256), ...)
}
}
scale_fill_SU <- function(palette = "main", discrete = TRUE, reverse = FALSE, ...) {
pal <- SU_pal(palette = palette, reverse = reverse)
if (discrete) {
discrete_scale("fill", paste0("SU_", palette), palette = pal, ...)
} else {
scale_fill_gradientn(colours = pal(256), ...)
}
}
theme_SU <- function(base_size) {
theme_minimal(
# base_family = "Segoe UI",
base_size = 12
) %+replace%
theme(
axis.title = element_text(size = 11, face = "bold", colour = SU_cols("charcoal")),
plot.title = element_text(hjust = 0, face = "bold", size = 12, colour = SU_cols("charcoal"), margin = margin(b = 4, unit = "pt")),
plot.subtitle = element_text(hjust = 0, face = "italic", size = 10, colour = SU_cols("charcoal"), margin = margin(b = 4, unit = "pt")),
plot.caption = element_text(hjust = 0, face = "italic", size = 9, colour = SU_cols("slate"), margin = margin(b = 4, unit = "pt")),
legend.text = element_text(size = 10, colour = SU_cols("charcoal")),
legend.title = element_text(face = "bold", size = 11, colour = SU_cols("charcoal"), margin = margin(b = 4, unit = "pt"))
)
}
theme_set(theme_SU())
# Import data and split into sub-cohorts ----
aggregate_data <-
read_csv("aggregate_data_recombined.csv") |>
mutate(flag_der_falls =
case_when(!is.na(flag_fall_imp_frac) ~ flag_fall_imp_frac,
TRUE ~ flag_fall_imp_tend)) |>
filter(der_activity_month <= 202408) # August 2024 last full month
# Create dataset of core cohorts
aggregate_data_core_cohorts <-
aggregate_data |>
filter(!(is.na(flag_frail) & is.na(flag_eol) & is.na(flag_elderly_emergency) & is.na(flag_der_falls)))
aggregate_data_frail <-
aggregate_data %>%
drop_na(flag_frail) %>%
mutate(year = lubridate::year(month))
aggregate_data_eol <-
aggregate_data %>%
drop_na(flag_eol) %>%
mutate(year = lubridate::year(month))
aggregate_data_falls <-
aggregate_data %>%
drop_na(flag_der_falls) %>%
mutate(year = lubridate::year(month))
#aggregate_data_falls <-
# aggregate_data %>%
# filter(!is.na(flag_falls_exp) |
# !is.na(flag_fall_imp_frac) |
# !is.na(flag_fall_imp_tend)) %>%
# mutate(year = lubridate::year(month))
aggregate_data_elderly_emergency <-
aggregate_data %>%
drop_na(flag_elderly_emergency) %>%
mutate(year = lubridate::year(month))
aggregate_data_amb_chronic <-
aggregate_data %>%
drop_na(amb_chronic) %>%
mutate(year = lubridate::year(month))
aggregate_data_amb_acute <-
aggregate_data %>%
drop_na(amb_acute) %>%
mutate(year = lubridate::year(month))
aggregate_data_amb_vacc_prev <-
aggregate_data %>%
drop_na(amb_vacc_prev) %>%
mutate(year = lubridate::year(month))
aggregate_data_eol_1_year <-
aggregate_data %>%
drop_na(death_location_type) %>%
mutate(year = lubridate::year(month))
# Patient characteristics ----
# Patchwork: age-gender and ethnicity-deprivation
aggregate_data_elderly_emergency %>%
select(age_range) %>%
distinct()
aggregate_data_elderly_emergency %>%
drop_na(age_range) %>%
filter(year == 2023,
!age_range %in% c("100-104","105-109")) %>%
group_by(age_range, sex) %>%
summarise(pts = sum(person_n)) |>
mutate(pts_2 =
case_when(sex == 1 ~ 0-pts,
TRUE ~ pts)) |>
mutate(sex =
case_when(sex == 1 ~ "Male",
TRUE ~ "Female")) %>%
#mutate(age_range_text = paste(age_range, "-", age_range+4)) |>
ggplot(aes(x = pts_2, y = age_range, fill = sex)) +
geom_col() +
geom_vline(xintercept = 0) +
scale_fill_SU() +
scale_x_continuous(labels = function(x) scales::comma(abs(x))) +
theme(legend.position = "bottom",
plot.subtitle = element_text(hjust = 0.5)
) +
labs(x = "Patients",
y = "Age-range",
fill = "Sex:",
subtitle = "Age and sex distribution",
#subtitle = "2023"
)
patchwork_function <- function(data, title_text) {
# Plot 1
plot_1 <-
data %>%
drop_na(age_range) %>%
filter(year == 2023,
!age_range %in% c("100-104","105-109")) %>%
group_by(age_range, sex) %>%
summarise(pts = sum(person_n)) |>
mutate(pts_2 =
case_when(sex == 1 ~ 0-pts,
TRUE ~ pts)) |>
mutate(sex =
case_when(sex == 1 ~ "Male",
TRUE ~ "Female")) %>%
ggplot(aes(x = pts_2, y = age_range, fill = sex)) +
geom_col() +
geom_vline(xintercept = 0) +
scale_fill_SU() +
scale_x_continuous(labels = function(x) scales::comma(abs(x))) +
theme(legend.position = "bottom",
plot.subtitle = element_text(hjust = 0.5)
) +
labs(x = "Patients",
y = "Age-range",
fill = "Sex:",
subtitle = "Age and sex distribution",
)
# Plot 2
ethnicity_imd <-
data |>
drop_na(age_range) %>%
filter(year == 2023,
!age_range %in% c("100-104","105-109")) %>%
mutate(ethnic_group = str_sub(ethnic_group, 1,1)) %>%
left_join(ethnicity_lookup, by = c("ethnic_group" = "Code")) |>
group_by(Description, imd_decile) |>
summarise(person_n = sum(person_n)) |>
ungroup()
# Reshape the data for the heatmap
heatmap_data <- dcast(ethnicity_imd, Description ~ imd_decile, value.var = "person_n")
# Convert the data to a matrix
heatmap_matrix <- as.matrix(heatmap_data[,-1])
rownames(heatmap_matrix) <- heatmap_data$ethnic_group
plot_2 <-
ethnicity_imd |>
group_by(Description) |>
drop_na(Description) |>
mutate(prop = person_n/sum(person_n) * 100) |>
ggplot(aes(x = factor(imd_decile), y = Description, fill = prop)) +
geom_tile(alpha = 0.9) +
scale_fill_gradient(low = "#686f73", high = "#f9bf07") +
theme(legend.position = "bottom",
plot.subtitle = element_text(hjust = 0.5)
) +
labs(x = "IMD Decile",
y = "Ethnic Group",
subtitle = "IMD distribution in patient ethnicity groups",
fill = "Proportion (%)"
)
plot_1 + plot_2 +
plot_annotation(title = paste0("Sub-cohort demographics - ", title_text),
subtitle = "Mitigable SUS admissions | 2023")
}
patchwork_function(aggregate_data_elderly_emergency, "Emergency elderly patients")
patchwork_function(aggregate_data_frail, "Frail patients")
patchwork_function(aggregate_data_falls, "Falls patients")
patchwork_function(aggregate_data_eol, "End-of-life patients")
patchwork_function(aggregate_data_eol_1_year, "End of life care - patients with inpatient activity within 1 year of death")
patchwork_function(aggregate_data_amb_acute, "Ambulatory care - acute patients")
patchwork_function(aggregate_data_amb_chronic, "Ambulatory care - chronic patients")
patchwork_function(aggregate_data_amb_vacc_prev, "Ambulatory care - vaccine preventable patients")
# Top diagnoses by cohort
read_diagnosis_list <- function(cohort) {
data <-
read_csv(paste0("top_diagnoses/", cohort, ".csv")) |>
clean_names() |>
select(4,2) |>
rename(Admissions = 2) |>
arrange(desc(Admissions)) |>
mutate(Proportion = round(Admissions/sum(Admissions)*100, 2)) |>
rename(`Primary diagnosis` = icd10_l4_desc) |>
head(10) |>
mutate(Admissions = scales::comma(Admissions))
data
}
read_diagnosis_list("frail")
read_diagnosis_list("emergency_elderly")
read_diagnosis_list("falls")
read_diagnosis_list("eol")
read_diagnosis_list("amb_chronic")
read_diagnosis_list("amb_acute")
read_diagnosis_list("amb_vacc_prev")
read_diagnosis_list("eol_broad")
# Top diagnoses by cohort
read_procedures_list <- function(cohort) {
data <-
read_csv(paste0("top_procedures/", cohort, ".csv")) |>
clean_names() |>
select(4,2) |>
rename(Admissions = 2) |>
arrange(desc(Admissions)) |>
mutate(Proportion = round(Admissions/sum(Admissions)*100, 2)) |>
rename(`Primary procedure` = opcs_l4_desc) |>
head(10)
data
}
read_procedures_list("frail")
read_procedures_list("emergency_elderly")
read_procedures_list("falls")
read_procedures_list("eol")
read_procedures_list("amb_chronic")
read_procedures_list("amb_acute")
read_procedures_list("amb_vacc_prev")
read_procedures_list("eol_broad")
# Plot aggregate activity ----
aggregate_data_core_cohorts %>%
group_by(month) %>%
summarise(`1. Spells` = sum(spells),
`2. Bed days` = sum(los_sum)) %>%
pivot_longer(cols = -month) %>%
ggplot(aes(x = month, y = value, colour = name)) +
#geom_point() +
geom_line(linewidth = 1) +
facet_wrap(~name, scales = "free_y") +
scale_y_continuous(labels = scales::comma) +
scale_color_SU() +
theme(legend.position = "none",
strip.background = element_rect(fill = NA, colour = "grey")) +
labs(x = "Month",
y = "Spells",
title = "National trend in mitigable acute inpatient admissions",
subtitle = "SUS Apr 2018 - Aug 2024")
# v2
aggregate_data_core_cohorts %>%
group_by(month) %>%
summarise(`1. Spells` = sum(spells),
`2. Individuals` = sum(person_n),
`3. Bed days` = sum(los_sum)) %>%
pivot_longer(cols = -month) %>%
ggplot(aes(x = month, y = value, colour = name)) +
#geom_point() +
geom_line(linewidth = 1) +
facet_wrap(~name, scales = "free_y", nrow = (3)) +
scale_y_continuous(labels = scales::comma) +
scale_color_SU() +
theme(legend.position = "none",
strip.background = element_rect(fill = NA, colour = "grey")) +
labs(x = "Month",
y = "Spells",
title = "National trend in mitigable acute inpatient admissions",
subtitle = "SUS Apr 2018 - Aug 2024")
# Annual table
aggregate_data_core_cohorts %>%
mutate(year = lubridate::year(month)) %>%
group_by(year) %>%
summarise(`1. Spells` = sum(spells),
`2. Individuals` = sum(person_n),
`3. Bed days` = sum(los_sum)
) %>%
pivot_longer(cols = -year) %>%
pivot_wider(id_cols = year, names_from = name, values_from = value)
# By cohort
aggregate_data_frail %>%
group_by(month) %>%
summarise(frail_spells = sum(spells),
frail_ind = sum(person_n),
frail_bed_days = sum(los_sum)) %>%
left_join(
aggregate_data_eol %>%
group_by(month) %>%
summarise(eol_short_spells = sum(spells),
eol_short_ind = sum(person_n),
eol_short_bed_days = sum(los_sum)),
by = "month"
) %>%
left_join(
aggregate_data_falls %>%
group_by(month) %>%
summarise(falls_spells = sum(spells),
falls_ind = sum(person_n),
falls_bed_days = sum(los_sum)),
by = "month"
) %>%
left_join(
aggregate_data_elderly_emergency %>%
group_by(month) %>%
summarise(emergency_elderly_spells = sum(spells),
emergency_elderly_ind = sum(person_n),
emergency_elderly_bed_days = sum(los_sum)),
by = "month"
) %>%
#left_join(
# aggregate_data_amb_acute %>%
# group_by(month) %>%
# summarise(amb_acute_spells = sum(spells),
# amb_acute_bed_days = sum(los_sum)),
# by = "month"
#) %>%
#left_join(
# aggregate_data_amb_chronic %>%
# group_by(month) %>%
# summarise(amb_chronic_spells = sum(spells),
# amb_chronic_bed_days = sum(los_sum)),
# by = "month"
#) %>%
#left_join(
# aggregate_data_amb_vacc_prev %>%
# group_by(month) %>%
# summarise(amb_vaccine_spells = sum(spells),
# amb_vaccine_bed_days = sum(los_sum)),
# by = "month"
#) %>%
#left_join(
# aggregate_data_eol_1_year %>%
# group_by(month) %>%
# summarise(eol_1_year_spells = sum(spells),
# eol_1_year_bed_days = sum(los_sum)),
# by = "month"
#) %>%
pivot_longer(cols = -month) %>%
filter(month != as.Date("2024-10-01")) |>
mutate(type = case_when(str_detect(name, "bed_days") ~ "3. Bed days",
str_detect(name, "ind") ~ "2. Individuals",
TRUE ~ "1. Spells"),
Cohort = case_when(str_detect(name, "frail") ~ "1. Frail",
str_detect(name, "eol_short") ~ "3. End of life",
str_detect(name, "falls") ~ "4. Falls",
str_detect(name, "emergency_elderly") ~ "2. Emergency elderly",
str_detect(name, "amb_acute") ~ "5. Ambulatory - Acute",
str_detect(name, "amb_chronic") ~ "6. Ambulatory - Chronic",
str_detect(name, "amb_vacc") ~ "7. Ambulatory - Vaccine preventable",
str_detect(name, "eol_1_year") ~ "8. End of life - 1 year"
)
) %>%
ggplot(aes(x = month, y = value, colour = Cohort)) +
#geom_point() +
geom_line(linewidth = 1) +
facet_wrap(~type, scales = "free_y") +
scale_y_continuous(labels = scales::comma) +
scale_color_SU() +
theme(axis.text.x = element_text(angle = 90),
strip.background = element_rect(fill = NA, colour = "grey")) +
labs(x = "Month",
y = "Spells",
title = "National trend in acute inpatient admissions",
subtitle = "SUS Apr 2018 - Aug 2024")
# Free axis
aggregate_data_frail %>%
group_by(month) %>%
summarise(frail_spells = sum(spells),
frail_ind = sum(person_n),
frail_bed_days = sum(los_sum)) %>%
left_join(
aggregate_data_eol %>%
group_by(month) %>%
summarise(eol_short_spells = sum(spells),
eol_short_ind = sum(person_n),
eol_short_bed_days = sum(los_sum)),
by = "month"
) %>%
left_join(
aggregate_data_falls %>%
group_by(month) %>%
summarise(falls_spells = sum(spells),
falls_ind = sum(person_n),
falls_bed_days = sum(los_sum)),
by = "month"
) %>%
left_join(
aggregate_data_elderly_emergency %>%
group_by(month) %>%
summarise(emergency_elderly_spells = sum(spells),
emergency_elderly_ind = sum(person_n),
emergency_elderly_bed_days = sum(los_sum)),
by = "month"
) %>%
#left_join(
# aggregate_data_amb_acute %>%
# group_by(month) %>%
# summarise(amb_acute_spells = sum(spells),
# amb_acute_bed_days = sum(los_sum)),
# by = "month"
#) %>%
#left_join(
# aggregate_data_amb_chronic %>%
# group_by(month) %>%
# summarise(amb_chronic_spells = sum(spells),
# amb_chronic_bed_days = sum(los_sum)),
# by = "month"
#) %>%
#left_join(
# aggregate_data_amb_vacc_prev %>%
# group_by(month) %>%
# summarise(amb_vaccine_spells = sum(spells),
# amb_vaccine_bed_days = sum(los_sum)),
# by = "month"
#) %>%
#left_join(
# aggregate_data_eol_1_year %>%
# group_by(month) %>%
# summarise(eol_1_year_spells = sum(spells),
# eol_1_year_bed_days = sum(los_sum)),
# by = "month"
#) %>%
pivot_longer(cols = -month) %>%
mutate(type = case_when(str_detect(name, "bed_days") ~ "2. Bed days",
TRUE ~ "1. Spells"),
Cohort = case_when(str_detect(name, "frail") ~ "1. Frail",
str_detect(name, "eol_short") ~ "3. End of life",
str_detect(name, "falls") ~ "4. Falls",
str_detect(name, "emergency_elderly") ~ "2. Emergency elderly",
str_detect(name, "amb_acute") ~ "5. Ambulatory - Acute",
str_detect(name, "amb_chronic") ~ "6. Ambulatory - Chronic",
str_detect(name, "amb_vacc") ~ "7. Ambulatory - Vaccine preventable",
str_detect(name, "eol_1_year") ~ "8. End of life - 1 year"
)
) %>%
#mutate(Cohort = factor(Cohort, levels = "Frail", "Emergency elderly", "End of life", "Falls")) %>%
ggplot(aes(x = month, y = value, colour = type)) +
#geom_point() +
geom_line() +
facet_wrap(Cohort~type, scales = "free", ncol = 2) +
scale_y_continuous(labels = scales::comma) +
scale_color_SU() +
theme(axis.text.x = element_text(angle = 90),
strip.background = element_rect(fill = NA, colour = "grey"),
legend.position = "none") +
labs(x = "Month",
y = "Spells",
title = "National trend in acute inpatient admissions",
subtitle = "SUS Apr 2018 - Aug 2024")
# Free axis - v2
aggregate_data_frail %>%
group_by(month) %>%
summarise(frail_spells = sum(spells),
frail_ind = sum(person_n),
frail_bed_days = sum(los_sum)) %>%
left_join(
aggregate_data_eol %>%
group_by(month) %>%
summarise(eol_short_spells = sum(spells),
eol_short_ind = sum(person_n),
eol_short_bed_days = sum(los_sum)),
by = "month"
) %>%
left_join(
aggregate_data_falls %>%
group_by(month) %>%
summarise(falls_spells = sum(spells),
falls_ind = sum(person_n),
falls_bed_days = sum(los_sum)),
by = "month"
) %>%
left_join(
aggregate_data_elderly_emergency %>%
group_by(month) %>%
summarise(emergency_elderly_spells = sum(spells),
emergency_elderly_ind = sum(person_n),
emergency_elderly_bed_days = sum(los_sum)),
by = "month"
) %>%
pivot_longer(cols = -month) %>%
mutate(type = case_when(str_detect(name, paste(c("spells", "ind"), collapse = "|")) ~ "1. Spells & Individuals",
#str_detect(name, "bed_days") ~ "2. Bed days",
TRUE ~ "2. Bed days"),
Cohort = case_when(str_detect(name, "frail") ~ "Frail",
str_detect(name, "eol") ~ "End of life",
str_detect(name, "falls") ~ "Falls",
str_detect(name, "emergency") ~ "Emergency elderly"),
label = case_when(str_detect(name, "spells") ~ "1. Spells",
str_detect(name, "ind") ~ "2. Individuals",
str_detect(name, "bed_days") ~ "3. Bed days")
) %>%
#mutate(Cohort = factor(Cohort, levels = "Frail", "Emergency elderly", "End of life", "Falls")) %>%
ggplot(aes(x = month, y = value, colour = label, group = name)) +
#geom_point() +
geom_line(linewidth = 1) +
facet_wrap(Cohort~type, scales = "free", ncol = 2) +
scale_y_continuous(labels = scales::comma) +
scale_color_SU() +
theme(axis.text.x = element_text(angle = 90),
strip.background = element_rect(fill = NA, colour = "grey"),
#legend.position = "none"
) +
labs(x = "Month",
y = "Spells",
colour = "",
title = "National trend in acute inpatient admissions",
subtitle = "SUS Apr 2018 - Aug 2024")
# Table
aggregate_data_frail %>%
group_by(year) %>%
summarise(frail_spells = sum(spells),
frail_ind = sum(person_n),
frail_bed_days = sum(los_sum)) %>%
left_join(
aggregate_data_eol %>%
group_by(year) %>%
summarise(eol_spells = sum(spells),
eol_ind = sum(person_n),
eol_bed_days = sum(los_sum)),
by = "year"
) %>%
left_join(
aggregate_data_falls %>%
group_by(year) %>%
summarise(falls_spells = sum(spells),
falls_ind = sum(person_n),
falls_bed_days = sum(los_sum)),
by = "year"
) %>%
left_join(
aggregate_data_elderly_emergency %>%
group_by(year) %>%
summarise(emergency_elderly_spells = sum(spells),
emergency_elderly_ind = sum(person_n),
emergency_elderly_bed_days = sum(los_sum)),
by = "year"
) %>%
pivot_longer(cols = -year) %>%
mutate(value = scales::comma(value)) |>
pivot_wider(id_cols = year, names_from = name, values_from = value) |>
rename(
`Frail - spells` = frail_spells,
`Frail - individuals` = frail_ind,
`Frail - bed days` = frail_bed_days,
`EoL - spells` = eol_spells,
`EoL - individuals` = eol_ind,
`Eol - bed days` = eol_bed_days,
`Falls - spells` = falls_spells,
`Falls - individuals` = falls_ind,
`Falls - bed days` = falls_bed_days,
`Emergency elderly - spells` = emergency_elderly_spells,
`Emergency elderly - individuals` = emergency_elderly_ind,
`Emergency elder - bed days` = emergency_elderly_bed_days
)
## Map ----
# Read in icb lookup
icb_lookup <-
read_csv("mapping/S_ICB_ICB_to_NHSE_R.csv") %>%
clean_names() %>%
select(icb23cd, icb23cdh, icb23nm, nhser23nm) %>%
distinct()
# Import icb shp file from geoportal
icb_23_shp <-
st_read("https://services1.arcgis.com/ESMARspQHYMw9BZ9/arcgis/rest/services/Integrated_Care_Boards_April_2023_EN_BGC/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson") %>%
clean_names()
icb_23_shp %>%
select(icb23cd) %>%
left_join(
aggregate_data %>%
mutate(year = year(month)) %>%
filter(year == 2023) %>%
group_by(icb_name_short) %>%
summarise(spells = sum(spells)) %>%
mutate(icb_code = str_extract(icb_name_short, "^[^:]+")) %>%
left_join(icb_lookup, by = c("icb_code" = "icb23cdh")),
by = "icb23cd"
) %>%
# Plot chloropleth map
ggplot() +
geom_sf(aes(fill = spells)) +
scale_fill_gradient(low = "yellow", high = "red",
name = "Admissions",
labels = function(x) format(x, big.mark = ",", scientific = FALSE)) +
theme(panel.grid = element_blank(),
axis.text = element_blank()
) +
labs(title = "Mitigable activity by ICB",
subtitle = "SUS 2023 | Admission identified: Frail, emergency elderly, falls & end of life")
# ICB activity
aggregate_data %>%
group_by(month, icb_name_short) %>%
summarise(spells = sum(spells)) %>%
filter(icb_name_short != "NULL") %>%
mutate(icb_name_short = str_extract(icb_name_short, "(?<=NHS).*")) %>%
mutate(icb_name_short = str_remove_all(icb_name_short, " ICB")) |>
ggplot(aes(x = month, y = spells, group = icb_name_short)) +
#geom_point() +
geom_line() +
facet_wrap(~str_wrap(icb_name_short, 20), scales = "free_y") +
scale_y_continuous(labels = scales::comma) +
labs(x = "Month",
y = "Spells",
title = "ICB trends in mitigable acute inpatient admissions",
subtitle = "SUS Apr 2018 - Aug 2024")
aggregate_data %>%
group_by(month, icb_name_short) %>%
summarise(spells = sum(spells)) %>%
filter(icb_name_short != "NULL") %>%
mutate(icb_name_short = str_extract(icb_name_short, "(?<=NHS).*")) %>%
ggplot(aes(x = month, y = spells)) +
#geom_point() +
geom_line(aes(group = icb_name_short), colour = "#2c2825") +
geom_smooth(aes(group = 1), method = "loess", colour = "#f9bf07") +
scale_y_continuous(labels = scales::comma) +
labs(x = "Month",
y = "Spells",
title = "ICB trends in mitigable acute inpatient admissions",
subtitle = "SUS Apr 2018 - Aug 2024")
## Share of total activity ----
icb_spell_denominators <-
read_csv("icb_spell_denominators.csv") |>
clean_names()
aggregate_data |>
group_by(der_activity_month) |>
summarise(cohort_spells = sum(spells)) |>
left_join(icb_spell_denominators |>
group_by(der_activity_month) |>
summarise(denom_spells = sum(spells)),
by = c("der_activity_month")) |>
mutate(prop = round(cohort_spells/denom_spells * 100, 3)) |>
mutate(month =
as.Date(
paste0(str_sub(der_activity_month,1,4),
"-",
str_sub(der_activity_month,5,6),
"-01")
)
) |>
ggplot(aes(x = month, y = prop)) +
#geom_point() +
geom_line() +
geom_smooth(method = "loess", colour = "#f9bf07") +
labs(y = "Cohort proportion",
title = "Cohort spells as a proportion of total spells",
subtitle = "NHS England admissions denominator | 2018-24")
spell_proportion <- function(data, label_input) {
data |>
group_by(der_activity_month) |>
summarise(cohort_spells = sum(spells)) |>
left_join(icb_spell_denominators |>
group_by(der_activity_month) |>
summarise(denom_spells = sum(spells)),
by = c("der_activity_month")) |>
mutate(prop = round(cohort_spells/denom_spells * 100, 3)) |>
mutate(month =
as.Date(
paste0(str_sub(der_activity_month,1,4),
"-",
str_sub(der_activity_month,5,6),
"-01")
)
) |>
mutate(id = label_input) |>
select(month, cohort_spells, denom_spells, prop, id)
}
spell_proportion(aggregate_data_elderly_emergency, "1. Emergency elderly") |>
union_all(spell_proportion(aggregate_data_frail, "2. Frail")) |>
union_all(spell_proportion(aggregate_data_falls, "3. Falls")) |>
union_all(spell_proportion(aggregate_data_eol, "4. End of life")) |>
union_all(spell_proportion(aggregate_data_amb_acute, "5. Ambulatory - acute")) |>
union_all(spell_proportion(aggregate_data_amb_chronic, "6. Ambulatory - chronic")) |>
union_all(spell_proportion(aggregate_data_amb_vacc_prev, "7. Ambulatory - vaccine preventable")) |>
union_all(spell_proportion(aggregate_data_eol_1_year, "8. End of life - 1 year")) |>
ggplot(aes(x = month, y = prop, colour = id)) +
geom_point() +
geom_line() +
facet_wrap(~id, scales = "free_y"
) +
geom_smooth(method = "loess") +
scale_color_SU() +
theme(strip.background = element_rect(fill = NA, colour = "grey"),
axis.title.x = element_blank(),
legend.position = "none"
) +
labs(y = "Cohort proportion",
colour = "Cohort:",
title = "Spells as a proportion of total spells",
subtitle = "NHS England | 2018-24")
# ICB boxplot with outliers
icb_spells_proportion_total <-
aggregate_data |>
group_by(der_activity_month, icb_name_short) |>
summarise(cohort_spells = sum(spells)) |>
left_join(icb_spell_denominators, by = c("der_activity_month", "icb_name_short")) |>
mutate(prop = cohort_spells/spells * 100) |>
mutate(month =
as.Date(
paste0(str_sub(der_activity_month,1,4),
"-",
str_sub(der_activity_month,5,6),
"-01")
)
) |>
mutate(icb_name_clean = str_sub(icb_name_short, 10,100)) |>
mutate(icb_name_clean = str_remove_all(icb_name_clean, " ICB"))
icb_spells_proportion_total |>
drop_na(icb_name_clean) |>
ggplot(aes(x = month, y = prop, group = month,)) +
geom_boxplot(width = 5 ) +
#stat_boxplot(geom = "errorbar", width = 0.2) +
geom_line(data = icb_spells_proportion_total |>
filter(icb_name_clean %in% c(#"Frimley",
"Dorset",
"North Central London",
"Sussex",
"Somerset",
"North East London")),
aes(colour = icb_name_clean, group = icb_name_clean), linewidth = 1) +
scale_color_SU() +
theme(axis.title.x = element_blank()) +
labs(y = "Cohort proportion",
colour = "Outlier ICB:",
title = "Cohort admissions as a proportion of total activity",
subtitle = "ICB 2018-24")
# Activity by ICB ----
## Age and sex adjusted population rate ----
icb_pop_2023 <-
read_excel("pop_estimates/sapehealthgeogstablefinal.xlsx",
sheet = "Mid-2022 ICB 2023", skip = 3) %>%
clean_names()
# Clean and group by ICB, sex and age-range
icb_pop_2023_sex_age_range <-
icb_pop_2023 %>%
pivot_longer(cols = starts_with("f") | starts_with("m"),