-
Notifications
You must be signed in to change notification settings - Fork 9
/
mreggerplot.ado
403 lines (376 loc) · 11 KB
/
mreggerplot.ado
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
*! version 0.1.0 Tom Palmer 06jun2016
program mreggerplot
version 12
local version : di "version " string(_caller()) ", missing :"
syntax varlist(min=4 max=4) [if] [in] [, ivw fe re ///
reslope recons median egger noLINE ///
noMCIS noLCI errorbars Level(cilevel) wmarkers ///
PENWeighted reps(integer 50) seed(string) Weighted ///
ellipses mlabel(varname) legend(string asis) ///
linetop GPCI ///
mleglabel(string) * ]
local callersversion = _caller()
// weighted or penweighted specified with median
if "`median'" == "" & ("`weighted'" != "" | "`penweighted'" != "") {
di as err "Please specify median with weighted or penweighted options"
exit 198
}
// check only one of "", egger, ivw, median specified
if "`ivw'" == "ivw" & "`egger'" == "egger" {
di as err "only one of options `ivw' or `egger' allowed"
exit 198
}
else if "`ivw'" == "ivw" & "`median'" == "median" {
di as err "only one of options `ivw' or `median' allowed"
exit 198
}
else if "`egger'" == "egger" & "`median'" == "median" {
di as err "only one of options `egger' or `median' allowed"
exit 198
}
if "`ivw'" == "" & "`median'" == "" & "`egger'" == "" {
local egger egger
}
if `"`mleglabel'"' == "" {
local mleglabel Instruments
}
if "`errorbars'" == "errorbars" & "`ellipses'" == "ellipses" {
di as err "Options ellipses and errorbars cannot both be specified"
exit 198
}
tokenize `"`varlist'"'
/*
4 variables:
1: gd beta
2: gd SE
3: gp beta
4: gp SE
*/
// check if addplot is installed
capture which addplot
if _rc {
di as error "-addplot- from SSC is required; install using"
di "{stata ssc install addplot}"
exit 499
}
// count no. instruments
qui count `if' `in'
local k = r(N)
local percentile = 1 - (1 - `level'/100)/2
if "`gpci'" == "gpci" & "`mcis'" == "nomcis" {
di as err "nomcis and gpci options may not be specified together"
exit 198
}
// copy contents of legend option
local legendusertext `legend'
if "`median'" == "median" {
local lci nolci
}
if "`ellipses'" == "" & "`errorbars'" == "" {
// set default as errobars
local errorbars errorbars
}
// set-up local for legend
if (`"`legend'"' != "off") & (strpos(`"`legend'"', "order(") == 0) {
if "`egger'" == "egger" {
local lname MR-Egger
}
if "`median'" == "median" {
local lname Median
}
if "`ivw'" == "ivw" {
local lname IVW
}
if "`lci'" == "" & "`gpci'" == "" & ///
"`mcis'" == "" {
if "`line'" == "" {
local lleg 3 "`lname'"
}
else {
local lleg ""
}
if "`linetop'" == "" {
if "`errorbars'" == "errorbars" {
local mlabn 5
}
else {
local mlabn = 4 + `k'
}
local legend `"order(`mlabn' "`mleglabel'" 4 "`level'% CIs" `lleg' 2 "`lname' `level'% CI")"'
}
else {
local legend `"order(3 "`mleglabel'" 2 "`level'% CIs" 5 "`lname'" 4 "`lname' `level'% CI")"'
}
}
if "`line'" == "" & "`lci'" == "" & "`gpci'" == "gpci" & ///
"`mcis'" == "" {
if "`linetop'" == "" {
local legend `"order(6 "`mleglabel'" 4 "`level'% CIs" 3 "`lname'" 2 "`lname' `level'% CI")"'
}
else {
local legend `"order(4 "`mleglabel'" 2 "`level'% CIs" 6 "`lname'" 5 "`lname' `level'% CI")"'
}
}
if "`line'" == "" & "`lci'" == "" & "`gpci'" == "" & ///
"`mcis'" == "nomcis" {
if "`linetop'" == "" {
local legend `"order(4 "`mleglabel'" 3 "`lname'" 2 "`lname' `level'% CI")"'
}
else {
local legend `"order(2 "`mleglabel'" 4 "`lname'" 3 "`lname' `level'% CI")"'
}
}
if "`line'" == "" & "`lci'" == "nolci" & "`gpci'" == "" & ///
"`mcis'" == "" {
if "`linetop'" == "" {
local legend `"order(4 "`mleglabel'" 3 "`level'% CIs" 2 "`lname'")"'
}
else {
local legend `"order(3 "`mleglabel'" 2 "`level'% CIs" 4 "`lname'")"'
}
}
if "`line'" == "" & "`lci'" == "nolci" & ///
"`gpci'" == "gpci" & "`mcis'" == "" {
if "`linetop'" == "" {
local legend `"order(5 "`mleglabel'" 4 "`level'% CIs" 2 "`lname'")"'
}
else {
local legend `"order(4 "`mleglabel'" 2 "`level'% CIs" 5 "`lname'")"'
}
}
if "`line'" == "" & "`lci'" == "nolci" & "`gpci'" == "" & ///
"`mcis'" == "nomcis" {
if "`linetop'" == "" {
local legend `"order(3 "`mleglabel'" 2 "`lname'")"'
}
else {
local legend `"order(2 "`mleglabel'" 3 "`lname'")"'
}
}
if (`callersversion' >= 18.0) & (strpos(`"`legendusertext'"', "pos") == 0) {
local legend `legend' pos(6)
}
if (strpos(`"`legendusertext'"', "rows") == 0) local legend `legend' rows(1)
if (strpos(`"`legendusertext'"', "size(") == 0) local legend `legend' size(small)
local legend `legendusertext' `legend'
}
// weighted markers
if "`wmarkers'" == "wmarkers" {
tempvar weightvar
qui gen double `weightvar' = 1/(`2'^2) `if' `in'
qui su `weightvar'
local weightvarsum = r(sum)
qui replace `weightvar' = `weightvar'/`weightvarsum' `if' `in'
local weight [aw=`weightvar']
}
// variables for mr-egger
if "`egger'" == "egger" {
tempvar eggery eggerx
qui gen double `eggery' = `1'*sign(`3') `if' `in'
qui gen double `eggerx' = abs(`3') `if' `in'
la var `eggery' `"Instrument-outcome associations"'
// la var `eggery' `""`1'{char 215}sign(`3')""'
la var `eggerx' `"Instrument-exposure associations"'
// la var `eggerx' "abs(`3')"
}
// set up the plot using a scatter plot with invisible markers
if ("`ivw'" == "ivw" | "`median'" == "median") & "`egger'" == "" {
la var `3' `"Instrument-exposure associations"'
la var `1' `"Instrument-outcome associations"'
twoway scatter `1' `3' `if' `in', mc(none) ///
graphregion(color(white)) `options' legend(`legend')
}
else if "`egger'" == "egger" {
twoway scatter `eggery' `eggerx' `if' `in', mc(none) ///
graphregion(color(white)) `options' legend(`legend')
}
// fit ivw, mr-egger, or weighted median
if "`egger'" == "egger" {
// mr-egger
qui mregger `1' `3' [aw=1/(`2'^2)] `if' `in', ///
`fe' `re' `reslope' `recons'
tempvar eggerline
qui gen double `eggerline' = _b[_cons] + _b[slope]*`eggerx' `if' `in'
la var `eggerline' "MR-Egger"
}
if "`ivw'" == "ivw" {
// ivw
qui mregger `1' `3' [aw=1/(`2'^2)] `if' `in', ///
`ivw' `fe' `re' `reslope' `recons'
tempvar ivwline
local ivwslope = _b[`3']
qui gen double `ivwline' = _b[`3']*`3' `if' `in'
la var `ivwline' "IVW"
}
if "`median'" == "median" {
// median
qui mrmedian `1' `2' `3' `4' `if' `in', ///
`penweighted' reps(`reps') ///
seed(`seed') `weighted'
local medianslope = _b[beta]
tempvar medianline
qui gen double `medianline' = _b[beta]*`3' `if' `in'
la var `medianline' "Median"
}
if "`linetop'" == "" {
// plot confidence intervals around fitted line
if "`lci'" == "" {
// code for cis around lines
if "`egger'" == "egger" {
addplot : lfitci `eggery' `eggerx' `if' `in' ///
[aw=1/(`2'^2)], nofit ///
fcolor("204 204 204") ///
fintensity(inten50) level(`level') ///
alwidth(vthin) legend(`legend')
}
if "`ivw'" == "ivw" {
addplot : lfitci `1' `3' `if' `in' ///
[aw=1/(`2'^2)], ///
estopts(nocons) nofit ///
fcolor("204 204 204") ///
fintensity(inten50) level(`level') ///
alwidth(vthin) legend(`legend')
}
}
// plot fitted line
if "`line'" == "" {
if "`egger'" == "egger" {
addplot : line `eggerline' `eggerx' `if' `in', ///
lc(gs0) sort ///
legend(`legend')
}
if "`ivw'" == "ivw" {
addplot : line `ivwline' `3' `if' `in', ///
lc(gs0) sort ///
legend(`legend')
}
if "`median'" == "median" {
addplot : line `medianline' `3' `if' `in', ///
lc(gs0) sort ///
legend(`legend')
}
}
}
// plot 95% cis around markers
if "`ellipses'" == "ellipses" & "`mcis'" == "" {
// ellipses code
tempvar x y
if "`egger'" == "egger" {
qui gen double `y' = `eggery' `if' `in'
qui gen double `x' = `eggerx' `if' `in'
la var `y' "Instrument-outcome associations"
// la var `y' "`1'{char 215}sign(`3')"
la var `x' "Instrument-exposure associations"
//la var `x' "abs(`3')"
}
else {
qui gen double `y' = `1' `if' `in'
qui gen double `x' = `3' `if' `in'
// la var `x' `1'
la var `x' "Instrument-exposure associations"
// la var `y' `3'
la var `y' "Instrument-outcome associations"
}
tempvar t
local obs = _N
qui range `t' 0 `=2*_pi' 400
tempname sqrtc
scalar `sqrtc' = sqrt(invchi2(2, `=`level'/100'))
local twycmd ""
tempname X gdval gdseval gpval gpseval
qui putmata `X'=(`y' `2' `x' `4') `if' `in', replace
forvalues i = 1/`k' {
tempvar y`i' x`i'
mata st_numscalar("`gdval'", `X'[`i', 1])
mata st_numscalar("`gdseval'", `X'[`i', 2])
mata st_numscalar("`gpval'", `X'[`i', 3])
mata st_numscalar("`gpseval'", `X'[`i', 4])
qui gen double `x`i'' = `gpval' + ///
`gpseval'*`sqrtc'*cos(`t')
qui gen double `y`i'' = `gdval' + `gdseval'*`sqrtc'* ///
cos(`t' + acos(0))
local twycmd ///
"`twycmd' line `y`i'' `x`i'', lc(gs8) lw(vthin) || "
}
addplot : `twycmd', legend(`legend')
if `obs' < 400 {
qui drop in `=`obs'+1'/400
}
}
if "`errorbars'" == "errorbars" & "`mcis'" == "" {
// confidence intervals as capped lines
tempvar gdciupp gdcilow gpciupp gpcilow x y
if "`egger'" == "egger" {
qui gen double `y' = `eggery' `if' `in'
qui gen double `x' = `eggerx' `if' `in'
}
else {
qui gen double `y' = `1' `if' `in'
qui gen double `x' = `3' `if' `in'
}
qui gen double `gdcilow' = `y' - invnormal(`percentile')*`2'
qui gen double `gdciupp' = `y' + invnormal(`percentile')*`2'
qui gen double `gpcilow' = `x' - invnormal(`percentile')*`4'
qui gen double `gpciupp' = `x' + invnormal(`percentile')*`4'
local rcgd rcap `gdcilow' `gdciupp' `x' `if' `in', lc(gs0) ///
lwidth(vthin) msize(vsmall) mfcolor(gs0) mlcolor(gs0) ///
mcolor(gs0)
local rcgp rcap `gpcilow' `gpciupp' `y' `if' `in', lc(gs0) ///
lwidth(vthin) msize(vsmall) mfcolor(gs0) mlcolor(gs0) ///
mcolor(gs0) horizontal
addplot : `rcgd' , legend(`legend')
if "`gpci'" == "gpci" {
addplot : `rcgp' , legend(`legend')
}
}
// replot the markers with colour
if "`egger'" == "egger" {
addplot : scatter `eggery' `eggerx' `if' `in' `weight', ///
m(s) mc(gs0) mlabel(`mlabel') mlabcolor(black) ///
mlabsize(vsmall) msize(vsmall) ///
legend(`legend')
}
else {
addplot : scatter `1' `3' `if' `in' `weight', ///
m(s) mc(gs0) mlabel(`mlabel') mlabcolor(black) ///
mlabsize(vsmall) msize(vsmall) ///
legend(`legend')
}
// draw fitted line (and ci) on top
if "`linetop'" != "" {
// plot confidence intervals around fitted line
if "`lci'" == "" {
// code for cis around lines
if "`egger'" == "egger" {
addplot : lfitci `eggery' `eggerx' `if' `in' ///
[aw=1/(`2'^2)], nofit ///
fcolor("204 204 204") ///
fintensity(inten50) level(`level') ///
alwidth(vthin) legend(`legend')
}
if "`ivw'" == "ivw" {
addplot : lfitci `1' `3' `if' `in' [aw=1/(`2'^2)], ///
estopts(nocons) nofit ///
fcolor("204 204 204") ///
fintensity(inten50) level(`level') ///
alwidth(vthin) legend(`legend')
}
}
// plot fitted line
if "`line'" == "" {
if "`egger'" == "egger" {
addplot : line `eggerline' `eggerx' `if' `in', ///
lc(gs0) sort legend(`legend')
}
if "`ivw'" == "ivw" {
addplot : line `ivwline' `3' `if' `in', ///
lc(gs0) sort legend(`legend')
}
if "`median'" == "median" {
addplot : line `medianline' `3' `if' `in', ///
lc(gs0) sort legend(`legend')
}
}
}
end
exit