forked from britishredcrosssociety/covid-19-vulnerability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep loneliness.r
241 lines (176 loc) · 9.38 KB
/
prep loneliness.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
##
## Load and process loneliness risks - Local Authorities
##
library(tidyverse)
library(janitor)
library(Hmisc)
source("init.r")
source("load lookup tables.r")
##
## load lookup tables
##
lookup_lsoa_msoa_lad = load_lookup_lsoa_msoa_lad()
lookup_lsoa_lad_ni = load_lookup_sa_lgd() %>% select(LSOA11CD, LAD19CD = LAD18CD) %>% distinct()
lad_17_19 = read_csv("data/LAD 2017 to LAD 2019 codes.csv") # lookup of Local Authority codes from 2017 to 2019
# wards
lookup_msoa_ward = load_lookup_msoa_ward(2017)
lookup_iz_ward = load_lookup_iz_ward()
##
## load loneliness risk data
##
# generated by code in https://github.com/matthewgthomas/loneliness
eng_msoa_loneliness = read_csv("https://github.com/matthewgthomas/loneliness/raw/master/England/msoa_loneliness.csv")
wal_msoa_loneliness = read_csv("https://github.com/matthewgthomas/loneliness/raw/master/Wales/msoa_loneliness.csv")
sco_msoa_loneliness = read_csv("https://github.com/matthewgthomas/loneliness/raw/master/Scotland/msoa_loneliness.csv")
ni_msoa_loneliness = read_csv("https://github.com/matthewgthomas/loneliness/raw/master/NI/msoa_loneliness.csv")
##
## England
##
# of the MSOAs with above-average loneliness risks (i.e. `loneills_2018` > 0), cut into two categories: lonely_q = 2 will be highest-risk category
eng_msoa_loneliness_high = eng_msoa_loneliness %>%
select(msoa11cd, loneills_2018) %>%
filter(loneills_2018 > 0) %>%
mutate(lonely_q = as.integer(cut2(loneills_2018, g = 2)))
# summarise loneliness risks into LADs - calculate proportion of highest-risk MSOAs (in the whole nation) within each LAD
lonely_eng = lookup_lsoa_msoa_lad %>%
# get full list of MSOAs
filter(startsWith(MSOA11CD, "E")) %>%
select(MSOA11CD, LAD17CD) %>%
distinct() %>%
# get 2019 LA codes
left_join(lad_17_19, by = "LAD17CD") %>%
# get quantiles for the above-average MSOAs
left_join(eng_msoa_loneliness_high, by = c("MSOA11CD" = "msoa11cd")) %>%
# if the MSOA has a quantile of `NA`, it means it's not above average - just label this as '3'
mutate(lonely_q = replace_na(lonely_q, 0)) %>%
# label deciles by whether they're in top 10 then summarise by this label
tabyl(LAD19CD, lonely_q) %>%
# calculate proportion of loneliest MSOAs in each LAD
mutate(Prop_loneliest = `2` / (`0` + `1` + `2`)) %>%
mutate(Loneliness_q = as.integer(cut2(Prop_loneliest, g = 5))) %>%
select(LAD19CD, Prop_loneliest, Loneliness_q)
# summarise loneliness risks into wards - calculate proportion of highest-risk MSOAs (in the whole nation) within each ward
lonely_ward_eng = lookup_msoa_ward %>%
# get full list of MSOAs
filter(startsWith(MSOA11CD, "E")) %>%
# get quantiles for the above-average MSOAs
left_join(eng_msoa_loneliness_high, by = c("MSOA11CD" = "msoa11cd")) %>%
# if the MSOA has a quantile of `NA`, it means it's not above average - just label this as '3'
mutate(lonely_q = replace_na(lonely_q, 0)) %>%
# label deciles by whether they're in top 10 then summarise by this label
tabyl(WD17CD, lonely_q) %>%
# calculate proportion of loneliest MSOAs in each LAD
mutate(Prop_loneliest = `2` / (`0` + `1` + `2`)) %>%
mutate(Loneliness_q = calc_risk_quantiles(Prop_loneliest)) %>%
select(WD17CD, Prop_loneliest, Loneliness_q)
##
## Wales
##
# of the MSOAs with above-average loneliness risks (i.e. `loneills_2018` > 0), cut into two categories: lonely_q = 2 will be highest-risk category
wal_msoa_loneliness_high = wal_msoa_loneliness %>%
select(msoa11cd, loneills_2018) %>%
filter(loneills_2018 > 0) %>%
mutate(lonely_q = as.integer(cut2(loneills_2018, g = 2)))
# summarise loneliness risks into LADs - calculate proportion of highest-risk MSOAs (in the whole nation) within each LAD
lonely_wal = lookup_lsoa_msoa_lad %>%
# get full list of MSOAs
filter(startsWith(MSOA11CD, "W")) %>%
select(MSOA11CD, LAD17CD) %>%
distinct() %>%
# get 2019 LA codes
left_join(lad_17_19, by = "LAD17CD") %>%
# get quantiles for the above-average MSOAs
left_join(wal_msoa_loneliness_high, by = c("MSOA11CD" = "msoa11cd")) %>%
# if the MSOA has a quantile of `NA`, it means it's not above average - just label this as '3'
mutate(lonely_q = replace_na(lonely_q, 0)) %>%
# label deciles by whether they're in top 10 then summarise by this label
tabyl(LAD19CD, lonely_q) %>%
# calculate proportion of loneliest MSOAs in each LAD
mutate(Prop_loneliest = `2` / (`0` + `1` + `2`)) %>%
mutate(Loneliness_q = as.integer(cut2(Prop_loneliest, g = 5))) %>%
select(LAD19CD, Prop_loneliest, Loneliness_q)
# summarise loneliness risks into wards - calculate proportion of highest-risk MSOAs (in the whole nation) within each ward
lonely_ward_wal = lookup_msoa_ward %>%
# get full list of MSOAs
filter(startsWith(MSOA11CD, "W")) %>%
# get quantiles for the above-average MSOAs
left_join(wal_msoa_loneliness_high, by = c("MSOA11CD" = "msoa11cd")) %>%
# if the MSOA has a quantile of `NA`, it means it's not above average - just label this as '3'
mutate(lonely_q = replace_na(lonely_q, 0)) %>%
# label deciles by whether they're in top 10 then summarise by this label
tabyl(WD17CD, lonely_q) %>%
# calculate proportion of loneliest MSOAs in each LAD
mutate(Prop_loneliest = `2` / (`0` + `1` + `2`)) %>%
mutate(Loneliness_q = calc_risk_quantiles(Prop_loneliest)) %>%
mutate(Loneliness_q = ifelse(Prop_loneliest == 1, 5, Loneliness_q)) %>% # there are only four real clusters of data in Wales, so manually set quintile to 5 is 100% of MSOAs in the ward are lonely
select(WD17CD, Prop_loneliest, Loneliness_q)
##
## Scotland
##
# of the MSOAs with above-average loneliness risks (i.e. `loneills_2018` > 0), cut into two categories: lonely_q = 2 will be highest-risk category
sco_msoa_loneliness_high = sco_msoa_loneliness %>%
select(msoa11cd = InterZone, loneills_2018) %>%
filter(loneills_2018 > 0) %>%
mutate(lonely_q = as.integer(cut2(loneills_2018, g = 2)))
# summarise loneliness risks into LADs - calculate proportion of highest-risk MSOAs (in the whole nation) within each LAD
lonely_sco = lookup_lsoa_msoa_lad %>%
# get full list of MSOAs
filter(startsWith(MSOA11CD, "S")) %>%
select(MSOA11CD, LAD17CD) %>%
distinct() %>%
# get 2019 LA codes
left_join(lad_17_19, by = "LAD17CD") %>%
# get quantiles for the above-average MSOAs
left_join(sco_msoa_loneliness_high, by = c("MSOA11CD" = "msoa11cd")) %>%
# if the MSOA has a quantile of `NA`, it means it's not above average - just label this as '3'
mutate(lonely_q = replace_na(lonely_q, 0)) %>%
# label deciles by whether they're in top 10 then summarise by this label
tabyl(LAD19CD, lonely_q) %>%
# calculate proportion of loneliest MSOAs in each LAD
mutate(Prop_loneliest = `2` / (`0` + `1` + `2`)) %>%
mutate(Loneliness_q = as.integer(cut2(Prop_loneliest, g = 5))) %>%
select(LAD19CD, Prop_loneliest, Loneliness_q)
# summarise loneliness risks into wards - calculate proportion of highest-risk MSOAs (in the whole nation) within each ward
lonely_ward_sco = lookup_iz_ward %>%
# get quantiles for the above-average MSOAs
left_join(sco_msoa_loneliness_high, by = c("MSOA11CD" = "msoa11cd")) %>%
# if the MSOA has a quantile of `NA`, it means it's not above average - just label this as '3'
mutate(lonely_q = replace_na(lonely_q, 0)) %>%
# label deciles by whether they're in top 10 then summarise by this label
tabyl(WD17CD, lonely_q) %>%
# calculate proportion of loneliest MSOAs in each LAD
mutate(Prop_loneliest = `2` / (`0` + `1` + `2`)) %>%
mutate(Loneliness_q = calc_risk_quantiles(Prop_loneliest)) %>%
select(WD17CD, Prop_loneliest, Loneliness_q)
##
## Northern Ireland
##
# of the MSOAs with above-average loneliness risks (i.e. `loneills_2018` > 0), cut into two categories: lonely_q = 2 will be highest-risk category
ni_msoa_loneliness_high = ni_msoa_loneliness %>%
select(LSOA11CD = SOA_CODE, loneills_2018) %>%
filter(loneills_2018 > 0) %>%
mutate(lonely_q = as.integer(cut2(loneills_2018, g = 2)))
# summarise loneliness risks into LADs - calculate proportion of highest-risk MSOAs (in the whole nation) within each LAD
lonely_ni = lookup_lsoa_lad_ni %>%
# get quantiles for the above-average MSOAs
left_join(ni_msoa_loneliness_high, by = "LSOA11CD") %>%
# if the MSOA has a quantile of `NA`, it means it's not above average - just label this as '3'
mutate(lonely_q = replace_na(lonely_q, 0)) %>%
# label deciles by whether they're in top 10 then summarise by this label
tabyl(LAD19CD, lonely_q) %>%
# calculate proportion of loneliest MSOAs in each LAD
mutate(Prop_loneliest = `2` / (`0` + `1` + `2`)) %>%
mutate(Loneliness_q = as.integer(cut2(Prop_loneliest, g = 5))) %>%
select(LAD19CD, Prop_loneliest, Loneliness_q)
##
## combine into UK-wide dataframes
##
lonely_lad = bind_rows(lonely_eng, lonely_wal, lonely_sco, lonely_ni)
write_csv(lonely_lad, "output/loneliness-LA.csv")
lonely_ward = bind_rows(lonely_ward_eng, lonely_ward_wal, lonely_ward_sco)
write_csv(lonely_ward, "output/loneliness-ward.csv")
rm(lookup_lsoa_msoa_lad, lad_17_19, lookup_lsoa_lad_ni, lookup_iz_ward, lookup_msoa_ward,
eng_msoa_loneliness, eng_msoa_loneliness_high, wal_msoa_loneliness, wal_msoa_loneliness_high,
sco_msoa_loneliness, sco_msoa_loneliness_high, ni_msoa_loneliness, ni_msoa_loneliness_high,
lonely_eng, lonely_wal, lonely_sco, lonely_ni,
lonely_ward_eng, lonely_ward_wal, lonely_ward_sco)