-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline_quarterly_dec23.R
5047 lines (4506 loc) · 187 KB
/
pipeline_quarterly_dec23.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
# pipeline_quarterly.R
# this script provides the code to run the reproducible analytical pipeline
# and produce the Medicines Used in Mental Health (MUMH) quarterly publication
# contains the following sections:
# 1. Setup and package installation
# 2. Data import
# 3. Aggregations and analysis
# 4. Model of pre-COVID-19-pandemic prescribing trends
# 5. Data tables
# 6. Charts and figures
# 7. Render outputs
# clear environment
rm(list = ls())
# source functions
# select all .R files in functions sub-folder
function_files <- list.files(path = "functions", pattern = "\\.R$")
# loop over function_files to source all files in functions sub-folder
for (file in function_files) {
source(file.path("functions", file))
}
#1. Setup and package installation ---------------------------------------------
# load GITHUB_KEY if available in environment or enter if not
if (Sys.getenv("GITHUB_PAT") == "") {
usethis::edit_r_environ()
stop(
"You need to set your GITHUB_PAT = YOUR PAT KEY in the .Renviron file which pops up. Please restart your R Studio after this and re-run the pipeline."
)
}
# load database credentials if available in environment or enter if not
if (Sys.getenv("DB_DWCP_USERNAME") == "") {
usethis::edit_r_environ()
stop(
"You need to set your DB_DWCP_USERNAME = YOUR DWCP USERNAME and DB_DWCP_PASSWORD = YOUR DWCP PASSWORD in the .Renviron file which pops up. Please restart your R Studio after this and re-run the pipeline."
)
}
# check if Excel outputs are required
makeSheet <- menu(c("Yes", "No"),
title = "Do you wish to generate the Excel outputs?")
# install and load devtools package
install.packages("devtools")
library(devtools)
# install nhsbsaUtils package first to use function check_and_install_packages()
devtools::install_github(
"nhsbsa-data-analytics/nhsbsaUtils",
auth_token = Sys.getenv("GITHUB_PAT"),
force = TRUE
)
library(nhsbsaUtils)
# install required packages
# double check required packages once full pipeline built eg. if maps used
req_pkgs <- c(
"broom",
"data.table",
"devtools",
"DBI",
"dbplyr",
"dplyr",
"DT" ,
"geojsonsf",
"highcharter",
"htmltools",
"janitor",
"kableExtra",
"lubridate",
"logr",
"magrittr",
"nhsbsa-data-analytics/nhsbsaR",
"nhsbsa-data-analytics/nhsbsaExternalData",
"nhsbsa-data-analytics/accessibleTables",
"nhsbsa-data-analytics/nhsbsaDataExtract",
"nhsbsa-data-analytics/nhsbsaVis",
"openxlsx",
"rmarkdown",
"rsample",
"sf",
"stringr",
"svDialogs",
"tcltk",
"tidyr",
"tidyverse",
"vroom",
"yaml"
)
# library/install packages as required
nhsbsaUtils::check_and_install_packages(req_pkgs)
# set up logging
lf <-
logr::log_open(paste0(
"Y:/Official Stats/MUMH/log/mumh_log",
format(Sys.time(), "%d%m%y%H%M%S"),
".log"
))
# load config
config <- yaml::yaml.load_file("config.yml")
log_print("Config loaded", hide_notes = TRUE)
log_print(config, hide_notes = TRUE)
# load options
nhsbsaUtils::publication_options()
log_print("Options loaded", hide_notes = TRUE)
#2. Data import ----------------------------------------------------------------
con <- nhsbsaR::con_nhsbsa(dsn = "FBS_8192k",
driver = "Oracle in OraClient19Home1",
"DWCP")
schema <-
as.character(svDialogs::dlgInput("Enter schema name: ")$res)
# quarterly data extracts
capture_rate_extract_quarter <-
capture_rate_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
national_extract_quarter <- national_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
paragraph_extract_quarter <- paragraph_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
chem_sub_extract_quarter <- chem_sub_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
icb_extract_quarter <- icb_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
ageband_data_quarter <- ageband_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
gender_extract_quarter <- gender_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
age_gender_extract_quarter <- age_gender_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
imd_extract_quarter <- imd_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
child_adult_extract_quarter <- child_adult_extract(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "quarter"
)
log_print("Quarterly extracts pulled", hide_notes = TRUE)
# monthly data extracts
national_extract_monthly <- national_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "month"
)
paragraph_extract_monthly <- paragraph_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "month"
)
chem_sub_extract_monthly <- chem_sub_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "month"
)
age_gender_extract_month <- age_gender_extract_period(
con = con,
schema = schema,
table = config$sql_table_name,
period_type = "month"
)
log_print("Monthly extracts pulled", hide_notes = TRUE)
# disconnect from data warehouse once all extracts pulled
DBI::dbDisconnect(con)
# external data extracts
# mid-year England population by ageband and gender 2015 to 2022
pop_agegen_2022 <- national_pop_agegen() |>
dplyr::mutate(`Year` = as.double(`Year`)) |>
dplyr::filter(`Sex` != "All")
log_print("External data pulled", hide_notes = TRUE)
#3. Aggregations and analysis --------------------------------------------------
# 0401 Hypnotics and anxiolytics workbook - quarterly
quarterly_0401 <- list()
quarterly_0401$patient_id <- capture_rate_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0401") |>
tidyr::pivot_longer(cols = (`2015/2016 Q1`:`2023/2024 Q3`),
names_to = "Financial Quarter",
values_to = "Patient Identification (%)") |>
dplyr::relocate(`Financial Quarter`)
quarterly_0401$national_total <- national_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0401")
quarterly_0401$national_paragraph <- paragraph_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0401")
quarterly_0401$icb <- icb_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0401") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0401$gender <- gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0401") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0401$ageband <- ageband_data_quarter |>
dplyr::filter(`BNF Section Code` == "0401") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0401$age_gender <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0401") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0401$imd <- imd_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0401") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0401$monthly_section <- national_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0401")
quarterly_0401$monthly_paragraph <- paragraph_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0401") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0401$monthly_chem_substance <- chem_sub_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0401") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0401$avg_per_pat <- chem_sub_extract_monthly |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F) |>
dplyr::filter(`BNF Section Code` == "0401",
`Identified Patient Flag` == "Y") |>
dplyr::mutate(
`Average Items per patient` = (`Total Items` / `Total Identified Patients`),
`Average NIC per Patient (GBP)` = (`Total Net Ingredient Cost (GBP)` /
`Total Identified Patients`)
)
quarterly_0401$pat_per_1000_pop <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0401") |>
apply_sdc(
suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F
) |>
dplyr::ungroup() |>
dplyr::select(!c(`Total Items`, `Total Net Ingredient Cost (GBP)`)) |>
dplyr::mutate(`Mid-year Population Year` = as.numeric((substr(
c(`Financial Year`), 1, 4
))), .after = `Financial Year`) |>
dplyr::filter(`Identified Patient Flag` == "Y",
`Age Band` != "Unknown") |>
stats::na.omit() |>
dplyr::left_join(
select(
pop_agegen_2022,
`Year`,
`Sex`,
`Age Band`,
`Mid-year Population Estimate`
),
by = c(
"Mid-year Population Year" = "Year",
"Patient Gender" = "Sex",
"Age Band" = "Age Band"
)
) |>
dplyr::mutate(`Patients per 1,000 Population` = ((`Total Identified Patients` /
`Mid-year Population Estimate`) * 1000
))
# 0402 Drugs used in psychoses and related disorders workbook - quarterly
quarterly_0402 <- list()
quarterly_0402$patient_id <- capture_rate_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0402") |>
tidyr::pivot_longer(cols = (`2015/2016 Q1`:`2023/2024 Q3`),
names_to = "Financial Quarter",
values_to = "Patient Identification (%)") |>
dplyr::relocate(`Financial Quarter`)
quarterly_0402$national_total <- national_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0402")
quarterly_0402$national_paragraph <- paragraph_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0402")
quarterly_0402$icb <- icb_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0402") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0402$gender <- gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0402") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0402$ageband <- ageband_data_quarter |>
dplyr::filter(`BNF Section Code` == "0402") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0402$age_gender <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0402") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0402$imd <- imd_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0402") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0402$monthly_section <- national_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0402") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0402$monthly_paragraph <- paragraph_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0402") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0402$monthly_chem_substance <-
chem_sub_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0402") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0402$avg_per_pat <- chem_sub_extract_monthly |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F) |>
dplyr::filter(`BNF Section Code` == "0402",
`Identified Patient Flag` == "Y") |>
dplyr::mutate(
`Average Items per patient` = (`Total Items` / `Total Identified Patients`),
`Average NIC per Patient (GBP)` = (`Total Net Ingredient Cost (GBP)` /
`Total Identified Patients`)
)
quarterly_0402$pat_per_1000_pop <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0402") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F) |>
dplyr::ungroup() |>
dplyr::select(!c(`Total Items`, `Total Net Ingredient Cost (GBP)`)) |>
dplyr::mutate(`Mid-year Population Year` = as.numeric((substr(
c(`Financial Year`), 1, 4
))), .after = `Financial Year`) |>
dplyr::filter(`Identified Patient Flag` == "Y",
`Age Band` != "Unknown") |>
stats::na.omit() |>
dplyr::left_join(
select(
pop_agegen_2022,
`Year`,
`Sex`,
`Age Band`,
`Mid-year Population Estimate`
),
by = c(
"Mid-year Population Year" = "Year",
"Patient Gender" = "Sex",
"Age Band" = "Age Band"
)
) |>
dplyr::mutate(`Patients per 1,000 Population` = ((`Total Identified Patients` /
`Mid-year Population Estimate`) * 1000
))
# 0403 Antidepressants workbook - quarterly
quarterly_0403 <- list()
quarterly_0403$patient_id <- capture_rate_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0403") |>
tidyr::pivot_longer(cols = (`2015/2016 Q1`:`2023/2024 Q3`),
names_to = "Financial Quarter",
values_to = "Patient Identification (%)") |>
dplyr::relocate(`Financial Quarter`)
quarterly_0403$national_total <- national_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0403")
quarterly_0403$national_paragraph <- paragraph_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0403")
quarterly_0403$icb <- icb_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0403$gender <- gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0403$ageband <- ageband_data_quarter |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0403$age_gender <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0403$imd <- imd_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0403$prescribing_in_children <-
child_adult_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0403$monthly_section <- national_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0403$monthly_paragraph <- paragraph_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
# add chemical substance level in monthly tables if needed
quarterly_0403$monthly_chem_substance <-
chem_sub_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0403$avg_per_pat <- chem_sub_extract_monthly |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F) |>
dplyr::filter(`BNF Section Code` == "0403",
`Identified Patient Flag` == "Y") |>
dplyr::mutate(
`Average Items per patient` = (`Total Items` / `Total Identified Patients`),
`Average NIC per Patient (GBP)` = (`Total Net Ingredient Cost (GBP)` /
`Total Identified Patients`)
)
quarterly_0403$pat_per_1000_pop <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0403") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F) |>
dplyr::ungroup() |>
dplyr::select(!c(`Total Items`, `Total Net Ingredient Cost (GBP)`)) |>
dplyr::mutate(`Mid-year Population Year` = as.numeric((substr(
c(`Financial Year`), 1, 4
))), .after = `Financial Year`) |>
dplyr::filter(`Identified Patient Flag` == "Y",
`Age Band` != "Unknown") |>
stats::na.omit() |>
dplyr::left_join(
select(
pop_agegen_2022,
`Year`,
`Sex`,
`Age Band`,
`Mid-year Population Estimate`
),
by = c(
"Mid-year Population Year" = "Year",
"Patient Gender" = "Sex",
"Age Band" = "Age Band"
)
) |>
dplyr::mutate(`Patients per 1,000 Population` = ((`Total Identified Patients` /
`Mid-year Population Estimate`) * 1000
))
# 0404 CNS stimulants and drugs used for ADHD - quarterly
quarterly_0404 <- list()
quarterly_0404$patient_id <- capture_rate_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0404") |>
tidyr::pivot_longer(cols = (`2015/2016 Q1`:`2023/2024 Q3`),
names_to = "Financial Quarter",
values_to = "Patient Identification (%)") |>
dplyr::relocate(`Financial Quarter`)
quarterly_0404$national_total <- national_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0404")
quarterly_0404$national_chem_substance <-
chem_sub_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0404$icb <- icb_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0404$gender <- gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0404$ageband <- ageband_data_quarter |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0404$age_gender <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0404$imd <- imd_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0404$prescribing_in_children <-
child_adult_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0404$monthly_section <- national_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0404$monthly_chem_substance <-
chem_sub_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0404$avg_per_pat <- chem_sub_extract_monthly |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F) |>
dplyr::filter(`BNF Section Code` == "0404",
`Identified Patient Flag` == "Y") |>
dplyr::mutate(
`Average Items per patient` = (`Total Items` / `Total Identified Patients`),
`Average NIC per Patient (GBP)` = (`Total Net Ingredient Cost (GBP)` /
`Total Identified Patients`)
)
quarterly_0404$pat_per_1000_pop <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0404") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F) |>
dplyr::ungroup() |>
dplyr::select(!c(`Total Items`, `Total Net Ingredient Cost (GBP)`)) |>
dplyr::mutate(`Mid-year Population Year` = as.numeric((substr(
c(`Financial Year`), 1, 4
))), .after = `Financial Year`) |>
dplyr::filter(`Identified Patient Flag` == "Y",
`Age Band` != "Unknown") |>
stats::na.omit() |>
dplyr::left_join(
select(
pop_agegen_2022,
`Year`,
`Sex`,
`Age Band`,
`Mid-year Population Estimate`
),
by = c(
"Mid-year Population Year" = "Year",
"Patient Gender" = "Sex",
"Age Band" = "Age Band"
)
) |>
dplyr::mutate(`Patients per 1,000 Population` = ((`Total Identified Patients` /
`Mid-year Population Estimate`) * 1000
))
# 0411 Drugs for dementia workbook - quarterly
quarterly_0411 <- list()
quarterly_0411$patient_id <- capture_rate_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0411") |>
tidyr::pivot_longer(cols = (`2015/2016 Q1`:`2023/2024 Q3`),
names_to = "Financial Quarter",
values_to = "Patient Identification (%)") |>
dplyr::relocate(`Financial Quarter`)
quarterly_0411$national_total <- national_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0411")
quarterly_0411$national_chem_substance <-
chem_sub_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0411") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0411$icb <- icb_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0411") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0411$gender <- gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0411") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0411$ageband <- ageband_data_quarter |>
dplyr::filter(`BNF Section Code` == "0411") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0411$age_gender <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0411") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0411$imd <- imd_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0411") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0411$monthly_section <- national_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0411") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0411$monthly_chem_substance <-
chem_sub_extract_monthly |>
dplyr::filter(`BNF Section Code` == "0411") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F)
quarterly_0411$avg_per_pat <- chem_sub_extract_monthly |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F) |>
dplyr::filter(`BNF Section Code` == "0411",
`Identified Patient Flag` == "Y") |>
dplyr::mutate(
`Average Items per patient` = (`Total Items` / `Total Identified Patients`),
`Average NIC per Patient (GBP)` = (`Total Net Ingredient Cost (GBP)` /
`Total Identified Patients`)
)
quarterly_0411$pat_per_1000_pop <- age_gender_extract_quarter |>
dplyr::filter(`BNF Section Code` == "0411") |>
apply_sdc(suppress_column = "Total Identified Patients",
exclude_columns = "Year Month",
rounding = F) |>
dplyr::ungroup() |>
dplyr::select(!c(`Total Items`, `Total Net Ingredient Cost (GBP)`)) |>
dplyr::mutate(`Mid-year Population Year` = as.numeric((substr(
c(`Financial Year`), 1, 4
))), .after = `Financial Year`) |>
dplyr::filter(`Identified Patient Flag` == "Y",
`Age Band` != "Unknown") |>
stats::na.omit() |>
dplyr::left_join(
select(
pop_agegen_2022,
`Year`,
`Sex`,
`Age Band`,
`Mid-year Population Estimate`
),
by = c(
"Mid-year Population Year" = "Year",
"Patient Gender" = "Sex",
"Age Band" = "Age Band"
)
) |>
dplyr::mutate(`Patients per 1,000 Population` = ((`Total Identified Patients` /
`Mid-year Population Estimate`) * 1000
))
# 4. Model of pre-COVID-19-pandemic prescribing trends -------------------------
# model of item predictions extrapolated from pre-covid pandemic prescribing trends
# get dispensing days for up to 2023/24 financial year
dispensing_days_data <- nhsbsaUtils::dispensing_days(2024)
# aggregate to 20 year agebands and calculate month position variables
df20 <- ageband_manip_20yr(age_gender_extract_month)
# split df20 into pre-March-2020 months, train model on pre-pandemic data only
df20_pc <- df20 |>
dplyr::filter(time_period == "pre_covid")
# build model for each BNF section, using pre-pandemic items
model_0401 <- covid_lm(df20_pc,
section_code = "0401")
model_0402 <- covid_lm(df20_pc,
section_code = "0402")
model_0403 <- covid_lm(df20_pc,
section_code = "0403")
model_0404 <- covid_lm(df20_pc,
section_code = "0404")
model_0411 <- covid_lm(df20_pc,
section_code = "0411")
# predict number of items by BNF section per month
# predictions are extrapolations of pre-COVID-19 trends, applied to full df20 data
# so will only predict for March 2020 onwards
predictions_0401 <- prediction_list(df20,
"0401",
model_0401,
pred_month_list)
predictions_0402 <- prediction_list(df20,
"0402",
model_0402,
pred_month_list)
predictions_0403 <- prediction_list(df20,
"0403",
model_0403,
pred_month_list)
predictions_0404 <- prediction_list(df20,
"0404",
model_0404,
pred_month_list)
predictions_0411 <- prediction_list(df20,
"0411",
model_0411,
pred_month_list)
#save covid estimated items to excel wb for QR
covid_model_predictions_sep <- rbind(
predictions_0401,
predictions_0402,
predictions_0403,
predictions_0404,
predictions_0411
)
# update month in file name for new publications
fwrite(covid_model_predictions_sep, "Y:/Official Stats/MUMH/Covid model tables/Dec23.csv")
# 5. Data tables ---------------------------------------------------------------
# data tables for spreadsheet outputs
# formatted according to accessibility standards
# user may need to update file name to write outputs to in future releases
# 0401 Hypnotics and anxiolytics workbook - quarterly
sheetNames <- c(
"Patient_Identification",
"National_Total",
"National_Paragraph",
"ICB",
"Gender",
"Age_Band",
"Age_Band_and_Gender",
"Population_by_Age_Gender",
"IMD",
"Monthly_Section",
"Monthly_Paragraph",
"Monthly_Chemical_Substance",
"Average_Items_per_Patient"
)
wb <- accessibleTables::create_wb(sheetNames)
# create metadata tab (will need to open file and auto row heights once ran)
meta_fields <- c(
"BNF Section Name",
"BNF Section Code",
"Identified Patient Flag",
"Total Identified Patients",
"Total Items",
"Total Net Ingredient Cost (GBP)",
"Financial Year",
"Financial Quarter",
"Year Month",
"BNF Paragraph Name",
"BNF Paragraph Code",
"ICB Name",
"ICB Code",
"BNF Chemical Substance Name",
"BNF Chemical Substance Code",
"Patient Gender",
"Age Band",
"IMD Quintile",
"Average Items per Patient",
"Average NIC per patient (GBP)",
"Mid-Year England Population Estimate",
"Mid-Year Population Year",
"Patients per 1,000 Population"
)
meta_descs <-
c(
"The name given to a British National Formulary (BNF) section. This is the next broadest grouping of the BNF therapeutical classification system after chapter.",
"The unique code used to refer to the British National Formulary (BNF) section.",
"This shows where an item has been attributed to an NHS number that has been verified by the Personal Demographics Service (PDS).",
"Where patients are identified via the flag, the number of patients that the data corresponds to. This will always be 0 where 'Identified Patient' = N.",
"The number of prescription items dispensed. 'Items' is the number of times a product appears on a prescription form. Prescription forms include both paper prescriptions and electronic messages.",
"Total Net Ingredient Cost is the amount that would be paid using the basic price of the prescribed drug or appliance and the quantity prescribed. Sometimes called the 'Net Ingredient Cost' (NIC). The basic price is given either in the Drug Tariff or is determined from prices published by manufacturers, wholesalers or suppliers. Basic price is set out in Parts 8 and 9 of the Drug Tariff. For any drugs or appliances not in Part 8, the price is usually taken from the manufacturer, wholesaler or supplier of the product. This is given in GBP (£).",
"The financial year to which the data belongs.",
"The financial quarter to which the data belongs.",
"The year and month to which the data belongs, denoted in YYYYMM format.",
"The name given to the British National Formulary (BNF) paragraph. This level of grouping of the BNF therapeutical classification system sits below BNF section.",
"The unique code used to refer to the British National Formulary (BNF) paragraph.",
"The name given to the Integrated Care Board (ICB) that a prescribing organisation belongs to. This is based upon NHSBSA administrative records, not geographical boundaries and more closely reflect the operational organisation of practices than other geographical data sources.",
"The unique code used to refer to an Integrated Care Board (ICB).",
"The name of the main active ingredient in a drug. Appliances do not hold a chemical substance, but instead inherit the corresponding BNF section. Determined by the British National Formulatory (BNF) for drugs, or the NHSBSA for appliances. For example, Amoxicillin.",
"The unique code used to refer to the British National Formulary (BNF) chemical substance.",
"The gender of the patient as at the time the prescription was processed. Please see the detailed Background Information and Methodology notice released with this publication for further information.",
"The age band of the patient as of the 30th September of the corresponding financial year the drug was prescribed.",
"The IMD quintile of the patient, usually based on the patient's postcode, where '1' is the 20% of areas with the highest deprivation score in the Index of Multiple Deprivation (IMD) from the English Indices of Deprivation 2019, and '5' is the 20% of areas with the lowest IMD deprivation score. If no patient postcode is available but the postcode of the prescribing practice is found in the data, the practice postcode is used as a secondary way of assigning to an IMD quintile. The IMD quintile has been recorded as 'Unknown' where the items are attributed to an unidentified patient, or where we have been unable to match the patient or practice postcode to a postcode in the National Statistics Postcode Lookup (NSPL).",
"The total number of items divided by the total number of identified patients, by month and chemical substance. This only uses items that have been attributed to patients via the identified patient flag. This average refers to the mean.",
"The total Net Ingredient Cost divided by the total number of identified patients, by month and chemical substance. This only uses items that have been attributed to patients via the identified patient flag. This average refers to the mean, and is given in GBP (£).",
"The population estimate for the corresponding Mid-Year Population Year.",
"The year in which population estimates were taken, required due to the presentation of this data in financial year format.",
"The number of identified patients by age band and gender divided by mid-year population of the same age band and gender group in England, multiplied by 1,000. Only identified patients with a known gender and age band are included. This is calculated by (Total Identified Patients / Mid-Year England Population Estimate) * 1000."
)
accessibleTables::create_metadata(wb,
meta_fields,
meta_descs)
# patient identification
# write data to sheet
accessibleTables::write_sheet(
wb,
"Patient_Identification",
paste0(
config$publication_table_title,
" - Proportion of items for which an NHS number was recorded (%)"
),
c(
"The below proportions reflect the percentage of prescription items where a PDS verified NHS number was recorded."
),
quarterly_0401$patient_id,
42
)
# left align columns A to B
accessibleTables::format_data(wb,
"Patient_Identification",
c("A", "B"),
"left",
"")
# right align columns, round to 2 decimal places
accessibleTables::format_data(
wb,
"Patient_Identification",
c(
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"AA",
"AB",
"AC",
"AD",
"AE",
"AF",
"AG",
"AH",
"AI",
"AJ",
"AK"