-
Notifications
You must be signed in to change notification settings - Fork 3
/
ultimatch.ado
1642 lines (1629 loc) · 39.8 KB
/
ultimatch.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 2021.02.02 \\
cap program drop ultimatch
program define ultimatch, rclass
version 9.0
syntax [varlist(default=none)] [if] [in], [EXAct(varlist)] [CAliper(real -1.0)] [Draw(integer -1)] [EXP(string)] [Limit(string)] Treated(varname) [REport(varlist))] [UNIt(varlist)] [Between] [Greedy] [SIngle] [SUpport] [UNMatched] [COpy] [Full] [RANk] [EUclid] [Mahalanobis] [RADius]
tempvar axis tr mark cell nouse base cluster claim lock cnt trcnt ctcnt miss order
tempname table cell matrix row
matrix `matrix' = (.)
return matrix match = `matrix'
if `caliper' <= 0 {
local caliper = .
}
if `draw' <= 0 {
local draw = .
}
tokenize `"`varlist'"'
local copy = "`copy'"' != ""
local unmatched = "`unmatched'" != ""
local between = "`between'" != ""
local greedy = "`greedy'" != ""
local single = "`single'" != ""
local support = "`support'" != ""
local rank = "`rank'" != ""
local euclid = "`euclid'" != ""
local mahalanobis = "`mahalanobis'" != ""
local full = "`full'" != ""
local radius = "`radius'" != ""
local clustered = 0
local method = ""
if `greedy' & `copy' {
di as error "options greedy and copy are mutually exclusive"
error 999
}
if `euclid' & `mahalanobis' {
di as error "options euclid and mahalanobis are mutually exclusive"
error 999
}
if `full' & `copy' == 0 {
di as error "full is a sub-option of copy"
error 999
}
if `unmatched' & "`report'" == "" {
di as error "unmatched is a sub-option of report"
error 999
}
if `"`1'"' != "" {
if `"`2'"' != "" {
local method = "Distance"
}
else {
local method = "Score"
}
}
if "`method'" == "" {
if `"`exact'"' != "" {
if `draw' != . | `caliper' != . | `between' | `greedy' | `support' | `copy' | `rank' | `euclid' | `mahalanobis' | `radius' | `"`limit'"' != "" | `"`exp'"' != "" {
di as error "coarsened exact mode does not support following options: draw, caliper, between, greedy, support, copy, rank, single, euclid, mahalanobis, radius, limit, exp"
error 999
}
local draw = .
local caliper = 1
}
else {
di as error "if varlist is empty, exact option has to be specified"
error 999
}
}
if "`method'" == "Distance" & `between' {
di as error "distance based matching does not support following options: between"
error 999
}
if "`method'" == "Score" & (`euclid' | `mahalanobis') {
di as error "score based matching does not support following options: euclid, mahalanobis"
error 999
}
if `radius' & (`between' | `greedy' | `single' | `draw' != .) {
di as error "radius matching does not support following options: between, greedy, single, draw"
error 999
}
if `radius' & `caliper' == . {
di as error "radius matching requires the specification of option: caliper"
error 999
}
if "`method'" == "Distance" {
if `rank' {
if `mahalanobis' == 0 {
local euclid = 1
}
}
else {
if `euclid' == 0 {
local mahalanobis = 1
}
}
}
if `full' {
local copy = 2
}
local matching = cond(`copy',"Copying ","")
local matching = "`matching'" + cond(`support',"Supported ","")
local matching = "`matching'" + cond(`single',"Single ","")
local matching = "`matching'" + cond(`greedy',"Greedy ","")
local matching = "`matching'" + cond(`between',"Sandwiched ","")
local matching = "`matching'" + cond(`euclid',"Euclidean ","")
local matching = "`matching'" + cond(`mahalanobis',"Mahalanobis ","")
local matching = "`matching'" + cond("`method'" != "","`method'-based ", "")
local matching = "`matching'" + cond(`rank',"Percentile Rank ", "")
local matching = "`matching'" + cond(`radius',"Radius ", "")
local matching = "`matching'" + cond("`method'" != "" & `radius' == 0,"Neighborhood ", "")
local matching = "`matching'" + cond("`method'" == "","Coarsened Exact ", "")
local matching = "`matching'" + "Matching"
cap confirm var _match
if _rc == 0 {
di as error "_match is already defined"
error 999
}
cap confirm var _weight
if _rc == 0 {
di as error "_weight is already defined"
error 999
}
if "`Method'" != "" {
cap confirm var _distance
if _rc == 0 {
di as error "_distance is already defined"
error 999
}
if `greedy' == 0 {
cap confirm var _copy
if _rc == 0 {
di as error "_copy is already defined"
error 999
}
}
}
if `support' {
cap confirm var _support
if _rc == 0 {
di as error "_support is already defined"
error 999
}
}
di as text "`matching'"
return local matching = "`matching'"
if "`exp'" != "" {
local exp = "`exp' "
local cmd = ""
local var = ""
local prev = ""
local len = length("`exp'")
forvalue i = 1/`len' {
local chr = substr("`exp'",`i',1)
if "`var'" == "" {
if regexm("`chr'","[a-zA-Z._]") {
local var = "`chr'"
}
else {
local cmd = "`cmd'`chr'"
}
}
else {
if regexm("`chr'","[a-zA-Z._0-9]") {
local var = "`var'`chr'"
}
else if "`chr'" == "(" {
local cmd = "`cmd'`var'`chr'"
local var = ""
}
else {
if regexm("`var'","t\..+") {
local var = substr("`var'",3,.)
cap confirm var `var'
if _rc == 0 {
local var = "`var'[t]"
}
}
else {
cap confirm var `var'
if _rc == 0 {
local var = "`var'[c]"
}
}
local cmd = "`cmd'`var'`chr'"
local var = ""
}
}
}
local test = subinstr(subinstr(`"`cmd'"',"[c]","[2]",.),"[t]","[1]",.)
cap local test = `test'
if _rc != 0 {
di as error `"invalid expression: `exp'"'
error 999
}
}
else {
local cmd = ""
}
qui des, varlist
local names = r(varlist)
qui gen byte `nouse' = 9
qui replace `nouse' = 0 `if' `in'
qui replace `nouse' = 9 if `treated' == .
if "`exact'" != "" {
qui egen long `miss' = rmiss(`exact') if `nouse' == 0
qui replace `nouse' = 9 if `miss' > 0 & `miss' != .
qui drop `miss'
}
if "`method'" != "" {
qui egen long `miss' = rmiss(`*') if `nouse' == 0
qui replace `nouse' = 9 if `miss' > 0 & `miss' != .
qui drop `miss'
local score = `"`1'"'
}
qui gen byte `tr' = (`treated' == 1) if `nouse' == 0
qui gen long `order' = _n
if `support' {
qui sum `score' if `nouse' == 0 & `tr' == 1
local Ns = r(N)
local max = r(max)
local min = r(min)
qui sum `score' if `nouse' == 0 & `tr' == 0
local Ns = `Ns'+r(N)
local max = min(r(max), `max')
local min = max(r(min), `min')
qui gen byte _support = `score' <= `max' & `score' >= `min' if `nouse' == 0
label var _support "common support"
qui replace `nouse' = 7 if `nouse' == 0 & _support == 0 // non-supported nouse < general nouse
}
if "`limit'" != "" | `rank' {
local vars = ""
local range = ""
local i = 1
local var = word("`limit'", `i')
while "`var'" != "" {
cap confirm var `var'
if _rc != 0 {
di as error `"invalid limitation variable `var'"'
error 999
}
local j = `j'+1
local vars = "`vars' `var'"
local i = `i'+1
local var = word("`limit'", `i')
cap confirm number `var'
if _rc == 0 {
local ranges = "`ranges' `var'"
local i = `i'+1
local var = word("`limit'", `i')
}
else {
local ranges = "`ranges' 5"
}
}
if "`vars'" != "" {
local vars = trim("`vars'")
local ranges = trim("`ranges'")
qui egen long `miss' = rmiss(`vars') if `nouse' == 0
qui replace `nouse' = 9 if `miss' > 0 & `miss' != .
qui drop `miss'
}
if `rank' {
local vars = "`vars' `*'"
local vars = trim("`vars'")
}
qui count if `nouse' == 0
local N = r(N)
local limit = ""
local ranks = ""
local i = 1
local var = word("`vars'", `i')
local range = word("`ranges'", `i')
while `"`var'"' != "" {
tempvar r`i'
sort `nouse' `var' `order'
qui egen double `cnt' = count(1) in 1/`N', by(`nouse' `var')
qui gen double `r`i'' = (_n - 1 + 0.5 * `cnt')/`N' * 100 in 1/`N' if `var' != `var'[_n-1]
qui replace `r`i'' = `r`i''[_n-1] in 1/`N' if `r`i'' == .
qui drop `cnt'
if "`range'" != "" {
local limit = `"`limit' & abs(`r`i''[t]-`r`i''[c]) <= `range'"'
}
else {
local ranks = "`ranks' `r`i''"
}
local i = `i'+1
local var = word("`vars'", `i')
local range = word("`ranges'", `i')
}
local limit = substr(trim(`"`limit'"'),3,.)
local ranks = trim("`ranks'")
}
else {
qui count if `nouse' == 0
local N = r(N)
sort `nouse' `order'
}
local obs1 = _N
if "`unit'" != "" {
if word("`unit'",2) == "" {
local cluster = "`unit'"
}
else {
qui egen long `cluster' = group(`unit') in 1/`N'
}
local clustered = 1
}
else {
qui gen long `cluster' = _n in 1/`N'
}
if `"`report'"' != "" & `unmatched' {
qui gen byte `base' = (`nouse' == 0)
}
if `"`exact'"' != "" {
qui egen long `cell' = group(`exact') in 1/`N'
}
else {
qui gen byte `cell' = 1 in 1/`N'
}
if "`method'" == "Distance" {
if `radius' {
qui gen long _match = .
qui gen double _weight = .
qui gen double _distance = .
label var _match "match id"
label var _weight "pweight"
if `rank' {
if `mahalanobis' {
label var _distance "closest mahalanobis rank radius distance"
}
else {
label var _distance "closest euclidean rank radius distance"
}
local distvars = "`ranks'"
}
else {
if `mahalanobis' {
label var _distance "closest mahalanobis radius distance"
}
else {
label var _distance "closest euclidean radius distance"
}
local distvars = "`*'"
}
qui gen double `axis' = .
order `nouse' `axis' `cell' `tr' _match _distance _weight `distvars'
local varcnt = wordcount("`distvars'")
if `mahalanobis' {
qui corr `distvars' in 1/`N', covar noformat
matrix `matrix' = invsym(r(C))
}
else {
matrix `matrix' = diag(J(1,`varcnt',1)) // identity matrix replaces inverted covariance matrix for eucledian distance
}
mata: _ultimatchdistanceaxis(`N', 2, 8, "`matrix'")
sort `nouse' `cell' `axis' `tr' `order'
mata: _ultimatchradius(`varcnt', "`matrix'", `caliper', `copy')
local M = r(obs)
if r(match) == 0 {
di as error "unable to match"
drop _match _weight _distance
error 999
}
return scalar comp = r(comp)
order _match _weight _distance
}
else {
qui gen long `claim' = .
qui gen byte `lock' = .
qui gen long _match = .
qui gen double _weight = .
qui gen double _distance = .
label var _match "match id"
label var _weight "pweight"
if `rank' {
if `mahalanobis' {
label var _distance "nearest mahalanobis rank distance"
}
else {
label var _distance "nearest euclidean rank distance"
}
local distvars = "`ranks'"
}
else {
if `mahalanobis' {
label var _distance "nearest mahalanobis distance"
}
else {
label var _distance "nearest euclidean distance"
}
local distvars = "`*'"
}
if `draw' == . {
local draw = 1
}
qui gen double `axis' = .
order `nouse' `axis' `cell' `tr' _match _distance _weight `claim' `lock' `distvars'
local varcnt = wordcount("`distvars'")
if `mahalanobis' {
qui corr `distvars' in 1/`N', covar noformat
matrix `matrix' = syminv(r(C))
}
else {
matrix `matrix' = diag(J(1,`varcnt',1)) // identity matrix replaces inverted covariance matrix for eucledian distance
}
mata: _ultimatchdistanceaxis(`N', 2, 10, "`matrix'")
sort `nouse' `cell' `axis' `tr' `order'
mata: _ultimatchdistance(`varcnt', "`matrix'", `caliper', `single', `draw', `greedy', `copy')
qui drop `claim'
local M = r(obs)
if r(match) == 0 {
di as error "unable to match"
drop _match _weight _distance
error 999
}
return scalar comp = r(comp)
order _match _weight _distance
}
}
else if "`method'" == "Score" {
if `radius' {
qui gen long _match = .
qui gen double _weight = .
qui gen double _distance = .
label var _match "match id"
label var _weight "pweight"
if `rank' {
label var _distance "closest rank radius distance"
local distvars = "`ranks'"
}
else {
label var _distance "closest radius distance"
local distvars = "`score'"
}
sort `nouse' `cell' `distvars' `tr' `order'
order `nouse' `distvars' `cell' `tr' _match _distance _weight
mata: _ultimatchradius(1, "", `caliper', `copy')
local M = r(obs)
if r(match) == 0 {
di as error "unable to match"
drop _match _weight _distance
error 999
}
return scalar comp = r(comp)
order _match _weight _distance
}
else {
qui gen long `claim' = .
qui gen long _match = .
qui gen double _weight = .
qui gen double _distance = .
label var _match "match id"
label var _weight "pweight"
if `rank' {
label var _distance "nearest rank distance"
local distvars = "`ranks'"
}
else {
label var _distance "nearest distance"
local distvars = "`score'"
}
if `draw' == . {
local draw = 1
}
sort `nouse' `cell' `distvars' `tr' `order'
order `nouse' `distvars' `cell' `tr' _match _distance _weight `claim'
mata: _ultimatchneighbor(`caliper', `single', `draw', `between', `greedy', `copy')
qui drop `claim'
local M = r(obs)
if r(match) == 0 {
di as error "unable to match"
drop _match _weight _distance
error 999
}
return scalar comp = r(comp)
order _match _weight _distance
}
}
else {
sort `nouse' `cell' `order'
qui egen double `trcnt' = sum(`tr') in 1/`N', by(`cell')
qui egen double `ctcnt' = sum(1-`tr') in 1/`N', by(`cell')
qui gen double _weight = `trcnt'/`ctcnt' in 1/`N' if `trcnt' > 0 & `ctcnt' > 0
label var _weight "pweight"
qui drop `trcnt' `ctcnt'
qui replace _weight = 1 in 1/`N' if `tr' == 1 & _weight != .
qui egen long _match = group(`cell') in 1/`N' if _weight != .
label var _match "match id"
local M = `N'
qui count in 1/`N' if _match != .
if r(N) == 0 {
di as error "unable to match"
drop _match _weight
error 999
}
order _match _weight
}
if `copy' {
qui gen byte _copy = .
label var _copy "copied observation"
local obs1 = `obs1'+1
local obs2 = _N
if `obs1' <= `obs2' {
qui replace _copy = 1 in `obs1'/`obs2'
local clustered = 1
}
}
order `names'
di as text "{hline 17}{c TT}{hline 32}"
di as text "{ralign 16:Support} {c |} Treated Control"
di as text "{hline 17}{c +}{hline 32}"
if `support' {
qui tab _support `tr' in 1/`Ns', matcell(`cell') matrow(`row')
if `row'[2,1] == . {
if `row'[1,1] == 1 {
matrix `cell' = J(1,2,0) \ `cell'
}
else {
matrix `cell' = `cell' \ J(1,2,0)
}
}
di as text "{ralign 16:Total} {c |} " as result %14.9g `cell'[1,2]+`cell'[2,2] " " %14.9g `cell'[1,1]+`cell'[2,1]
di as text "{ralign 16:Without} {c |} " as result %14.9g `cell'[1,2] " " %14.9g `cell'[1,1]
di as text "{ralign 16:With} {c |} " as result %14.9g `cell'[2,2] " " %14.9g `cell'[2,1]
matrix `matrix' = (`cell'[1,2]+`cell'[2,2],`cell'[1,1]+`cell'[2,1],.,.,.,.)
matrix `matrix' = `matrix' \ (`cell'[1,2],`cell'[1,1],.,.,.,.)
matrix `matrix' = `matrix' \ (`cell'[2,2],`cell'[2,1],.,.,.,.)
}
else {
qui count in 1/`M' if `tr' == 1
di as text "{ralign 16:Total} {c |} " as result %14.9g r(N) " " %14.9g `M'-r(N)
di as text "{ralign 16:Without} {c |} " as result %14.9g 0 " " %14.9g 0
di as text "{ralign 16:With} {c |} " as result %14.9g r(N) " " %14.9g `M'-r(N)
matrix `matrix' = (r(N), `M'-r(N),.,.,.,.)
matrix `matrix' = `matrix' \ (0,0,.,.,.,.)
matrix `matrix' = `matrix' \ (r(N), `M'-r(N),.,.,.,.)
}
local rownames = "total without with "
qui replace `nouse' = 1 if _match == 0 | _match == .
sort `nouse' `tr' `cluster'
qui count if `nouse' == 0
local N = r(N)
if `N' == 0 {
di as text "{hline 17}{c BT}{hline 32}
di as error "no observations"
error 999
}
qui count in 1/`N' if `tr' == 0
local ntr = r(N)
local tre = `ntr'+1
if `ntr' == 0 {
di as text "{hline 17}{c BT}{hline 32}
di as error "no control observations"
error 999
}
if `tre' > `N' {
di as text "{hline 17}{c BT}{hline 32}
di as error "no treated observations"
error 999
}
di as text "{hline 17}{c +}{hline 32}
matrix `cell' = (`N'-`ntr',`ntr')
di as text "{ralign 16:Matched} {c |} " as result %14.9g `cell'[1,1] " " %14.9g `cell'[1,2]
matrix `matrix' = `matrix' \ (`cell'[1,1],`cell'[1,2],.,.,.,.)
local rownames = "`rownames' matched"
qui gen byte `mark' = 1 in 1/`ntr' if `cluster' == `cluster'[_n-1] | `cluster' == `cluster'[_n+1]
qui replace `mark' = `cluster' == `cluster'[`ntr'-1] in `ntr'
qui count in 1/`ntr' if `mark' == 1
local cluc = r(N)
qui drop `mark'
qui gen byte `mark' = 1 in `tre'/`N' if `cluster' == `cluster'[_n-1] | `cluster' == `cluster'[_n+1]
qui replace `mark' = `cluster' == `cluster'[`N'-1] in `N'
qui count in `tre'/`N' if `mark' == 1
local clutr = r(N)
qui drop `mark'
di as text "{ralign 16:Clustered} {c |} " as result %14.9g `clutr' " " %14.9g `cluc'
matrix `matrix' = `matrix' \ (`clutr',`cluc',.,.,.,.)
local rownames = "`rownames' clustered"
qui count in 1/`ntr' if `cluster' != `cluster'[_n-1]
local cluc = r(N)
qui count in `tre'/`N' if `cluster' != `cluster'[_n-1]
local clutr = r(N)
if `cluster'[`tre'] == `cluster'[`tre'-1] {
local clutr = `clutr'+1
}
di as text "{ralign 16: Clusters} {c |} " as result %14.9g `clutr' " " %14.9g `cluc'
matrix `matrix' = `matrix' \ (`clutr',`cluc',.,.,.,.)
local rownames = "`rownames' clusters"
if "`report'" == "" {
di as text "{hline 17}{c BT}{hline 32}"
}
if "`report'" != "" {
di as text "{hline 17}{c +}{hline 32}{c TT}{hline 36}"
if `unmatched' {
if "`unit'" != "" & `clustered' {
di as text "{dup 7: }Unmatched {c |} Treated Control {c |} CluStdErr t p>|t| SDM"
}
else {
di as text "{dup 7: }Unmatched {c |} Treated Control {c |} StdErr t p>|t| SDM"
}
di as text "{hline 17}{c +}{hline 32}{c +}{hline 36}"
foreach v of varlist `report' {
if "`unit'" == "" & `clustered' {
qui reg `v' `tr' if `base' == 1, vce(cluster `cluster')
}
else {
qui reg `v' `tr' if `base' == 1, robust
}
matrix `table' = r(table)
qui sum `v' if `tr' == 1 & `base' == 1
local m1 = r(mean)
local s1 = r(sd)
local n1 = r(N)
qui sum `v' if `tr' == 0 & `base' == 1
local m0 = r(mean)
local s0 = r(sd)
local n0 = r(N)
local nx = (`n1'+`n0'-2)
local sdm = (`m1'-`m0')/ sqrt(((`n1'-1)*`s1'^2 + (`n0'-1)*`s0'^2)/(`nx'))
if `n1'+`n0' <= 40 { // Hedges'g bias correction
local sdm = `sdm'*exp(lngamma(`nx'/2))/(sqrt(`nx'/2)*exp(lngamma((`nx'-1)/2)))
}
else {
local sdm = `sdm'*(1-3/(4*(`n1'+`n0')-9))
}
local tmean = `table'[1,1]+`table'[1,2]
local cmean = `table'[1,2]
local r = `table'[2,1]
local t = `table'[3,1]
local p = `table'[4,1]
local var = substr("`v'",1,16)
if abs(`sdm') < 10 {
local sdmf = `"" " %8.5f `sdm'"'
}
else {
local sdmf = `"" " %8.5g `sdm'"'
}
di as text "{ralign 16:`var'} {c |} " as result %14.9g round(`tmean',0.000000001) " " %14.9g round(`cmean',0.000000001) as text " {c |} " as result %9.6g `r' " " %7.2f `t' " " %5.3f `p' `sdmf'
matrix `matrix' = `matrix' \ (`tmean',`cmean',`r',`t',`p',`sdm')
local rownames = "`rownames' u_`v'"
}
di as text "{hline 17}{c +}{hline 32}{c +}{hline 36}"
}
if `clustered' {
di as text "{dup 9: }Matched {c |} Treated Control {c |} CluStdErr t p>|t| SDM"
}
else {
di as text "{dup 9: }Matched {c |} Treated Control {c |} StdErr t p>|t| SDM"
}
di as text "{hline 17}{c +}{hline 32}{c +}{hline 36}"
foreach v of varlist `report' {
if `clustered' {
qui reg `v' `tr' [pweight=_weight] in 1/`N', vce(cluster `cluster')
}
else {
qui reg `v' `tr' [pweight=_weight] in 1/`N', robust
}
matrix `table' = r(table)
qui sum `v' [aweight=_weight] in `tre'/`N'
local m1 = r(mean)
local s1 = r(sd)
local n1 = r(sum_w)
qui sum `v' [aweight=_weight] in 1/`ntr'
local m0 = r(mean)
local s0 = r(sd)
local n0 = r(sum_w)
local nx = (`n1'+`n0'-2)
local sdm = (`m1'-`m0')/ sqrt(((`n1'-1)*`s1'^2 + (`n0'-1)*`s0'^2)/(`nx'))
if `n1'+`n0' <= 40 { // Hedges'g bias correction
local sdm = `sdm'*exp(lngamma(`nx'/2))/(sqrt(`nx'/2)*exp(lngamma((`nx'-1)/2)))
}
else {
local sdm = `sdm'*(1-3/(4*(`n1'+`n0')-9))
}
local tmean = `table'[1,1]+`table'[1,2]
local cmean = `table'[1,2]
local r = `table'[2,1]
local t = `table'[3,1]
local p = `table'[4,1]
local var = substr("`v'",1,16)
if abs(`sdm') < 10 {
local sdmf = `"" " %8.5f `sdm'"'
}
else {
local sdmf = `"" " %8.5g `sdm'"'
}
di as text "{ralign 16:`var'} {c |} " as result %14.9g round(`tmean',0.000000001) " " %14.9g round(`cmean',0.000000001) as text " {c |} " as result %9.6g `r' " " %7.2f `t' " " %5.3f `p' `sdmf'
matrix `matrix' = `matrix' \ (`tmean',`cmean',`r',`t',`p',`sdm')
local rownames = "`rownames' m_`v'"
}
di as text "{hline 17}{c BT}{hline 32}{c BT}{hline 36}"
}
matrix rownames `matrix' = `rownames'
if "`report'" == "" {
matrix `matrix' = `matrix'[.,1..2]
matrix colnames `matrix' = treated control
}
else {
matrix colnames `matrix' = treated control stderr t p sdm
}
return matrix match = `matrix'
end
mata:
void _ultimatchneighbor(real scalar caliper, real scalar single, real scalar draw, real scalar between, real scalar greedy, real scalar copy)
{ real matrix D, neighbor, nb, miss
real scalar hood, top, bot, tgo, bgo, tdif, bdif, tprev, bprev, tcnt, bcnt, cnt, rows
real scalar i, j, w, reset, claim
real scalar match, obs, uselimit, usecmd
string scalar limit, cmd, explimit, expcmd, exp, str
real scalar comp
obs = st_nobs()
for (i=1; i <= obs; i++)
{ if (_st_data(i,1) != 0) // nouse
{ break
}
}
obs = i-1
if (obs <= 0)
{ st_numscalar("r(obs)", 0)
st_numscalar("r(match)", 0)
return
}
st_view(D=.,(1,obs),(2..8)) // score:1, cell:2, treated:3, match:4, distance:5, weight:6, claim:7
limit = st_local("limit")
cmd = st_local("cmd")
uselimit = 0
usecmd = 0
if (limit != "")
{ limit = "local limitok = "+limit
uselimit = 1
}
if (cmd != "")
{ cmd = "local cmdok = "+cmd
usecmd = 1
}
if (greedy)
{ copy = 0
}
comp = 0
miss = J(1,4,.)
neighbor = J(obs,3,0)
match = 0
i = 0
while (1)
{ i++
if (i > obs)
{ break
}
if (D[i,3] == 0)
{ continue
}
if (D[i,4] != .)
{ match = D[i,4]
if (D[i,6] != .)
{ continue
}
}
else
{ match++
}
if (uselimit)
{ explimit = subinstr(limit,"[t]","["+strofreal(i)+"]")
}
if (usecmd)
{ expcmd = subinstr(cmd,"[t]","["+strofreal(i)+"]")
}
top = i
bot = i
tgo = 1
bgo = 1
tcnt = 0
bcnt = 0
tprev = .
bprev = .
hood = 0
while (1)
{ while (tgo & top != .)
{ top--
if (top < 1)
{ top = .
break
}
if (D[i,2] != D[top,2])
{ top = .
break
}
if (D[top,3] == 1)
{ continue
}
if (greedy & D[top,4] == match)
{ tdif = D[top,5]
D[top,4..7] = miss
comp++
break
}
tdif = D[i,1]-D[top,1]
if (tdif > caliper)
{ top = .
break
}
comp++
if (greedy & tdif >= D[top,5])
{ continue
}
if (uselimit | usecmd)
{ str = strofreal(top)
if (uselimit)
{ exp = subinstr(explimit,"[c]","["+str+"]")
stata(exp,1,1)
if (st_local("limitok") != "1")
{ continue
}
}
if (usecmd)
{ exp = subinstr(expcmd,"[c]","["+str+"]")
stata(exp,1,1)
if (st_local("cmdok") != "1")
{ continue
}
}
}
break
}
while (bgo & bot != .)
{ bot++
if (bot > obs)
{ bot = .
break
}
if (D[i,2] != D[bot,2])
{ bot = .
break
}
if (D[bot,3] == 1)
{ continue
}
if (greedy & D[bot,4] == match)
{ bdif = D[bot,5]
D[bot,4..7] = miss
comp++
break
}
bdif = D[bot,1]-D[i,1]
if (bdif > caliper)
{ bot = .
break
}
comp++
if (greedy & bdif >= D[bot,5])
{ continue
}
if (uselimit | usecmd)
{ str = strofreal(bot)
if (uselimit)
{ exp = subinstr(explimit,"[c]","["+str+"]")
stata(exp,1,1)
if (st_local("limitok") != "1")
{ continue
}
}
if (usecmd)
{ exp = subinstr(expcmd,"[c]","["+str+"]")
stata(exp,1,1)
if (st_local("cmdok") != "1")
{ continue
}
}
}
break
}
bgo = 0
tgo = 0
if (top != . & (between | bot == . | tdif <= bdif))
{ tgo = 1
if (tprev != tdif)
{ tcnt++
if (between)
{ if (tcnt > draw)
{ top = .
}
}
else
{ if (tcnt+bcnt > draw)
{ top = .
bot = .
}
}
}
if (top != .)
{ tprev = tdif
hood++
neighbor[hood,1] = top
neighbor[hood,2] = tdif
neighbor[hood,3] = 0
}
}
if (bot != . & (between | top == . | bdif < tdif))
{ bgo = 1
if (bprev != bdif)
{ bcnt++
if (between)
{ if (bcnt > draw)
{ bot = .
}
}
else
{ if (tcnt+bcnt > draw)
{ top = .
bot = .
}
}
}
if (bot != .)
{ bprev = bdif
hood++
neighbor[hood,1] = bot
neighbor[hood,2] = bdif
neighbor[hood,3] = 1
}
}
if (bot == . & top == .)
{ break
}
}
if (hood == 0)
{ D[i,4..7] = miss
match--
continue
}
if (single)
{ nb = _ultimatchsingle(neighbor[1..hood,.],between,draw)
hood = rows(nb)
}
else
{ nb = neighbor[1..hood,.]
}
w = 1/hood
D[i,4] = match
D[i,5] = 0
D[i,6] = 1
if (copy == 1)
{ for (j = 1; j <= hood; j++)
{ pos = nb[j,1]
dif = nb[j,2]
if (D[pos,4] == .)
{ D[pos,4] = match
D[pos,5] = dif
D[pos,6] = w
}
else
{ _ultimatchcopy(pos)
_st_store(st_nobs(), 5, match)
_st_store(st_nobs(), 6, dif)
_st_store(st_nobs(), 7, w)
}
}
}
else if (copy == 2)
{ D[i,6] = w
for (j = 1; j <= hood; j++)
{ pos = nb[j,1]
dif = nb[j,2]
if (j > 1)
{ match++
_ultimatchcopy(i)
_st_store(st_nobs(), 5, match)
}
if (D[pos,4] == .)
{ D[pos,4] = match
D[pos,5] = dif
D[pos,6] = w
}
else
{ _ultimatchcopy(pos)
_st_store(st_nobs(), 5, match)
_st_store(st_nobs(), 6, dif)
_st_store(st_nobs(), 7, w)
}
}
}
else if (greedy)
{ reset = i
for (j = 1; j <= hood; j++)
{ pos = nb[j,1]
dif = nb[j,2]
claim = D[pos,7]
if (claim != .)
{ if (claim < reset)
{ reset = claim
}
D[claim,6] = .
}
D[pos,4] = match
D[pos,5] = dif
D[pos,6] = w
D[pos,7] = i
}
if (reset < i)
{ i = reset-1