-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmrleaveoneout.ado
202 lines (176 loc) · 4.69 KB
/
mrleaveoneout.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
*! version 0.1.0 31jul2020 Tom Palmer
program mrleaveoneout, rclass
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) [if] [in] , ///
gyse(varname) ///
[Method(string) ///
gxse(varlist) ///
noPRINT ///
genotype(varname) ///
noPLOT ///
metanopts(string asis) ///
TEXTSize(real 0) ///
ASTEXT(integer 0) ///
*]
local callersversion = _caller()
// 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
}
local nvarlist = wordcount("`varlist'")
local npheno = `nvarlist' - 1
tokenize "`varlist'"
local gd `1'
if `nvarlist' == 2 {
local gp `2'
}
local phenovarlist : subinstr local varlist "`gd' " ""
// set default method
if "`method'" == "" local method ivw
// check method
if !inlist("`method'", "ivw", "egger", "mregger", "mrivw", ///
"median" "mrmedian", "mrmodal", "modal", "mode") & ///
!inlist("`method'", "mvmr", "mvivw", "mvegger") {
di as error "method should be one of: ivw, egger, mregger," ///
"mrivw, median, mrmedian, mrmodal, modal, mode, mvmr, mvivw, mvegger"
exit 198
}
// number of genotypes (i.e. rows of data used in estimation)
qui count `if' `in'
local k = r(N)
tempname table
// preserve
preserve
if "`if'`in'" != "" qui keep `if'`in'
local tempvarlist b se z pvalue ll ul df crit eform
tempvar `tempvarlist'
foreach var in `tempvarlist' {
qui gen double ``var'' = .
}
if "`print'" == "noprint" local qui qui
if "`genotype'" == "" {
tempvar genotype
qui gen str `genotype' = ""
qui forvalues i = 1/`k' {
replace `genotype' = "`i'" in `i'
}
label variable `genotype' "Genotypes"
}
`qui' forvalues i = 1/`=_N' {
di _n as txt "Model fitted without genotype:", as res `genotype'[`i']
if inlist("`method'", "ivw", "mrivw") {
mregger `varlist' [aw=1/`gyse'^2] if _n != `i', ivw fe `options'
}
else if inlist("`method'", "egger", "mregger") {
mregger `varlist' [aw=1/`gyse'^2] if _n != `i', `options'
}
else if inlist("`method'", "median", "mrmedian") {
mrmedian `gd' `gyse' `gp' `gxse' if _n != `i', `options'
}
else if inlist("`method'", "mrmodal", "modal", "mode") {
mrmodal `gd' `gyse' `gp' `gxse' if _n != `i', `options'
}
else if inlist("`method'", "mvmr", "mvivw") {
mrmvivw `varlist' [aw=1/`gyse'^2] if _n != `i', `options'
}
else if "`method'" == "mvegger" {
mrmvegger `varlist' [aw=1/`gyse'^2] if _n != `i', `options'
}
mat `table' = r(table)
local j = 1
foreach var in `tempvarlist' {
qui replace ``var'' = `table'[`j',1] in `i'
local j = `j' + 1
}
}
if "`plot'" != "noplot" {
if "`print'" != "noprint" di _n as txt "Model fitted to all genotypes"
if inlist("`method'", "ivw", "mrivw") {
`qui' mregger `varlist' [aw=1/`gyse'^2], ivw fe `options'
local methodstring IVW
}
else if inlist("`method'", "egger", "mregger") {
`qui' mregger `varlist' [aw=1/`gyse'^2], `options'
local methodstring MR-Egger
}
else if inlist("`method'", "median", "mrmedian") {
`qui' mrmedian `gd' `gyse' `gp' `gxse', `options'
local methodstring Median
}
else if inlist("`method'", "mrmodal", "modal", "mode") {
`qui' mrmodal `gd' `gyse' `gp' `gxse', `options'
local methodstring Modal
}
else if inlist("`method'", "mvmr", "mvivw") {
`qui' mrmvivw `varlist' [aw=1/`gyse'^2], `options'
local methodstring MVMR
}
else if "`method'" == "mvegger" {
`qui' mrmvegger `varlist' [aw=1/`gyse'^2], `options'
local methodstring MVMR-Egger
}
mat `table' = r(table)
qui set obs `=_N+1'
local nnew = _N
local j = 1
foreach var in `tempvarlist' {
qui replace ``var'' = `table'[`j',1] in `nnew'
local j = `j' + 1
}
qui replace `genotype' = "`methodstring'" in `nnew'
tempvar group
qui gen `group' = "{bf:Omitted genotype}"
qui replace `group' = "{bf:All genotypes}" in `=_N'
local bycmd by(`group') nosubgroup
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 50
}
else if `k' >= 50 {
if `textsize' == 0 local textsize 350.0
if `astext' == 0 local astext 50
}
`metancmd' `b' `se', ///
fixedi ///
notable ///
nowt ///
nobox ///
texts(`textsize') ///
astext(`astext') ///
nooverall lcols(`genotype') ///
effect("Estimate") ///
graphregion(color(white)) ///
nohet ///
`bycmd' ///
nokeep ///
`metanopts'
}
// restore
restore
end
exit