-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmig.ado
561 lines (460 loc) · 11.3 KB
/
mig.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
559
*! version 1.0.0 24sep2021
cap program drop mig
program mig, rclass
syntax [varlist(default=none)], testsyntax(string) ///
[pe(string) ve(string) nimp(numlist) ///
DFMethod(string) DFResidual(string) DFParameter(numlist) ///
chi2(string) chi2_df(string) converged(string) ///
fvalue(string) f_df(string) ///
pvalue(string)]
*default input
cap assert ("`pe'"!="" & "`ve'" !="") | "`chi2'"!="" | "`pvalue'"!="" | "`fvalue'"!=""
if _rc {
dis "Please specify either pe and ve, chi2 or pvalue options"
}
*testsyntax
if "`varlist'" !="" {
local ts=subinstr("`testsyntax'","variable","`1'",.)
local ts=subinstr("`ts'","group","`2'",.)
}
else {
local ts = "`testsyntax'"
}
*dfmethod
if inlist("`dfmethod'","small","adjusted") {
local dfmethod adj
}
cap assert inlist("`dfmethod'","adj","old","large","small","")
if _rc {
dis "dfmethod not recognized, only large and small are allowed, default large is used."
local dfmethod
}
local collectdf 0
if "`dfmethod'"=="adj" {
cap assert "`dfresidual'"!="" | "`dfparameter'"!=""
if _rc {
dis as text "Degrees of freedom of residuals (dfresidual) or parameter (dfparameter) have to be defined to use small sample adjustment for degress of freedom---large sample approximation used."
local dfmethod
}
if "`dfresidual'"!="" {
cap assert "`dfparameter'"==""
if _rc {
dis as text "Only one of dfresidual and dfparameter should be given---dfparameter ignored."
local dfparameter
}
cap confirm number `dfresidual'
if _rc {
local collectdf 1
}
}
if "`dfparameter'"!="" {
local wc: word count `dfparameter'
cap assert `wc'==1
if _rc {
local dfparameter: word 1 of `dfparameter'
dis as text "Only first element of dfparameter is used (`dfparameter')"
}
cap confirm number `dfparameter'
if _rc {
dis as text "dfparameter has to be a number---large sample approximation used"
local dfmethod
}
else {
qui count if _mi_m==0
local nt = `r(N)'
local dfresidual = `nt'-`dfparameter'
}
}
}
if "`nimp'"!="" {
local wc: word count `nimp'
cap assert `wc'==1
if _rc {
local nimp: word 1 of `nimp'
dis as text "Only first element of nimp is used (`nimp')"
}
}
if ("`pe'"!="" & "`ve'" !="") {
if "`ts'"=="none" {
if "`nimp'"=="" {
qui count
local M=`r(N)'
}
else {
local M=`nimp'
}
//point estimate
sum `pe'
local qn=`r(mean)'
//variance between
local bn=(`r(sd)')^2
//variance within
sum `ve'
local u=`r(mean)'
//total variance
local vt=`u'+(1+1/`M')*`bn'
local se=sqrt(`vt')
//degrees of freedom according to Rubin (1987), Van Buuren (2018)):
local df=(`M'-1)*(1+`u'/((1+1/`M')*`bn'))^2
if "`dfmethod'"=="adj" {
if `collectdf'==1 {
sum `dfresidual'
local dfresidual = `r(mean)'
}
//adjusted df according to Barnard and Rubin (1999), used in R (mice) and Stata if e(df_r) is defined
local lambda = (`bn' + `bn'/`M')/`vt'
local dfobs = `dfresidual'*(`dfresidual'+1)/(`dfresidual'+3)*(1-`lambda')
local dfadj = `df'*`dfobs'/(`df'+`dfobs')
local dfu = `dfadj'
}
else {
local dfu = `df'
}
local Mcount = `M'
}
else {
*point estimate and variance
qui mi query
local M = r(M)
if "`nimp'"!="" {
cap assert `M'==`nimp'
if _rc {
if `nimp'<`M' {
local M = `nimp'
dis as text "More than `nimp' imputations found, only the first `nimp' were considered."
}
else {
dis as text "Less than `nimp' imputations found, only `M' imputations were considered."
}
}
}
tempname z v dfcol
scalar `z'=0
scalar `v'=0
local Mcount
//df (if applicable)
scalar `dfcol' = 0
if "`dfmethod'"=="adj" {
if `collectdf'==1 {
local add scalar `dfcol' = `dfcol' + `dfresidual'
}
}
if "`converged'" == "" {
qui mi xeq 1/`M': `ts'; scalar `z' = `z' + `pe'; scalar `v' = `v' + `ve'; `add'
local Mcount = `M'
}
else {
forvalues i=1/`M' {
preserve
mi extract `i', clear
cap `ts'
if `converged' == 1 {
scalar `z' = `z' + `pe'
scalar `v' = `v' + `ve'
local Mcount = `Mcount' + 1
`add'
}
restore
}
if `Mcount' != `M' {
dis "Only `Mcount' imputation converged"
}
}
//point estimate:
local qn=`z'/`Mcount'
//variance within:
local u=`v'/`Mcount'
//adjusted df (if applicable)
local dfcoll = `dfcol'/`Mcount'
//variance between:
tempname b
scalar `b'=0
if "`converged'" == "" {
qui mi xeq 1/`M': `ts'; scalar `b' = `b' + (`pe'-`qn')^2; `add'
}
else {
forvalues i=1/`M' {
preserve
mi extract `i', clear
cap `ts'
if `converged' == 1 {
scalar `b' = `b' + (`pe'-`qn')^2
}
restore
}
}
local bn=1/(`Mcount'-1)*`b'
//total variance:
local vt=`u'+(1+1/`Mcount')*`bn'
local se=sqrt(`vt')
//degrees of freedom according to Rubin (1987), Van Buuren (2018)):
local df=(`Mcount'-1)*(1+`u'/((1+1/`Mcount')*`bn'))^2
//adjusted df (if applicable)
if `dfcoll' != 0 {
local dfresidual = `dfcoll'
}
if "`dfmethod'"=="adj" {
//adjusted df according to Barnard and Rubin (1999), used in R (mice) but not in Stata
//following https://bookdown.org/mwheymans/bookmi/rubins-rules.html#significance-testing
local lambda = (`bn' + `bn'/`Mcount')/`vt'
//local riv = (`bn' + `bn'/`Mcount')/`u'
//local dfold = (`Mcount'-1)*(1+1/(`riv'))^2
//local dfold = (`Mcount'-1)/(`lambda'^2)
//the same as df above
local dfobs = `dfresidual'*(`dfresidual'+1)/(`dfresidual'+3)*(1-`lambda')
local dfadj = `df'*`dfobs'/(`df'+`dfobs')
local dfu = `dfadj'
}
else {
local dfu = `df'
}
}
local t=`qn'/sqrt(`vt')
if `dfu'>10^10 {
local pv= 2*(1-normal(abs(`t')))
local ll=`qn'-invnormal(0.975)*`se'
local ul=`qn'+invnormal(0.975)*`se'
local crit=invnormal(0.975)
}
else {
local pv= 2*(1-t(`dfu',abs(`t')))
local ll=`qn'-invt(`dfu',0.975)*`se'
local ul=`qn'+invt(`dfu',0.975)*`se'
local crit=invt(`dfu',0.975)
}
tempname R
matrix `R' = (`qn' \ `se' \ `t' \ `pv' \ `ll' \ `ul' \ `dfu' \ `crit')
mat colnames `R' = test
mat rownames `R' = b se t pvalue ll ul df crit
return scalar M = `Mcount'
return matrix table `R'
dis ""
dis "mean: `qn'"
dis "se: `se'"
dis "t: `t'"
dis "pvalue: `pv'"
dis "ll: `ll'"
dis "ul: `ul'"
dis "df: `dfu'"
//dis "dfadj: `dfadj'"
}
*F-values
if "`fvalue'" != "" {
if "`f_df'"=="" {
dis "Degress of freedom for numerator of F-statistic not given, 1 assumed."
local f_df = 1
}
if "`ts'"=="none" {
tempvar fchi2
qui gen `fchi2' = `f_df'*`fvalue'
}
else {
local fchi2 `f_df'*`fvalue'
}
local chi2_df `f_df'
mig_chi2, ts2(`ts') chi2(`fchi2') chi2_df(`f_df') nimp(`nimp') converged(`coverged')
return scalar p_chi2 = `r(p_chi2)'
dis "chi2 pvalue: `r(p_chi2)'"
return scalar Dx = r(Dx)
return scalar vx = r(vx)
return scalar chi2_df = `r(chi2_df)'
return scalar av_chi2=`r(av_chi2)'
return scalar M = `r(M)'
}
//repeat chi2
*chi2
if "`chi2'"!="" {
mig_chi2, ts2(`ts') chi2(`chi2') chi2_df(`chi2_df') nimp(`nimp') converged(`coverged')
return scalar p_chi2 = `r(p_chi2)'
dis "chi2 pvalue: `r(p_chi2)'"
return scalar Dx = r(Dx)
return scalar vx = r(vx)
return scalar chi2_df = `r(chi2_df)'
return scalar av_chi2=`r(av_chi2)'
return scalar M = `r(M)'
}
*median p-value
if "`pvalue'"!="" {
mig_pv, ts2(`ts') pvalue(`pvalue') nimp(`nimp') converged(`coverged')
return scalar p_med = `r(p_med)'
return scalar M = `r(M)'
dis "median pvalue: `r(p_med)'"
}
end
*******************
*Helper functions
**************
*chi squared
***************
//https://bookdown.org/mwheymans/bookmi/pooling-methods-for-categorical-variables.html
//Multiple parameter Wald test or D2 method
//Enders (2012), Marshall (2009)
cap program drop mig_chi2
program mig_chi2, rclass
syntax, ts2(string) chi2(string) [chi2_df(string) nimp(numlist) converged(string)]
if "`chi2_df'"=="" {
local chi2_df=1
dis "Degress of freedom for Chi2-statistic not given, 1 assumed."
}
if "`ts2'"=="none" {
if "`nimp'"=="" {
qui count
local M=`r(N)'
}
else {
local M=`nimp'
}
qui sum `chi2'
local av_x2=`r(mean)'
tempvar sqrt_chi2
gen `sqrt_chi2'=sqrt(`chi2')
qui sum `sqrt_chi2'
local av_sqrt_chi2=`r(mean)'
tempvar diff2
gen `diff2'=(`sqrt_chi2'-`av_sqrt_chi2')^2
qui sum `diff2'
local sd2=`r(sum)'
local Mcount = `M'
qui sum `chi2_df'
local avchi2_df = `r(mean)'
}
else {
qui mi query
local M = r(M)
if "`nimp'"!="" {
cap assert `M'==`nimp'
if _rc {
if `nimp'<`M' {
local M = `nimp'
dis as text "More than `nimp' imputations found, only the first `nimp' were considered."
}
else {
dis as text "Less than `nimp' imputations found, only `M' imputations were considered."
}
}
}
scalar X2=0
scalar sqrtX2=0
local Mcount
tempname A
tempname B
tempname C
tempname C2
matrix `A'=J(1,1,.)
scalar X2df=0
if "`converged'" == "" {
qui mi xeq 1/`M': `ts2'; scalar X2 = X2 + `chi2'; ///
matrix `A' = `A', sqrt(abs(`chi2')); ///
scalar sqrtX2=sqrtX2 + sqrt(abs(`chi2')); ///
scalar X2df = X2df + `chi2_df'
local Mcount = `M'
}
else {
forvalues i=1/`M' {
preserve
mi extract `i', clear
cap `ts2'
if `converged' == 1 {
local Mcount = `Mcount' + 1
scalar X2 = X2 + `chi2'
matrix `A' = `A', sqrt(abs(`chi2'))
scalar sqrtX2=sqrtX2 + sqrt(abs(`chi2'))
scalar X2df = X2df + `chi2_df'
}
restore
}
if `Mcount' != `M' {
dis "Only `Mcount' imputation converged"
}
}
matrix `A'=`A'[1...,2...]
local av_x2=X2/`Mcount'
matrix `B'=J(1,`Mcount',sqrtX2/`Mcount')
matrix `C'=(`B'-`A')
matrix `C2'=`C'*`C''
local sd2=`C2'[1,1]
local avchi2_df = X2df/`Mcount'
}
scalar rx=(1+1/`Mcount')*1/(`Mcount'-1)*`sd2'
scalar Dx=(`av_x2'/`avchi2_df'-(`Mcount'+1)/(`Mcount'-1)*rx)/(1+rx)
scalar vx=`avchi2_df'^(-3/`Mcount')*(`Mcount'-1)*(1+1/rx)^2
if vx>(10^15) {
local pv_chi2=1-chi2(`avchi2_df',Dx)
}
else {
local pv_chi2=1-F(`avchi2_df',vx,Dx)
}
return scalar p_chi2 = `pv_chi2'
return scalar Dx = Dx
return scalar vx = vx
return scalar chi2_df = `avchi2_df'
return scalar av_chi2=`av_x2'
return scalar M = `Mcount'
end
*median p-value
***********************
//The Median P Rule
//Eekhout, Wiel, and Heymans (2017)
// MI procedure without the outcome variable in the imputation model?
cap program drop mig_pv
program mig_pv, rclass
syntax, ts2(string) pvalue(string) [nimp(numlist) converged(string)]
if "`ts2'"=="none" {
if "`nimp'"=="" {
qui count
local M=`r(N)'
}
else {
local M=`nimp'
}
qui sum `pvalue',d
local medp=`r(p50)'
}
else {
qui mi query
local M = r(M)
if "`nimp'"!="" {
cap assert `M'==`nimp'
if _rc {
if `nimp'<`M' {
local M = `nimp'
dis as text "More than `nimp' imputations found, only the first `nimp' were considered."
}
else {
dis as text "Less than `nimp' imputations found, only `M' imputations were considered."
}
}
}
local Mcount
tempname D
matrix `D'=J(1,1,.)
if "`converged'" == "" {
qui mi xeq 1/`M': `ts2'; matrix `D' = `D', `pvalue'
local Mcount = `M'
}
else {
forvalues i=1/`M' {
preserve
mi extract `i', clear
cap `ts2'
if `converged' == 1 {
local Mcount = `Mcount' + 1
matrix `D' = `D',`pvalue'
}
restore
}
if `Mcount' != `M' {
dis "Only `Mcount' imputation converged"
}
}
matrix `D'=`D'[1...,2...]
matrix `D'=`D''
tempvar dvar
svmat `D', names(`dvar')
qui sum `dvar',d
local medp = `r(p50)'
drop `dvar'
}
return scalar p_med = `medp'
return scalar M = `Mcount'
end