-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultilevel_modelling.Rmd
1307 lines (910 loc) · 36.6 KB
/
multilevel_modelling.Rmd
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
---
title: "Multilevel Modelling Screening Uptake"
author: "Simon Hailstone"
date: "20 May 2018"
output:
html_document:
number_sections: yes
toc: yes
---
# Set up
```{r setup, include=FALSE}
knitr::opts_chunk$set()
```
```{r, warning=FALSE, message=FALSE}
library("knitr")
library("ggplot2"); theme_set(theme_bw())
library("dplyr")
library("dbplyr")
library("tidyr")
library("reshape2")
library("fingertipsR")
library("RSQLite")
library("lme4")
library("lmerTest")
library("bbmle")
library("forcats")
library("rgdal")
library("rgeos")
library("maptools")
library("broom")
library("PerformanceAnalytics") # for scatterplot matrix
library("spdep") # for poly2nb
library("ape") # for moran's I
library("arm") # to get se.ranef function
library("kableExtra") # to make tables nicer
```
# Get data
```{r}
# Downloaded from NOMIS
lsoa_demographics <- read.csv(file="data\\lsoa_demographics.csv", stringsAsFactors=F)
lsoa_demographics <- melt(lsoa_demographics, id=1:3, measure=4:7, variable.name="Age_Band", value.name="Female_Population")
# Create age splits for each lsoa
lsoa_age_demographics <- lsoa_demographics %>%
group_by(Age_Band, LSOA) %>%
summarise("Female_Population"=sum(Female_Population)) %>%
spread(Age_Band, Female_Population)
# Create ethnicity splits for each lsoa
# could be done in more detail, keeping simple for now
lsoa_ethnic_demographics <- lsoa_demographics %>%
group_by(Ethnic_Cat, LSOA) %>%
summarise("Female_Population"=sum(Female_Population)) %>%
spread(Ethnic_Cat, Female_Population)
# tidy up!
# use sort(sapply(ls(),function(x){object.size(get(x))}))
rm(lsoa_demographics)
```
```{r}
# GP catchment female population for Jan 2017
# From: http://digital.nhs.uk/catalogue/PUB23139
gp_lsoa_catch <- read.csv(file="data\\gp-reg-patients-LSOA-alt-tall.csv", stringsAsFactors=F)
# remove instances where lsoa11 code missing
gp_lsoa_catch <- filter(gp_lsoa_catch, LSOA_CODE != "NO2011")
# calculate total LSOA population
LSOA_POP <- gp_lsoa_catch %>%
group_by(LSOA_CODE) %>%
summarise("TOTAL_FEMALE"=sum(FEMALE_PATIENTS))
# Calc percentage of total lsoa pop registered with each practice
gp_lsoa_catch <- gp_lsoa_catch %>%
left_join(LSOA_POP) %>%
mutate("PERCENTAGE_OF_LSOA"=FEMALE_PATIENTS/TOTAL_FEMALE)
# Calc weighted ethnicity
gp_lsoa_catch <- gp_lsoa_catch %>%
left_join(lsoa_ethnic_demographics, by=c("LSOA_CODE"="LSOA"))
gp_lsoa_catch[,9:13] <- (gp_lsoa_catch[,9:13] * gp_lsoa_catch$PERCENTAGE_OF_LSOA)
# Calc weighted age bands
gp_lsoa_catch <- gp_lsoa_catch %>%
left_join(lsoa_age_demographics, by=c("LSOA_CODE"="LSOA"))
gp_lsoa_catch[,14:17] <- (gp_lsoa_catch[,14:17] * gp_lsoa_catch$PERCENTAGE_OF_LSOA)
```
```{r}
# Create median age for each practice
# read in population by single year of age by lsoa
lsoa_syoa <- read.csv(file="data\\LSOA_SYOA.csv", stringsAsFactors=F)
# tidy the data
lsoa_syoa <- lsoa_syoa %>%
dplyr::select(-X) %>%
gather(-LSOA, key="Age", value="TOTAL_FEMALE") %>%
mutate("Age"=as.numeric(gsub("X","",Age)),
"TOTAL_FEMALE"=as.numeric(TOTAL_FEMALE))
# join to GP proportions
# Error: cannot allocate vector of size 547.3 Mb
# gp_lsoa_catch %>%
# dplyr::select(PRACTICE_CODE, LSOA_CODE, PERCENTAGE_OF_LSOA) %>%
# left_join(lsoa_syoa, by=c("LSOA_CODE"="LSOA"))
# so try using a SQL lite database instead
mydb <- dbConnect(RSQLite::SQLite(), "medianAgeDB")
dbWriteTable(mydb, "lsoa_syoa", lsoa_syoa)
dbWriteTable(mydb, "gp_lsoa_catch", gp_lsoa_catch)
# dbListTables(mydb)
dbGetQuery(mydb,
"CREATE INDEX index_lsoa_age ON lsoa_syoa(LSOA)")
dbGetQuery(mydb,
"CREATE INDEX index_lsoa_catch ON gp_lsoa_catch(LSOA_CODE)")
dbGetQuery(mydb,
"
CREATE TABLE gp_age_profile AS
SELECT
gp.PRACTICE_CODE,
age.Age,
SUM(age.TOTAL_FEMALE) AS 'TOTAL_FEMALE'
FROM
gp_lsoa_catch gp
LEFT OUTER JOIN
lsoa_syoa age
ON
gp.LSOA_CODE = age.LSOA
GROUP BY
age.Age,
gp.PRACTICE_CODE
")
gp_age_profile <- dbGetQuery(mydb,
"
SELECT
PRACTICE_CODE,
Age,
TOTAL_FEMALE
FROM
gp_age_profile
")
gp_age_profile <- gp_age_profile %>%
#filter(PRACTICE_CODE %in% c("A86037", "A87003")) %>%
dplyr::select(PRACTICE_CODE,
Age,
TOTAL_FEMALE) %>%
group_by(PRACTICE_CODE) %>%
arrange(PRACTICE_CODE, Age) %>%
mutate(
"MEDIAN_POINT"=sum(TOTAL_FEMALE)/2,
"ROLLING_TOTAL_FEMALE"=cumsum(TOTAL_FEMALE),
"TOTAL_FEMALE_RUN_PERCENT" = cumsum(TOTAL_FEMALE)/sum(TOTAL_FEMALE)
) %>%
filter(TOTAL_FEMALE_RUN_PERCENT >= 0.5) %>%
group_by(PRACTICE_CODE) %>%
summarise("MEDIAN_AGE"=min(Age))
# Clean up sqlite database
dbDisconnect(mydb)
unlink("medianAgeDB.sqlite")
```
```{r}
# now group up to practice level
gp_demographics <- gp_lsoa_catch %>%
group_by(PRACTICE_CODE) %>%
summarise_at(funs(sum(.,na.rm = TRUE)) , .vars=c(8:16)) %>%
mutate("Total_Pop"=`Asian/Asian British` +
`Black/African/Caribbean/Black British` +
`Mixed/multiple ethnic group` +
`Other ethnic group` +
`White`) %>%
mutate(
"Ethn_Asian"=`Asian/Asian British` / Total_Pop * 100,
"Ethn_Black"=`Black/African/Caribbean/Black British` / Total_Pop * 100,
"Ethn_Mixed"=`Mixed/multiple ethnic group` / Total_Pop * 100,
"Ethn_Other"=`Other ethnic group` / Total_Pop * 100,
"Ethn_White"=`White` / Total_Pop * 100) %>%
mutate(
"Age_0_to_24"=Age_0_to_24 / Total_Pop * 100,
"Age_25_to_49"=Age_25_to_49 / Total_Pop * 100,
"Age_50_to_64"=Age_50_to_64 / Total_Pop * 100,
"Age_65_and_over"=Age_65_and_over / Total_Pop * 100
) %>%
dplyr::select(PRACTICE_CODE, starts_with("Ethn_"),starts_with("Age_"))
# tidy up!
# use sort(sapply(ls(),function(x){object.size(get(x))}))
rm(gp_lsoa_catch)
```
```{r}
# load fingertips data
# IMD 2015
imd_2015 <- fingertips_data(IndicatorID=91872,
AreaTypeID = 7)
imd_2015 <- imd_2015 %>% filter(AreaType=="GP") %>%
dplyr::select(AreaCode, "IMD_2015"=Value)
# Patient satisfaction with opening hours
satisfied_opening_hours <- fingertips_data(IndicatorID=1942,
AreaTypeID = 7)
satisfied_opening_hours <- satisfied_opening_hours %>%
filter(AreaType=="GP", Timeperiod=="2016/17") %>%
dplyr::select(AreaCode, "Satisfied_Opening_Hours"=Value)
```
```{r}
# Load coverage data
coverage_data <- read.csv(file="data\\Cerv_Cov_MachRead_GP_Q1_1718.csv", stringsAsFactors=F)
coverage_data <- coverage_data %>%
filter(Age=="25_49", Year=="2017/18", Quarter=="Q1") %>%
spread(DataType, Value) %>%
mutate("Coverage"=Screened/Eligible*100)
# drop supressed values
nrow(coverage_data)
# How many records are dropped
nrow(coverage_data %>%
filter(is.na(Coverage)==TRUE))
coverage_data <- coverage_data %>%
filter(is.na(Coverage)==FALSE)
nrow(coverage_data)
```
```{r}
# gp descriptive data
# gpp
gpp_info <- read.csv(file="data\\epraccur.csv", stringsAsFactors=FALSE, header=FALSE)
gpp_info <- gpp_info %>%
mutate("pcd_spaceless"=gsub(" ","", V10)) %>%
dplyr::select("practice_code"=V1, pcd_spaceless, "ccg_code" = V15)
gp_staffing <- read.csv("data\\General_Practice_March_2016_Practice_Level.csv", stringsAsFactors=FALSE)
# only select columns we need
gp_staffing <- gp_staffing %>% dplyr::select(PRAC_CODE,
TOTAL_PATIENTS,
TOTAL_GP_HC,
TOTAL_GP_FTE,
MALE_GP_FTE,
FEMALE_GP_FTE,
TOTAL_NURSES_FTE,
TOTAL_GP_HC_COQ_UK
) %>%
filter_at(vars(-PRAC_CODE),all_vars(. != "NS")) %>%
gather(-PRAC_CODE, key="COL", value="VALUE") %>%
mutate("VALUE"=as.numeric(VALUE)) %>%
spread(COL, VALUE) %>%
mutate(
"FEMALE_GP_PROPORTION"=FEMALE_GP_FTE/TOTAL_GP_FTE,
"NON_UKQ_GP_PROPORTION"=1-(TOTAL_GP_HC_COQ_UK/TOTAL_GP_HC)
) %>%
mutate(
"FEMALE_GP_PROPORTION"=ifelse(is.na(FEMALE_GP_PROPORTION),0,FEMALE_GP_PROPORTION),
"NON_UKQ_GP_PROPORTION"=ifelse(is.na(NON_UKQ_GP_PROPORTION),0,NON_UKQ_GP_PROPORTION)
)
```
```{r}
# load the spatial lookups
# postcode data
con = dbConnect(SQLite(), dbname="C:\\Users\\User1\\Documents\\Rstudio_Projects\\ONS_Lookup_Database\\ons_lkp_db")
# dbListTables(con)
pcd <- dbGetQuery(con, "SELECT pcd_spaceless, oseast1m, osnrth1m, lsoa11 FROM ONS_PD")
# limit the size of the postcode dataframe to only those postcodes in the gp practice data
pcd <- pcd %>% filter(pcd_spaceless %in% unique(gpp_info$pcd_spaceless))
# rural/urban lsoa classification
# https://ons.maps.arcgis.com/home/item.html?id=9855221596994bde8363a685cb3dd58a
urban_rural <- read.csv(file="data\\RUC11_LSOA11_EW.csv", stringsAsFactors=FALSE)
```
## Join data
```{r}
# combine it all
nrow(coverage_data)
coverage_data_supplemented <- coverage_data %>%
inner_join(gp_demographics, by=c("OrganisationCode"="PRACTICE_CODE"))
# ----------
nrow(coverage_data_supplemented)
# ----------
coverage_data_supplemented <- coverage_data_supplemented %>%
inner_join(imd_2015, by=c("OrganisationCode"="AreaCode"))
# ----------
nrow(coverage_data_supplemented)
# ----------
coverage_data_supplemented <- coverage_data_supplemented %>%
inner_join(satisfied_opening_hours, by=c("OrganisationCode"="AreaCode"))
# ----------
nrow(coverage_data_supplemented)
# ----------
#coverage_data_supplemented <- coverage_data_supplemented %>%
# inner_join(gp_info, by=c("OrganisationCode"="practice_code"))
coverage_data_supplemented <- coverage_data_supplemented %>%
inner_join(gp_staffing, by=c("OrganisationCode"="PRAC_CODE"))
# ----------
nrow(coverage_data_supplemented)
# ----------
coverage_data_supplemented <- coverage_data_supplemented %>%
inner_join(gpp_info, by=c("OrganisationCode"="practice_code"))
# ----------
nrow(coverage_data_supplemented)
# ----------
coverage_data_supplemented <- coverage_data_supplemented %>%
inner_join(pcd, by=c("pcd_spaceless"="pcd_spaceless"))
# ----------
nrow(coverage_data_supplemented)
# ----------
coverage_data_supplemented <- coverage_data_supplemented %>%
inner_join(urban_rural, by=c("lsoa11"="LSOA11CD"))
# ----------
nrow(coverage_data_supplemented)
# ----------
coverage_data_supplemented <- coverage_data_supplemented %>%
inner_join(gp_age_profile, by=c("OrganisationCode"="PRACTICE_CODE"))
# ----------
nrow(coverage_data_supplemented)
# ----------
# minor adjustments
coverage_data_supplemented <- coverage_data_supplemented %>%
mutate("osnrth100km"=osnrth1m/100000,
"IMD_2015_Rank"=dense_rank(IMD_2015),
"IMD_2015_Quintile"=ntile(IMD_2015, 5),
"gp_per_1k_eligible_women"=TOTAL_GP_FTE/Eligible*1000,
"Urban_Rural"=ifelse(substring(RUC11,1,3)=="Urb", "Urban", "Rural"),
"nurses_per_1k_eligible_women"=TOTAL_NURSES_FTE/Eligible*1000)
```
## Fix CCG coding
```{r}
# Add the ons ccg codes based on lsoa to prevent pain and heartache later on
# from https://ons.maps.arcgis.com/home/item.html?id=19e5c35c6a504a7b9e1b74bed1b6225f
ccg_lkp <- read.csv(file="data\\LSOA11_CCG16_LAD16_EN_LU.csv", stringsAsFactors=F)
coverage_data_supplemented <- coverage_data_supplemented %>%
left_join(ccg_lkp, by=c("lsoa11"="LSOA11CD"))
```
# Exploration
```{r, warning=FALSE, fig.width=10, fig.height=10}
# scatterplot matrix 1
PerformanceAnalytics::chart.Correlation(coverage_data_supplemented[,c(9:19)], method="pearson")
# scatterplot matrix 2
PerformanceAnalytics::chart.Correlation(coverage_data_supplemented[,c(9,20,23,26:29, 41, 43)], method="pearson")
```
```{r, warning=FALSE, message=FALSE}
ggplot(coverage_data_supplemented, aes(Eligible, Screened, col=Coverage)) +
geom_point(alpha=0.1) +
geom_smooth(method="lm", se=F, col="red") +
#scale_colour_gradient2(mid="#aaaaaa", high="#0571b0", low="#ca0020", midpoint=0.4) +
labs(title="Screened vs ELigible",
x="Eligible",
y="Screened")
ggplot(coverage_data_supplemented, aes(Urban_Rural, Coverage)) +
geom_boxplot(notch=T, fill="light blue") +
coord_flip() +
labs(title="Coverage Compared Across Urban and Rural Practices",
x="Urban or Rural Practice",
y="Coverage (%)")
ggplot(coverage_data_supplemented, aes(IMD_2015_Rank, Coverage)) +
geom_point(alpha=0.1) +
geom_smooth(method="lm", se=F, col="red") +
# facet_wrap(~RUC11) +
labs(title="Coverage by IMD 2015 Rank",
x="IMD 2015 Rank (1 = least deprived)",
y="Coverage (%)")
ggplot(coverage_data_supplemented, aes(osnrth100km, Coverage)) +
geom_point(alpha=0.25)+
facet_wrap(~Urban_Rural) +
geom_smooth(method="lm", se=F, col="red") +
labs(title="Coverage by Distance North",
x="Distance North from Origin of OSGB36 Grid (100km units)",
y="Coverage (%)")
ggplot(coverage_data_supplemented, aes(Satisfied_Opening_Hours, Coverage)) +
geom_point(alpha=0.3) +
geom_smooth(method="lm", se=F, col="red") +
labs(title="Coverage by Satisfaction With Opening Hours",
x="Satisfaction With Opening Hours (%)",
y="Coverage (%)")
ethn_plot_data <- gather(coverage_data_supplemented[,c(9:14)], key="Ethnic_Group", value="Percentage", -Coverage)
ggplot(ethn_plot_data, aes(Percentage, Coverage)) +
geom_point(alpha=0.3) +
facet_wrap(~Ethnic_Group, scale="free") +
geom_smooth(method="lm", se=F, col="red") +
labs(title="Coverage by Proportion of Ethnic Group Registrants (modelled)",
subtitle="Note that axes may vary across sub-plots",
x="Percentage of Registrants of Ethnic Group (%)",
y="Coverage (%)")
age_plot_data <- gather(coverage_data_supplemented[,c(9,15:18)], key="Age_Group", value="Percentage", -Coverage)
ggplot(age_plot_data, aes(Percentage, Coverage)) +
geom_point(alpha=0.3) +
facet_wrap(~Age_Group, scale="free") +
geom_smooth(method="lm", se=F, col="red") +
labs(title="Coverage by Proportion of Age Group Registrants (modelled)",
subtitle="Note that axes may vary across sub-plots",
x="Percentage of Registrants of Age Group (%)",
y="Coverage (%)")
ccg_sample <- sample(unique(coverage_data_supplemented$CCG16CD),20)
coverage_data_supplemented %>%
filter(CCG16CD %in% ccg_sample) %>%
ggplot(data=., aes(CCG16NM, Coverage)) +
geom_boxplot(fill="light blue") +
coord_flip() +
labs(title="Coverage Distribution by CCG",
subtitle="Based on practices from a random sample of 20 CCGs",
x="CCG Name",
y="Coverage (%)")
```
## Exploration of Median Age
```{r}
ggplot(coverage_data_supplemented, aes(MEDIAN_AGE, Coverage, col=IMD_2015_Quintile)) +
geom_jitter(alpha=0.2) +
geom_smooth(method="lm", se=F, col="red")
summary(lm(data=coverage_data_supplemented, Coverage ~ MEDIAN_AGE))
```
## Additional Exploration of Ethnicity Data
```{r, fig.width=9}
coverage_data_supplemented %>%
dplyr::select(OrganisationCode,
Coverage,
IMD_2015_Quintile,
Ethn_Asian,
Ethn_Black,
Ethn_Mixed,
Ethn_Other,
Ethn_White) %>%
gather(key="Ethnic_Group", value="Population_Proportion",-Coverage, -IMD_2015_Quintile, -OrganisationCode) %>%
mutate("Coverage"=Coverage/100,
"Population_Proportion"=Population_Proportion/100) %>%
ggplot(data=., aes(log(Population_Proportion), Coverage)) +
geom_point(alpha=0.1) +
geom_smooth(method="lm", se=F, col="red") +
facet_grid(IMD_2015_Quintile~Ethnic_Group) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1))
coverage_data_supplemented %>%
dplyr::select(OrganisationCode,
Coverage,
IMD_2015,
Ethn_Asian,
Ethn_Black,
Ethn_Mixed,
Ethn_Other,
Ethn_White) %>%
gather(key="Ethnic_Group", value="Population_Proportion",-Coverage, -IMD_2015, -OrganisationCode) %>%
mutate("Coverage"=Coverage/100,
"Population_Proportion"=Population_Proportion/100) %>%
ggplot(data=., aes(log(Population_Proportion), IMD_2015, col=Coverage)) +
geom_point(alpha=0.1) +
facet_wrap(~Ethnic_Group)
```
# Principal Component Analysis
```{r, fig.width=9}
# create a dataset of potential features to include in the model
pca_data <- coverage_data_supplemented[,c(
"Ethn_Asian",
"Ethn_Black",
"Ethn_Mixed",
"Ethn_Other",
"Ethn_White",
"Age_0_to_24",
"Age_25_to_49",
"Age_50_to_64",
"Age_65_and_over",
"IMD_2015_Rank",
"Satisfied_Opening_Hours",
"gp_per_1k_eligible_women",
"osnrth100km",
"FEMALE_GP_PROPORTION",
"NON_UKQ_GP_PROPORTION",
"nurses_per_1k_eligible_women",
"Eligible")]
# run PCA
pca_results <- prcomp(pca_data,
center = TRUE,
scale. = TRUE)
# PCA Results
summary(pca_results)
plot(pca_results)
# extract PCA rotation info
pca_rotation_df <- as.data.frame(pca_results$rotation)
pca_rotation_df$Var <- rownames(pca_rotation_df)
# calculate absolute contributions to each rotation
abs_pca_rotations <- abs(pca_results$rotation)
pca_abs_rotation_df <- as.data.frame(sweep(abs_pca_rotations, 2, colSums(abs_pca_rotations), "/"))
pca_abs_rotation_df$Var <- rownames(pca_abs_rotation_df)
# plot contributions
pca_abs_rotation_df %>%
gather(-Var, key=PC, value=value) %>%
mutate("PC"=factor(PC, ordered=T, levels=paste0("PC",1:20))) %>%
ggplot(data=., aes(PC, Var, fill=value)) +
geom_tile() +
geom_text(aes(label=paste0(round(value,2)*100,"%")), size=2.5) +
scale_fill_distiller(palette = "GnBu", direction=1)
```
# Additional Exploration to address reviewer comments
The following variables caused convergence issues in the model and will be investigated, any issues addressed and then the model rerun.
- gp_per_1k_eligible_women
- nurses_per_1k_eligible_women
- Eligible
- IMD_2015_Rank
## gp_per_1k_eligible_women
```{r}
coverage_data_supplemented %>%
ggplot(data=., aes(gp_per_1k_eligible_women, Coverage)) + geom_point(alpha=0.25)
coverage_data_supplemented %>%
dplyr::select(Organisation,
OrganisationCode,
Screened,
Eligible,
Coverage,
TOTAL_GP_FTE,
gp_per_1k_eligible_women) %>%
filter(gp_per_1k_eligible_women==0) %>%
summarise(n())
# 283 practices with 0 GPs
```
## nurses_per_1k_eligible_women
```{r}
coverage_data_supplemented %>%
ggplot(data=., aes(nurses_per_1k_eligible_women, Coverage)) + geom_point(alpha=0.25)
coverage_data_supplemented %>%
dplyr::select(Organisation,
OrganisationCode,
Screened,
Eligible,
Coverage,
TOTAL_GP_FTE,
nurses_per_1k_eligible_women) %>%
filter(nurses_per_1k_eligible_women==0) %>%
summarise(n())
# 291 practices with 0 nursing staff
```
### Are these the same practices?
```{r}
# Only 53 are zero for both staff groups
coverage_data_supplemented %>%
filter(nurses_per_1k_eligible_women==0 & gp_per_1k_eligible_women==0)
```
## Eligible and total population numbers
```{r}
## Eligible pop
# over view of relationship looks linear as would be expected
coverage_data_supplemented %>%
ggplot(data=., aes(Eligible, Screened)) + geom_point()
# look for extremes and logical inconsistencies
coverage_data_supplemented %>%
filter(Eligible == Screened | Eligible == 0)
# none noted
## Total pop
# interpret with caution, I don't use total patient numbers in the model,
# it comes from the workforce dataset which is based on a survey rather than 'actual' numbers registered
coverage_data_supplemented %>%
filter(TOTAL_PATIENTS==0 |
TOTAL_PATIENTS < Eligible |
TOTAL_PATIENTS < Screened)
# 2 practices
ggplot(coverage_data_supplemented, aes(TOTAL_PATIENTS, Eligible, colour=Coverage)) +
geom_point()
```
# Modelling
## Prep data
```{r}
# take a copy of the main dataset to use for modelling (some variables will be altered)
coverage_data_supplemented2 <- coverage_data_supplemented
# convert percentage variables by dividing by 100
coverage_data_supplemented2[,9:18] <- coverage_data_supplemented2[,9:18]/100
coverage_data_supplemented2[,10:18] <- as.vector(coverage_data_supplemented2[,10:18])
coverage_data_supplemented2$Satisfied_Opening_Hours <- as.vector(coverage_data_supplemented2$Satisfied_Opening_Hours/100)
coverage_data_supplemented2$CCG16CD <- factor(coverage_data_supplemented2$CCG16CD)
# No longer used, make any transformations or scalings here
# coverage_data_supplemented2$IMD_2015_Rank <- as.vector(coverage_data_supplemented2$IMD_2015_Rank)
# coverage_data_supplemented2$osnrth100km <- as.vector(coverage_data_supplemented2$osnrth100km)
# coverage_data_supplemented2$gp_number <- as.vector(coverage_data_supplemented2$gp_number)
# coverage_data_supplemented2$gp_per_1k_eligible_women <- as.vector(coverage_data_supplemented2$gp_per_1k_eligible_women)
# Remove practices with zero nurses
coverage_data_supplemented2 <- coverage_data_supplemented2 %>% filter(nurses_per_1k_eligible_women>0)
# Remove practices with more than 10 nurses per 1k
coverage_data_supplemented2 <- coverage_data_supplemented2 %>% filter(nurses_per_1k_eligible_women<10)
# Log transform nurses per 1k
coverage_data_supplemented2$nurses_per_1k_eligible_women_log <- log(coverage_data_supplemented2$nurses_per_1k_eligible_women)
# Log transform eligible
coverage_data_supplemented2$Eligible_log <- log(coverage_data_supplemented2$Eligible)
```
## Summary stats table
# Sumary statistics table
```{r}
summary_stats <- coverage_data_supplemented2 %>%
dplyr::select(OrganisationCode,
Eligible,
Screened,
Coverage,
starts_with("Ethn_"),
starts_with("Age_"),
IMD_2015,
IMD_2015_Rank,
Satisfied_Opening_Hours,
osnrth100km,
gp_per_1k_eligible_women,
nurses_per_1k_eligible_women,
FEMALE_GP_PROPORTION,
NON_UKQ_GP_PROPORTION,
TOTAL_PATIENTS,
TOTAL_NURSES_FTE) %>%
gather(key="Variable", value="Value",-OrganisationCode) %>%
group_by(Variable) %>%
summarise(
"n"=n(),
"mean"=mean(Value),
"sd"=sd(Value),
"min"=min(Value),
"percentile_25"=quantile(Value, probs=0.25),
"median"=median(Value),
"percentile_75"=quantile(Value, probs=0.75),
"max"=max(Value)
)
summary_stats %>%
kable(digits=2) %>%
kable_styling()
```
## Check multilevel structure
```{r}
# check for multilevel structure
# null multilevel model
fit_null_multi <- glmer(Coverage ~ (1 | CCG16CD),
family=binomial, weights=Eligible,
data=coverage_data_supplemented2)
# null single level model
fit_null_single <- glm(Coverage ~ 1,
family=binomial, weights=Eligible,
data=coverage_data_supplemented2)
# compare AIC
bbmle::AICtab(fit_null_single, fit_null_multi)
AIC(fit_null_single, fit_null_multi)
# Likelihood Ratio Testing
# create function to perform likelihood ratio test
# https://stat.ethz.ch/pipermail/r-sig-mixed-models/2008q3/001175.html
lrt <- function (obj1, obj2) {
L0 <- logLik(obj1)
L1 <- logLik(obj2)
L01 <- as.vector(- 2 * (L0 - L1))
df <- attr(L1, "df") - attr(L0, "df")
list(L01 = L01, df = df,
"p-value" = pchisq(L01, df, lower.tail = FALSE))
}
# perform a likelihood ratio test
lrt(fit_null_single,fit_null_multi)
# note that ANOVA gives identical results
anova(fit_null_multi, fit_null_single)
```
# Build GLMM model
```{r}
fit_2 <- glmer(Coverage ~
Ethn_Asian +
Ethn_Black +
Ethn_Mixed +
Ethn_Other +
Age_25_to_49 +
Age_65_and_over +
#IMD_2015 +
Satisfied_Opening_Hours +
osnrth100km +
Urban_Rural +
FEMALE_GP_PROPORTION +
NON_UKQ_GP_PROPORTION +
nurses_per_1k_eligible_women_log +
Eligible_log +
(1 | CCG16CD),
family=binomial(link=logit),
weights=Eligible,
data=coverage_data_supplemented2)
summary(fit_2)
# perform a likelihood ratio test
lrt(fit_null_multi, fit_2)
```
```{r}
plot(fitted(fit_2,type = "response"), residuals(fit_2))
```
## Map residuals too
```{r}
coverage_data_supplemented2$residual <- as.vector(residuals(fit_2))
x <- jitter(coverage_data_supplemented2$oseast1m, amount=0.01)
y <- jitter(coverage_data_supplemented2$osnrth1m, amount=0.01)
distMat <- as.matrix(dist(cbind(x, y)))
invDistMat <- 1/distMat
diag(invDistMat) <- 0
MI = ape::Moran.I(coverage_data_supplemented2$residual, weight = invDistMat)
MI
# tidy up!
# use sort(sapply(ls(),function(x){object.size(get(x))}))
rm(invDistMat)
rm(distMat)
```
## Extract model information
```{r, fig.width=10}
# Add the fitted values back to the main dataframe
coverage_data_supplemented2$fitted_values <- fitted(fit_2,type = "response")
coverage_data_supplemented2$fitted_values_logit <- fitted(fit_2,type = "logit")
#predict(fit_2, coverage_data_supplemented3, type = "response") - fitted_values
# Add residual quintiles
coverage_data_supplemented2 <- coverage_data_supplemented2 %>%
mutate("residual_quintile"=ntile(coverage_data_supplemented2$residual,5))
```
# Inspect random effects
```{r, fig.height=30, fig.width=10}
# caterpillar plot
# get random effects
random_effects <- ranef(fit_2)
random_intercept <- random_effects$cond
# get variances
random_effect_var <- TMB::sdreport(fit_2$obj, getJointPrecision=TRUE)
random_effect_sd <- sqrt(random_effect_var$diag.cov.random)
caterpillar_data <- data.frame(
"intercepts"=random_intercept$CCG16CD$`(Intercept)`,
"sd"=random_effect_sd,
"CCG16CD"=factor(row.names(random_effects$cond$CCG16CD))
)
# calc confidence interval
caterpillar_data$ucl <- caterpillar_data$intercepts + (caterpillar_data$sd * 1.96)
caterpillar_data$lcl <- caterpillar_data$intercepts - (caterpillar_data$sd * 1.96)
# categorise for colour coding in plot
caterpillar_data$category <- ifelse(caterpillar_data$lcl > 0, "High",