-
Notifications
You must be signed in to change notification settings - Fork 9
/
mregger.ado
558 lines (519 loc) · 15.1 KB
/
mregger.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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
*! version 0.1.0 04jun2016 Tom Palmer
program mregger, eclass
version 9
local version : di "version " string(_caller()) ", missing :"
local replay = replay()
if replay() {
if _by() {
error 190
}
`version' Display `0'
exit
}
syntax varlist(min=2 max=2) [aweight] [if] [in] [, ivw fe re ///
reslope recons HETerogi noRESCale PENWeighted Level(cilevel) ///
gxse(varname numeric) tdist RADial unwi2gx oldnames *]
local callersversion = _caller()
// number of genotypes (i.e. rows of data used in estimation)
qui count `if' `in'
local k = r(N)
if `k' < 3 {
di as err "mregger requires a minimum of 3 genotypes"
exit 2001
}
tokenize `"`varlist'"'
/*
2 variables:
1: gd beta
2: gp beta
aw: gd SE
*/
if "`exp'" == "" {
di as err "you must specify aweights"
exit 198
}
if "`fe'" == "fe" & "`re'" == "re" {
di as err "both fe and re not allowed"
exit 198
}
// re option only under version 13 and above
if "`re'" == "re" & `callersversion' < 13 {
di as err "option re requires Stata version 13 or higher"
exit 9
}
tempvar invvar gyse
qui gen double `invvar' `exp' `if' `in'
qui gen double `gyse' = 1/sqrt(`invvar') `if' `in'
// sort out the re options
if "`re'" == "" & ("`recons'" != "" | "`reslope'" != "") {
di as err "option `reslope' `recons' must be specified with option re"
exit 198
}
if "`ivw'" == "ivw" & "`re'" == "re" & "`recons'" == "recons" {
di as err _c "For IVW estimator option recons not allowed "
di as err "for re estimation (as there is no constant)"
exit 198
}
if "`ivw'" == "" & "`re'" == "re" & "`reslope'" == "" & "`recons'" == "" {
// by default include both random _cons and slope
local reslope reslope
local recons recons
}
if "`heterogi'" != "" {
// check if heterogi is installed
capture which heterogi
if _rc {
di as error "-heterogi- from SSC is required; install using"
di "{stata ssc install heterogi}"
exit 499
}
}
if "`penweighted'" == "penweighted" {
tempvar pweights rweights
}
tempname phi // residual variance
tempname dfr // degrees of freedom of residuals
// temp variables for radial estimation
if "`radial'" == "radial" {
tempvar wrad wrady wradx
qui gen double `wrad' = sqrt(`invvar') `if'`in'
qui gen double `wrady' = `wrad'*sign(`2')*`1' `if'`in'
qui gen double `wradx' = `wrad'*abs(`2') `if'`in'
}
// prevent option combinations I am uncertain of
if "`radial'" != "" & "`penweighted'" != "" {
di as err "Options radial and penweighted currently not supported."
exit 198
}
** estimation
if "`ivw'" == "ivw" {
if "`heterogi'" != "" & "`penweighted'" == "" {
tempname gdtr gptr invse
qui gen double `invse' = sqrt(`invvar') `if' `in'
qui gen double `gdtr' = `1'*`invse' `if' `in'
qui gen double `gptr' = `2'*`invse' `if' `in'
if "`radial'" == "" {
qui sem (`gdtr' <- `gptr', nocons) `if' `in'
// alternative: glm `gdtr' `gptr' `if' `in', nocons
if e(converged) == 1 {
local df = e(N) - 1
local qstat = _b[var(e.`gdtr'):_cons]*e(N)
// if using glm: local qstat = e(phi)*(e(N) - 1)
scalar `dfr' = e(df)
}
}
else {
qui sem (`wrady' <- `wradx', nocons) `if'`in'
if e(converged) == 1 {
local df = e(N) - 1
local qstat = _b[var(e.`wrad'):_cons]*e(N)
// if using glm: local qstat = e(phi)*(e(N) - 1)
scalar `dfr' = e(df)
}
}
}
if "`fe'" == "" & "`re'" == "" {
tempname betaivw
// qui reg `1' `2' [aw=`invvar'] `if' `in', nocons
qui glm `1' `2' [iw=`invvar'] `if' `in', nocons
scalar `betaivw' = _b[`2']
scalar `dfr' = e(df)
scalar `phi' = e(phi)
if "`penweighted'" != "" {
qui gen double `pweights' = chi2tail(1, ///
(`2'^2*`invvar')* ///
(`1'/`2' - scalar(`betaivw'))^2) `if' `in'
qui gen double `rweights' = `pweights'*20 `if' `in'
qui replace `rweights' = 1 if `rweights' > 1
qui replace `rweights' = `invvar'*`rweights' `if' `in'
qui glm `1' `2' [iw=`rweights'] `if' `in', nocons
scalar `dfr' = e(df)
scalar `phi' = e(phi)
}
if e(phi) < 1 & "`rescale'" == "" {
di as txt _c "Residual variance =", e(phi) "; "
di as txt _c "rerunning with residual "
di as txt "variance set to 1"
if "`penweighted'" == "" {
qui glm `1' `2' [iw=`invvar'] `if' `in', ///
nocons scale(1)
scalar `dfr' = e(df)
}
else {
qui glm `1' `2' [iw=`rweights'] ///
`if' `in', ///
nocons scale(1)
scalar `dfr' = e(df)
}
scalar `phi' = 1
}
else if e(phi) < 1 & "`rescale'" != "" {
di as txt _c "Residual variance =", e(phi)
di as txt _c "; recommend refitting without "
di as txt "norescale option"
}
}
else if "`fe'" == "fe" {
if "`radial'" == "" {
qui glm `1' `2' [iw=`invvar'] `if' `in', scale(1) nocons
scalar `dfr' = e(df)
scalar `phi' = e(phi)
// alt:
// sem (`gdtr' <- `gptr', nocons) `if' `in', var(e.`gdtr'@1)
}
else {
qui glm `wrady' `wradx' `if'`in', scale(1) nocons
scalar `dfr' = e(df)
scalar `phi' = e(phi)
}
}
else if "`re'" == "re" {
tempvar genoDisease slope
cap gen double `genoDisease' = `1'*sqrt(`invvar') `if' `in'
if _rc != 0 {
qui replace `genoDisease' = `1'*sqrt(`invvar') `if' `in'
}
cap gen double `slope' = `2'*sqrt(`invvar') `if' `in'
if _rc != 0 {
qui replace `slope' = `2'*sqrt(`invvar') `if' `in'
}
if "`radial'" == "" {
cap `version' gsem (`genoDisease' <- `slope' c.M#c.`slope', nocons) ///
`if' `in', ///
var(e.`genoDisease'@1)
}
else {
cap `version' gsem (`wrady' <- `wradx' c.M#c.`wradx', nocons) ///
`if' `in', ///
var(e.`wrady'@1)
}
}
}
else { // MR-Egger
tempvar `1'2 `2'2
qui gen double ``1'2' = `1'*sign(`2') `if' `in'
qui gen double ``2'2' = abs(`2') `if' `in'
if "`heterogi'" != "" & "`penweighted'" == "" {
tempname gdtr gptr invse
qui gen double `invse' = sqrt(`invvar') `if' `in'
qui gen double `gdtr' = ``1'2'*`invse' `if' `in'
qui gen double `gptr' = ``2'2'*`invse' `if' `in'
if "`radial'" == "" {
qui sem (`gdtr' <- `gptr' `invse', nocons) `if' `in'
// alternative: glm `gdtr' `gptr' `invse' `if' `in', nocons
if e(converged) == 1 {
local df = e(N) - 2
local qstat = _b[var(e.`gdtr'):_cons]*e(N)
// if using glm: local qstat di e(phi)*(e(N) - 2)
scalar `dfr' = e(N) - (e(df_m) - 1)
}
}
else {
qui sem (`wrady' <- `wradx') `if' `in'
if e(converged) == 1 {
local df = e(N) - 2
local qstat = _b[var(e.`wrady'):_cons]*e(N)
// if using glm: local qstat di e(phi)*(e(N) - 2)
scalar `dfr' = e(N) - (e(df_m) - 1)
}
}
}
if "`fe'" == "" & "`re'" == "" {
tempname betaegger interegger
if "`radial'" == "" {
// qui reg ``1'2' ``2'2' [aw=`invvar'] `if' `in'
qui glm ``1'2' ``2'2' [iw=`invvar'] `if' `in'
scalar `betaegger' = _b[``2'2']
}
else {
qui glm `wrady' `wradx' `if' `in'
scalar `betaegger' = _b[`wradx']
}
scalar `interegger'= _b[_cons]
scalar `phi' = e(phi)
scalar `dfr' = e(df)
if "`penweighted'" != "" {
qui gen double `pweights' = chi2tail(1, ///
`invvar'*(``1'2' - scalar(`interegger') - ///
scalar(`betaegger')*``2'2')^2) `if' `in'
qui gen double `rweights' = `pweights'*20 `if' `in'
qui replace `rweights' = 1 if `rweights' > 1
qui replace `rweights' = `invvar'*`rweights' `if' `in'
qui glm ``1'2' ``2'2' [iw=`rweights'] `if' `in'
scalar `phi' = e(phi)
scalar `dfr' = e(df)
}
if e(phi) < 1 & "`rescale'" == "" {
di as txt _c "Residual variance =", e(phi) "; "
di as txt _c "rerunning with residual "
di as txt "variance set to 1"
if "`radial'" == "" {
if "`penweighted'" == "" {
qui glm ``1'2' ``2'2' [iw=`invvar'] ///
`if' `in', scale(1)
scalar `dfr' = e(df)
}
else {
qui glm ``1'2' ``2'2' [iw=`rweights'] ///
`if' `in', scale(1)
scalar `dfr' = e(df)
}
}
else {
qui glm `wrady' `wradx' `if'`in', scale(1)
scalar `dfr' = e(df)
}
}
else if e(phi) < 1 & "`rescale'" != "" {
di as txt _c "Residual variance =", e(phi)
di as txt _c "; recommend refitting without "
di as txt "norescale option"
}
}
else if "`fe'" == "fe" {
// alternative syntax:
// sem (``1'2' <- ``2'2') [iw=`invvar'] `if' `in', ///
// var(e.``1'2'@1)
if "`radial'" == "" {
qui glm ``1'2' ``2'2' [iw=`invvar'] `if' `in', scale(1)
}
else {
qui glm `wrady' `wradx' `if' `in', scale(1)
}
scalar `dfr' = e(df)
}
else if "`re'" == "re" {
tempvar genoDisease slope cons
cap gen double `genoDisease' = ``1'2'*sqrt(`invvar') `if' `in'
if _rc != 0 qui replace `genoDisease' = ``1'2'*sqrt(`invvar') ///
`if' `in'
cap gen double `slope' = ``2'2'*sqrt(`invvar') `if' `in'
if _rc != 0 qui replace `slope' = ``2'2'*sqrt(`invvar') `if' `in'
if "`radial'" == "" {
cap gen double `cons' = sqrt(`invvar') `if' `in'
}
else {
cap gen byte `cons' = 1 `if' `in'
}
if _rc != 0 qui replace `cons' = sqrt(`invvar') `if' `in'
if "`reslope'" == "" & "`recons'" == "recons" {
if "`radial'" == "" {
cap `version' gsem (`genoDisease' <- `slope' ///
`cons' c.`cons'#M@1) ///
`if' `in', nocons ///
var(e.`genoDisease'@1) `options'
}
else {
cap `version' gsem (`wrady' <- `wradx' ///
`cons' c.`cons'#M@1) ///
`if' `in', nocons ///
var(e.`wrady'@1) `options'
}
}
else if "`reslope'" == "reslope" & "`recons'" == "" {
if "`radial'" == "" {
cap `version' gsem ///
(`genoDisease' <- `slope' `cons' c.`slope'#c.M@1) ///
`if' `in', nocons var(e.`genoDisease'@1) ///
`options'
}
else {
cap `version' gsem ///
(`wrady' <- `wradx' `cons' c.`wradx'#c.M@1) ///
`if' `in', nocons var(e.`wrady'@1) ///
`options'
}
}
else if "`reslope'" == "reslope" & "`recons'" == "recons" {
if "`radial'" == "" {
cap `version' gsem (`genoDisease' <- `slope' `cons' ///
c.`cons'#M1@1 c.`slope'#c.M2@1) `if' `in', ///
nocons var(e.`genoDisease'@1) `options'
}
else {
cap `version' gsem (`wrady' <- `wradx' `cons' ///
c.`cons'#M1@1 c.`wradx'#c.M2@1) `if' `in', ///
nocons var(e.`wrady'@1) `options'
}
}
}
}
// column and rownames for e(b) and e(V)
mat b = e(b)
mat V = e(V)
if "`re'" == "" {
if "`radial'" == "" {
if "`ivw'" == "" {
if "`oldnames'" == "" {
local names `1':slope `1':_cons
}
else {
local names `1'*sign(`2'):slope `1'*sign(`2'):_cons
}
}
else {
local names `1':`2'
}
}
else {
if "`ivw'" == "" {
local names radialGD:radialGP radialGD:_cons
}
else {
local names radialGD:radialGP
}
}
}
else {
if "`radial'" == "" {
if "`ivw'" == "" {
local names GD:GP GD:_cons GD:c._cons#c.M1 GD:c.GP#c.M2 /:var(M1) /:var(M2) /:cov(M1,M2) /:var(e.GD)
}
else {
local names GD:GP GD:c.GP#c.M /:var(M) /:var(e.GD)
}
}
else {
if "`ivw'" == "" {
local names radialGD:radialGP radialGD:_cons radialGD:c._cons#c.M1 radialGD:c.radialGP#c.M2 radialGD:cons /:var(M1) /:var(M2) /:cov(M1,M2) /:var(e.radialGD)
}
else {
local names radialGD:radialGP radialGD:c.radialGP#c.M radialGD:_cons /:var(M) /:var(e.radialGD)
}
}
}
if !("`re'" == "re" & "`radial'" == "") {
mat colnames b = `names'
mat colnames V = `names'
mat rownames V = `names'
ereturn post b V
}
if "`penweighted'" != "" | "`heterogi'" == "" {
local qstat 0
local df 1
}
if "`tdist'" != "" {
// use t-dist for ereturn display Wald test and CI limits
ereturn scalar df_r = `dfr'
}
else {
// if df_r == . then Stata uses Normal dist
ereturn scalar df_r = .
}
if "`ivw'" == "ivw" & "`fe'" == "fe" {
ereturn scalar phi = 1
}
** start of displaying output
di ""
if "`gxse'" != "" & "`ivw'" == "" {
** I-squared GX
tempname gammabar nobs qgx QGX I2GX gammabarw qgxw QGXw I2GXw gpw
if "`penweighted'" == "" {
* weighted mean of genotype-exposure associations
qui su ``2'2' [aw=1/(`gxse'^2)] `if' `in'
scalar `nobs' = r(N)
scalar `gammabar' = r(mean)
* QGX
qui gen double `qgx' = (``2'2' - `gammabar')^2/(`gxse'^2) `if' `in'
* weighted QGX
qui gen double `gpw' = ``2'2' / `gyse' `if' `in'
qui su `gpw' [aw=(`gyse'/`gxse')^2] `if' `in'
scalar `gammabarw' = r(mean)
qui gen double `qgxw' = (`gpw' - `gammabarw')^2/(`gxse' / `gyse')^2 `if' `in'
}
else {
tempvar gxscaled segxscaled
qui gen double `gxscaled' = ``2'2'*sqrt(`invvar') `if' `in'
qui gen double `segxscaled' = `gxse'*sqrt(`invvar') `if' `in'
qui su `gxscaled' [aw=1/(`segxscaled'^2)] `if' `in'
scalar `nobs' = r(N)
scalar `gammabar' = r(mean)
* QGX
qui gen double `qgx' = ///
(`gxscaled' - `gammabar')^2/(`segxscaled'^2) `if' `in'
}
qui su `qgx' `if' `in'
scalar `QGX' = r(sum)
qui su `qgxw' `if' `in'
scalar `QGXw' = r(sum)
* I2GX
scalar `I2GX' = (`QGX' - (`nobs' - 1))/`QGX'
scalar `I2GX' = max(0, `I2GX')
scalar `I2GXw' = (`QGXw' - (`nobs' - 1))/`QGXw'
scalar `I2GXw' = max(0, `I2GXw')
di _col(44) as txt "Q_GX statistic (weighted) =", %6.2f as res `QGXw'
di _col(42) as txt "I^2_GX statistic (weighted) =", %6.2f as res 100*`I2GXw' as res "%"
ereturn scalar I2GX = `I2GXw'
ereturn scalar QGX = `QGXw'
if "`unwi2gx'" == "unwi2gx" {
di _col(43) as txt "Q_GX statistic (unweighted):", %6.2f `QGX'
di _col(41) as txt "I^2_GX statistic (unweighted):", %6.2f 100*`I2GX' "%"
ereturn scalar I2GXunw = `I2GX'
ereturn scalar QGXunw = `QGX'
}
// heterogi `QGXw' `nobs', level(`level')
}
** number of genotypes
local digits : length local k
local colstart = 79 - (22 + `digits')
di _col(`colstart') as txt "Number of genotypes = " as res %`digits'.0fc `k'
** residual standard error
if "`fe'" == "" {
di _col(47) as txt "Residual standard error =", as res %6.3f sqrt(`phi')
ereturn scalar phi = sqrt(`phi')
}
else {
di _col(39) as txt "Residual standard error constrained at 1"
ereturn scalar phi = 1
}
** heterogi
if "`heterogi'" != "" & "`penweighted'" == "" & "`re'" == "" {
if "`ivw'" == "ivw" {
local qname Cochran's
}
else {
local qname Ruecker's
}
qui heterogi `qstat' `df', level(`level')
local colstart = 79 - (44 + 16 + `=length("`r(df)'")')
di _col(`colstart') as txt "`qname' Q for heterogeneity; chi2(" ///
as res r(df) as txt ") =", ///
as res %5.2f r(Q), as txt "(p = ", as res %5.4f r(pval) as txt ")"
local colstart = 79 - 49
di _col(`colstart') as txt "I-squared statistic =", ///
as res %4.1f r(I2)*100 "%", ///
as txt "(95% CI", ///
as res %4.1f r(lb_I2_M1)*100 "%" ///
as txt ",", ///
as res %4.1f r(ub_I2_M1)*100 "%" ///
as txt ")"
ereturn scalar I2 = r(I2)
ereturn scalar ub_I2_M1 = r(ub_I2_M1)
ereturn scalar lb_I2_M1 = r(lb_I2_M1)
ereturn scalar ub_H_M1 = r(ub_H_M1)
ereturn scalar lb_H_M1 = r(lb_H_M1)
ereturn scalar H = r(H)
ereturn scalar pval = r(pval)
ereturn scalar df = r(df)
ereturn scalar Q = r(Q)
}
** display coefficient table
Display , `re' level(`level') `radial'
** additional e-returned items
if "`re'" == "" {
ereturn local cmd "mregger"
ereturn local cmdline `"mregger `0'"'
}
ereturn scalar k = `k'
end
program Display, rclass
syntax [, re Level(cilevel) radial]
if "`re'" == "re" & "`radial'" == "" {
`version' gsem, noheader nocnsr nodvhead level(`level')
}
else {
ereturn display, level(`level') noomitted
return add // r(table)
}
end
exit