-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmrforest.ado
296 lines (275 loc) · 7.8 KB
/
mrforest.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
*! version 0.1.0 Tom Palmer 15jun2017
program mrforest
version 12
local version : di "version " string(_caller()) ", missing :"
local callersversion = _caller()
syntax varlist(min=3 max=5) [if] [in] [, ///
ivid(varname) ///
models(integer 2) ///
mrivestsopts(string asis) ///
effect(string asis) ///
ivwopts(string asis) ///
mreggeropts(string asis) ///
mrmedianopts(string asis) ///
mrmodalopts(string asis) ///
gsort(string) ///
TEXTSize(real 0) ///
ASTEXT(integer 0) ///
zcis ///
Level(cilevel) ///
noNOTE ///
ividlabel(string) ///
modelslabel(string) ///
modelsonly ///
noSTATS ///
ivwlabel(string) ///
mreggerlabel(string) ///
mrmedianlabel(string) ///
mrmodallabel(string) ///
noFE * ]
// check metan is installed
capture which metan
if _rc {
di as error "-metan- from SSC is required; install using"
di "{stata ssc install metan}"
exit 499
}
// check if metan9 is installed
// - if not I assume the old version of metan installed, which is what is required
capture which metan9
if _rc {
local metancmd metan
}
else {
local metancmd metan9
}
// tokenize varlist
tempvar gd gdse gp gpse cov
local varlistlength : word count `varlist'
tokenize `"`varlist'"'
qui gen double `gd' = `1' `if' `in'
qui gen double `gdse' = `2' `if' `in'
qui gen double `gp' = `3' `if' `in'
local mrivestscmd `gd' `gdse' `gp'
if `varlistlength' > 3 {
qui gen double `gpse' = `4' `if' `in'
local mrivestscmd `mrivestscmd' `gpse'
}
if `varlistlength' == 5 {
qui gen double `cov' = `5' `if' `in'
local mrivestscmd `mrivestscmd' `5'
}
// number of genotypes
qui count `if' `in'
local k = r(N)
// locals for confidence interval level
local leveltail = (100 - `level')/200
local levelupp = (100 - (100 - `level')/2)/100
// generate genotype specific estimates, SEs, and CI limits
tempvar ivest ivse ivcilow ivciupp
qui mrivests `mrivestscmd' `if' `in', `mrivestsopts' generate(`ivest' `ivse')
qui gen double `ivcilow' = `ivest' - invnormal(`levelupp')*`ivse' `if' `in'
qui gen double `ivciupp' = `ivest' + invnormal(`levelupp')*`ivse' `if' `in'
// check gsort option and set local for command
if !inlist("`gsort'", "ascending", "descending", "unsorted", "") {
di as err "gsort() should be one of: ascending, descending, or unsorted"
exit 198
}
if "`gsort'" == "" {
local gsort ascending
}
if "`gsort'" == "ascending" {
local gsortcmd qui gsort -`ivest'
}
else if "`gsort'" == "descending" {
local gsortcmd qui gsort +`ivest'
}
else if "`gsort'" == "unsorted" {
local gsortcmd
}
// genotype id variable
tempvar ivid2
if "`ivid'" == "" {
qui gen `ivid2' = "" `if' `in'
}
else {
qui gen `ivid2' = `ivid' `if' `in'
}
if (`"`ividlabel'"' == "") | (`"`ividlabel'"' != "" & `models' > 0) {
la var `ivid2' " "
}
if `"`ividlabel'"' != "" & `models' == 0 {
la var `ivid2' `ividlabel'
}
// fit the summary data models
if `models' < 0 | `models' > 4 {
di as err "models(#) should be between 0 and 4"
exit 198
}
local nmodels `models'
// drop the observations not selected
preserve
if "`if'`in'" != "" {
qui keep `if' `in'
}
// sort the genotypes if required to
`gsortcmd'
// make fe SEs the default for the IVW fit
if inlist(`"`ivwopts'"', "fe") & "`fe'" == "nofe" {
di as err "Options ivwopts(fe) and nofe may not be specified together"
exit 198
}
if !inlist(`"`ivwopts'"', "fe") & "`fe'" == "" local ivwopts "fe"
if `models' > 0 {
if "`ivwlabel'" == "" local ivwlabel "IVW"
if "`mreggerlabel'" == "" local mreggerlabel "MR-Egger"
if "`mrmedianlabel'" == "" local mrmedianlabel "Median"
if "`mrmodallabel'" == "" local mrmodallabel "Modal"
tempvar group
local n = _N
qui set obs `=`n' + `nmodels''
if `"`ividlabel'"' == "" {
qui gen `group' = "{bf:Genotypes}"
}
else {
qui gen `group' = "{bf:`ividlabel'}"
}
if `"`modelslabel'"' == "" {
qui replace `group' = "{bf:Summary}" ///
in `=`n' + 1'/`=`n' + `nmodels''
}
else {
qui replace `group' = "{bf:`modelslabel'}" ///
in `=`n' + 1'/`=`n' + `nmodels''
}
local bycmd by(`group') nosubgroup
if `nmodels' >= 1 {
qui replace `ivid2' = "`ivwlabel'" in `=`n' + 1'
qui mregger `gd' `gp' [aw=1/(`gdse'^2)] in 1/`k', ///
ivw `ivwopts'
qui replace `ivest' = _b[`gp'] in `=`n' + 1'
qui replace `ivse' = _se[`gp'] in `=`n' + 1'
if "`zcis'" == "" {
qui replace `ivcilow' = `ivest' - invttail(`=e(k) - 1', `leveltail')*`ivse' in `=`n' + 1'
qui replace `ivciupp' = `ivest' + invttail(`=e(k) - 1', `leveltail')*`ivse' in `=`n' + 1'
}
else {
qui replace `ivcilow' = `ivest' - invnormal(`levelupp')*`ivse' in `=`n' + 1'
qui replace `ivciupp' = `ivest' + invnormal(`levelupp')*`ivse' in `=`n' + 1'
}
}
if `nmodels' >= 2 {
qui replace `ivid2' = "`mreggerlabel'" in `=`n' + 2'
if `varlistlength' > 3 {
local gxsecmd gxse(`gpse')
}
qui mregger `gd' `gp' [aw=1/(`gdse'^2)] in 1/`k', ///
`mreggeropts' `gxsecmd'
if `varlistlength' > 3 {
local i2gx = e(I2GX)
}
qui replace `ivest' = _b[slope] in `=`n' + 2'
qui replace `ivse' = _se[slope] in `=`n' + 2'
if "`zcis'" == "" {
qui replace `ivcilow' = `ivest' - invttail(`=e(k) - 2', `leveltail')*`ivse' in `=`n' + 2'
qui replace `ivciupp' = `ivest' + invttail(`=e(k) - 2', `leveltail')*`ivse' in `=`n' + 2'
}
else {
qui replace `ivcilow' = `ivest' - invnormal(`levelupp')*`ivse' in `=`n' + 2'
qui replace `ivciupp' = `ivest' + invnormal(`levelupp')*`ivse' in `=`n' + 2'
}
}
if `nmodels' >= 3 {
qui replace `ivid2' = "`mrmedianlabel'" in `=`n' + 3'
qui mrmedian `gd' `gdse' `gp' `gpse' in 1/`k', ///
`mrmedianopts'
qui replace `ivest' = _b[beta] in `=`n' + 3'
qui replace `ivse' = _se[beta] in `=`n' + 3'
qui replace `ivcilow' = `ivest' - invnormal(`levelupp')*`ivse' in `=`n' + 3'
qui replace `ivciupp' = `ivest' + invnormal(`levelupp')*`ivse' in `=`n' + 3'
}
if `nmodels' >= 4 {
qui replace `ivid2' = "`mrmodallabel'" in `=`n' + 4'
qui mrmodal `gd' `gdse' `gp' `gpse' in 1/`k', ///
`mrmodalopts'
qui replace `ivest' = _b[beta] in `=`n' + 4'
qui replace `ivse' = _se[beta] in `=`n' + 4'
qui replace `ivcilow' = `ivest' - invnormal(`levelupp')*`ivse' in `=`n' + 4'
qui replace `ivciupp' = `ivest' + invnormal(`levelupp')*`ivse' in `=`n' + 4'
}
}
// estimate title on plot
if "`effect'" == "" {
local effect "Estimate"
}
// I2GX note
if (`models' > 1) & (`varlistlength' > 3) & ("`note'" == "") {
local i2gxrnd : di %2.1f 100*`i2gx'
local notecmd note("{it:I{sub:GX}{sup:2}}=`i2gxrnd'%", size(vsmall))
}
// modelsonly
if "`modelsonly'" != "" {
local modelsin in `=`n' + 1'/`=`n' + `models''
}
else {
local modelsin
}
// guesses for text size and space for text
if "`modelsonly'" == "" {
if "`stats'" == "" {
if `k' < 30 {
if `textsize' == 0 local textsize 110.0
if `astext' == 0 local astext 50
}
else if `k' >= 30 & `k' < 50 {
if `textsize' == 0 local textsize 150.0
if `astext' == 0 local astext 40
}
else if `k' >= 50 {
if `textsize' == 0 local textsize 200.0
if `astext' == 0 local astext 30
}
}
else {
if `k' < 30 {
if `textsize' == 0 local textsize 110.0
if `astext' == 0 local astext 50
}
else if `k' >= 30 & `k' < 50 {
if `textsize' == 0 local textsize 310.0
if `astext' == 0 local astext 40
}
else if `k' >= 50 {
if `textsize' == 0 local textsize 450.0
if `astext' == 0 local astext 35
}
}
}
else {
if "`stats'" == "" {
if `textsize' == 0 local textsize 150.0
if `astext' == 0 local astext 50
}
else {
if `textsize' == 0 local textsize 67.0
if `astext' == 0 local astext 40
}
}
// call to metan
`metancmd' `ivest' `ivcilow' `ivciupp' `modelsin', ///
notable nooverall nobox ///
lcols(`ivid2') ///
effect(`"`effect'"') ///
graphregion(color(white)) ///
texts(`textsize') ///
astext(`astext') ///
nokeep ///
ilevel(`level') ///
`notecmd' ///
`stats' ///
`options' ///
`bycmd' nohet
// restore the observations that weren't selected - technically not needed
restore
end
exit