generated from opensafely/research-template
/
03a_hhClassif_an_hist_hhSizebyEthnicity.do
466 lines (345 loc) · 14.5 KB
/
03a_hhClassif_an_hist_hhSizebyEthnicity.do
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
/*==============================================================================
DO FILE NAME: 03a_hh_an_descriptive_plots
PROJECT: Household classfication
AUTHOR: K Wing
DATE: 14th June 2021
DESCRIPTION OF FILE: Outputs sanity checking histograms of secondary cases by household size, also by date and ethnicity
DATASETS USED: hh_analysis_datasetREDVARS
DATASETS CREATED: None
OTHER OUTPUT: Log file: $logdir\02_an_hist_descriptive_plots
cd ${outputData}
clear all
use hh_analysis_dataset_DRAFT.dta, clear
==============================================================================*/
*cd ${outputData}
*clear all
*use hh_analysis_dataset.dta, clear
* Open a log file
*cap log close
*log using "02_an_hist_descriptive_plots", replace t
/*These need updated - don't need to program that removes all bins <5, only need to redact when:
1. If the total in the histogram is <5 then don't include in the hh size histogram
2. Always include the total number of households with 0 cases somewhere in the plot
3. The first 3 bars of the histogram have to add up to more than 5, otherwise need to redact
Based on meeting between Roz, Amir, Stephen and Kevin 7th October 2020
*/
*========================(1) HISTOGRAMS OF TOTAL HOUSEHOLD SIZE=====================================
sysdir set PLUS ./analysis/adofiles
sysdir set PERSONAL ./analysis/adofiles
local dataset `1'
* Open a log file
capture log close
log using ./logs/03a_hhClassif_an_hist_hhSizebyEthnicity_`dataset'.log, replace t
*use dataset setup for descriptive analysis
use ./output/allHH_sizedBetween1And20_`dataset'.dta
*reduce to one record per household id
duplicates drop hh_id, force
**bughunting**
/*
use ./output/allHH_beforeDropping_largerThan10_MAIN.dta, clear
sum hh_size, detail
hist hh_size, discrete title(Overall, size (medium))
graph save ./output/overallHHSizeDist_MAIN.gph, replace
sum hh_size if eth5==1, detail
hist hh_size if eth5==1, discrete title(White, size (medium))
graph save ./output/whiteHHSizeDist_MAIN.gph, replace
gr combine ./output/overallHHSizeDist_MAIN.gph ./output/whiteHHSizeDist_MAIN.gph, title (HH size distribution)
gr export ./output/HHdistHists_MAIN.pdf, replace
*/
**endofbughunting**
*set colour schemes
graph query, schemes
set scheme economist
*overall distribution of hh_sizes
la var hh_size "Household size"
sum hh_size, detail
hist hh_size, freq xtitle("Household size", size(small)) xlabel(0(5)20, labsize(small) noticks) ytitle("n (houses)", size(small)) ylabel (#3, format(%5.0f) labsize(small)) discrete title(All ethnicities, size (medium))
graph save ./output/overallHHSizeDist_`dataset'.gph, replace
program histByEth
if `1'==1 {
local ethnicity="White"
}
else if `1'==2 {
local ethnicity="South Asian"
}
else if `1'==3 {
local ethnicity="Black"
}
hist hh_size if eth5==`1', freq xtitle("Household size", size(small)) xlabel(0(5)20, labsize(small) noticks) ytitle("n (houses)", size(small)) ylabel (#3, format(%5.0f) labsize(small)) discrete title(`ethnicity' ethnicity, size (medium))
end
*plot of distrubtion of hh_sizes by ethnicity
*1 - white
sum hh_size if eth5==1, detail
histByEth 1
graph save ./output/whiteHHSizeDist_`dataset'.gph, replace
*2 - south asian
sum hh_size if eth5==2, detail
histByEth 2
graph save ./output/southAsianHHSizeDist_`dataset'.gph, replace
*3 - black
sum hh_size if eth5==3, detail
capture noisily histByEth 3
capture noisily graph save ./output/blackHHSizeDist_`dataset'.gph, replace
*capture noisily gr combine ./output/overallHHSizeDist_`dataset'.gph ./output/whiteHHSizeDist_`dataset'.gph ./output/southAsianHHSizeDist_`dataset'.gph ./output/blackHHSizeDist_`dataset'.gph, title (Household size distribution, size(medium))
*gr export ./output/HHdistHists_`dataset'.pdf, replace
*BUGHUNTING
capture noisily gr combine ./output/overallHHSizeDist_`dataset'.gph ./output/whiteHHSizeDist_`dataset'.gph ./output/southAsianHHSizeDist_`dataset'.gph ./output/blackHHSizeDist_`dataset'.gph, title(Household size distribution, size(medium))
gr export ./output/HHdistHists_`dataset'.pdf, replace
log close
/*
*========================(2) HISTOGRAMS OF HHCASES BY ETHNICITY=====================================
*PROGRAMS*
*this is the basic (vanilla) version of the hhCases histogram program
program hhCasesHist
hist `1' if hh_size==`2', frequency addlabels discrete xlabel(1(1)`2') ylabel (, format(%5.0f)) title(Household size: `2', size (medium)) subtitle((households with no cases: `3'), size (medium)) saving(hh_size`2'', replace)
end
******************(b) Set 2 of histograms: distribution of total number of cases by household size, by ethnicity to start with******************
program hhCasesHistByEthnicity
if `3'==1 {
local ethnicity="White"
}
else if `3'==2 {
local ethnicity="South_Asian"
}
else if `3'==3 {
local ethnicity="Black"
}
hist `1' if hh_size==`2' & eth5==`3', frequency addlabels discrete xlabel(1(1)`2') title (Household size: `2', size (medium)) subtitle(`ethnicity' "(households with no cases: `4')", size (medium)) saving(`2'_`ethnicity', replace)
end
/*
e.g. in a household size of 4, how many houses had 1 case, how many had 2, how many had 3, how many had 4
-so instead of case_date as the parameter, I want number of cases in the household
*/
************basic histograms (not stratified)**********
use ./output/hh_analysis_dataset.dta, clear
*NEW case definition
*use E:\high_privacy\workspaces\households\output\hh_analysis_dataset.dta
*reduce to one record per household id
duplicates drop hh_id, force
preserve
keep if totCasesInHH==0
save hhWithZeroCases.dta, replace
restore
*keep only houses with at least one case for this descriptive analysis
keep if totCasesInHH>0
count
*drop cases that are dates prior to Feb012020
*drop if case_date<date("20200201", "YMD")
tempfile forHistOutput
save `forHistOutput'
*create a single combined pdf of all the (<5 redacted) histograms (with histograms showing number of houses with specific numbers of cases by household size)
*macro for number of houshold sizes
levelsof hh_size, local(levels)
foreach l of local levels {
*histogram showing distribution of total number of cases in household by ethnicity
use hhWithZeroCases.dta, clear
keep if hh_size==`l'
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHist totCasesInHH `l' `hhWithNoCases'
*combine into single pdfs - original case definition
gr export totCasesinHHsize`l'.pdf, replace
*combine into single pdfs - new case definition
*gr export totCasesinHHsize`l'wSGSS.pdf, replace
}
**************histograms by ETHNICITY*******************
*now all ethnicities - I want single pdfs each with three graphs on: white, black, south asian for each household size
use hh_analysis_dataset.dta, clear
*first of all, create a number of cases in the household variable
bysort hh_id:egen totCasesInHH=total(case)
*then reduce to one record per household id
duplicates drop hh_id, force
preserve
keep if totCasesInHH==0
save hhWithZeroCases.dta, replace
restore
*keep only houses with at least one case for this descriptive analysis
keep if totCasesInHH>0
count
*drop cases that are dates prior to Feb012020
*drop if case_date<date("20200201", "YMD")
tempfile forHistOutput
save `forHistOutput'
*create a single combined pdf of all the (<5 redacted) histograms (with histograms showing number of houses with specific numbers of cases by household size)
*macro for number of houshold sizes
levelsof hh_size, local(levels)
foreach l of local levels {
*histogram showing distribution of total number of cases in household by ethnicity
use hhWithZeroCases.dta, clear
keep if hh_size==`l' & eth5==1
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHistByEthnicity totCasesInHH `l' 1 `hhWithNoCases' /*white*/
use hhWithZeroCases.dta, clear
keep if hh_size==`l' & eth5==2
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHistByEthnicity totCasesInHH `l' 2 `hhWithNoCases' /*south asian*/
use hhWithZeroCases.dta, clear
keep if hh_size==`l' & eth5==3
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHistByEthnicity totCasesInHH `l' 3 `hhWithNoCases' /*black*/
*combine into single pdfs
gr combine `l'_white.gph `l'_south_asian.gph `l'_black.gph
gr export totCasesinHHsize`l'ByEthnicity.pdf, replace
}
**************repeat above by RURAL URBAN broad categories*******************
use hh_analysis_dataset.dta, clear
numlabel, add
*first of all, create a number of cases in the household variable
bysort hh_id:egen totCasesInHH=total(case)
*then reduce to one record per household id
duplicates drop hh_id, force
*keep only households in conurbations
tab rural_urbanFive
keep if rural_urbanFive==1|rural_urbanFive==2
tab rural_urbanFive
preserve
keep if totCasesInHH==0
save hhWithZeroCases.dta, replace
restore
*keep only houses with at least one case for this descriptive analysis
keep if totCasesInHH>0
count
*drop cases that are dates prior to Feb012020
*drop if case_date<date("20200201", "YMD")
tempfile forHistOutput
save `forHistOutput'
*create a single combined pdf of all the (<5 redacted) histograms (with histograms showing number of houses with specific numbers of cases by household size)
*macro for number of houshold sizes
levelsof hh_size, local(levels)
foreach l of local levels {
*histogram showing distribution of total number of cases in household by ethnicity
use hhWithZeroCases.dta, clear
keep if hh_size==`l' & eth5==1
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHistByEthnicity totCasesInHH `l' 1 `hhWithNoCases' /*white*/
use hhWithZeroCases.dta, clear
keep if hh_size==`l' & eth5==2
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHistByEthnicity totCasesInHH `l' 2 `hhWithNoCases' /*south asian*/
use hhWithZeroCases.dta, clear
keep if hh_size==`l' & eth5==3
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHistByEthnicity totCasesInHH `l' 3 `hhWithNoCases' /*black*/
*combine into single pdfs
gr combine `l'_white.gph `l'_south_asian.gph `l'_black.gph, title (Urban (major or minor conurbation))
gr export totCasesinHHsize`l'ByEthnicity_Conurbations.pdf, replace
}
**************repeat above by RURAL URBAN broad categories*******************
use hh_analysis_dataset.dta, clear
numlabel, add
*first of all, create a number of cases in the household variable
bysort hh_id:egen totCasesInHH=total(case)
*then reduce to one record per household id
duplicates drop hh_id, force
*keep only households outside of conurbations
tab rural_urbanFive
keep if rural_urbanFive==3|rural_urbanFive==4|rural_urbanFive==5
tab rural_urbanFive
preserve
keep if totCasesInHH==0
save hhWithZeroCases.dta, replace
restore
*keep only houses with at least one case for this descriptive analysis
keep if totCasesInHH>0
count
*drop cases that are dates prior to Feb012020
*drop if case_date<date("20200201", "YMD")
tempfile forHistOutput
save `forHistOutput'
*create a single combined pdf of all the (<5 redacted) histograms (with histograms showing number of houses with specific numbers of cases by household size)
*macro for number of houshold sizes
levelsof hh_size, local(levels)
foreach l of local levels {
*histogram showing distribution of total number of cases in household by ethnicity
use hhWithZeroCases.dta, clear
keep if hh_size==`l' & eth5==1
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHistByEthnicity totCasesInHH `l' 1 `hhWithNoCases' /*white*/
use hhWithZeroCases.dta, clear
keep if hh_size==`l' & eth5==2
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHistByEthnicity totCasesInHH `l' 2 `hhWithNoCases' /*south asian*/
use hhWithZeroCases.dta, clear
keep if hh_size==`l' & eth5==3
count
local hhWithNoCases=r(N)
use `forHistOutput', clear
hhCasesHistByEthnicity totCasesInHH `l' 3 `hhWithNoCases' /*black*/
*combine into single pdfs
gr combine `l'_white.gph `l'_south_asian.gph `l'_black.gph, title (More rural)
gr export totCasesinHHsize`l'ByEthnicity_More_Rural.pdf, replace
}
**************repeat above by rural urban broad categories*******************
*=========RURAL LOCATION===============
*now all ethnicities - I want single pdfs each with three graphs on: white, black, south asian for each household size
use hh_analysis_dataset.dta, clear
numlabel, add
*keep only cases for this descriptive analysis
keep if case==1
*drop cases that are dates prior to Feb012020
drop if case_date<date("20200201", "YMD")
*keep only the more well off househholds
tab rural_urbanBroad
keep if rural_urbanBroad==0
tempfile forHistOutput
save `forHistOutput'
*create a single combined pdf of all the (<5 redacted) histograms (with histograms showing number of houses with specific numbers of cases by household size)
*macro for number of houshold sizes
levelsof hh_size, local(levels)
foreach l of local levels {
*histogram showing distribution of total number of cases in household by ethnicity
use `forHistOutput', clear
redactedHHCasesHistByEthnicity totCasesInHH `l' 1 /*white*/
use `forHistOutput', clear
redactedHHCasesHistByEthnicity totCasesInHH `l' 2 /*south asian*/
use `forHistOutput', clear
redactedHHCasesHistByEthnicity totCasesInHH `l' 3 /*black*/
*combine into single pdfs
gr combine `l'_white.gph `l'_south_asian.gph `l'_black.gph, title (Rural)
gr export totCasesinHHsize`l'ByEthnicity_Rural.pdf, replace
}
*=========URBAN LOCATION===============
*now all ethnicities - I want single pdfs each with three graphs on: white, black, south asian for each household size
use hh_analysis_dataset.dta, clear
numlabel, add
*keep only cases for this descriptive analysis
keep if case==1
*drop cases that are dates prior to Feb012020
drop if case_date<date("20200201", "YMD")
*keep only the more well off househholds
tab rural_urbanBroad
keep if rural_urbanBroad==1
tempfile forHistOutput
save `forHistOutput'
*create a single combined pdf of all the (<5 redacted) histograms (with histograms showing number of houses with specific numbers of cases by household size)
*macro for number of houshold sizes
levelsof hh_size, local(levels)
foreach l of local levels {
*histogram showing distribution of total number of cases in household by ethnicity
use `forHistOutput', clear
redactedHHCasesHistByEthnicity totCasesInHH `l' 1 /*white*/
use `forHistOutput', clear
redactedHHCasesHistByEthnicity totCasesInHH `l' 2 /*south asian*/
use `forHistOutput', clear
redactedHHCasesHistByEthnicity totCasesInHH `l' 3 /*black*/
*combine into single pdfs
gr combine `l'_white.gph `l'_south_asian.gph `l'_black.gph, title (Urban)
gr export totCasesinHHsize`l'ByEthnicity_Urban.pdf, replace
}