Announcement

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

  • Residual extraction by com_id

    From the appended dataset, I require monthly residuals series for each company (identified by a unique com_id) by regressing the monthly rt minus rf of each company on their monthly market and industry. The market and industry for each company are calculated as below:

    1. The market is the cross-sectional average of rt of all companies in the sample minus the rf. The market variable is the same for all companies.
    2. The industry for a company is the cross-sectional average of rt of all companies in a given industry type (identified by ind_id), excluding the rt of the company whose industry is to be calculated. The industry is therefore unique for each company.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(com_id ind_id) str52 company_name str30 industry_type int date float mdate double pr float(rt rf)
     1 1 "3M India Ltd."                      "chemical and chemical products" 18353 603  2311.7   .03894294       .0269
     1 1 "3M India Ltd."                      "chemical and chemical products" 18383 604 2523.25   .09151274  -.06165488
     1 1 "3M India Ltd."                      "chemical and chemical products" 18414 605  2772.7    .0988606 -.000396616
     1 1 "3M India Ltd."                      "chemical and chemical products" 18444 606 3497.65   .26145995   .03200635
     1 1 "3M India Ltd."                      "chemical and chemical products" 18475 607 3305.15  -.05503695  .017044727
     2 5 "A B B India Ltd."                   "machinery"                      18353 603   791.5  -.04661527       .0269
     2 5 "A B B India Ltd."                   "machinery"                      18383 604  857.05   .08281744  -.06165488
     2 5 "A B B India Ltd."                   "machinery"                      18414 605  862.15  .005950645 -.000396616
     2 5 "A B B India Ltd."                   "machinery"                      18444 606  812.05  -.05811054   .03200635
     2 5 "A B B India Ltd."                   "machinery"                      18475 607     777  -.04316237  .017044727
     3 2 "A C C Ltd."                         "construction material"          18353 603  906.15   -.0466095       .0269
     3 2 "A C C Ltd."                         "construction material"          18383 604     817  -.09838327  -.06165488
     3 2 "A C C Ltd."                         "construction material"          18414 605   874.5  .070379436 -.000396616
     3 2 "A C C Ltd."                         "construction material"          18444 606  831.45  -.04922813   .03200635
     3 2 "A C C Ltd."                         "construction material"          18475 607   871.8   .04852968  .017044727
     4 4 "A D F Foods Ltd."                   "food and agro based products"   18353 603    95.7  .025174076       .0269
     4 4 "A D F Foods Ltd."                   "food and agro based products"   18383 604    73.6     -.23093  -.06165488
     4 4 "A D F Foods Ltd."                   "food and agro based products"   18414 605    81.3   .10461956 -.000396616
     4 4 "A D F Foods Ltd."                   "food and agro based products"   18444 606   75.65  -.06949569   .03200635
     4 4 "A D F Foods Ltd."                   "food and agro based products"   18475 607   69.55   -.0806345  .017044727
     5 7 "A G I Greenpac Ltd."                "miscellaneous"                  18353 603    87.1   .12459651       .0269
     5 7 "A G I Greenpac Ltd."                "miscellaneous"                  18383 604    86.5 -.006888634  -.06165488
     5 7 "A G I Greenpac Ltd."                "miscellaneous"                  18414 605   92.95   .07456648 -.000396616
     5 7 "A G I Greenpac Ltd."                "miscellaneous"                  18444 606  122.05   .31307155   .03200635
     5 7 "A G I Greenpac Ltd."                "miscellaneous"                  18475 607  130.25   .06718558  .017044727
     6 6 "A I A Engineering Ltd."             "material and material products" 18353 603   392.9 -.015411602       .0269
     6 6 "A I A Engineering Ltd."             "material and material products" 18383 604   401.3  .021379486  -.06165488
     6 6 "A I A Engineering Ltd."             "material and material products" 18414 605   383.1   -.0453526 -.000396616
     6 6 "A I A Engineering Ltd."             "material and material products" 18444 606  357.85  -.06590968   .03200635
     6 6 "A I A Engineering Ltd."             "material and material products" 18475 607  361.45   .01006008  .017044727
     7 8 "A I Champdany Inds. Ltd."           "textile"                        18353 603    29.6   .23333333       .0269
     7 8 "A I Champdany Inds. Ltd."           "textile"                        18383 604      29  -.02027027  -.06165488
     7 8 "A I Champdany Inds. Ltd."           "textile"                        18414 605   33.05   .13965517 -.000396616
     7 8 "A I Champdany Inds. Ltd."           "textile"                        18444 606    35.9   .08623298   .03200635
     7 8 "A I Champdany Inds. Ltd."           "textile"                        18475 607    39.3   .09470752  .017044727
     8 1 "A M D Industries Ltd."              "chemical and chemical products" 18353 603   25.65   .11038961       .0269
     8 1 "A M D Industries Ltd."              "chemical and chemical products" 18383 604   23.55  -.08187135  -.06165488
     8 1 "A M D Industries Ltd."              "chemical and chemical products" 18414 605    22.8 -.031847134 -.000396616
     8 1 "A M D Industries Ltd."              "chemical and chemical products" 18444 606      25   .09649123   .03200635
     8 1 "A M D Industries Ltd."              "chemical and chemical products" 18475 607    25.5         .02  .017044727
     9 6 "A P L Apollo Tubes Ltd."            "material and material products" 18353 603   12.27   .10341727       .0269
     9 6 "A P L Apollo Tubes Ltd."            "material and material products" 18383 604   11.27   -.0814996  -.06165488
     9 6 "A P L Apollo Tubes Ltd."            "material and material products" 18414 605   10.75  -.04614019 -.000396616
     9 6 "A P L Apollo Tubes Ltd."            "material and material products" 18444 606   12.02   .11813954   .03200635
     9 6 "A P L Apollo Tubes Ltd."            "material and material products" 18475 607   13.39    .1139767  .017044727
    10 8 "A P M Industries Ltd."              "textile"                        18353 603    9.52  -.04225352       .0269
    10 8 "A P M Industries Ltd."              "textile"                        18383 604    9.78  .027310925  -.06165488
    10 8 "A P M Industries Ltd."              "textile"                        18414 605   11.36    .1615542 -.000396616
    10 8 "A P M Industries Ltd."              "textile"                        18444 606   11.37 .0008802817   .03200635
    10 8 "A P M Industries Ltd."              "textile"                        18475 607   14.05     .235708  .017044727
    11 8 "A Y M Syntex Ltd."                  "textile"                        18353 603   16.12   .30844155       .0269
    11 8 "A Y M Syntex Ltd."                  "textile"                        18383 604    15.1 -.063275434  -.06165488
    11 8 "A Y M Syntex Ltd."                  "textile"                        18414 605    14.8  -.01986755 -.000396616
    11 8 "A Y M Syntex Ltd."                  "textile"                        18444 606   14.73  -.00472973   .03200635
    11 8 "A Y M Syntex Ltd."                  "textile"                        18475 607    16.8   .14052953  .017044727
    12 1 "Aarti Drugs Ltd."                   "chemical and chemical products" 18353 603   14.71   .15191856       .0269
    12 1 "Aarti Drugs Ltd."                   "chemical and chemical products" 18383 604   14.95  .016315432  -.06165488
    12 1 "Aarti Drugs Ltd."                   "chemical and chemical products" 18414 605   16.79   .12307692 -.000396616
    12 1 "Aarti Drugs Ltd."                   "chemical and chemical products" 18444 606   17.83   .06194163   .03200635
    12 1 "Aarti Drugs Ltd."                   "chemical and chemical products" 18475 607   17.77 -.003365115  .017044727
    13 1 "Aarti Industries Ltd."              "chemical and chemical products" 18353 603   12.28  .035413153       .0269
    13 1 "Aarti Industries Ltd."              "chemical and chemical products" 18383 604   11.94 -.027687296  -.06165488
    13 1 "Aarti Industries Ltd."              "chemical and chemical products" 18414 605   12.29   .02931323 -.000396616
    13 1 "Aarti Industries Ltd."              "chemical and chemical products" 18444 606   12.86   .04637917   .03200635
    13 1 "Aarti Industries Ltd."              "chemical and chemical products" 18475 607   13.63   .05987558  .017044727
    14 8 "Aarvee Denims & Exports Ltd."       "textile"                        18353 603   63.65  -.06190125       .0269
    14 8 "Aarvee Denims & Exports Ltd."       "textile"                        18383 604    57.1  -.10290652  -.06165488
    14 8 "Aarvee Denims & Exports Ltd."       "textile"                        18414 605    58.7  .028021015 -.000396616
    14 8 "Aarvee Denims & Exports Ltd."       "textile"                        18444 606   64.25   .09454855   .03200635
    14 8 "Aarvee Denims & Exports Ltd."       "textile"                        18475 607    85.1    .3245136  .017044727
    15 1 "Abbott India Ltd."                  "chemical and chemical products" 18353 603  936.25   .05886677       .0269
    15 1 "Abbott India Ltd."                  "chemical and chemical products" 18383 604 1019.55   .08897197  -.06165488
    15 1 "Abbott India Ltd."                  "chemical and chemical products" 18414 605 1141.45   .11956255 -.000396616
    15 1 "Abbott India Ltd."                  "chemical and chemical products" 18444 606  1138.4  -.00267204   .03200635
    15 1 "Abbott India Ltd."                  "chemical and chemical products" 18475 607  1129.7 -.007642305  .017044727
    16 8 "Acknit Industries Ltd."             "textile"                        18353 603   29.45  -.02644628       .0269
    16 8 "Acknit Industries Ltd."             "textile"                        18383 604    30.2  .025466893  -.06165488
    16 8 "Acknit Industries Ltd."             "textile"                        18414 605    28.8  -.04635762 -.000396616
    16 8 "Acknit Industries Ltd."             "textile"                        18444 606      29  .006944444   .03200635
    16 8 "Acknit Industries Ltd."             "textile"                        18475 607   34.15    .1775862  .017044727
    17 5 "Action Construction Equipment Ltd." "machinery"                      18353 603    50.2   .14742857       .0269
    17 5 "Action Construction Equipment Ltd." "machinery"                      18383 604   48.45  -.03486056  -.06165488
    17 5 "Action Construction Equipment Ltd." "machinery"                      18414 605      49   .01135191 -.000396616
    17 5 "Action Construction Equipment Ltd." "machinery"                      18444 606    56.4    .1510204   .03200635
    17 5 "Action Construction Equipment Ltd." "machinery"                      18475 607    65.1    .1542553  .017044727
    18 5 "Ador Fontech Ltd."                  "machinery"                      18353 603   25.68    .4665905       .0269
    18 5 "Ador Fontech Ltd."                  "machinery"                      18383 604   26.33  .025311526  -.06165488
    18 5 "Ador Fontech Ltd."                  "machinery"                      18414 605   29.11   .10558298 -.000396616
    18 5 "Ador Fontech Ltd."                  "machinery"                      18444 606   26.34   -.0951563   .03200635
    18 5 "Ador Fontech Ltd."                  "machinery"                      18475 607   29.81    .1317388  .017044727
    19 5 "Ador Welding Ltd."                  "machinery"                      18353 603   181.6   .08095238       .0269
    19 5 "Ador Welding Ltd."                  "machinery"                      18383 604  188.15  .036068283  -.06165488
    19 5 "Ador Welding Ltd."                  "machinery"                      18414 605  212.75   .13074675 -.000396616
    19 5 "Ador Welding Ltd."                  "machinery"                      18444 606  211.75 -.004700352   .03200635
    19 5 "Ador Welding Ltd."                  "machinery"                      18475 607  203.25  -.04014168  .017044727
    20 4 "Agro Tech Foods Ltd."               "food and agro based products"   18353 603   265.2    .0732497       .0269
    20 4 "Agro Tech Foods Ltd."               "food and agro based products"   18383 604  234.95  -.11406486  -.06165488
    20 4 "Agro Tech Foods Ltd."               "food and agro based products"   18414 605  328.25   .39710575 -.000396616
    20 4 "Agro Tech Foods Ltd."               "food and agro based products"   18444 606   298.6   -.0903275   .03200635
    20 4 "Agro Tech Foods Ltd."               "food and agro based products"   18475 607   260.2  -.12860014  .017044727
    end
    format %td date
    format %tm mdate

  • #2
    What is unclear to me is whether you envision a single regression involving all companies--this is what the text of your post describes. But the title of the thread, extraction by com_id suggests you may really want a separate regression for each company. The code below does both. You should delete the code for the one you don't want.

    Code:
    //  CALCULATE CROSS-SECTIONAL AVERAGE OF RT FOR ALL COMPANIES
    by mdate, sort: egen avg_rt_this_month = mean(rt)
    
    //  CALCULATE REGRESSION VARIABLES
    gen market_var = avg_rt_this_month - rf
    rangestat (mean) industry_var = rt, by(ind_id) excludeself interval(rt . .)
    gen outcome_var = rt - rf
    
    //  DO THE REGRESSION AND CALCULATE RESIDUALS
    //  METHOD 1: A SINGLE REGRESSION FOR THE ENTIRE DATA SET
    regress outcome_var market_var industry_var
    predict residual1, resid
    
    //  METHOD 2: A SEPARATE REGRESSION FOR EACH COMPANY
    capture program drop one_com_id
    program define one_com_id
        regress outcome_var market_var industry_var
        predict residual2, resid
        exit
    end
    runby one_com_id, by(com_id)
    -runby- is by Robert Picard and me, and is available from SSC. -rangestat- is by Robert Picard, Nick Cox, and Roberto Ferrer, also available from SSC.

    Comment


    • #3
      I need a modification to the one_com_id program part of the below code.

      Step 1: For each year, run regressions as specified in #1 using previous 36 months of data. From the regression coefficients at the beginning of the year, calculate expected value of stock returns for each month and then subtract actual returns for each month from the expected stock returns to obtain the idiosyncratic returns for each month in the year.

      Step 2: Then in next step collapse these monthly idiosyncratic returns for each year for each firm into a mean value.

      This will give rise to yearly cross sectional idiosyncratic returns of firms eventually.

      Code:
      //  CALCULATE CROSS-SECTIONAL AVERAGE OF RT FOR ALL COMPANIES
      by mdate, sort: egen avg_rt_this_month = mean(rt)
      
      //  CALCULATE REGRESSION VARIABLES
      gen market_var = avg_rt_this_month - rf
      rangestat (mean) avg_industry_var = rt, by(ind_id) excludeself interval(rt . .)
      gen industry_var= avg_industry_var-rf
      gen outcome_var = rt - rf
      
      //  METHOD 2: A SEPARATE REGRESSION FOR EACH COMPANY
      capture program drop one_com_id
      program define one_com_id
          regress outcome_var market_var industry_var
          predict residual2, resid
          exit
      end
      runby one_com_id, by(com_id)
      Last edited by Sartaj Hussain; 18 Apr 2023, 04:10.

      Comment


      • #4
        To work this out, I will need a new data example that covers at least 48 months of data. It will be sufficient to do that for a small number of firms in the same industry.

        In addition, there are a few things I don't understand. The regressions done in the code in #3 have rt - rf as their outcome variable. Do you still want to use that, or are you specifically interested in rt as the outcome? If we are using rt - rf as the outcome, then we would be able to predict rt - rf, not rt. Is that what you want?

        I'm not sure what you mean by using the coefficients "at the beginning of the year." From what you have described, the coefficients will not change during the course of the year--they will be calculated separately for each year based on the previous 36 months of data, but there will just be a single set of coefficients for a given year..

        Comment


        • #5
          First of all in #3,
          Step 2: Then in next step collapse these monthly idiosyncratic returns for each year for each firm into a mean value.
          should be read as: Then in next step collapse these monthly idiosyncratic returns for each year for each firm into two variables: a simple arithmetic mean value and a geometric mean value.


          In addition, there are a few things I don't understand. The regressions done in the code in #3 have rt - rf as their outcome variable. Do you still want to use that, or are you specifically interested in rt as the outcome? If we are using rt - rf as the outcome, then we would be able to predict rt - rf, not rt. Is that what you want?
          Yes the outcome variable should be rt and not rt minus rf.

          I'm not sure what you mean by using the coefficients "at the beginning of the year." From what you have described, the coefficients will not change during the course of the year--they will be calculated separately for each year based on the previous 36 months of data, but there will just be a single set of coefficients for a given year..
          Yes you have correctly interpreted it. For each year, coefficients at the beginning of the year mean coefficients based on previous 36 months of data and these coefficients will not change during the course of the year but will be calculated separately for each year.

          To work this out, I will need a new data example that covers at least 48 months of data. It will be sufficient to do that for a small number of firms in the same industry.
          Here is the new example data.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(com_id ind_id) str52 company_name str30 industry_type int date float mdate double pr float(rt rf)
           7 8 "A I Champdany Inds. Ltd."     "textile" 18353 603  29.6   .23333333       .0269
           7 8 "A I Champdany Inds. Ltd."     "textile" 18383 604    29  -.02027027  -.06165488
           7 8 "A I Champdany Inds. Ltd."     "textile" 18414 605 33.05   .13965517 -.000396616
           7 8 "A I Champdany Inds. Ltd."     "textile" 18444 606  35.9   .08623298   .03200635
           7 8 "A I Champdany Inds. Ltd."     "textile" 18475 607  39.3   .09470752  .017044727
           7 8 "A I Champdany Inds. Ltd."     "textile" 18506 608  40.5   .03053435 -.010584677
           7 8 "A I Champdany Inds. Ltd."     "textile" 18536 609  39.1   -.0345679  .034258787
           7 8 "A I Champdany Inds. Ltd."     "textile" 18567 610    34  -.13043478 -.006772565
           7 8 "A I Champdany Inds. Ltd."     "textile" 18597 611 35.95   .05735294  -.01896851
           7 8 "A I Champdany Inds. Ltd."     "textile" 18628 612  28.6   -.2044506  .029697966
           7 8 "A I Champdany Inds. Ltd."     "textile" 18659 613 23.75   -.1695804 -.019145804
           7 8 "A I Champdany Inds. Ltd."     "textile" 18687 614 24.65   .03789474 -.000875876
           7 8 "A I Champdany Inds. Ltd."     "textile" 18718 615  21.7  -.11967546   .01878522
           7 8 "A I Champdany Inds. Ltd."     "textile" 18748 616  23.5    .0829493  .033927474
           7 8 "A I Champdany Inds. Ltd."     "textile" 18779 617  21.8  -.07234043 -.010105814
           7 8 "A I Champdany Inds. Ltd."     "textile" 18809 618  28.8    .3211009   .01537353
           7 8 "A I Champdany Inds. Ltd."     "textile" 18840 619 30.75  .067708336 -.015968772
           7 8 "A I Champdany Inds. Ltd."     "textile" 18871 620 27.55  -.10406504   .01478543
           7 8 "A I Champdany Inds. Ltd."     "textile" 18901 621  21.7   -.2123412   .05176498
           7 8 "A I Champdany Inds. Ltd."     "textile" 18932 622  19.3  -.11059908 -.015880167
           7 8 "A I Champdany Inds. Ltd."     "textile" 18962 623 17.05   -.1165803 -.020370794
           7 8 "A I Champdany Inds. Ltd."     "textile" 18993 624 18.55   .08797654 -.034228973
           7 8 "A I Champdany Inds. Ltd."     "textile" 19024 625  17.3  -.06738544 -.008225475
           7 8 "A I Champdany Inds. Ltd."     "textile" 19053 626    17  -.01734104   .04549335
           7 8 "A I Champdany Inds. Ltd."     "textile" 19084 627  17.2  .011764706   .01154923
           7 8 "A I Champdany Inds. Ltd."     "textile" 19114 628  24.3    .4127907 -.033906125
           7 8 "A I Champdany Inds. Ltd."     "textile" 19145 629 24.15  -.00617284  .000358123
           7 8 "A I Champdany Inds. Ltd."     "textile" 19175 630 20.75  -.14078675 -.016109785
           7 8 "A I Champdany Inds. Ltd."     "textile" 19206 631  19.8  -.04578313 -.000485143
           7 8 "A I Champdany Inds. Ltd."     "textile" 19237 632    20   .01010101 -.011163694
           7 8 "A I Champdany Inds. Ltd."     "textile" 19267 633 18.55      -.0725  .008099153
           7 8 "A I Champdany Inds. Ltd."     "textile" 19298 634  20.8    .1212938 -.004869142
           7 8 "A I Champdany Inds. Ltd."     "textile" 19328 635  24.7       .1875 -.015412844
           7 8 "A I Champdany Inds. Ltd."     "textile" 19359 636  21.3  -.13765182 -.017020749
           7 8 "A I Champdany Inds. Ltd."     "textile" 19390 637  19.7  -.07511737 -.005055612
           7 8 "A I Champdany Inds. Ltd."     "textile" 19418 638  20.5   .04060914   .01003557
           7 8 "A I Champdany Inds. Ltd."     "textile" 19449 639    20 -.024390243 -.027669476
           7 8 "A I Champdany Inds. Ltd."     "textile" 19479 640  20.4         .02  -.03725262
           7 8 "A I Champdany Inds. Ltd."     "textile" 19510 641  19.9 -.024509804 -.000537418
           7 8 "A I Champdany Inds. Ltd."     "textile" 19540 642 17.75   -.1080402   .09853475
           7 8 "A I Champdany Inds. Ltd."     "textile" 19571 643    17  -.04225352   .05188448
           7 8 "A I Champdany Inds. Ltd."     "textile" 19602 644  17.3   .01764706   .01977664
           7 8 "A I Champdany Inds. Ltd."     "textile" 19632 645  17.2 -.005780347 -.016084872
           7 8 "A I Champdany Inds. Ltd."     "textile" 19663 646  18.5    .0755814   .04857971
           7 8 "A I Champdany Inds. Ltd."     "textile" 19693 647 18.45 -.002702703   -.0247678
           7 8 "A I Champdany Inds. Ltd."     "textile" 19724 648  17.2  -.06775068 -.005668934
           7 8 "A I Champdany Inds. Ltd."     "textile" 19755 649  18.2   .05813954  .010490308
           7 8 "A I Champdany Inds. Ltd."     "textile" 19783 650  15.6  -.14285715 -.006544798
          10 8 "A P M Industries Ltd."        "textile" 18353 603  9.52  -.04225352       .0269
          10 8 "A P M Industries Ltd."        "textile" 18383 604  9.78  .027310925  -.06165488
          10 8 "A P M Industries Ltd."        "textile" 18414 605 11.36    .1615542 -.000396616
          10 8 "A P M Industries Ltd."        "textile" 18444 606 11.37 .0008802817   .03200635
          10 8 "A P M Industries Ltd."        "textile" 18475 607 14.05     .235708  .017044727
          10 8 "A P M Industries Ltd."        "textile" 18506 608 17.16    .2213523 -.010584677
          10 8 "A P M Industries Ltd."        "textile" 18536 609 20.47   .19289044  .034258787
          10 8 "A P M Industries Ltd."        "textile" 18567 610 16.69   -.1846605 -.006772565
          10 8 "A P M Industries Ltd."        "textile" 18597 611 17.82  .067705214  -.01896851
          10 8 "A P M Industries Ltd."        "textile" 18628 612  16.5 -.074074075  .029697966
          10 8 "A P M Industries Ltd."        "textile" 18659 613 14.45  -.12424242 -.019145804
          10 8 "A P M Industries Ltd."        "textile" 18687 614  14.4 -.003460208 -.000875876
          10 8 "A P M Industries Ltd."        "textile" 18718 615 14.84  .030555556   .01878522
          10 8 "A P M Industries Ltd."        "textile" 18748 616 14.14  -.04716981  .033927474
          10 8 "A P M Industries Ltd."        "textile" 18779 617  12.3   -.1301273 -.010105814
          10 8 "A P M Industries Ltd."        "textile" 18809 618 13.62   .10731708   .01537353
          10 8 "A P M Industries Ltd."        "textile" 18840 619 11.89  -.12701909 -.015968772
          10 8 "A P M Industries Ltd."        "textile" 18871 620    12  .009251472   .01478543
          10 8 "A P M Industries Ltd."        "textile" 18901 621 12.48         .04   .05176498
          10 8 "A P M Industries Ltd."        "textile" 18932 622 12.16 -.025641026 -.015880167
          10 8 "A P M Industries Ltd."        "textile" 18962 623 10.96  -.09868421 -.020370794
          10 8 "A P M Industries Ltd."        "textile" 18993 624 12.94   .18065694 -.034228973
          10 8 "A P M Industries Ltd."        "textile" 19024 625 11.38  -.12055641 -.008225475
          10 8 "A P M Industries Ltd."        "textile" 19053 626 11.12   -.0228471   .04549335
          10 8 "A P M Industries Ltd."        "textile" 19084 627 10.32  -.07194245   .01154923
          10 8 "A P M Industries Ltd."        "textile" 19114 628 10.99   .06492248 -.033906125
          10 8 "A P M Industries Ltd."        "textile" 19145 629 10.27   -.0655141  .000358123
          10 8 "A P M Industries Ltd."        "textile" 19175 630    10 -.026290165 -.016109785
          10 8 "A P M Industries Ltd."        "textile" 19206 631    12          .2 -.000485143
          10 8 "A P M Industries Ltd."        "textile" 19237 632 15.44   .28666666 -.011163694
          10 8 "A P M Industries Ltd."        "textile" 19267 633 16.75   .08484456  .008099153
          10 8 "A P M Industries Ltd."        "textile" 19298 634 17.95   .07164179 -.004869142
          10 8 "A P M Industries Ltd."        "textile" 19328 635 18.25  .016713092 -.015412844
          10 8 "A P M Industries Ltd."        "textile" 19359 636    19   .04109589 -.017020749
          10 8 "A P M Industries Ltd."        "textile" 19390 637 18.15  -.04473684 -.005055612
          10 8 "A P M Industries Ltd."        "textile" 19418 638  16.8  -.07438017   .01003557
          10 8 "A P M Industries Ltd."        "textile" 19449 639 18.25   .08630952 -.027669476
          10 8 "A P M Industries Ltd."        "textile" 19479 640 20.35    .1150685  -.03725262
          10 8 "A P M Industries Ltd."        "textile" 19510 641 19.45  -.04422604 -.000537418
          10 8 "A P M Industries Ltd."        "textile" 19540 642  19.7   .01285347   .09853475
          10 8 "A P M Industries Ltd."        "textile" 19571 643  18.5   -.0609137   .05188448
          10 8 "A P M Industries Ltd."        "textile" 19602 644 20.65   .11621621   .01977664
          10 8 "A P M Industries Ltd."        "textile" 19632 645  24.5   .18644068 -.016084872
          10 8 "A P M Industries Ltd."        "textile" 19663 646 25.25  .030612245   .04857971
          10 8 "A P M Industries Ltd."        "textile" 19693 647  27.2   .07722772   -.0247678
          10 8 "A P M Industries Ltd."        "textile" 19724 648  23.4  -.13970588 -.005668934
          10 8 "A P M Industries Ltd."        "textile" 19755 649  22.1  -.05555556  .010490308
          10 8 "A P M Industries Ltd."        "textile" 19783 650 22.65   .02488688 -.006544798
          11 8 "A Y M Syntex Ltd."            "textile" 18353 603 16.12   .30844155       .0269
          11 8 "A Y M Syntex Ltd."            "textile" 18383 604  15.1 -.063275434  -.06165488
          11 8 "A Y M Syntex Ltd."            "textile" 18414 605  14.8  -.01986755 -.000396616
          11 8 "A Y M Syntex Ltd."            "textile" 18444 606 14.73  -.00472973   .03200635
          11 8 "A Y M Syntex Ltd."            "textile" 18475 607  16.8   .14052953  .017044727
          11 8 "A Y M Syntex Ltd."            "textile" 18506 608  20.3   .20833333 -.010584677
          11 8 "A Y M Syntex Ltd."            "textile" 18536 609  20.3           0  .034258787
          11 8 "A Y M Syntex Ltd."            "textile" 18567 610 20.65   .01724138 -.006772565
          11 8 "A Y M Syntex Ltd."            "textile" 18597 611 19.95 -.033898305  -.01896851
          11 8 "A Y M Syntex Ltd."            "textile" 18628 612  16.9   -.1528822  .029697966
          11 8 "A Y M Syntex Ltd."            "textile" 18659 613 14.85  -.12130178 -.019145804
          11 8 "A Y M Syntex Ltd."            "textile" 18687 614 13.85  -.06734007 -.000875876
          11 8 "A Y M Syntex Ltd."            "textile" 18718 615 16.09   .16173285   .01878522
          11 8 "A Y M Syntex Ltd."            "textile" 18748 616  13.8  -.14232442  .033927474
          11 8 "A Y M Syntex Ltd."            "textile" 18779 617 13.95  .010869565 -.010105814
          11 8 "A Y M Syntex Ltd."            "textile" 18809 618 14.67    .0516129   .01537353
          11 8 "A Y M Syntex Ltd."            "textile" 18840 619 11.85  -.19222903 -.015968772
          11 8 "A Y M Syntex Ltd."            "textile" 18871 620 12.31   .03881856   .01478543
          11 8 "A Y M Syntex Ltd."            "textile" 18901 621 12.05  -.02112104   .05176498
          11 8 "A Y M Syntex Ltd."            "textile" 18932 622 10.75  -.10788382 -.015880167
          11 8 "A Y M Syntex Ltd."            "textile" 18962 623 10.49 -.024186047 -.020370794
          11 8 "A Y M Syntex Ltd."            "textile" 18993 624 13.53    .2897998 -.034228973
          11 8 "A Y M Syntex Ltd."            "textile" 19024 625  12.1  -.10569106 -.008225475
          11 8 "A Y M Syntex Ltd."            "textile" 19053 626 10.69  -.11652893   .04549335
          11 8 "A Y M Syntex Ltd."            "textile" 19084 627 10.57 -.011225444   .01154923
          11 8 "A Y M Syntex Ltd."            "textile" 19114 628   9.8  -.07284768 -.033906125
          11 8 "A Y M Syntex Ltd."            "textile" 19145 629 10.03   .02346939  .000358123
          11 8 "A Y M Syntex Ltd."            "textile" 19175 630  9.36   -.0667996 -.016109785
          11 8 "A Y M Syntex Ltd."            "textile" 19206 631  9.04 -.034188036 -.000485143
          11 8 "A Y M Syntex Ltd."            "textile" 19237 632 10.41   .15154867 -.011163694
          11 8 "A Y M Syntex Ltd."            "textile" 19267 633 11.64   .11815562  .008099153
          11 8 "A Y M Syntex Ltd."            "textile" 19298 634 12.46   .07044674 -.004869142
          11 8 "A Y M Syntex Ltd."            "textile" 19328 635 13.21   .06019261 -.015412844
          11 8 "A Y M Syntex Ltd."            "textile" 19359 636 13.06 -.011355034 -.017020749
          11 8 "A Y M Syntex Ltd."            "textile" 19390 637  12.8 -.019908117 -.005055612
          11 8 "A Y M Syntex Ltd."            "textile" 19418 638   9.2     -.28125   .01003557
          11 8 "A Y M Syntex Ltd."            "textile" 19449 639  9.55   .03804348 -.027669476
          11 8 "A Y M Syntex Ltd."            "textile" 19479 640  8.75  -.08376963  -.03725262
          11 8 "A Y M Syntex Ltd."            "textile" 19510 641  7.87  -.10057143 -.000537418
          11 8 "A Y M Syntex Ltd."            "textile" 19540 642  6.25    -.205845   .09853475
          11 8 "A Y M Syntex Ltd."            "textile" 19571 643  6.99       .1184   .05188448
          11 8 "A Y M Syntex Ltd."            "textile" 19602 644     8   .14449213   .01977664
          11 8 "A Y M Syntex Ltd."            "textile" 19632 645   7.9      -.0125 -.016084872
          11 8 "A Y M Syntex Ltd."            "textile" 19663 646  7.31  -.07468355   .04857971
          11 8 "A Y M Syntex Ltd."            "textile" 19693 647  8.35   .14227086   -.0247678
          11 8 "A Y M Syntex Ltd."            "textile" 19724 648  7.71   -.0766467 -.005668934
          11 8 "A Y M Syntex Ltd."            "textile" 19755 649  8.44   .09468223  .010490308
          11 8 "A Y M Syntex Ltd."            "textile" 19783 650  7.67  -.09123223 -.006544798
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18353 603 63.65  -.06190125       .0269
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18383 604  57.1  -.10290652  -.06165488
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18414 605  58.7  .028021015 -.000396616
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18444 606 64.25   .09454855   .03200635
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18475 607  85.1    .3245136  .017044727
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18506 608  77.6   -.0881316 -.010584677
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18536 609 84.95    .0947165  .034258787
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18567 610  73.1  -.13949382 -.006772565
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18597 611 80.65   .10328317  -.01896851
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18628 612  64.5    -.200248  .029697966
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18659 613 58.75  -.08914728 -.019145804
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18687 614 56.55  -.03744681 -.000875876
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18718 615 64.15   .13439435   .01878522
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18748 616    52  -.18939984  .033927474
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18779 617  50.7       -.025 -.010105814
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18809 618  48.6  -.04142012   .01537353
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18840 619 33.75  -.30555555 -.015968772
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18871 620 36.45         .08   .01478543
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18901 621  35.5   -.0260631   .05176498
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18932 622 31.55   -.1112676 -.015880167
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18962 623  27.9  -.11568938 -.020370794
          14 8 "Aarvee Denims & Exports Ltd." "textile" 18993 624 36.05    .2921147 -.034228973
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19024 625    38   .05409154 -.008225475
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19053 626    35  -.07894737   .04549335
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19084 627 33.85 -.032857142   .01154923
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19114 628 36.05  .064992614 -.033906125
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19145 629  35.7 -.009708738  .000358123
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19175 630  33.5  -.06162465 -.016109785
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19206 631    37   .10447761 -.000485143
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19237 632 41.05   .10945946 -.011163694
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19267 633  54.5    .3276492  .008099153
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19298 634  67.1   .23119266 -.004869142
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19328 635  65.8  -.01937407 -.015412844
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19359 636  76.1   .15653495 -.017020749
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19390 637 38.25   -.4973719 -.005055612
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19418 638  35.5  -.07189543   .01003557
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19449 639    35 -.014084507 -.027669476
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19479 640  38.1   .08857143  -.03725262
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19510 641 34.35   -.0984252 -.000537418
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19540 642  35.8   .04221252   .09853475
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19571 643 32.05   -.1047486   .05188448
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19602 644    31  -.03276131   .01977664
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19632 645 35.15   .13387097 -.016084872
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19663 646  43.4    .2347084   .04857971
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19693 647  46.8   .07834101   -.0247678
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19724 648 41.35    -.116453 -.005668934
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19755 649 33.45    -.191052  .010490308
          14 8 "Aarvee Denims & Exports Ltd." "textile" 19783 650 35.85   .07174888 -.006544798
          16 8 "Acknit Industries Ltd."       "textile" 18353 603 29.45  -.02644628       .0269
          16 8 "Acknit Industries Ltd."       "textile" 18383 604  30.2  .025466893  -.06165488
          16 8 "Acknit Industries Ltd."       "textile" 18414 605  28.8  -.04635762 -.000396616
          16 8 "Acknit Industries Ltd."       "textile" 18444 606    29  .006944444   .03200635
          16 8 "Acknit Industries Ltd."       "textile" 18475 607 34.15    .1775862  .017044727
          16 8 "Acknit Industries Ltd."       "textile" 18506 608 31.25  -.08491948 -.010584677
          16 8 "Acknit Industries Ltd."       "textile" 18536 609 31.85       .0192  .034258787
          16 8 "Acknit Industries Ltd."       "textile" 18567 610  29.5  -.07378336 -.006772565
          16 8 "Acknit Industries Ltd."       "textile" 18597 611  29.8   .01016949  -.01896851
          16 8 "Acknit Industries Ltd."       "textile" 18628 612 28.55  -.04194631  .029697966
          16 8 "Acknit Industries Ltd."       "textile" 18659 613    28  -.01926445 -.019145804
          16 8 "Acknit Industries Ltd."       "textile" 18687 614 26.35  -.05892857 -.000875876
          16 8 "Acknit Industries Ltd."       "textile" 18718 615  30.5   .15749526   .01878522
          16 8 "Acknit Industries Ltd."       "textile" 18748 616  25.4   -.1672131  .033927474
          16 8 "Acknit Industries Ltd."       "textile" 18779 617  26.6   .04724409 -.010105814
          16 8 "Acknit Industries Ltd."       "textile" 18809 618 28.75   .08082706   .01537353
          16 8 "Acknit Industries Ltd."       "textile" 18840 619  28.3 -.015652174 -.015968772
          16 8 "Acknit Industries Ltd."       "textile" 18871 620 26.35  -.06890459   .01478543
          16 8 "Acknit Industries Ltd."       "textile" 18901 621  29.5    .1195446   .05176498
          16 8 "Acknit Industries Ltd."       "textile" 18932 622    25  -.15254237 -.015880167
          16 8 "Acknit Industries Ltd."       "textile" 18962 623 22.35       -.106 -.020370794
          16 8 "Acknit Industries Ltd."       "textile" 18993 624    24    .0738255 -.034228973
          16 8 "Acknit Industries Ltd."       "textile" 19024 625  24.9       .0375 -.008225475
          16 8 "Acknit Industries Ltd."       "textile" 19053 626  26.5  .064257026   .04549335
          16 8 "Acknit Industries Ltd."       "textile" 19084 627    27  .018867925   .01154923
          16 8 "Acknit Industries Ltd."       "textile" 19114 628  27.2  .007407407 -.033906125
          16 8 "Acknit Industries Ltd."       "textile" 19145 629 29.45   .08272059  .000358123
          16 8 "Acknit Industries Ltd."       "textile" 19175 630    32   .08658744 -.016109785
          16 8 "Acknit Industries Ltd."       "textile" 19206 631 26.95   -.1578125 -.000485143
          16 8 "Acknit Industries Ltd."       "textile" 19237 632 28.35   .05194805 -.011163694
          16 8 "Acknit Industries Ltd."       "textile" 19267 633 30.05   .05996473  .008099153
          16 8 "Acknit Industries Ltd."       "textile" 19298 634 26.95   -.1031614 -.004869142
          16 8 "Acknit Industries Ltd."       "textile" 19328 635  26.1 -.031539887 -.015412844
          16 8 "Acknit Industries Ltd."       "textile" 19359 636    29   .11111111 -.017020749
          16 8 "Acknit Industries Ltd."       "textile" 19390 637 26.75   -.0775862 -.005055612
          16 8 "Acknit Industries Ltd."       "textile" 19418 638 25.75  -.03738318   .01003557
          16 8 "Acknit Industries Ltd."       "textile" 19449 639 23.85  -.07378641 -.027669476
          16 8 "Acknit Industries Ltd."       "textile" 19479 640  25.5   .06918239  -.03725262
          16 8 "Acknit Industries Ltd."       "textile" 19510 641 24.25  -.04901961 -.000537418
          16 8 "Acknit Industries Ltd."       "textile" 19540 642  24.9  .026804123   .09853475
          16 8 "Acknit Industries Ltd."       "textile" 19571 643 33.25    .3353414   .05188448
          16 8 "Acknit Industries Ltd."       "textile" 19602 644  30.6  -.07969925   .01977664
          16 8 "Acknit Industries Ltd."       "textile" 19632 645  33.5   .09477124 -.016084872
          16 8 "Acknit Industries Ltd."       "textile" 19663 646  28.8   -.1402985   .04857971
          16 8 "Acknit Industries Ltd."       "textile" 19693 647  28.6 -.006944444   -.0247678
          16 8 "Acknit Industries Ltd."       "textile" 19724 648    32   .11888112 -.005668934
          16 8 "Acknit Industries Ltd."       "textile" 19755 649 29.55   -.0765625  .010490308
          16 8 "Acknit Industries Ltd."       "textile" 19783 650  33.9   .14720812 -.006544798
          28 8 "Alka India Ltd."              "textile" 18353 603    .4  .025641026       .0269
          28 8 "Alka India Ltd."              "textile" 18383 604   .36         -.1  -.06165488
          28 8 "Alka India Ltd."              "textile" 18414 605   .36           0 -.000396616
          28 8 "Alka India Ltd."              "textile" 18444 606   .36           0   .03200635
          28 8 "Alka India Ltd."              "textile" 18475 607   .37   .02777778  .017044727
          28 8 "Alka India Ltd."              "textile" 18506 608   .38   .02702703 -.010584677
          28 8 "Alka India Ltd."              "textile" 18536 609   .36  -.05263158  .034258787
          28 8 "Alka India Ltd."              "textile" 18567 610   .33  -.08333334 -.006772565
          28 8 "Alka India Ltd."              "textile" 18597 611   .34   .03030303  -.01896851
          28 8 "Alka India Ltd."              "textile" 18628 612   .31   -.0882353  .029697966
          28 8 "Alka India Ltd."              "textile" 18659 613   .28   -.0967742 -.019145804
          28 8 "Alka India Ltd."              "textile" 18687 614   .27 -.035714287 -.000875876
          28 8 "Alka India Ltd."              "textile" 18718 615   .27           0   .01878522
          28 8 "Alka India Ltd."              "textile" 18748 616   .23  -.14814815  .033927474
          28 8 "Alka India Ltd."              "textile" 18779 617   .21  -.08695652 -.010105814
          28 8 "Alka India Ltd."              "textile" 18809 618   .24   .14285715   .01537353
          28 8 "Alka India Ltd."              "textile" 18840 619   .22  -.08333334 -.015968772
          28 8 "Alka India Ltd."              "textile" 18871 620   .21  -.04545455   .01478543
          28 8 "Alka India Ltd."              "textile" 18901 621   .21           0   .05176498
          28 8 "Alka India Ltd."              "textile" 18932 622   .19   -.0952381 -.015880167
          28 8 "Alka India Ltd."              "textile" 18962 623   .19           0 -.020370794
          28 8 "Alka India Ltd."              "textile" 18993 624   .21   .10526316 -.034228973
          28 8 "Alka India Ltd."              "textile" 19024 625    .2  -.04761905 -.008225475
          28 8 "Alka India Ltd."              "textile" 19053 626   .19        -.05   .04549335
          28 8 "Alka India Ltd."              "textile" 19084 627   .19           0   .01154923
          28 8 "Alka India Ltd."              "textile" 19114 628   .19           0 -.033906125
          28 8 "Alka India Ltd."              "textile" 19145 629   .17  -.10526316  .000358123
          28 8 "Alka India Ltd."              "textile" 19175 630   .16  -.05882353 -.016109785
          28 8 "Alka India Ltd."              "textile" 19206 631   .14       -.125 -.000485143
          28 8 "Alka India Ltd."              "textile" 19237 632   .15  .071428575 -.011163694
          28 8 "Alka India Ltd."              "textile" 19267 633   .15           0  .008099153
          28 8 "Alka India Ltd."              "textile" 19298 634   .14  -.06666667 -.004869142
          28 8 "Alka India Ltd."              "textile" 19328 635   .13 -.071428575 -.015412844
          28 8 "Alka India Ltd."              "textile" 19359 636   .12  -.07692308 -.017020749
          28 8 "Alka India Ltd."              "textile" 19390 637   .11  -.08333334 -.005055612
          28 8 "Alka India Ltd."              "textile" 19418 638   .09   -.1818182   .01003557
          28 8 "Alka India Ltd."              "textile" 19449 639   .12    .3333333 -.027669476
          28 8 "Alka India Ltd."              "textile" 19479 640    .1  -.16666667  -.03725262
          28 8 "Alka India Ltd."              "textile" 19510 641   .12          .2 -.000537418
          28 8 "Alka India Ltd."              "textile" 19540 642   .09        -.25   .09853475
          28 8 "Alka India Ltd."              "textile" 19571 643   .09           0   .05188448
          28 8 "Alka India Ltd."              "textile" 19602 644    .1   .11111111   .01977664
          28 8 "Alka India Ltd."              "textile" 19632 645    .1           0 -.016084872
          28 8 "Alka India Ltd."              "textile" 19663 646   .11          .1   .04857971
          28 8 "Alka India Ltd."              "textile" 19693 647   .11           0   -.0247678
          28 8 "Alka India Ltd."              "textile" 19724 648    .1   -.0909091 -.005668934
          28 8 "Alka India Ltd."              "textile" 19755 649    .1           0  .010490308
          28 8 "Alka India Ltd."              "textile" 19783 650   .09         -.1 -.006544798
          end
          format %td date
          format %tm mdate

          Comment


          • #6
            OK, there is something I'm not understanding here. Some of the idiosyncratic rt values are going to be negative. In fact, for some of the firm-years in your example data, even the mean idiosyncratic_rt is negative. It is not possible to calculate a geometric mean of a set of numbers that includes negatives.

            Leaving aside the geometric mean part, I believe the following does what you ask:
            Code:
            //  CALCULATE REGRESSION VARIABLES
            by mdate, sort: egen market_var = mean(rt)
            replace market_var = market_var - rf
            rangestat (mean) industry_var = rt, by(ind_id) excludeself interval(rt . .)
            replace industry_var = industry_var -rf
            gen int year = year(date)
            
            capture program drop one_com_id
            program define one_com_id
                local years_back = 3
                gen idio_rt = .
                summ year, meanonly
                forvalues y = `=r(min)'/`=r(max)' {
                    count if inrange(year - `y', -`years_back', -1) ///
                        & !missing(rt, market_var, industry_var)
                    if r(N) >= 12*`years_back' {
                        regress rt market_var industry_var ///
                            if inrange(year - `y', -`years_back', -1)
                        predict resid, resid
                        replace idio_rt = resid if year == `y'
                        drop resid
                    }
                }
                collapse (mean) amean_idiosyncratic_rt = idio_rt, by(com_id year)
                exit
            end
            
            
            runby one_com_id, by(com_id)

            Comment


            • #7
              Extending the code #6 further, i want to calculate Peer idiosyncratic risk or PIR for each company given by com_id for each year. The PIR of company i is equal to average of the amean_idiosyncratic_rt or IR for all the companies in a given industry, j to which company i belong excluding the amean_idiosyncratic_rt or IR of company i. How can we do that?

              Mathematically it can be shown as PIRi=((IRj-IRi)/(n-1)

              Where, PIR is the peer idiosyncratic risk of company i
              IRj is the sum of amean_idiosyncratic_rt of all the companies in industry j.
              IRi is amean_idiosyncratic_rt of the company i.
              n
              is the number of companies in the industry j.
              Last edited by Sartaj Hussain; 25 Aug 2023, 12:30.

              Comment


              • #8
                This requires a small adjustment to the code in #6 so that industry type is retained when it is run. After that, while one can directly implement the formulas you show in #7, it is simpler (from a coding perspective) to use -rangestat-, which has an -excludeself- option

                Code:
                //  CALCULATE REGRESSION VARIABLES
                by mdate, sort: egen market_var = mean(rt)
                replace market_var = market_var - rf
                rangestat (mean) industry_var = rt, by(ind_id) excludeself interval(rt . .)
                replace industry_var = industry_var -rf
                gen int year = year(date)
                
                capture program drop one_com_id
                program define one_com_id
                    local years_back = 3
                    gen idio_rt = .
                    summ year, meanonly
                    forvalues y = `=r(min)'/`=r(max)' {
                        count if inrange(year - `y', -`years_back', -1) ///
                            & !missing(rt, market_var, industry_var)
                        if r(N) >= 12*`years_back' {
                            regress rt market_var industry_var ///
                                if inrange(year - `y', -`years_back', -1)
                            predict resid, resid
                            replace idio_rt = resid if year == `y'
                            drop resid
                        }
                    }
                    collapse (mean) amean_idiosyncratic_rt = idio_rt (first) industry_type, by(com_id year)
                    exit
                end
                
                
                runby one_com_id, by(com_id)
                
                rangestat (mean) PIR = amean_idiosyncratic_rt, by(industry_type) excludeself ///
                    interval(amean_idiosyncratic_rt . .)

                Comment


                • #9
                  From the model in #8, we require descriptive statistics (mean, median and SD) of the following parameters/variables:

                  1. intercept_hat of the regressions.
                  2. beta_hat of first dep_var.
                  3. beta_hat of second dep_var.
                  4. # of observations per regression.
                  5. R_square
                  6. Monthly returns (indep_var)
                  7. Expected Monthly returns.
                  8. amean_idiosyncratic_rt

                  The output preferably should generate in the form of a table.

                  Comment


                  • #10
                    Code:
                    //  CALCULATE REGRESSION VARIABLES
                    by mdate, sort: egen market_var = mean(rt)
                    replace market_var = market_var - rf
                    rangestat (mean) industry_var = rt, by(ind_id) excludeself interval(rt . .)
                    replace industry_var = industry_var -rf
                    gen int year = year(date)
                    
                    capture program drop one_com_id
                    program define one_com_id
                        local years_back = 3
                        gen idio_rt = .
                        summ year, meanonly
                        forvalues y = `=r(min)'/`=r(max)' {
                            count if inrange(year - `y', -`years_back', -1) ///
                                & !missing(rt, market_var, industry_var)
                            if r(N) >= 12*`years_back' {
                                regress rt market_var industry_var ///
                                    if inrange(year - `y', -`years_back', -1)
                                predict resid, resid
                                predict expected_returns, xb
                                replace idio_rt = resid if year == `y'
                                gen intercept_hat = _b[_cons]
                                gen beta_market_var_hat = _b[market_var]
                                gen beta_industry_var_hat = _b[industry_var]
                                gen long N = e(N)
                                gen r_square = e(r2)
                                drop resid
                            }
                        }
                        collapse (mean) amean_idiosyncratic_rt = idio_rt (first) industry_type ///
                            intercept_hat beta_*var_hat N r_square rt expected_returns, ///
                            by(com_id year)
                        exit
                    end
                    
                    
                    runby one_com_id, by(com_id)
                    
                    rangestat (mean) PIR = amean_idiosyncratic_rt, by(industry_type) excludeself ///
                        interval(amean_idiosyncratic_rt . .)
                        
                    tabstat *_hat N r_square rt expected_returns amean_idiosyncratic_rt, ///
                        statistics(mean sd median) varwidth(24) columns(statistics)
                    Note: In the output table with the new statistics, the colulmn head for median says p50.

                    Comment

                    Working...
                    X