Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to export results of group regressions and subsequent calculations using runby

    I need to run panel regressions with fixed effects multiple times. Regressions are run (1) for dependent variables : ret1_1, ret1_2, ret1_3, ret1_4, respectively; (2) for group aumrank=3,4, respectively; (3) for group amirank=1,2, respectively.
    If I don't use the nlcom command, this can be realized via 'statsby'. However, statsby does not seem to support multiple subsequent commands. So I tried to use 'runby' but couldn't make it work.
    Could someone please advise? The codes and sample data are as below.


    global datapath `"D:..."'
    display `datapath'

    /******code of sample data******************/
    clear
    input long dt float ret byte(news unsche sche) float(to ami) byte jump float(ret1_1 ret1_2 ret1_3 ret1_4 int_retami int_retto int_retsche int_retunsche) int year byte(etfnumid aumrank amirank)
    1609666200 -.002180594 1 1 0 .001124295 .005375426 0 .002527068 .001685422 .00173495 .002368695 -.000011722 -2.45e-06 0 -.002180594 2011 4 3 2
    1609667100 .002527068 1 1 0 .000629071 .0111055 0 -.000841647 -.000792118 -.000158374 .001038704 .0000280644 1.59e-06 0 .002527068 2011 4 3 2
    1609670700 .001197078 0 0 0 .0001091 .03030171 0 -.0008952 -.000098878 -.000890252 .0000939248 .0000362735 1.31e-07 0 0 2011 4 3 2
    1609677000 .000098956 1 0 1 .000108709 .002515881 0 -.000593883 -.000217716 -.000217716 -.001089055 2.49e-07 1.08e-08 .000098956 0 2011 4 3 2
    1609688700 -.000495909 0 0 0 .000259986 .005285485 0 -.001515249 -.002973243 -.004960328 -.004274337 -2.62e-06 -1.29e-07 0 0 2011 4 3 2
    1609752600 -.001515249 1 0 1 .000452209 .009275963 0 -.001457994 -.003445078 -.002759088 -.003440106 -.000014055 -6.85e-07 -.001515249 0 2011 4 3 2
    1609753500 -.001457994 1 0 1 .002042456 .001979022 0 -.001987085 -.001301094 -.001982112 -.001092408 -2.89e-06 -2.98e-06 -.001457994 0 2011 4 3 2
    1609754400 -.001987085 0 0 0 .000165913 .03326962 0 .00068599 4.97e-06 .000894677 -.0007412 -.00006611 -3.30e-07 0 0 2011 4 3 2
    1609755300 .00068599 0 0 0 .00010033 .01898024 0 -.000681018 .000208686 -.00142719 -.000238552 .0000130203 6.88e-08 0 0 2011 4 3 2
    1609756200 -.000681018 0 0 0 .000789788 .00239528 0 .000889704 -.000746172 .000442466 .000989063 -1.63e-06 -5.38e-07 0 0 2011 4 3 2
    1609757100 .000889704 0 0 0 .000361377 .006832943 0 -.001635876 -.000447238 .0000993591 -.000894677 6.08e-06 3.22e-07 0 0 2011 4 3 2
    1609758000 -.001635876 0 0 0 .000343836 .013226113 0 .001188638 .001735235 .0007412 -.000751709 -.000021636 -5.62e-07 0 0 2011 4 3 2
    1609758900 .001188638 1 0 1 .000417686 .007901614 0 .000546597 -.000447438 -.001940347 -.002817226 9.39e-06 4.96e-07 .001188638 0 2011 4 3 2
    1609759800 .000546597 1 0 1 .000184962 .008200959 0 -.000994036 -.002486945 -.003363823 -.003483457 4.48e-06 1.01e-07 .000546597 0 2011 4 3 2
    1609760700 -.000994036 1 0 1 .000189598 .014563915 0 -.001492909 -.002369787 -.002489421 -.002798542 -.000014477 -1.88e-07 -.000994036 0 2011 4 3 2
    1609761600 -.001492909 1 1 0 .0000598849 .06935453 0 -.000876878 -.000996512 -.001305633 -.000498132 -.00010354 -8.94e-08 0 -.001492909 2011 4 3 2
    1609762500 -.000876878 1 1 0 .000314452 .00776471 0 -.000119634 -.000428754 .000378746 .001225423 -6.81e-06 -2.76e-07 0 -.000876878 2011 4 3 2
    1609763400 -.000119634 1 1 0 .0000847997 .003928725 0 -.000309121 .00049838 .001345057 .001275358 -4.70e-07 -1.01e-08 0 -.000119634 2011 4 3 2
    1609764300 -.000309121 1 1 0 .0000668678 .012877663 0 .000807501 .001654178 .001584478 .002301153 -3.98e-06 -2.07e-08 0 -.000309121 2011 4 3 2
    1692287100 -.002313923 0 0 0 .000130497 .024277354 0 0 .00212581 .002724087 .002492538 -.000056176 -3.02e-07 0 0 2013 8 3 2
    1692523800 0 1 0 1 .000250993 0 0 .00212581 .002724087 .002492538 .001565808 0 0 0 0 2013 8 3 2
    1692524700 .00212581 1 0 1 .000117518 .02476693 0 .000598277 .000366728 -.000560002 -.000560002 .0000526498 2.50e-07 .00212581 0 2013 8 3 2
    1692525600 .000598277 0 0 0 9.79e-06 .08364372 0 -.000231548 -.001158279 -.001158279 -.000791345 .0000500421 5.86e-09 0 0 2013 8 3 2
    1692526500 -.000231548 0 0 0 .0000273759 .01157619 0 -.00092673 -.00092673 -.000559797 -.002651189 -2.68e-06 -6.34e-09 0 0 2013 8 3 2
    1692527400 -.00092673 0 0 0 .0000660284 .01922725 0 0 .000366934 -.001724458 -.002338015 -.000017818 -6.12e-08 0 0 2013 8 3 2
    1692528300 . 0 0 0 . . 0 .000366934 -.001724458 -.002338015 -.001747678 . . . . 2013 8 3 2
    1692529200 .000366934 1 0 1 .0000934752 .005375589 0 -.002091392 -.002704949 -.002114611 -.002242327 1.97e-06 3.43e-08 .000366934 0 2013 8 3 2
    1692530100 -.002091392 1 0 1 .0000760993 .03771361 0 -.000613557 -.000023219 -.000150935 -.000286409 -.000078874 -1.59e-07 -.002091392 0 2013 8 3 2
    1692531000 -.000613557 1 0 1 .0000368794 .02284443 0 .000590338 .000462622 .000327148 .001157121 -.000014016 -2.26e-08 -.000613557 0 2013 8 3 2
    1692531900 .000590338 1 0 1 .0000407801 .019865755 0 -.000127716 -.000263189 .000566783 7.74e-06 .0000117275 2.41e-08 .000590338 0 2013 8 3 2
    1692612000 .001062853 1 1 0 .000156028 .00933105 0 .000096567 .00187945 .002025952 .002604043 9.92e-06 1.66e-07 0 .001062853 2013 8 3 2
    1692612900 .000096567 1 1 0 .0000551064 .002400191 0 .001782883 .001929385 .002507476 .001352889 2.32e-07 5.32e-09 0 .000096567 2013 8 3 2
    1692613800 .001782883 1 1 0 .000124255 .01961789 0 .000146503 .000724593 -.000429993 .000223601 .0000349764 2.22e-07 0 .001782883 2013 8 3 2
    1692614700 .000146503 0 0 0 .0000301418 .006644412 0 .00057809 -.000576496 .000077098 .000221641 9.73e-07 4.42e-09 0 0 2013 8 3 2
    1692615600 .00057809 0 0 0 .0000735461 .010739053 0 -.001154587 -.000500992 -.00035645 -.000366085 6.21e-06 4.25e-08 0 0 2013 8 3 2
    1609932600 -.000771374 1 0 1 .00287378 .0000311048 0 .000257191 -.000171497 -.000171497 .000771374 -2.40e-08 -2.22e-06 -.000771374 0 2011 9 4 1
    1609933500 .000257191 0 0 0 .001743209 .0000170927 0 -.000428688 -.000428688 .000514183 .000633264 4.40e-09 4.48e-07 0 0 2011 9 4 1
    1609934400 -.000428688 0 0 0 .002290391 .0000216932 0 0 .000942871 .001061952 0 -9.30e-09 -9.82e-07 0 0 2011 9 4 1
    1609935300 0 0 0 0 .000890945 0 0 .000942871 .001061952 0 0 0 0 0 0 2011 9 4 1
    1609936200 .000942871 0 0 0 .001725612 .000063269 0 .000119081 -.000942871 -.000942871 -.000342759 5.97e-08 1.63e-06 0 0 2011 9 4 1
    1609937100 .000119081 0 0 0 .001120097 .0000123089 0 -.001061952 -.001061952 -.000461841 .000822918 1.47e-09 1.33e-07 0 0 2011 9 4 1
    1609938000 -.001061952 0 0 0 .002189755 .0000562083 0 0 .000600112 .00188487 .00171365 -5.97e-08 -2.33e-06 0 0 2011 9 4 1
    1609938900 0 0 0 0 .001325774 0 0 .000600112 .00188487 .00171365 .001628037 0 0 0 0 2011 9 4 1
    1609939800 .000600112 0 0 0 .000935329 .0000743187 0 .001284759 .001113538 .001027925 -8.57e-07 4.46e-08 5.61e-07 0 0 2011 9 4 1
    1609940700 .001284759 0 0 0 .000942965 .000157615 0 -.000171221 -.000256833 -.001285616 -.001627636 2.02e-07 1.21e-06 0 0 2011 9 4 1
    1609941600 -.000171221 0 0 0 .001539384 .0000128693 0 -.000085613 -.001114395 -.001456415 -.000428137 -2.20e-09 -2.64e-07 0 0 2011 9 4 1
    1609942500 -.000085613 0 0 0 .000573713 .0000172675 0 -.001028782 -.001370802 -.000342524 -.001199349 -1.48e-09 -4.91e-08 0 0 2011 9 4 1
    1609943400 -.001028782 0 0 0 .001452696 .0000820313 0 -.00034202 .000686258 -.000170567 .000686258 -8.44e-08 -1.49e-06 0 0 2011 9 4 1
    1609944300 -.00034202 0 0 0 .002366901 .0000167437 0 .001028278 .000171453 .001028278 .001028278 -5.73e-09 -8.10e-07 0 0 2011 9 4 1
    1609945200 .001028278 0 0 0 .001476578 .0000806096 0 -.000856825 0 0 3.42e-08 8.29e-08 1.52e-06 0 0 2011 9 4 1
    1609946100 -.000856825 0 0 0 .003282025 .0000302451 0 .000856825 .000856825 .000856859 -.000684008 -2.59e-08 -2.81e-06 0 0 2011 9 4 1
    1609947000 .000856825 0 0 0 .001758913 .0000563872 0 0 3.42e-08 -.001540832 -.00008554 4.83e-08 1.51e-06 0 0 2011 9 4 1
    1609947900 0 0 0 0 .00512868 0 0 3.42e-08 -.001540832 -.00008554 .000171057 0 0 0 0 2011 9 4 1
    1610013600 .001455293 1 1 0 .00358266 .0000469632 0 .000256597 -.001369629 -.001969432 -.002055147 6.83e-08 5.21e-06 0 .001455293 2011 9 4 1
    1610014500 .000256597 1 1 0 .002846506 .0000104193 0 -.001626225 -.002226028 -.002311744 -.003770033 2.67e-09 7.30e-07 0 .000256597 2011 9 4 1
    1610015400 -.001626225 1 1 0 .003627558 .0000519008 0 -.000599803 -.000685518 -.002143807 -.003088541 -8.44e-08 -5.90e-06 0 -.001626225 2011 9 4 1
    1610016300 -.000599803 1 1 0 .004677037 .0000148561 0 -.000085716 -.001544004 -.002488738 -.002488738 -8.91e-09 -2.81e-06 0 -.000599803 2011 9 4 1
    1610017200 -.000085716 0 0 0 .004742384 2.09e-06 0 -.001458289 -.002403022 -.002403022 -.003004681 -1.79e-10 -4.06e-07 0 0 2011 9 4 1
    1610018100 -.001458289 0 0 0 .00370888 .0000456183 0 -.000944733 -.000944733 -.001546392 -.00266472 -6.65e-08 -5.41e-06 0 0 2011 9 4 1
    1610019000 -.000944733 0 0 0 .005087634 .0000215646 0 0 -.000601659 -.001719987 -.003322234 -2.04e-08 -4.81e-06 0 0 2011 9 4 1
    1610019900 0 0 0 0 .002857035 0 0 -.000601659 -.001719987 -.003322234 -.002494947 0 0 0 0 2011 9 4 1
    1610020800 -.000601659 0 0 0 .000934937 .0000747787 0 -.001118328 -.002720575 -.001893288 -.001979432 -4.50e-08 -5.63e-07 0 0 2011 9 4 1
    1610021700 -.001118328 0 0 0 .002839654 .0000458141 0 -.001602247 -.00077496 -.000861104 .0000860696 -5.12e-08 -3.18e-06 0 0 2011 9 4 1
    1610022600 -.001602247 0 0 0 .024852196 7.51e-06 0 .000827287 .000741143 .001688317 .002548606 -1.20e-08 -.000039819 0 0 2011 9 4 1
    1610023500 .000827287 0 0 0 .00608566 .0000158263 0 -.000086144 .00086103 .001721319 .001119194 1.31e-08 5.03e-06 0 0 2011 9 4 1
    1610024400 -.000086144 0 0 0 .00283245 3.54e-06 0 .000947174 .001807463 .001205338 .001807463 -3.05e-10 -2.44e-07 0 0 2011 9 4 1
    1610025300 .000947174 1 1 0 .003012407 .0000365741 0 .000860289 .000258165 .000860289 .001290156 3.46e-08 2.85e-06 0 .000947174 2011 9 4 1
    1610026200 .000860289 1 1 0 .003948831 .0000253198 0 -.000602125 0 .000429867 .000773602 2.18e-08 3.40e-06 0 .000860289 2011 9 4 1
    1610027100 -.000602125 1 1 0 .003316151 .0000211153 0 .000602125 .001031992 .001375727 .001461674 -1.27e-08 -2.00e-06 0 -.000602125 2011 9 4 1
    1610028000 .000602125 0 0 0 .003355994 .0000208521 0 .000429867 .000773602 .00085955 .000945464 1.26e-08 2.02e-06 0 0 2011 9 4 1
    1610028900 .000429867 0 0 0 .003425706 .0000145775 0 .000343735 .000429683 .000515597 .000601504 6.27e-09 1.47e-06 0 0 2011 9 4 1
    1610029800 .000343735 0 0 0 .001377267 .0000289837 0 .0000859475 .000171862 .000257769 .001459692 9.96e-09 4.73e-07 0 0 2011 9 4 1
    1610030700 .0000859475 0 0 0 .001588052 6.28e-06 0 .0000859143 .000171821 .001373745 .001459541 5.40e-10 1.36e-07 0 0 2011 9 4 1
    1610031600 .0000859143 0 0 0 .00146709 6.80e-06 0 .000085907 .00128783 .001373627 .00085692 5.84e-10 1.26e-07 0 0 2011 9 4 1
    1610032500 .000085907 0 0 0 .003985741 2.50e-06 0 .001201923 .00128772 .000771013 -.003199301 2.15e-10 3.42e-07 0 0 2011 9 4 1
    1610033400 .001201923 0 0 0 .004587512 .0000303819 0 .0000857964 -.00043091 -.004401225 -.002586734 3.65e-08 5.51e-06 0 0 2011 9 4 1
    1610034300 .0000857964 0 0 0 .005537546 1.80e-06 0 -.000516707 -.004487021 -.002672531 -.003017894 1.54e-10 4.75e-07 0 0 2011 9 4 1
    1610272800 .001814491 0 0 0 .004811677 .0000439974 0 -.000345363 .000604099 .000172637 0 7.98e-08 8.73e-06 0 0 2011 9 4 1
    1610273700 -.000345363 0 0 0 .0095684 4.21e-06 0 .000949463 .000518001 .000345364 .001121996 -1.45e-09 -3.30e-06 0 0 2011 9 4 1
    1610274600 .000949463 0 0 0 .004819976 .0000229688 0 -.000431462 -.000604099 .000172533 .000603735 2.18e-08 4.58e-06 0 0 2011 9 4 1
    1610275500 -.000431462 0 0 0 .003594783 .0000140011 0 -.000172637 .000603995 .001035197 .002413786 -6.04e-09 -1.55e-06 0 0 2011 9 4 1
    1610276400 -.000172637 1 0 1 .002481471 8.12e-06 0 .000776632 .001207834 .002586423 .002155824 -1.40e-09 -4.28e-07 -.000172637 0 2011 9 4 1
    1610277300 .000776632 1 0 1 .002075605 .0000436216 0 .000431202 .001809791 .001379192 .002068058 3.39e-08 1.61e-06 .000776632 0 2011 9 4 1
    1610278200 .000431202 1 0 1 .001699338 .0000295695 0 .001378589 .00094799 .001636856 .00180902 1.28e-08 7.33e-07 .000431202 0 2011 9 4 1
    1610279100 .001378589 1 0 1 .003114678 .0000515071 0 -.000430599 .000258267 .000430431 -.000175657 7.10e-08 4.29e-06 .001378589 0 2011 9 4 1
    1610280000 -.000430599 0 0 0 .001723057 .0000290941 0 .000688866 .00086103 .000254942 .001033147 -1.25e-08 -7.42e-07 0 0 2011 9 4 1
    1610280900 .000688866 0 0 0 .004363455 .000018367 0 .000172164 -.000433924 .000344281 .000258226 1.27e-08 3.01e-06 0 0 2011 9 4 1
    1610281800 .000172164 0 0 0 .001737181 .000011528 0 -.000606088 .000172117 .0000860622 .0000869228 1.98e-09 2.99e-07 0 0 2011 9 4 1
    1610282700 -.000606088 1 1 0 .001463373 .0000482061 0 .000778205 .00069215 .000693011 .000175666 -2.92e-08 -8.87e-07 0 -.000606088 2011 9 4 1
    1610283600 .000778205 1 1 0 .001080038 .0000837989 0 -.000086055 -.000085194 -.000602539 -.001119291 6.52e-08 8.40e-07 0 .000778205 2011 9 4 1
    1610284500 -.000086055 1 1 0 .002473997 4.05e-06 0 8.61e-07 -.000516484 -.001033236 -.000431246 -3.48e-10 -2.13e-07 0 -.000086055 2011 9 4 1
    1610285400 8.61e-07 0 0 0 .000893851 1.12e-07 0 -.000517345 -.001034096 -.000432107 -.000345154 9.64e-14 7.69e-10 0 0 2011 9 4 1
    1610286300 -.000517345 0 0 0 .000687728 .0000875404 0 -.000516751 .0000852383 .000172191 .000946685 -4.53e-08 -3.56e-07 0 0 2011 9 4 1
    1610287200 -.000516751 0 0 0 .002884728 .0000208567 0 .00060199 .000688943 .001463436 .001377411 -1.08e-08 -1.49e-06 0 0 2011 9 4 1
    1610288100 .00060199 0 0 0 .000938586 .0000746316 0 .0000869528 .000861446 .000775421 .001549382 4.49e-08 5.65e-07 0 0 2011 9 4 1
    1610289000 .0000869528 0 0 0 .000827923 .0000122198 0 .000774493 .000688468 .001462429 .000774493 1.06e-09 7.20e-08 0 0 2011 9 4 1
    1610289900 .000774493 0 0 0 .001850534 .000048658 0 -.000086025 .000687935 0 -.0004302 3.77e-08 1.43e-06 0 0 2011 9 4 1
    1610290800 -.000086025 0 0 0 .002227571 4.49e-06 0 .000773961 .0000860252 -.000344175 .000170229 -3.86e-10 -1.92e-07 0 0 2011 9 4 1
    1610291700 .000773961 0 0 0 .001962225 .0000458253 0 -.000687935 -.001118135 -.000603732 -.001437198 3.55e-08 1.52e-06 0 0 2011 9 4 1
    1610292600 -.000687935 0 0 0 .001778497 .0000449706 0 -.0004302 .0000842033 -.000749263 -.000172974 -3.09e-08 -1.22e-06 0 0 2011 9 4 1
    end

    /*********single regression with nlcom*****/
    use "$datapath\subtest.dta",clear
    xtset etfnumid dt
    set matsize 5400
    xtreg ret1_4 ret int_retsche int_retunsche sche unsche int_retto to int_retami ami i.year if jump ==1,fe vce(robust)

    nlcom (sche:_b[ret]+_b[int_retsche ]) (unsche:_b[ret]+_b[int_retunsche ])(unsche_sche:_b[int_retunsche]-_b[int_retsche])
    return list
    return list
    mat b1=r(b)
    mat v1=r(V)

    local sche=b1[1,1]
    local sesche=sqrt(v1[1,1])
    local zsche = `sche'/`sesche'
    local psche = 2*normal(-abs(`zsche'))

    local unsche=b1[1,2]
    local seunsche=sqrt(v1[2,2])
    local zunsche = `unsche'/`seunsche'
    local punsche = 2*normal(-abs(`zunsche'))


    outreg2 using fe5.doc, keep(ret int_retsche int_retunsche sche unsche int_retto to int_retami ami) nocons /*
    */adds(schen, `sche', psche,`psche', unschen, `unsche', punsche,`punsche') /*
    */ tstat bdec(4) tdec(2) rdec(4) adec(4) addtext(Firm FE, Yes, Year FE, Yes)
    /************************************************** **/



    //////////////////////group regression without nlcom using statsby//////////////////////////////
    foreach depid of numlist 1/4 {
    foreach groupid in aumrank amirank{
    use "$datapath\subtest.dta",clear
    xtset etfnumid dt
    set matsize 5400

    di "`groupid'"
    sort `groupid'
    statsby _b _se Rsq=e(r2) adjRsq=e(r2_a) nobs=e(N) , by(`groupid') saving($datapath\by_`groupid'.dta, replace):/*
    */xtreg ret1_`depid' ret int_retsche int_retunsche sche unsche int_retto to int_retami ami i.year if jump ==1,fe vce(robust)


    use "$datapath\by_`groupid'.dta",clear
    cls
    local regid "5"
    gen regid= "`regid'"
    gen depid= "`depid'"
    gen groupid= "`groupid'"
    rename `groupid' rank
    keep _b_* _se_* _eq2* regid depid groupid rank
    save group_`groupid'_`regid'_`depid'toami.dta, replace
    }
    }
    ///////////////////////////////////////////////////////////////////////////////////


    /******************test code using runby with bugs, not working**********************************/

    capture program drop group_regression
    program define group_regression
    use "$datapath\subtest.dta",clear
    xtset etfnumid dt
    set matsize 5400
    xtreg ret1_`depid' ret int_retsche int_retunsche sche unsche int_retto to int_retami ami i.year if jump ==1,fe vce(robust)

    nlcom (sche:_b[ret]+_b[int_retsche ]) (unsche:_b[ret]+_b[int_retunsche ])(unsche_sche:_b[int_retunsche]-_b[int_retsche])
    return list
    return list
    mat b1=r(b)
    mat v1=r(V)

    local sche=b1[1,1]
    local sesche=sqrt(v1[1,1])
    local zsche = `sche'/`sesche'
    local psche = 2*normal(-abs(`zsche'))

    local unsche=b1[1,2]
    local seunsche=sqrt(v1[2,2])
    local zunsche = `unsche'/`seunsche'
    local punsche = 2*normal(-abs(`zunsche'))


    rename `groupid' rank
    keep _b_* _se_* _eq2* rank , `sche', `psche', `unsche', `punsche'

    exit

    end

    foreach depid of numlist 1/4 {
    use "$datapath\subtest.dta",clear
    xtset etfnumid dt
    set matsize 5400
    local groupid aumrank
    di "`groupid'"
    local regid "5"


    runby group_regression, by(`groupid') status
    save group_`groupid'_`regid'_`depid'toami.dta, replace }



    /************************************************** **/




  • #2
    There are a number of problems with this code. To hit the high points: 1. local macros defined in the main code are undefined within program group_regression, and vice versa. So there is considerable lack of communication up and down. 2. As far as I can see, local macro depid is never defined anywhere. 3. You can't -keep- local macros, only variables. 4, You attempt to access e(r2), but there is no such thing after -xtreg,fe-. Rather there are three different R2 statistics, and I have no idea which one you are interested in. 5. Within program group_regression you clear the data and load in $datapath\subtest.dta. By doing that, you over-ride the segmentation of the data set into subsets defined by aumrank or amirank and instead just repeatedly perform the regression on the entire original data set.

    Your example data, unfortunately is not useful for developing and testing the code. This is because the regressions are to be run only on the subset of observations with jump == 1, but in your example, jump is always 0.

    Anyway, I think the code you want will look something like this:

    Code:
    capture program drop one_regression
    program define one_regression
        xtset etfnumid dt
        capture xtreg dep_var ret int_retsche int_retunsche sche unsche int_retto ///
            to int_retami ami i.year if jump ==1, fe vce(robust)
            
        if c(rc) == 0 {
            gen n_obs = e(N)
            // INSERT CODE TO RECORD THE APPROPRIATE R2 VALUE
            
            nlcom (sche:_b[ret]+_b[int_retsche ]) (unsche:_b[ret]+_b[int_retunsche ])(unsche_sche:_b[int_retunsche]-_b[int_retsche])
            mat b1=r(b)
            mat v1=r(V)
    
            gen sche_com=b1[1,1]
            gen sesche_com=sqrt(v1[1,1])
            gen zsche_com = sche_com/sesche_com
            gen psche_com = 2*normal(-abs(zsche_com))
    
            gen unsche_com=b1[1,2]
            gen seunsche_com=sqrt(v1[2,2])
            gen zunsche_com = unsche_com/seunsche_com
            gen punsche_com = 2*normal(-abs(zunsche_com))
        }
        else if inlist(c(rc), 2000, 2001) {
            gen problem = "No or insufficient observations"
        }
        else {
            gen problem = "Unexpected error in -xtreg-"
        }
        exit
    end
    
    
    
    // use "$datapath/subtest.dta", clear
    set matsize 5400
    
    gen dep_var = .
    forvalues i = 1/4 {
        replace dep_var = ret1_`i'
        foreach groupid in aumrank amirank {
            preserve
            runby one_regression, by(`groupid')
            save group_`i'_`groupid'_5_toami, replace
            restore
        }
    }
    A few notes. In recent versions of Stata, -set matsize- is not an operational command: it does not create an error, but it does nothing. So unless you are using an old version of Stata, you can drop this.

    The code produces a separate output file for each of the 8 combinations of dependent variable (4 of them) and group id variable (2 of them). If you want to, you can append them all together instead--but if you do that you will have to add variables identifying the dependent variable and group id.

    The code creates variables sche_com and unsche_com instead of local macros sche and unsche. The reason for variables instead of sche and unsche is the impossibility of -keep-ing local macros in the data: variables are required for this. The reason for adding _com to the name is that the original data set already has variables named sche and unsche. The _com suffix is to remind you that they come from nlcom. To keep things consistent, I also used the _com suffix on the variables storing the standard errors, z statistics and p-values.

    If your real data set is large, you can speed this up by running -keep if jump == 1- beforehand and eliminating the -if jump == 1- clause from the -xtreg- command.

    Because you are likely to have some combinations of dep_var and groupid for which there are no or insufficient observations to do the regression, this code creates a new string variable, called problem, that identifies any such groups of observations. It will also point out any other, unexpected, errors that may arise when trying to run -xtreg, fe-. If you encounter any outputs where problem shows an unexpected error, you need to investigate what is going on with that (those) combination(s).






    Comment


    • #3
      Thanks very much, Clyde. That's very helpful.

      I took the first 100 obs from the original dataset as the sample therefore jump is always zero in the subsample. Sorry for the confusion. A updated subsample can be found at the end of this post.
      Thanks for the tips on -set matsize- and speeding up. The problem indicator is quite useful as well. -xtreg,fe- specifies e(r2) as e(r2_w), which is fine for my analysis.

      The output from your code merges *_com to the origninal data file. I would like to store the parameter estimates from -xtreg,fe- and *_com, not merged with the original data. I noticed that the coefficient vector and variance-covariance matrix of the estimators are stored in e(b) and e(V), respectively. How can I generate the result in the following pattern?

      rank b_ret b_int_retsche b_int_retunsche b_sche b_unsche b_int_retto b_to b_int_retami b_ami ///
      se_ret se_int_retsche se_int_retunsche se_sche se_unsche se_int_retto se_to se_int_retami se_ami ///
      Rsq n_obs regid depid groupid problem


      Each row summarizes the results from a -xtreg,fe- regression. Rank is the level of `groupid', i.e. 3 or 4 for `groupid'=aumrank, and 1 or 2 for `groupid'= amirank. b_* is the coefficient, element of e(b). se_* is the standard error, sqrt of diagnal elemet of e(V), I suppose? regid is 5. depid is `i' as in the loop -forvalues i = 1/4-. groupid is aumrank or amirank.





      clear
      input long dt float ret byte(news unsche sche) float(to ami) byte jump float(ret1_1 ret1_2 ret1_3 ret1_4 int_retami int_retto int_retsche int_retunsche) int year byte(etfnumid aumrank amirank)
      1611151200 .0027954055 0 0 0 .0003478018 .02218325 1 0 .0003062772 .0007062723 .0009876544 .0000620112 9.722471e-07 0 0 2011 4 3 2
      1615286700 -.003957043 1 0 1 .0002998864 .0307552 1 -.000283246 .001603547 .0017354893 .0020746896 -.0001217 -1.186663e-06 -.003957043 0 2011 4 3 2
      1630332900 .004661092 1 1 0 .0000533618 .19966127 1 0 .0008420099 .0009798154 .0009847367 .0009306396 2.4872423e-07 0 .004661092 2011 4 3 2
      1649080800 -.004317709 1 1 0 .0002857596 .02190482 1 .0008001372 -.000114358 .0002515407 .001257071 -.000094579 -1.233827e-06 0 -.004317709 2012 4 3 2
      1651580100 .00179619 0 0 0 .0003259862 .008155556 1 .0003515476 -.000013699 -.000013699 -.00042933 .0000146489 5.855331e-07 0 0 2012 4 3 2
      1732786200 -.006269613 1 0 1 .0003519931 .014715454 1 .0004191993 .0005072089 -.000209666 -.000880891 -.00009226 -2.20686e-06 -.006269613 0 2014 4 3 2
      1732807800 .010050336 0 0 0 7.428425e-06 1.1107799 1 0 -.003423575 -.007198845 -.006773946 .01116371 7.465817e-08 0 0 2014 4 3 2
      1687274100 .00323212 1 1 0 .002434955 .0020309805 1 -.001330708 -.005962031 -.006308052 -.008138763 6.564372e-06 7.870066e-06 0 .00323212 2013 7 3 2
      1687275900 -.004631323 1 1 0 .0004127928 .017269168 1 -.000346021 -.002176732 -.004681413 -.004333889 -.000079979 -1.911777e-06 0 -.004631323 2013 7 3 2
      1700575200 -.005964979 1 1 0 .0021015333 .0029035746 1 .0019912615 -.000156526 -.000769053 -.000769053 -.00001732 -.000012536 0 -.005964979 2013 7 3 2
      1701348300 -.00267698 0 0 0 .0001431579 .01832062 1 0 0 0 0 -.000049044 -3.832308e-07 0 0 2013 7 3 2
      1641807000 -.014508026 1 0 1 .0015767857 .02540846 1 .0020855065 .0020855065 .0025483624 .002918493 -.000368627 -.000022876 -.014508026 0 2012 8 3 2
      1642682700 .002355061 1 0 1 .0001460714 .04426376 1 .0006455183 .0004150239 .0008298756 .0009796408 .0001042439 3.440071e-07 .002355061 0 2012 8 3 2
      1647271800 .003667917 0 0 0 .0000706522 .12528777 1 .0009121081 .0024654204 .0027979594 .0032566816 .0004595451 2.5914628e-07 0 0 2012 8 3 2
      1649854800 .001441125 1 1 0 .0000515957 .06728085 1 0 .0001245281 .0003622204 .0003622204 .0000969601 7.43559e-08 0 .001441125 2012 8 3 2
      1650203100 .0021260201 1 1 0 .000026383 .1938902 1 0 -.000316692 -.000542962 -.000099521 .0004122145 5.609074e-08 0 .0021260201 2012 8 3 2
      1656938700 .002616246 0 0 0 .0002716908 .020090396 1 .0004317789 .0004317789 .0004317789 .0004317789 .0000525614 7.1081e-07 0 0 2012 8 3 2
      1659969900 -.00163515 1 1 0 .0027414784 .0011022923 1 -.000042507 -.000042507 -.000391133 -.001957198 -1.802413e-06 -4.482727e-06 0 -.00163515 2012 8 3 2
      1667821500 .0043300586 1 0 1 .000246805 .031111827 1 0 .000811671 -.00014959 -.000833716 .000134716 1.06868e-06 .0043300586 0 2012 8 3 2
      1668423600 .004966716 1 0 1 .0000862241 .10479403 1 -.000353009 -.002544008 -.001667472 -.003425714 .0005204822 4.282504e-07 .004966716 0 2012 8 3 2
      1669293900 .0043208287 0 0 0 .0000555187 .1404968 1 0 0 0 0 .0006070626 2.3988667e-07 0 0 2012 8 3 2
      1671961500 -.003435529 1 1 0 .000041841 .14975496 1 -.000043591 .0019161265 .000610075 .000610075 -.000514487 -1.43746e-07 0 -.003435529 2012 8 3 2
      1672840800 -.00304746 1 1 0 .000115641 .04810695 1 -2.135837e-06 -2.135837e-06 .0014705085 .0002221022 -.000146604 -3.524114e-07 0 -.00304746 2013 8 3 2
      1683645300 .0016903282 0 0 0 .0001580843 .015392748 1 -.00004321 .000377536 -.0006899 -.001340694 .0000260188 2.6721435e-07 0 0 2013 8 3 2
      1687274100 .003079021 1 1 0 .0001089531 .03886549 1 -.002861232 -.007072571 -.009629027 -.009163731 .0001196677 3.354688e-07 0 .003079021 2013 8 3 2
      1687275000 -.002861232 1 1 0 .0002743682 .01438311 1 -.004211339 -.006767795 -.006302498 -.011978368 -.000041153 -7.850312e-07 0 -.002861232 2013 8 3 2
      1687275900 -.004211339 1 1 0 .0002560289 .022782097 1 -.002556456 -.00209116 -.007767029 -.0083715 -.000095943 -1.078224e-06 0 -.004211339 2013 8 3 2
      1693915200 .002331072 1 0 1 .0000390071 .08292693 1 -.000180024 .0011537297 .0002171593 .0002230278 .0001933086 9.092834e-08 .002331072 0 2013 8 3 2
      1693921500 -.002520802 1 0 1 .0000241135 .14517541 1 .0005461587 -1.958095e-06 .0006635733 .000667487 -.000365958 -6.078529e-08 -.002520802 0 2013 8 3 2
      1699544700 .006166786 1 1 0 .000119635 .0686593 1 .0009322646 .002721239 .001827152 .0014619885 .0004234072 7.377636e-07 0 .006166786 2013 8 3 2
      1701348300 -.002556568 0 0 0 .0000938406 .036071334 1 0 0 0 0 -.000092219 -2.399099e-07 0 0 2013 8 3 2
      1328179500 -.004132446 0 0 0 .010462855 .0001292988 1 -.000303046 0 .0002019794 .0002019794 -5.343203e-07 -.000043237 0 0 2002 9 4 1
      1336478400 .008718106 0 0 0 .0030404804 .0007196179 1 -.000493316 -.00029596 -.002172411 -.001875895 6.273704e-06 .0000265072 0 0 2002 9 4 1
      1347361200 .01533739 1 1 0 .0003868428 .01024119 1 .0025088394 .001426249 -.000342603 -.002286238 .0001570731 5.933159e-06 0 .01533739 2002 9 4 1
      1350907200 -.010235986 0 0 0 .004966449 .0005379999 1 -.001657001 -.003435412 -.000828157 -.000709807 -5.506959e-06 -.000050836 0 0 2002 9 4 1
      1377684000 -.008150179 0 0 0 .006975489 .0002057173 1 .0012912947 .0021512324 .001721356 .0039761486 -1.676633e-06 -.000056851 0 0 2003 9 4 1
      1392126300 .004005408 0 0 0 .008837286 .000063781 1 -.000185943 -.000557932 .0002788493 .0000929584 2.5546902e-07 .0000353969 0 0 2004 9 4 1
      1393939800 .002451444 0 0 0 .00363631 .0000936909 1 -.000282552 -.000376754 -.000470965 -.000376754 2.296781e-07 8.9142095e-06 0 0 2004 9 4 1
      1401971400 .002914038 1 0 1 .005021606 .0000794831 1 -.000096998 -.001261707 -.001067495 -.002330551 2.3161677e-07 .0000146332 .002914038 0 2004 9 4 1
      1411397100 .0035094596 1 0 1 .006836185 .0000710464 1 .0002918998 -.000584055 -.00165571 -.00175319 2.4933445e-07 .0000239913 .0035094596 0 2004 9 4 1
      1413292500 -.004680584 0 0 0 .003843798 .0001654453 1 -.000898787 -.002598702 -.001998402 -.002298507 -7.743805e-07 -.000017991 0 0 2004 9 4 1
      1417345200 -.004871768 1 1 0 .009335656 .0000644618 1 -.001053892 -.002204861 -.001916995 -.001916995 -3.140431e-07 -.000045481 0 -.004871768 2004 9 4 1
      1419330600 .003618654 0 0 0 .012401088 .0000369356 1 .0001852195 .0003704047 -.000185254 -.000463199 1.3365715e-07 .0000448752 0 0 2004 9 4 1
      1420636500 .0031019435 0 0 0 .007075786 .0000548776 1 -.000375481 0 -.000657184 -.000187723 1.702272e-07 .0000219487 0 0 2005 9 4 1
      1423660500 .0028815104 1 0 1 .0022702336 .0001600967 1 -.000093123 .000930752 .000837716 .000930752 4.613203e-07 6.541702e-06 .0028815104 0 2005 9 4 1
      1429704000 .003854331 0 0 0 .012883082 .0000447278 1 0 -.000493316 -.000789422 .0000986339 1.7239584e-07 .0000496557 0 0 2005 9 4 1
      1431959400 .005445365 0 0 0 .007734918 .0001056333 1 .0006785905 .0000969697 .0008723889 .0027116034 5.75212e-07 .0000421195 0 0 2005 9 4 1
      1433673900 -.002681223 0 0 0 .0044648093 .0000785257 1 .0005751534 .0012457478 .0012457478 .0010541953 -2.105449e-07 -.000011971 0 0 2005 9 4 1
      1442846700 -.004282254 1 0 1 .02837305 .0000215845 1 -.000954108 .0001907123 -.00009537 -.00066778 -9.24302e-08 -.000121501 -.004282254 0 2005 9 4 1
      1447157700 .004161156 0 0 0 .006179854 .0000887079 1 -.00009438 .0005660912 -.00009438 0 3.691276e-07 .0000257153 0 0 2005 9 4 1
      1448287200 .003601611 1 1 0 .017330673 .0000267424 1 -.0003688 -.00055325 .0014738396 .002117962 9.631572e-08 .0000624183 0 .003601611 2005 9 4 1
      1448546400 -.00210671 0 0 0 1.3959462e-06 .19316983 1 0 0 0 0 -.000406953 -2.940854e-09 0 0 2005 9 4 1
      1452165300 .00320851 0 0 0 .008755909 .0000470455 1 -.000274612 -.000640879 -.000457729 .0004575193 1.5094606e-07 .0000280934 0 0 2006 9 4 1
      1455030000 .003229232 0 0 0 .007246431 .0000771023 1 .0000921107 .001104769 .000368392 -.000275138 2.489811e-07 .0000234004 0 0 2006 9 4 1
      1470138300 .002947878 0 0 0 .006369574 .0000732642 1 -.000356856 -.00044609 -.000535332 -.000624582 2.159739e-07 .0000187767 0 0 2006 9 4 1
      1471959000 -.002128226 0 0 0 .00466404 .0000718888 1 .0000887666 .0004437542 .0003550191 .0003550191 -1.529957e-07 -9.926133e-06 0 0 2006 9 4 1
      1486477800 -.002370606 0 0 0 .014099944 .000031972 1 0 .0004745709 .0007117719 .0012650223 -7.579307e-08 -.000033425 0 0 2007 9 4 1
      1487066400 .003071473 1 1 0 .02845874 .0000203996 1 .00086461 .0013359006 .0012573675 .0019639428 6.265684e-08 .0000874103 0 .003071473 2007 9 4 1
      1488362400 .01078777 0 0 0 .06282102 .0000230377 1 -.001639479 .0003275735 .0008187326 .0009823987 2.4852562e-07 .0006776987 0 0 2007 9 4 1
      1490105700 .00649353 1 0 1 .03774098 .0000228004 1 .003634165 .006853484 .0053256 .004681581 1.4805497e-07 .0002450722 .00649353 0 2007 9 4 1
      1495548900 -.003843884 0 0 0 .030186765 .0000197486 1 -.000148137 .0020716195 .0014062098 .001923789 -7.59114e-08 -.000116034 0 0 2007 9 4 1
      1505744100 .008348466 1 0 1 .08292532 .0000130732 1 .005283253 .005649111 .007768452 .0083523085 1.0914132e-07 .0006922992 .008348466 0 2007 9 4 1
      1505745000 .005283253 1 0 1 .029140266 .0000234194 1 .0003658581 .0024851996 .003069056 .004818583 1.237308e-07 .0001539554 .005283253 0 2007 9 4 1
      1513001700 -.013813485 1 0 1 .05747906 .0000302279 1 -.004499362 -.003169575 -.006422553 -.007683253 -4.175526e-07 -.000793986 -.013813485 0 2007 9 4 1
      1521470700 .012411362 0 0 0 .02291444 .000066232 1 -.001299229 -.00081182 .003402466 .005583668 8.22029e-07 .0002843994 0 0 2008 9 4 1
      1531052100 -.009580487 1 1 0 .02831669 .0000410771 1 .0009799992 -.004198121 -.004198121 -.00590025 -3.93539e-07 -.000271288 0 -.009580487 2008 9 4 1
      1535543100 .004969167 1 0 1 .020635547 .0000254174 1 -.000769461 -.000085466 -.002138855 -.001796331 1.2630333e-07 .0001025415 .004969167 0 2008 9 4 1
      1540914300 -.0411771 1 0 1 .0582493 .000093018 1 .0015195922 -.00479095 -.00370008 -.010702297 -3.830213e-06 -.002398538 -.0411771 0 2008 9 4 1
      1553004900 .017115168 1 0 1 .0579775 .0000381572 1 .006517996 .01051375 .005463402 -.001738549 6.53067e-07 .0009922946 .017115168 0 2009 9 4 1
      1558951200 .013039242 1 0 1 .023347467 .0000785699 1 .0011877897 .003322262 .003559145 .003559145 1.0244919e-06 .0003044333 .013039242 0 2009 9 4 1
      1564587000 -.006320769 1 0 1 .010561282 .0000795762 1 -.002064796 .0024065566 .0010999363 .0005550069 -5.029826e-07 -.000066755 -.006320769 0 2009 9 4 1
      1566301500 .007775993 0 0 0 .012666102 .0000827582 1 .0021571761 .0019265407 .001282626 .0019265407 6.435269e-07 .0000984915 0 0 2009 9 4 1
      1566468000 .00761264 1 0 1 .02406915 .0000417781 1 -.00134913 -.000105335 -.000948417 -.001030655 3.180419e-07 .0001832298 .00761264 0 2009 9 4 1
      1582724700 .004680652 0 0 0 .005447635 .0001032086 1 .0002918146 .000097281 .0007779831 .0011667866 4.8308374e-07 .0000254985 0 0 2010 9 4 1
      1588508100 .0044043036 0 0 0 .00724852 .0000661021 1 .0002779883 .0015235026 .0005379719 .0007172315 2.911336e-07 .0000319247 0 0 2010 9 4 1
      1590669000 -.008390273 0 0 0 .021246705 .0000485192 1 .001478634 .0019710266 .002955085 .0022663458 -4.070893e-07 -.000178266 0 0 2010 9 4 1
      1599655500 -.00363776 1 0 1 .0016104304 .0002860273 1 -.001439609 -.001919939 -.000863516 -.002208248 -1.040499e-06 -5.85836e-06 -.00363776 0 2010 9 4 1
      1600697700 .0038091745 1 0 1 .020271165 .0000230063 1 .003332719 .001667748 .0002781512 -.000092734 8.763505e-08 .0000772164 .0038091745 0 2010 9 4 1
      1605693600 .004736593 1 0 1 .01901568 .0000309567 1 .0008911862 -8.915874e-06 .0006239137 .000534807 1.466293e-07 .0000900695 .004736593 0 2010 9 4 1
      1607352300 -.004027321 1 1 0 .005924384 .0000859428 1 .0004385388 -.000615155 -.001220149 -.002459378 -3.461192e-07 -.000023859 0 -.004027321 2010 9 4 1
      1608639300 .0016438256 0 0 0 .0009983571 .0002052296 1 -.000173021 -.00008652 -.000346047 -.000475835 3.3736166e-07 1.641125e-06 0 0 2010 9 4 1
      1609845300 .002484048 1 0 1 .005635424 .0000509663 1 .0004276611 .0005131714 .0011115387 .001790679 1.2660269e-07 .0000139987 .002484048 0 2011 9 4 1
      1610370000 -.002932126 1 0 1 .006438194 .0000528493 1 .0007038083 .0003605491 -.00118556 -.001184718 -1.549607e-07 -.000018878 -.002932126 0 2011 9 4 1
      1637152200 -.008472347 1 1 0 .012690263 .0000599953 1 .000765534 .0000510538 .0002552431 -.001788528 -5.08301e-07 -.000107516 0 -.008472347 2011 9 4 1
      1639926000 -.005782821 0 0 0 .005653465 .0000997705 1 -.00017059 .0000852842 .0011081278 .006969319 -5.769547e-07 -.000032693 0 0 2011 9 4 1
      1645613100 .0023180353 1 1 0 .006664625 .0000303866 1 .0005401026 .0002315083 .0002315083 .0002315083 7.043727e-08 .0000154488 0 .0023180353 2012 9 4 1
      1646923500 -.002089541 0 0 0 .005627813 .0000325608 1 .0001549307 .0003098373 .0003098373 .0007744134 -6.803713e-08 -.00001176 0 0 2012 9 4 1
      1647270000 .0045807976 1 1 0 .0113868 .0000346936 1 .0004577654 .0026636766 .003575046 .0046367785 1.5892456e-07 .0000521606 0 .0045807976 2012 9 4 1
      1651671000 -.003869357 1 1 0 .002476784 .0001353481 1 .0005768006 .0002280242 .000607949 .0003040207 -5.237103e-07 -9.583563e-06 0 -.003869357 2012 9 4 1
      1666788300 .003221109 1 0 1 .0018026686 .0001598502 1 -.000842621 .0000765667 -.000153151 -.000306326 5.1489485e-07 5.806591e-06 .003221109 0 2012 9 4 1
      1667820600 .004763156 1 0 1 .0044303727 .0001028373 1 .0002342018 .0009893177 .0009138318 .0003852706 4.8983026e-07 .0000211026 .004763156 0 2012 9 4 1
      1675077300 .0025908614 1 0 1 .005799945 .0000384321 1 -.000359441 -.000431344 -.000467298 -.000575167 9.957238e-08 .0000150269 .0025908614 0 2013 9 4 1
      1687274100 .0028316504 1 1 0 .006221499 .0000365477 1 -.002436055 -.006531443 -.009003632 -.009271263 1.034902e-07 .0000176171 0 .0028316504 2013 9 4 1
      1687275900 -.004095389 1 1 0 .01415021 .0000233929 1 -.002472189 -.002739819 -.004950505 -.006561352 -9.580295e-08 -.000057951 0 -.004095389 2013 9 4 1
      1693826100 -.005050686 1 0 1 .005134963 .0000847086 1 .0001350165 .0001350165 -.000135035 0 -4.278366e-07 -.000025935 -.005050686 0 2013 9 4 1
      1697535900 .006152657 1 1 0 .01063967 .0000498193 1 -.000783341 -.001109914 -.001044591 .001173862 3.065208e-07 .0000654623 0 .006152657 2013 9 4 1
      1700575200 -.003955925 1 1 0 .01922421 .0000177132 1 .001383213 -.000692325 -.000503461 -.002646004 -7.0072e-08 -.00007605 0 -.003955925 2013 9 4 1
      1703512800 -.003315124 0 0 0 2.7513344e-08 10.192854 1 0 0 0 0 -.03379057 -9.12101e-11 0 0 2013 9 4 1
      1703519100 .003744058 0 0 0 4.1270014e-06 .07645772 1 .0011606953 .001771047 .0010996397 .0014659177 .0002862622 1.5451734e-08 0 0 2013 9 4 1
      1705921200 -.005110561 1 0 1 .013698936 .0000295595 1 .0006747432 .0007973748 .0004429282 -.00012273 -1.510658e-07 -.000070009 -.005110561 0 2014 9 4 1
      end

      Comment


      • #4
        I believe the following will do what you ask:

        Code:
        capture program drop one_regression
        program define one_regression
            xtset etfnumid dt
            local ind_vars  ret int_retsche int_retunsche sche unsche int_retto ///
                to int_retami ami
            capture xtreg dep_var `ind_vars' i.year if jump ==1, fe vce(robust)
                
            if c(rc) == 0 {
                local groupid = groupid[1]
                gen rank = `groupid'[1]
               
                 foreach v of varlist `ind_vars' {
                    gen b_`v' = _b[`v']
                 }
                 foreach v of varlist `ind_vars' {
                    gen se_`v' = _se[`v']
                }
                gen r2_w = e(r2_w)
                gen n_obs = e(N)
        
                keep rank b_* se_* r2_w n_obs regid depid groupid
                order rank b_* se_* r2_w n_obs regid depid groupid
                
            }
            else if inlist(c(rc), 2000, 2001) {
                gen problem = "No or insufficient observations"
                keep problem regid depid groupid
            }
            else {
                gen problem = "Unexpected error in -xtreg-"
                keep problem regid depid groupid
            }
        
            exit
        end
        
        
        
        use "$datapath/subtest.dta", clear
        
        gen dep_var = .
        forvalues i = 1/4 {
            replace dep_var = ret1_`i'
            foreach groupid in aumrank amirank {
                preserve
                gen groupid = "`groupid'"
                gen regid = 5
                gen depid = `i'
                runby one_regression, by(`groupid')
                capture order problem, last
                if !inlist(c(rc), 0, 111) {
                    display as error "Unexpected difficulty ordering variables"
                    error c(rc)
                }
                save group_`i'_`groupid'_5_toami, replace
                restore
            }
        }
        Note that you do not need to calculate the standard errors from e(V), nor extract the coefficients from e(b). Although both are possible and will give correct results, it is much simpler to get them from the vectors _b and _se that are created by any Stata estimation command.

        Comment


        • #5
          Thanks again, Clyde. That works really well!

          Comment

          Working...
          X