forked from kkranker/psweight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
psweight_example.log
5322 lines (4969 loc) · 264 KB
/
psweight_example.log
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
----------------------------------------------------------------------------------------------------------------------------------------------------------------
name: psweight_example
log: C:\Users\kkranker\Documents\Stata\psweight\code-psweight\psweight_example.log
log type: text
opened on: 5 Nov 2019, 22:15:35
.
. // ***************************************************************************
. *! psweight_example.do
. *! IPW- and CBPS-type propensity score reweighting, with extentions
. *! Examples
. //
. *! By Keith Kranker
. //
. // Copyright (C) Mathematica Policy Research, Inc.
. // This code cannot be copied, distributed or used without the express written
. // permission of Mathematica Policy Research, Inc.
. // ***************************************************************************
.
. about
Stata/MP 16.0 for Windows (64-bit x86-64)
Revision 16 Oct 2019
Copyright 1985-2019 StataCorp LLC
Total physical memory: 32.00 GB
Available physical memory: 24.95 GB
Single-user 64-core Stata network license expires 15 Nov 2019:
Serial number: 501609253383
Licensed to: SCCMAdminRoot
Mathematica
. set type double
. di as txt "Current user: `c(username)'" _n "Environment: `c(os)' `c(machine_type)' `: environment computername'" _n "Stata: `c(stata_version)'" cond(c(stata_v
> ersion)==c(version),""," (set to version `c(version)')") _n "Date: " c(current_date) " " c(current_time)
Current user: KKranker
Environment: Windows PC (64-bit x86-64) KKRANKER
Stata: 16
Date: 5 Nov 2019 22:15:35
.
. which psweight
.\psweight.ado
*! psweight.ado
*! IPW- and CBPS-type propensity score reweighting, with various extentions
*! Stata command to estimate models
*! By Keith Kranker
. mata: mata describe using lpsweight
(library contains 58 members)
# bytes type name and extent
-------------------------------------------------------------------------------
4,552 auto classdef scalar psweight()
852 auto real matrix ::Ct()
1,408 auto void ::balanceresults()
2,276 auto real matrix ::balancetable()
628 auto void ::calcN()
816 auto void ::calccovariances()
320 auto void ::calcmeans()
576 auto void ::calcvariances()
356 auto real rowvector ::cbps()
2,136 auto void ::cbps_port_r()
768 auto void ::cbps_port_stata()
676 auto real matrix ::cbps_port_stata_gradient()
752 auto real colvector ::cbps_port_stata_moments()
1,668 auto real matrix ::cbps_port_stata_wgt_matrix()
3,312 auto void ::cbpseval()
356 auto real rowvector ::cbpsoid()
592 auto void ::clone()
568 auto real rowvector ::diagvariance()
360 auto real rowvector ::diff()
1,804 auto void ::fill_vars()
1,516 auto real matrix ::get_N()
64 auto real colvector ::get_pscore()
64 auto real colvector ::get_weight()
64 auto real colvector ::get_weight_mtch()
736 auto real rowvector ::ipw()
1,900 auto real rowvector ::logitbeta()
524 auto real colvector ::logitpredict()
1,296 auto real colvector ::logitweights()
360 auto real scalar ::max_asd()
360 auto real scalar ::mean_asd()
352 auto real scalar ::mean_sd()
436 auto real scalar ::mean_sd_sq()
96 auto void ::new()
688 auto real rowvector ::olsbeta()
468 auto real colvector ::olspredict()
276 auto real rowvector ::pomean()
756 auto void ::postbeta()
2,000 auto real rowvector ::progdiff()
712 auto void ::reweight()
232 auto real rowvector ::sd_sq()
1,448 auto void ::set()
388 auto void ::set_depvars()
6,560 auto real rowvector ::solve()
1,432 auto void ::st_set()
400 auto void ::st_set_depvars()
1,012 auto real rowvector ::stddiff()
2,068 auto real rowvector ::stdprogdiff()
512 auto real colvector ::trim()
368 auto real rowvector ::varratio()
288 auto real scalar ::wgt_cv()
356 auto real scalar ::wgt_kurtosis()
580 auto real scalar ::wgt_max()
784 auto real rowvector ::wgt_moments()
228 auto real scalar ::wgt_sd()
372 auto real scalar ::wgt_skewness()
412 auto void psweight_cbps_eval()
2,276 auto void psweight_init_mlopts()
504 auto void psweight_logit_eval()
-------------------------------------------------------------------------------
.
. if 1 {
.
. ************************************************************************************
. * Simple examples in the help file (psweight.sthlp)
. ************************************************************************************
.
. // Setup
. webuse cattaneo2
(Excerpt from Cattaneo (2010) Journal of Econometrics 155: 138-154)
.
. // Balance before reweighting
. psweight balanceonly mbsmoke mmarried mage fbaby medu, ntab
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_pool var_ratio
-------------+-----------------------------------------------------------------
mmarried | .4733796 .7514558 -.2780762 -.6065725 .4584385 1.335944
mage | 25.16667 26.81048 -1.643815 -.2925445 5.619026 .8818025
fbaby | .3715278 .4531498 -.081622 -.1644978 .4961893 .9430944
medu | 11.63889 12.92986 -1.290968 -.5121546 2.520661 .7315846
-------------------------------------------------------------------------------
Mean standardized diff. = -0.39394
Mean absolute standardized diff. = 0.39394
Maximum absolute standardized diff. = 0.60657
New variables created: _weight _weight_mtch _pscore _treated
-------------------------------------------------------
| Treatment Control Total
----------------------+--------------------------------
Number of rows | 864 3778 4642
Sum of sample weights | 864 3778 4642
Sum of weights | 864 3778 4642
-------------------------------------------------------
.
. // Estimate the average treatment effect of smoking on birthweight, using a logit model to predict treatment status
. psweight ipw mbsmoke mmarried mage fbaby medu
initial: f(p) = -3217.5892
alternative: f(p) = -2632.6654
rescale: f(p) = -2317.1998
Iteration 0: f(p) = -2317.1998
Iteration 1: f(p) = -2156.2731
Iteration 2: f(p) = -2058.0577
Iteration 3: f(p) = -2057.5952
Iteration 4: f(p) = -2057.5951
Propensity score model coefficients Number of obs = 4,642
Propensity score reweigting
Loss = IPW
------------------------------------------------------------------------------
mbsmoke | Coef.
-------------+----------------------------------------------------------------
mmarried | -1.036252
mage | -.0036626
fbaby | -.4765406
medu | -.127238
_cons | 1.024681
------------------------------------------------------------------------------
New variables created: _weight _weight_mtch _pscore _treated
. psweight call balanceresults()
Balance:
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_pool var_ratio
-------------+-----------------------------------------------------------------
mmarried | .6865574 .6976686 -.0111112 -.0241441 .4602022 1.021152
mage | 26.03507 26.47091 -.4358363 -.0769175 5.666282 .8180025
fbaby | .4485481 .4382549 .0102932 .0207332 .496461 1.005631
medu | 12.28844 12.62439 -.3359562 -.125102 2.685459 .5000611
-------------------------------------------------------------------------------
Mean standardized diff. = -0.05136
Mean absolute standardized diff. = 0.06172
Maximum absolute standardized diff. = 0.12510
C.V. of matching weights: = 0.28260
S.D. of matching weights: = 0.28260
Skewness of matching weights: = 0.03601
Kurtosis of matching weights: = 0.00341
Maximum matching weight: = 3.29874
.
. psweight call mystddiff = stddiff()
. mata: mystddiff
1 2 3 4
+-------------------------------------------------------------+
1 | -.0241441463 -.076917512 .020733199 -.1251019701 |
+-------------------------------------------------------------+
.
. // Estimate the average treatment effect on the treated with CBPS
. psweight cbps mbsmoke mmarried mage fbaby medu, atet
Step 1 (initial values from logit model):
initial: f(p) = -3217.5892
alternative: f(p) = -2632.6654
rescale: f(p) = -2317.1998
Iteration 0: f(p) = -2317.1998
Iteration 1: f(p) = -2156.2731
Iteration 2: f(p) = -2058.0577
Iteration 3: f(p) = -2057.5952
Iteration 4: f(p) = -2057.5951
Step 2 (CBPS) :
Iteration 0: f(p) = 5.800e-11
Iteration 1: f(p) = 5.705e-11
Iteration 2: f(p) = 4.419e-11
Iteration 3: f(p) = 3.123e-11
Iteration 4: f(p) = 2.530e-11
Iteration 5: f(p) = 1.774e-11
Iteration 6: f(p) = 1.666e-11
Iteration 7: f(p) = 8.483e-12
Iteration 8: f(p) = 2.502e-12
Iteration 9: f(p) = 2.123e-12
Iteration 10: f(p) = 8.205e-13
Iteration 11: f(p) = 3.774e-14
Iteration 12: f(p) = 1.171e-16
Iteration 13: f(p) = 1.190e-18
Iteration 14: f(p) = 7.414e-21
Propensity score model coefficients Number of obs = 4,642
Propensity score reweigting
Loss = CBPS (just identified)
------------------------------------------------------------------------------
mbsmoke | Coef.
-------------+----------------------------------------------------------------
mmarried | -1.079169
mage | -.0027934
fbaby | -.4962329
medu | -.0967742
_cons | .6621757
------------------------------------------------------------------------------
New variables created: _weight _weight_mtch _pscore _treated
. psweight call balanceresults()
Balance:
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_pool var_ratio
-------------+-----------------------------------------------------------------
mmarried | .4733796 .4733793 3.25e-07 6.51e-07 .4993446 1.000894
mage | 25.16667 25.16667 -2.25e-06 -3.82e-07 5.888802 .7765934
fbaby | .3715278 .3715275 2.86e-07 5.93e-07 .4832651 1.000894
medu | 11.63889 11.63889 1.09e-06 3.57e-07 3.047441 .4545646
-------------------------------------------------------------------------------
Mean standardized diff. = 0.00000
Mean absolute standardized diff. = 0.00000
Maximum absolute standardized diff. = 0.00000
C.V. of matching weights: = 0.81153
S.D. of matching weights: = 0.81153
Skewness of matching weights: = 0.04626
Kurtosis of matching weights: = 0.00464
Maximum matching weight: = 8.08544
.
. // Estimate the average treatment effect on the treated with Penalized CBPS
. psweight pcbps mbsmoke mmarried mage fbaby medu, atet cvtarget(1 .5 6)
Step 1 (initial values from logit model):
initial: f(p) = -3217.5892
alternative: f(p) = -2632.6654
rescale: f(p) = -2317.1998
Iteration 0: f(p) = -2317.1998
Iteration 1: f(p) = -2156.2731
Iteration 2: f(p) = -2058.0577
Iteration 3: f(p) = -2057.5952
Iteration 4: f(p) = -2057.5951
Step 2 (CBPS) :
Iteration 0: f(p) = .00683679
Iteration 1: f(p) = 4.220e-06
Iteration 2: f(p) = 1.820e-07
Iteration 3: f(p) = 1.820e-07
Iteration 4: f(p) = 1.038e-07
Iteration 5: f(p) = 1.760e-08
Iteration 6: f(p) = 1.257e-08
Iteration 7: f(p) = 1.237e-08
Iteration 8: f(p) = 1.176e-08
Iteration 9: f(p) = 1.125e-08
Iteration 10: f(p) = 1.122e-08
Iteration 11: f(p) = 1.122e-08
Iteration 12: f(p) = 1.122e-08
Iteration 13: f(p) = 1.080e-08
Iteration 14: f(p) = 5.482e-09
Iteration 15: f(p) = 7.642e-10
Iteration 16: f(p) = 6.450e-10
Iteration 17: f(p) = 6.146e-10
Iteration 18: f(p) = 6.102e-10
Iteration 19: f(p) = 6.024e-10
Iteration 20: f(p) = 6.020e-10
Iteration 21: f(p) = 6.017e-10
Iteration 22: f(p) = 6.010e-10
Iteration 23: f(p) = 5.933e-10
Iteration 24: f(p) = 5.892e-10
Iteration 25: f(p) = 5.886e-10
Iteration 26: f(p) = 5.881e-10
Iteration 27: f(p) = 5.881e-10
Iteration 28: f(p) = 5.881e-10
Iteration 29: f(p) = 5.881e-10
Iteration 30: f(p) = 5.881e-10
Iteration 31: f(p) = 5.881e-10
Iteration 32: f(p) = 5.881e-10
Iteration 33: f(p) = 5.608e-10
Iteration 34: f(p) = 4.903e-10
Iteration 35: f(p) = 4.504e-10
Iteration 36: f(p) = 4.424e-10
Iteration 37: f(p) = 4.358e-10
Iteration 38: f(p) = 4.350e-10
Iteration 39: f(p) = 4.347e-10
Iteration 40: f(p) = 4.346e-10
Iteration 41: f(p) = 4.346e-10
Iteration 42: f(p) = 4.346e-10
Iteration 43: f(p) = 4.346e-10
Iteration 44: f(p) = 4.346e-10
Iteration 45: f(p) = 4.346e-10
Iteration 46: f(p) = 4.345e-10
Iteration 47: f(p) = 4.221e-10
Iteration 48: f(p) = 3.377e-10
Iteration 49: f(p) = 2.689e-10
Iteration 50: f(p) = 2.587e-10
Iteration 51: f(p) = 2.503e-10
Iteration 52: f(p) = 2.473e-10
Iteration 53: f(p) = 2.459e-10
Iteration 54: f(p) = 2.459e-10
Iteration 55: f(p) = 2.459e-10
Iteration 56: f(p) = 2.459e-10
Propensity score model coefficients Number of obs = 4,642
Propensity score reweigting
Loss = CBPS (just identified) + 1*abs(wgt_cv()-.5)^6)
------------------------------------------------------------------------------
mbsmoke | Coef.
-------------+----------------------------------------------------------------
mmarried | -.7698567
mage | -.0071298
fbaby | -.3632981
medu | -.0538343
_cons | -.0291464
------------------------------------------------------------------------------
New variables created: _weight _weight_mtch _pscore _treated
. psweight call balanceresults()
Balance:
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_pool var_ratio
-------------+-----------------------------------------------------------------
mmarried | .4733796 .5602882 -.0869086 -.1744789 .498104 1.012781
mage | 25.16667 25.57391 -.4072411 -.0700995 5.809475 .8025661
fbaby | .3715278 .396575 -.0250472 -.0513021 .48823 .9766
medu | 11.63889 12.15997 -.5210767 -.1898107 2.745244 .5775639
-------------------------------------------------------------------------------
Mean standardized diff. = -0.12142
Mean absolute standardized diff. = 0.12142
Maximum absolute standardized diff. = 0.18981
C.V. of matching weights: = 0.51226
S.D. of matching weights: = 0.51226
Skewness of matching weights: = 0.02582
Kurtosis of matching weights: = 0.00151
Maximum matching weight: = 4.07165
.
.
. ************************************************************************************
. * Examples in the help file (psweight_class.sthlp)
. ************************************************************************************
.
.
. // Setup
. webuse cattaneo2, clear
(Excerpt from Cattaneo (2010) Journal of Econometrics 155: 138-154)
. gen byte touse=1
.
. mata:
------------------------------------------------- mata (type end to exit) --------------------------------------------------------------------------------------
:
: // Create an instance of the class, tell it where the data are
: P = psweight()
: P.st_set("mbsmoke", "mmarried mage fbaby medu", "touse")
:
: // Balance before reweighting
: P.balancetable(2)
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_pool var_ratio
-------------+-----------------------------------------------------------------
mmarried | .4733796 .7514558 -.2780762 -.6065725 .4584385 1.335944
mage | 25.16667 26.81048 -1.643815 -.2925445 5.619026 .8818025
fbaby | .3715278 .4531498 -.081622 -.1644978 .4961893 .9430944
medu | 11.63889 12.92986 -1.290968 -.5121546 2.520661 .7315846
-------------------------------------------------------------------------------
Mean standardized diff. = -0.39394
Mean absolute standardized diff. = 0.39394
Maximum absolute standardized diff. = 0.60657
1 2 3 4 5 6
+-------------------------------------------------------------------------------------------+
1 | .4733796296 .7514557967 -.2780761671 -.6065725095 .4584384599 1.335943589 |
2 | 25.16666667 26.81048174 -1.64381507 -.2925444767 5.619026168 .8818025263 |
3 | .3715277778 .4531498147 -.0816220369 -.1644977841 .4961892793 .9430944404 |
4 | 11.63888889 12.92985707 -1.290968178 -.5121545711 2.520661244 .7315846294 |
+-------------------------------------------------------------------------------------------+
:
: // Estimate the average treatment effect of smoking on birthweight, using a logit model to predict treatment status
: P.ipw()
initial: f(p) = -3217.5892
alternative: f(p) = -2632.6654
rescale: f(p) = -2317.1998
Iteration 0: f(p) = -2317.1998
Iteration 1: f(p) = -2156.2731
Iteration 2: f(p) = -2058.0577
Iteration 3: f(p) = -2057.5952
Iteration 4: f(p) = -2057.5951
1 2 3 4 5
+----------------------------------------------------------------------------+
1 | -1.036251862 -.003662621 -.476540647 -.1272379645 1.024680503 |
+----------------------------------------------------------------------------+
: P.balanceresults("ate", 1)
Balance:
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_T var_ratio
-------------+-----------------------------------------------------------------
mmarried | .6865574 .6976686 -.0111112 -.0239382 .4641613 1.021152
mage | 26.03507 26.47091 -.4358363 -.0836215 5.21201 .8180025
fbaby | .4485481 .4382549 .0102932 .0206843 .4976337 1.005631
medu | 12.28844 12.62439 -.3359562 -.1686665 1.991838 .5000611
-------------------------------------------------------------------------------
Mean standardized diff. = -0.06389
Mean absolute standardized diff. = 0.07423
Maximum absolute standardized diff. = 0.16867
C.V. of matching weights: = 0.28260
S.D. of matching weights: = 0.28260
Skewness of matching weights: = 0.03601
Kurtosis of matching weights: = 0.00341
Maximum matching weight: = 3.29874
:
: // Estimate the average treatment effect on the treated with CBPS
: P.cbps("atet")
Step 1 (initial values from logit model):
initial: f(p) = -3217.5892
alternative: f(p) = -2632.6654
rescale: f(p) = -2317.1998
Iteration 0: f(p) = -2317.1998
Iteration 1: f(p) = -2156.2731
Iteration 2: f(p) = -2058.0577
Iteration 3: f(p) = -2057.5952
Iteration 4: f(p) = -2057.5951
Step 2 (CBPS) :
Iteration 0: f(p) = 5.800e-11
Iteration 1: f(p) = 5.705e-11
Iteration 2: f(p) = 4.419e-11
Iteration 3: f(p) = 3.123e-11
Iteration 4: f(p) = 2.530e-11
Iteration 5: f(p) = 1.774e-11
Iteration 6: f(p) = 1.666e-11
Iteration 7: f(p) = 8.483e-12
Iteration 8: f(p) = 2.502e-12
Iteration 9: f(p) = 2.123e-12
Iteration 10: f(p) = 8.205e-13
Iteration 11: f(p) = 3.774e-14
Iteration 12: f(p) = 1.171e-16
Iteration 13: f(p) = 1.190e-18
Iteration 14: f(p) = 7.414e-21
1 2 3 4 5
+----------------------------------------------------------------------------+
1 | -1.079169095 -.0027934281 -.4962329208 -.0967741508 .6621757391 |
+----------------------------------------------------------------------------+
: P.balanceresults("atet", 1)
Balance:
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_T var_ratio
-------------+-----------------------------------------------------------------
mmarried | .4733796 .4733793 3.25e-07 6.51e-07 .49958 1.000894
mage | 25.16667 25.16667 -2.25e-06 -4.24e-07 5.301348 .7765934
fbaby | .3715278 .3715275 2.86e-07 5.93e-07 .483493 1.000894
medu | 11.63889 11.63889 1.09e-06 5.02e-07 2.167743 .4545646
-------------------------------------------------------------------------------
Mean standardized diff. = 0.00000
Mean absolute standardized diff. = 0.00000
Maximum absolute standardized diff. = 0.00000
C.V. of matching weights: = 0.81153
S.D. of matching weights: = 0.81153
Skewness of matching weights: = 0.04626
Kurtosis of matching weights: = 0.00464
Maximum matching weight: = 8.08544
:
: // Estimate the average treatment effect on the treated with Penalized CBPS
: P.solve("atet", "cbps", 2, (1, .5, 6))
Step 1 (initial values from logit model):
initial: f(p) = -3217.5892
alternative: f(p) = -2632.6654
rescale: f(p) = -2317.1998
Iteration 0: f(p) = -2317.1998
Iteration 1: f(p) = -2156.2731
Iteration 2: f(p) = -2058.0577
Iteration 3: f(p) = -2057.5952
Iteration 4: f(p) = -2057.5951
Step 2 (CBPS) :
Iteration 0: f(p) = .00683679
Iteration 1: f(p) = 4.220e-06
Iteration 2: f(p) = 1.820e-07
Iteration 3: f(p) = 1.820e-07
Iteration 4: f(p) = 1.038e-07
Iteration 5: f(p) = 1.760e-08
Iteration 6: f(p) = 1.257e-08
Iteration 7: f(p) = 1.237e-08
Iteration 8: f(p) = 1.176e-08
Iteration 9: f(p) = 1.125e-08
Iteration 10: f(p) = 1.122e-08
Iteration 11: f(p) = 1.122e-08
Iteration 12: f(p) = 1.122e-08
Iteration 13: f(p) = 1.080e-08
Iteration 14: f(p) = 5.482e-09
Iteration 15: f(p) = 7.642e-10
Iteration 16: f(p) = 6.450e-10
Iteration 17: f(p) = 6.146e-10
Iteration 18: f(p) = 6.102e-10
Iteration 19: f(p) = 6.024e-10
Iteration 20: f(p) = 6.020e-10
Iteration 21: f(p) = 6.017e-10
Iteration 22: f(p) = 6.010e-10
Iteration 23: f(p) = 5.933e-10
Iteration 24: f(p) = 5.892e-10
Iteration 25: f(p) = 5.886e-10
Iteration 26: f(p) = 5.881e-10
Iteration 27: f(p) = 5.881e-10
Iteration 28: f(p) = 5.881e-10
Iteration 29: f(p) = 5.881e-10
Iteration 30: f(p) = 5.881e-10
Iteration 31: f(p) = 5.881e-10
Iteration 32: f(p) = 5.881e-10
Iteration 33: f(p) = 5.608e-10
Iteration 34: f(p) = 4.903e-10
Iteration 35: f(p) = 4.504e-10
Iteration 36: f(p) = 4.424e-10
Iteration 37: f(p) = 4.358e-10
Iteration 38: f(p) = 4.350e-10
Iteration 39: f(p) = 4.347e-10
Iteration 40: f(p) = 4.346e-10
Iteration 41: f(p) = 4.346e-10
Iteration 42: f(p) = 4.346e-10
Iteration 43: f(p) = 4.346e-10
Iteration 44: f(p) = 4.346e-10
Iteration 45: f(p) = 4.346e-10
Iteration 46: f(p) = 4.345e-10
Iteration 47: f(p) = 4.221e-10
Iteration 48: f(p) = 3.377e-10
Iteration 49: f(p) = 2.689e-10
Iteration 50: f(p) = 2.587e-10
Iteration 51: f(p) = 2.503e-10
Iteration 52: f(p) = 2.473e-10
Iteration 53: f(p) = 2.459e-10
Iteration 54: f(p) = 2.459e-10
Iteration 55: f(p) = 2.459e-10
Iteration 56: f(p) = 2.459e-10
1 2 3 4 5
+----------------------------------------------------------------------------+
1 | -.7698566831 -.0071297919 -.3632980692 -.0538343295 -.029146411 |
+----------------------------------------------------------------------------+
: P.balanceresults("atet", 1)
Balance:
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_T var_ratio
-------------+-----------------------------------------------------------------
mmarried | .4733796 .5602882 -.0869086 -.1739633 .49958 1.012781
mage | 25.16667 25.57391 -.4072411 -.0768184 5.301348 .8025661
fbaby | .3715278 .396575 -.0250472 -.0518048 .483493 .9766
medu | 11.63889 12.15997 -.5210767 -.2403775 2.167743 .5775639
-------------------------------------------------------------------------------
Mean standardized diff. = -0.13574
Mean absolute standardized diff. = 0.13574
Maximum absolute standardized diff. = 0.24038
C.V. of matching weights: = 0.51226
S.D. of matching weights: = 0.51226
Skewness of matching weights: = 0.02582
Kurtosis of matching weights: = 0.00151
Maximum matching weight: = 4.07165
:
: end // end of Mata
----------------------------------------------------------------------------------------------------------------------------------------------------------------
.
. } // end of simple examples
.
.
. ************************************************************************************
. * Describe/summarize the example datasets
. ************************************************************************************
.
. *** Input data file (simple_cattaneo_data) comes from the program named Make_example_datasets.do (in C:\Users\kkranker\Documents\Stata\Multiple-Equation-Model
> s)
.
. use "C:\Users\kkranker\Documents\Stata\Multiple-Equation-Models\simple_cattaneo_data.dta", clear
(Cattaneo (2010) study on the effect of maternal smoking on birth weight)
. desc, short
Contains data from C:\Users\kkranker\Documents\Stata\Multiple-Equation-Models\simple_cattaneo_data.dta
obs: 4,642 Cattaneo (2010) study on the effect of maternal smoking on birth weight
vars: 11 8 Apr 2015 05:07
Sorted by:
. notes _dta
_dta:
1. Cattaneo (2010) study on the effect of maternal smoking on birth weight
2. Dataset created by Keith Kranker (program name: make_example_datasets.do)
3. Source: Cattaneo, M. D. 2010. "Efficient Semiparametric Estimation of Multi-Valued Treatment Effects under Ignorability." Journal of Econometrics, 155(2):
138154, doi:10.1016/j.jeconom.2009.09.023.
4. Source: Almond, D., Chay, K.Y., Lee, D.S., 2005. "The costs of low birth weight." Quarterly Journal of Economics, 120: 1031-1083.
5. Original dataset downloaded 4/3/2015 via Stata command ". webuse cattaneo2"
6. The variable names in the original datasets are replaced with "generic" names: (1) The outcome variables are named y1 y2 y3... (2) The treatment variable
is named treat (binary) or treat_cat (categorical). (3) The control variables are named x1 x2 x3...
. summ, sep(0)
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
treat | 4,642 .1861267 .3892508 0 1
treat_cat | 4,642 .3996122 .8988632 0 3
x1 | 4,642 .6996984 .4584385 0 1
x2 | 4,642 .0340371 .1813439 0 1
x3 | 4,642 .0534252 .2249042 0 1
x4 | 4,642 .4379578 .4961893 0 1
x5 | 4,642 26.50452 5.619026 13 45
x6 | 4,642 12.68957 2.520661 0 17
x7 | 4,642 6.540069 3.359167 1 12
y1 | 4,642 3361.68 578.8196 340 5500
y1_binary | 4,642 .9396812 .2381022 0 1
. tab treat treat_cat, mi
| treat_cat
treat | Untreated 1st Treat 2nd Treat 3rd Treat | Total
-----------+--------------------------------------------+----------
Untreated | 3,778 0 0 0 | 3,778
Treated | 0 200 337 327 | 864
-----------+--------------------------------------------+----------
Total | 3,778 200 337 327 | 4,642
. corr treat y1 y1_binary
(obs=4,642)
| treat y1 y1_bin~y
-------------+---------------------------
treat | 1.0000
y1 | -0.1851 1.0000
y1_binary | -0.0997 0.5924 1.0000
.
. local if if _n<=500
. set seed 1
. gen wgt = max(.1, rnormal(2,.4))
. gen fwgt = round(rnormal(2,.4))
. // forvalues i = 20/200 {
. forvalues i = 90/95 {
2. gen x`i' = rnormal()
3. }
.
. local depvarlist = "y1 y1_binary"
. local treatvar = "treat"
. local varlist = "x1 i.x2 i.x3 x4 x5 x6 x7 x9*"
. local wgtvar = "wgt"
. local tousevar = "touse"
. local estimate = "atet"
.
.
. // some automatic parsing based on options above, since Mata doesn't have this stuff
. if "`wgtvar'"!="" local wgtexp "[iw=`wgtvar']"
. mark `tousevar' `if' `in' `wgtexp'
. markout `tousevar' `depvarlist' `treatvar' `varlist'
. local varlist_orig : copy local varlist
.
. _rmcoll `treatvar' `varlist' if `tousevar' `wgtexp', expand logit touse(`tousevar')
. local varlist `r(varlist)'
. gettoken trash varlist : varlist
.
.
. // *******************************
. // * RUN MODELS DIRECTLY IN MATA *
. // *******************************
.
. if 1 {
.
. mata:
------------------------------------------------- mata (type end to exit) --------------------------------------------------------------------------------------
:
: depvarlist = st_local("depvarlist")
: treatvar = st_local("treatvar")
: wgtvar = st_local("wgtvar")
: varlist = st_local("varlist")
: tousevar = st_local("tousevar")
: estimate = st_local("estimateerror")
: // * UNWEIGHTED DATA EXAMPLES *
:
: D = psweight()
: D.st_set(treatvar, varlist, tousevar)
: if (depvarlist!="") D.st_set_depvars(depvarlist, tousevar)
>
> // Misc balance measures
> D.diff()
1 2 3 4 5 6 7 8 9 10
+-------------------------------------------------------------------------------------------------------------------------------------------------------
1 | -.2826086957 0 -.0160912191 0 -.0223785166 -.0603154305 -1.101236147 -1.289748508 .1626172208 .1457104569
+-------------------------------------------------------------------------------------------------------------------------------------------------------
11 12 13 14 15
----------------------------------------------------------------------------+
1 -.0812723301 .0681588142 .0461833248 -.1459731942 -.0148968728 |
----------------------------------------------------------------------------+
: D.stddiff()
1 2 3 4 5 6 7 8 9 10
+-------------------------------------------------------------------------------------------------------------------------------------------------------
1 | -.6149214017 . -.1050324967 . -.1140856324 -.1205719184 -.1896218542 -.5792408015 .0485796206 .1481119269
+-------------------------------------------------------------------------------------------------------------------------------------------------------
11 12 13 14 15
----------------------------------------------------------------------------+
1 -.0817632833 .0671946445 .0471721373 -.148808753 -.0150884239 |
----------------------------------------------------------------------------+
: D.stddiff(1)
1 2 3 4 5 6 7 8 9 10
+-------------------------------------------------------------------------------------------------------------------------------------------------------
1 | -.563336463 . -.1543415516 . -.1526193687 -.1210072453 -.202287272 -.8035533326 .0457213254 .1527707004
+-------------------------------------------------------------------------------------------------------------------------------------------------------
11 12 13 14 15
----------------------------------------------------------------------------+
1 -.08618905 .0648405717 .0493954389 -.1578414414 -.016298459 |
----------------------------------------------------------------------------+
: D.mean_asd(1)
.1977078631
: D.stddiff(1)
1 2 3 4 5 6 7 8 9 10
+-------------------------------------------------------------------------------------------------------------------------------------------------------
1 | -.563336463 . -.1543415516 . -.1526193687 -.1210072453 -.202287272 -.8035533326 .0457213254 .1527707004
+-------------------------------------------------------------------------------------------------------------------------------------------------------
11 12 13 14 15
----------------------------------------------------------------------------+
1 -.08618905 .0648405717 .0493954389 -.1578414414 -.016298459 |
----------------------------------------------------------------------------+
: D.stddiff(0)
1 2 3 4 5 6 7 8 9 10
+-------------------------------------------------------------------------------------------------------------------------------------------------------
1 | -.6518565108 . -.0992258067 . -.1088403741 -.120488729 -.1874830308 -.5658268397 .0492441608 .1472428581
+-------------------------------------------------------------------------------------------------------------------------------------------------------
11 12 13 14 15
----------------------------------------------------------------------------+
1 -.0808153611 .0677010963 .0466680215 -.1471098503 -.0148355423 |
----------------------------------------------------------------------------+
: D.varratio()
1 2 3 4 5 6 7 8 9 10
+---------------------------------------------------------------------------------------------------------------------------------------------
1 | 1.338962118 . .4133172708 . .5085817193 .9914483579 .8589874442 .4958355659 1.160037045 .9289414423
+---------------------------------------------------------------------------------------------------------------------------------------------
11 12 13 14 15
-----------------------------------------------------------------------+
1 .8791918375 1.090178798 .8926168505 .8686432131 .8285405428 |
-----------------------------------------------------------------------+
: D.progdiff(1)
------------------------------------------------------------------------
| mean_yhat_T mean_yhat_C diff std_diff mean_y_C
-------------+----------------------------------------------------------
y1 | 3444.538 3460.475 -15.93746 -.1234546 3460.475
y1_binary | .9482656 .9607843 -.0125187 -.3307945 .9607843
------------------------------------------------------------------------
Note: The std_diff column does not account for the standard error of the linear predictions.
1 2
+-------------------------------+
1 | -15.93745522 -.0125187159 |
+-------------------------------+
: D.get_N(1)
-------------------------------------------------------
| Treatment Control Total
----------------------+--------------------------------
Number of rows | 92 408 500
Sum of sample weights | 92 408 500
Sum of weights | 92 408 500
-------------------------------------------------------
1 2 3
+-------------------+
1 | 92 408 500 |
2 | 92 408 500 |
3 | 92 408 500 |
+-------------------+
:
: "Balance table before matching"
Balance table before matching
: table = D.balancetable(1)
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_T var_ratio
-------------+-----------------------------------------------------------------
x1 | .4673913 .75 -.2826087 -.5633365 .5016695 1.338962
|
x2 |
0 | (empty)
1 | .0108696 .0269608 -.0160912 -.1543416 .1042572 .4133173
|
x3 |
0 | (empty)
1 | .0217391 .0441176 -.0223785 -.1526194 .1466296 .5085817
|
x4 | .4347826 .495098 -.0603154 -.1210072 .4984448 .9914484
x5 | 25.46739 26.56863 -1.101236 -.2022873 5.443922 .8589874
x6 | 11.86957 13.15931 -1.289749 -.8035533 1.605057 .4958356
x7 | 6.858696 6.696078 .1626172 .0457213 3.556704 1.160037
x90 | .0972024 -.048508 .1457105 .1527707 .9537854 .9289414
x91 | -.0890157 -.0077434 -.0812723 -.0861891 .9429542 .8791918
x92 | .0453787 -.0227801 .0681588 .0648406 1.051175 1.090179
x93 | .0865687 .0403853 .0461833 .0493954 .9349714 .8926169
x94 | -.1299173 .0160559 -.1459732 -.1578414 .9248091 .8686432
x95 | -.0807251 -.0658282 -.0148969 -.0162985 .914005 .8285405
-------------------------------------------------------------------------------
Mean standardized diff. = -0.14960
Mean absolute standardized diff. = 0.19771
Maximum absolute standardized diff. = 0.80355
:
: // Replicate CBPS
:
: "--- ATE (not overidentified) ---"; ""; ""
--- ATE (not overidentified) ---
: stata(`"cbps `treatvar' `varlist' if `tousevar' , ate logit optimization_technique("nr") evaluator_type("gf1")"')
Iteration 0: f(p) = .00647386
Iteration 1: f(p) = .00451406
Iteration 2: f(p) = .0005336
Iteration 3: f(p) = .00001373
Iteration 4: f(p) = 2.637e-08
Iteration 5: f(p) = 6.777e-13
Iteration 6: f(p) = 1.066e-21
------------------------------------------------------------------------------
treat | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
treat |
x1 | -1.441064 .5046768 -2.86 0.004 -2.430213 -.4519161
1.x2 | -2.548784 3.501511 -0.73 0.467 -9.41162 4.314051
1.x3 | -1.02318 2.005094 -0.51 0.610 -4.953091 2.906732
x4 | -.2320966 .5678014 -0.41 0.683 -1.344967 .8807737
x5 | .0736983 .042799 1.72 0.085 -.0101862 .1575829
x6 | -.4863391 .1923869 -2.53 0.011 -.8634106 -.1092676
x7 | .0655415 .0720901 0.91 0.363 -.0757525 .2068354
x90 | .0303861 .203675 0.15 0.881 -.3688096 .4295819
x91 | -.0236437 .2499136 -0.09 0.925 -.5134655 .466178
x92 | .2014652 .2092517 0.96 0.336 -.2086606 .6115911
x93 | -.0067202 .2511949 -0.03 0.979 -.4990531 .4856126
x94 | -.3662317 .2431817 -1.51 0.132 -.842859 .1103956
x95 | -.0080118 .2531324 -0.03 0.975 -.5041423 .4881187
_cons | 3.127763 1.826429 1.71 0.087 -.4519713 6.707497
------------------------------------------------------------------------------
: stata(`"cbps_imbalance"')
Total average covariate imbalance: 1.5e-10
Average covariate imbalance on the treated: 1.26
: D.cbps("ate", 2)
Step 1 (initial values from logit model):
initial: f(p) = -346.57359
alternative: f(p) = -283.03849
rescale: f(p) = -247.46401
Iteration 0: f(p) = -247.46401
Iteration 1: f(p) = -223.85141
Iteration 2: f(p) = -211.21543
Iteration 3: f(p) = -211.17754
Iteration 4: f(p) = -211.17753
Step 2 (CBPS) :
Iteration 0: f(p) = 2.590e-08
Iteration 1: f(p) = 2.548e-08 (backed up)
Iteration 2: f(p) = 1.980e-08
Iteration 3: f(p) = 1.652e-08
Iteration 4: f(p) = 1.627e-08
Iteration 5: f(p) = 1.540e-08
Iteration 6: f(p) = 1.451e-08
Iteration 7: f(p) = 1.377e-08
Iteration 8: f(p) = 1.290e-08
Iteration 9: f(p) = 1.195e-08
Iteration 10: f(p) = 1.168e-08
Iteration 11: f(p) = 1.075e-08
Iteration 12: f(p) = 1.026e-08
Iteration 13: f(p) = 1.005e-08
Iteration 14: f(p) = 9.509e-09
Iteration 15: f(p) = 9.074e-09
Iteration 16: f(p) = 6.361e-09
Iteration 17: f(p) = 2.702e-09
Iteration 18: f(p) = 2.272e-09
Iteration 19: f(p) = 1.867e-09
Iteration 20: f(p) = 1.339e-09
Iteration 21: f(p) = 1.242e-09
Iteration 22: f(p) = 1.149e-09
Iteration 23: f(p) = 9.784e-10
Iteration 24: f(p) = 9.420e-10
Iteration 25: f(p) = 9.360e-10
Iteration 26: f(p) = 9.200e-10
Iteration 27: f(p) = 9.147e-10
Iteration 28: f(p) = 9.058e-10
Iteration 29: f(p) = 7.129e-10
Iteration 30: f(p) = 3.744e-10
Iteration 31: f(p) = 2.987e-10
Iteration 32: f(p) = 2.963e-10
Iteration 33: f(p) = 2.653e-10
Iteration 34: f(p) = 9.786e-11
Iteration 35: f(p) = 7.971e-12
Iteration 36: f(p) = 7.291e-13
Iteration 37: f(p) = 7.440e-15
Iteration 38: f(p) = 1.955e-16
Iteration 39: f(p) = 1.491e-19
1 2 3 4 5 6 7 8 9 10
+-------------------------------------------------------------------------------------------------------------------------------------------------------
1 | -1.441063943 0 -2.548778665 0 -1.023181048 -.2320959429 .0736983529 -.4863391027 .065541404 .0303861341
+-------------------------------------------------------------------------------------------------------------------------------------------------------
11 12 13 14 15 16
-------------------------------------------------------------------------------------------+
1 -.0236438487 .201465 -.0067203894 -.3662317672 -.0080116617 3.127762038 |
-------------------------------------------------------------------------------------------+
: D.balanceresults("ate", 2)
Balance:
-------------------------------------------------------------------------------
| mean_T mean_C diff std_diff sd_pool var_ratio
-------------+-----------------------------------------------------------------
x1 | .6750468 .6750468 -3.26e-08 -6.96e-08 .4688264 1.008511
|
x2 |
0 | (empty)
1 | .0219356 .0219357 -9.38e-08 -6.39e-07 .1466203 1.008507
|
x3 |
0 | (empty)