generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_RCGPsafeguard.do
147 lines (101 loc) · 5.06 KB
/
model_RCGPsafeguard.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
*** Child safeguarding - based on RCGP codelist
/* PREAMBLE */
global dir "`c(pwd)'"
*global dir "C:/Users/dy21108/OneDrive - University of Bristol/Documents/GitHub/lockdown-and-vulnerable-groups"
/* SETUP DATA */
*Get Covid weekly case counts;
import delimited using "$dir/output/CovidNewCaseCounts.csv", clear
save "$dir/output/CovidNewCaseCounts.dta", replace
*Get CSV
import delimited using "$dir/output/measure_RCGPsafeguard_rate.csv", clear
*Set up time variables
generate date2 = date(date, "YMD")
format %td date2
gen week_date=date2
format %tw week_date
gen period = 0 /*pre-lockdown period */
replace period = 1 if date2>=d(23mar2020) /*start of first lockdown*/
replace period = 2 if date2>=d(13may2020) /*beginning of inter lockdown period */
replace period = 3 if date2>=d(05nov2020) /*start of second/third lockdown period */
replace period = 4 if date2>=d(29mar2021) /*beginning of transition out of third lockdown */
sort date2
gen year = year(date2)
gen month = month(date2)
gen week = week(date2)
gen time = 0
replace time = week - 63.5 if year==2019
replace time = week - 11.5 if year==2020
replace time = week + 40.5 if year==2021
sort rcgpsafeguard time
by rcgpsafeguard: gen trperiod=_n
*define interaction terms as per the itsa function
gen _z=rcgpsafeguard
gen _t=trperiod
gen _z_t=_z*trperiod
gen _x30=period
replace _x30=1 if period>=1
gen _x_t30=_x30*(_t-30)
gen _z_x30=_z*_x30
gen _z_x_t30=_z_x30*(_t-30)
gen _x37=0
replace _x37=1 if period>=2
gen _x_t37=_x37*(_t-37)
gen _z_x37=_z*_x37
gen _z_x_t37=_z_x37*(_t-37)
gen _x62=0
replace _x62=1 if period>=3
gen _x_t62=_x62*(_t-62)
gen _z_x62=_z*_x62
gen _z_x_t62=_z_x62*(_t-62)
gen _x83=0
replace _x83=1 if period>=4
gen _x_t83=_x83*(_t-83)
gen _z_x83=_z*_x83
gen _z_x_t83=_z_x83*(_t-83)
*Indicator variables for public holidays
gen xmas=0
replace xmas=1 if date2==d(23dec2019)
replace xmas=1 if date2==d(21dec2020)
replace xmas=1 if date2==d(20dec2021)
gen ny=0
replace ny=1 if date2==d(30dec2019)
replace ny=1 if date2==d(28dec2020)
replace ny=1 if date2==d(27dec2021)
gen easter=0
replace easter=1 if date2==d(06apr2020)
replace easter=1 if date2==d(13apr2020)
replace easter=1 if date2==d(29mar2021)
replace easter=1 if date2==d(05apr2021)
gen pubhol=0
replace pubhol=1 if date2==d(04may2020)
replace pubhol=1 if date2==d(25may2020)
replace pubhol=1 if date2==d(31aug2020)
replace pubhol=1 if date2==d(03may2021)
replace pubhol=1 if date2==d(31may2021)
replace pubhol=1 if date2==d(30aug2021)
*Merge on covid case counts
merge m:1 trperiod using "$dir/output/CovidNewCaseCounts.dta"
replace newcases=0 if newcases==.
export delimited using "$dir/output/check.csv", replace
/*** CITS model for first lockdown ***/
preserve
drop if _t>61
* run NegBin model using variables defined above: z=group x=period(pre/post) t=time
xi: glm consultations newcases i.month xmas ny easter pubhol _t _z _z_t _x30 _x_t30 _z_x30 _z_x_t30 _x37 _x_t37 _z_x37 _z_x_t37, family(poisson) link(log) exposure(population) vce(robust)
xi: glm consultations newcases i.month xmas ny easter pubhol _t _z _z_t _x30 _x_t30 _z_x30 _z_x_t30 _x37 _x_t37 _z_x37 _z_x_t37, family(nb) link(log) exposure(population) vce(robust)
* plot observed and predicted values
predict rcgpsafeguard_yhat
gen rcgpsafeguard_pred_rate=rcgpsafeguard_yhat/population
graph twoway (line rcgpsafeguard_pred_rate time if _z==1, lcolor(black)) (line rcgpsafeguard_pred_rate time if _z==0, lcolor(gray)) (scatter value time if _z==1, mcolor(black) msymbol(o)) (scatter value time if _z==0, mcolor(gray) msymbol(o)), legend(order(1 "Intervention estimate" 2 "Control estimate" 3 "Intervention rates" 4 "Control rates")) xline(0, lcolor(black) lpattern(dash)) xline(8, lcolor(black) lpattern(dash)) xscale(range(-30 32)) xlabel(-30 -20 -10 0 10 20 30) yscale(range(0 0.15))
graph export "$dir/output/RCGPsafeguard_plot1.png", as(png) replace
restore
/*** CITS model for second and thrid lockdowns ***/
drop if date2<d(11may2020)|date2>d(20sep2021)
* run NegBin model using variables defined above: z=group x=period(pre/post) t=time
xi: glm consultations newcases i.month xmas ny easter pubhol _t _z _z_t _x62 _x_t62 _z_x62 _z_x_t62 _x83 _x_t83 _z_x83 _z_x_t83, family(poisson) link(log) exposure(population) vce(robust)
xi: glm consultations newcases i.month xmas ny easter pubhol _t _z _z_t _x62 _x_t62 _z_x62 _z_x_t62 _x83 _x_t83 _z_x83 _z_x_t83, family(nb) link(log) exposure(population) vce(robust)
* plot observed and predicted values
predict rcgpsafeguard_yhat2
gen rcgpsafeguard_pred_rate2=rcgpsafeguard_yhat2/population
graph twoway (line rcgpsafeguard_pred_rate2 time if _z==1, lcolor(black)) (line rcgpsafeguard_pred_rate2 time if _z==0, lcolor(gray)) (scatter value time if _z==1, mcolor(black) msymbol(o)) (scatter value time if _z==0, mcolor(gray) msymbol(o)), legend(order(1 "Intervention estimate" 2 "Control estimate" 3 "Intervention rates" 4 "Control rates")) xline(33, lcolor(black) lpattern(dash)) xline(54, lcolor(black) lpattern(dash)) xscale(range(8 79)) xlabel(10 20 30 40 50 60 70 80) yscale(range(0 0.15))
graph export "$dir/output/RCGPsafeguard_plot2.png", as(png) replace