Announcement

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

  • Gen Peer Var

    For each of the x variables in the data example, peer variables need to be generated. For example, the formula for peer variable for variable x1 is:

    Say peer variable for x1 of company i in the industry j for the year t = [( sum of x1 variables in the industry j in year t minus x1 variable of company i in the industry j for the year t) divided by number of the companies in industry j in the year t minus one]

    The industry is identified by the ind_id.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str54 company_name int(com_id year) byte ind_id float(nwctosales_w nwctota_w artota_w invtota_w paytota_w ntctota_w cacltota_w pnwcsales pnwcta parta)
    "3M India Ltd."            1 2011 36  .1931265   .3131105 .26320764  .2086703 .15876746  .10444018   .3601381 .1701606 .2277101 .2258299
    "3M India Ltd."            1 2012 36 .20771056    .326363  .2530571   .228248 .15494207  .09811502  .29039356 .2585797  .270235 .2517065
    "3M India Ltd."            1 2013 36  .2236201  .27043068 .19926818 .16481394 .09365143  .10561674   .3763694 .1896778 .2491033 .2424825
    "3M India Ltd."            1 2014 36 .22534087  .28514853 .19549543 .17973775 .09008463   .1054108   .4104095 .1810781 .2571848 .2628902
    "3M India Ltd."            1 2015 36  .2151627  .27047947 .17990884 .19272827 .10215765   .0777512   .3120089 .2040146 .2738895 .2741427
    "3M India Ltd."            1 2016 36  .1930388  .28627732 .24202897  .1949863 .15073793  .09129104    .469493 .1953774 .2741912 .2832505
    "3M India Ltd."            1 2017 36  .1472832   .1823145  .2168636 .16709475 .20164385  .01521973   .4869106 .2925568 .2519943 .2853366
    "3M India Ltd."            1 2018 36 .22685155  .26675993 .25509372 .15825956 .14659333   .1085004  .58617735  .232727 .2586449  .268443
    "3M India Ltd."            1 2019 36  .2280152  .25012204 .22844277 .14809158 .12641232  .10203045   .3977372 .2576113 .2709098 .2724651
    "3M India Ltd."            1 2020 36  .2344301  .24783564 .20400046  .1511071 .10727192  .09672854   .4749484 .2861314 .2694951 .2581544
    "3M India Ltd."            1 2021 36  .1914054   .1556331 .16948748 .14454359 .15839794  .01108954   .4749217  .282703 .2470171  .233897
    "3M India Ltd."            1 2022 36   .154764  .14380234 .15933836 .13688102 .15241703  .00692132   .4990658 .2681836 .2605985 .2377224
    "A D F Foods Ltd."         2 2011  9  .3298189   .2310752  .1711598   .085864 .02594861  .14521119   .3642907 .1630386 .0672256 .0549824
    "A D F Foods Ltd."         2 2012  9  .3575473   .2369393 .15831135 .10255057  .0239226  .13438874   .3735561 .0501534 .0630577 .0484759
    "A D F Foods Ltd."         2 2013  9  .3728762   .2385312 .16323133  .0996328 .02433293  .13889842   .4153733 .0709289 .0887031  .070201
    "A D F Foods Ltd."         2 2014  9 .29012045  .19306862 .13051939 .10327573  .0407265   .0897929    .432609 .1261392 .1066517  .068557
    "A D F Foods Ltd."         2 2015  9 .31476045  .20938388 .13742898 .10475852 .03280362  .10462536     .44731 .1454528 .1197488 .0913579
    "A D F Foods Ltd."         2 2016  9 .29355118  .19417275 .12817457 .10109312 .03509493  .09307964   .3287581 .1436258 .1440006 .0999846
    "A D F Foods Ltd."         2 2017  9  .2849037  .20589775  .1370616 .10813994 .03930379   .0977578   .3178328  .092829 .1079472 .0883539
    "A D F Foods Ltd."         2 2018  9 .29617333  .20913877 .16373283 .09081188 .04540594  .11832689   .3584766  .177791 .1523371 .0992702
    "A D F Foods Ltd."         2 2019  9 .28681356  .24048936 .17688955 .10968661 .04608681  .13080275    .361572 .1701164 .1632242 .0939087
    "A D F Foods Ltd."         2 2020  9 .29469842   .1986482 .15244368  .0955286 .04932409  .10311958  .29608318 .2321783 .1581936 .0878174
    "A D F Foods Ltd."         2 2021  9 .27920046  .19743605 .16226393 .09090366 .05573153  .10653239   .4610925 .1843831 .1552089 .0827193
    "A D F Foods Ltd."         2 2022  9 .24130853   .1795649 .15031907 .08702294 .05777712  .09254195  .48771775  .129016 .1595833 .0922418
    "A F Enterprises Ltd."     3 2011 36         .          .         .         .         .          . -1.3246377        .        .        .
    "A F Enterprises Ltd."     3 2012 36         .          .  .3076923         .         .          . -1.3246377        .        . .2501455
    "A F Enterprises Ltd."     3 2013 36         .          .  .3076923         . .11538462   .1923077 -1.3246377        .        . .2393846
    "A F Enterprises Ltd."     3 2014 36         .          .  .4335664         .  .4475524 -.01398601    .493007        .        . .2560882
    "A F Enterprises Ltd."     3 2015 36 .00676329   .0122807  .4438596 .02105263  .4526316 -.00877193   .5017544 .2099689 .2812666 .2666013
    "A F Enterprises Ltd."     3 2016 36         .          .  .2920635         . .03492063  .25714287   .7664042        .        . .2818209
    "A F Enterprises Ltd."     3 2017 36 3.1027305  .20165746 .40607736   .339779  .5441989 -.13812155   .3701657 .2081154 .2514416 .2799305
    "A F Enterprises Ltd."     3 2018 36 -.3628889 -.05723542 .22408207  .5739741   .640713  -.4070564 -.07235421 .2495767  .267902 .2693291
    "A F Enterprises Ltd."     3 2019 36  .0609074  .05183814  .6397778 .23009786   .640713  -.1782597 -.02406771 .2623858  .276575 .2607126
    "A F Enterprises Ltd."     3 2020 36 .13820244  .07789126  .6414905 .06829671   .640713  .00959455  -.0299185 .2888807 .2743507 .2456547
    "A F Enterprises Ltd."     3 2021 36  .7136539  .12184993  .6414905 .00943841   .640713   .1124115  .04936291 .2677816 .2479823 .2204112
    "A F Enterprises Ltd."     3 2022 36   .547928  .11315698  .6414905 .03440526   .640713  .07875173  .12776625 .2569504  .261474 .2239466
    "A G I Greenpac Ltd."      4 2011 96 .24303874   .1895539 .11423577   .144728 .06940987   .0448259  .14950357 .1256141 .1287398 .1042076
    "A G I Greenpac Ltd."      4 2012 96 .22393277  .13714696 .09700472  .1168023 .07666006  .02034466  .03986015  .239023 .1334975 .1037655
    "A G I Greenpac Ltd."      4 2013 96  .3239214  .20393836 .13226545 .13462654 .06295363  .06931183  .09251368 .2945965 .1760508 .1280374
    "A G I Greenpac Ltd."      4 2014 96  .3473314  .22718063 .14607956 .14149354 .06039248  .08568708    .057327 .2375556 .1506042 .1272247
    "A G I Greenpac Ltd."      4 2015 96  .3453279   .2365324 .14562929  .1503641 .05946095  .08616834   .1279256 .5003894 .1642284 .1180926
    "A G I Greenpac Ltd."      4 2016 96  .3498736  .26932317 .15023765  .1805608 .06147526  .08876239  .05469953 .4668845 .1865631 .1327478
    "A G I Greenpac Ltd."      4 2017 96  .3123413  .22022355 .13343403  .1566084 .06981891  .06361512  .05928741 .6616021  .172413 .1120849
    "A G I Greenpac Ltd."      4 2018 96  .3796552  .24467798 .15348354  .1601501 .06895563   .0845279  .07735053 .6100315 .1779277 .1352314
    "A G I Greenpac Ltd."      4 2019 96 .26375026  .14696588 .11551201 .10060533 .06915146  .04636055  .04541915 .5379459 .1644409 .1288472
    "A G I Greenpac Ltd."      4 2020 96 .27232233  .17328472 .10419935 .13915303 .07006767  .03413168  .10518507 .6519631 .1610242 .1251965
    "A G I Greenpac Ltd."      4 2021 96  .4274132  .18297626 .11539661  .1494297 .08185005  .03354656  .08852098 .4289725 .1570852 .1352989
    "A G I Greenpac Ltd."      4 2022 96 .27079055   .1165815   .098612 .09103064 .07306112  .02555087  .20713226 .3224918 .1791769 .1222747
    "A I A Engineering Ltd."   5 2011 63  .3073394   .3223567  .2490324  .1350167 .06169237     .18734   .6344023 .1998776 .2156472   .14397
    "A I A Engineering Ltd."   5 2012 63  .2932044   .3383424  .2600735 .14186943 .06360056  .19647296   .5789077 .1991583 .2582689 .1928046
    "A I A Engineering Ltd."   5 2013 63  .2171952   .2496855 .16320483 .14651532 .06003466  .10317017   .6177211 .2066758 .2576991 .2033967
    "A I A Engineering Ltd."   5 2014 63   .174062   .1897434 .13081351 .12965524 .07072534  .06008817   .6081563 .2253075 .2465166 .1995731
    "A I A Engineering Ltd."   5 2015 63  .1580793   .1679114  .1118191  .1118438 .05575149  .05606761   .5998163 .2435773 .2511605 .2055306
    "A I A Engineering Ltd."   5 2016 63  .2725947  .21131542  .1649626 .08568797 .03933515  .12562746  .59268713 .2420825 .2479061 .2128442
    "A I A Engineering Ltd."   5 2017 63   .397352   .2970166 .23280165  .1053438 .04112884   .1916728   .6197537 .3648114 .2429943  .210857
    "A I A Engineering Ltd."   5 2018 63 .42754245  .28874704 .21940884 .10756527 .03822707  .18118177   .6305891 .2753311 .2388503 .2244667
    "A I A Engineering Ltd."   5 2019 63  .4204532   .3307844 .24972987 .12681262 .04575804  .20397183   .6484699 .3129784 .2320814 .2220346
    "A I A Engineering Ltd."   5 2020 63  .4946374   .3203042  .2426447 .10880134 .03114183  .21150286   .6831271 .2757255 .2291576 .1999903
    "A I A Engineering Ltd."   5 2021 63  .5365686   .2159515 .17297564 .06825567 .02527982  .14769582   .4995078 .3371114 .2450543  .209742
    "A I A Engineering Ltd."   5 2022 63  .5723672  .27931756   .205287 .09944106  .0254105   .1798765   .5467263 .2984701 .2788097 .2014804
    "A I Champdany Inds. Ltd." 6 2011 23  .2830057   .3270586 .04969465  .4738944 .19653043 -.14683577  .02925978 .2488429 .1853864 .1035412
    "A I Champdany Inds. Ltd." 6 2012 23  .3130852   .4267926 .09473903  .4511086 .11905503   -.024316  .07294801 .3167528 .2093491 .1137653
    "A I Champdany Inds. Ltd." 6 2013 23 .34400785   .4297083 .09350021  .4683074 .13209929 -.03859907  .07853036 .3657283 .2215722  .138201
    "A I Champdany Inds. Ltd." 6 2014 23  .3621368   .4074637 .10363276  .4775099   .173679 -.07004624  .09151255 .3418338 .2212249 .1538978
    "A I Champdany Inds. Ltd." 6 2015 23  .4254147   .4115329 .12352607  .4602434 .17223658 -.04871051  .11930134 .2880788 .2286446 .1584886
    "A I Champdany Inds. Ltd." 6 2016 23 .53221494   .4100468 .07461748  .4726822 .13725285 -.06263537  .11363795 .3596653 .2300836  .149325
    "A I Champdany Inds. Ltd." 6 2017 23 1.1185294  .46140495 .07330416  .4807591 .09265827 -.01935411  .19018336 .3630392 .2169941 .1474246
    "A I Champdany Inds. Ltd." 6 2018 23 1.0177624   .4563194 .06164242  .5385782 .14390126 -.08225884  .30245465 .4127679 .2321389 .1593757
    "A I Champdany Inds. Ltd." 6 2019 23 1.1550585   .3995234 .04612522  .4810994 .12770125 -.08157603    .424561 .3909292 .2178117 .1592152
    "A I Champdany Inds. Ltd." 6 2020 23 1.0042562   .4032472 .05057602  .4788581 .12618686 -.07561084   .4416382 .3616173 .2169317 .1462307
    "A I Champdany Inds. Ltd." 6 2021 23 1.9443527   .4105203 .04883301   .481078  .1193907 -.07055768   .4076068 .4827308 .2308421 .1540659
    "A I Champdany Inds. Ltd." 6 2022 23 3.1027305   .4143426 .04629103  .4773604  .1093088 -.06301777   .4622462 .4112053  .240999 .1580484
    "A K I India Ltd."         7 2011 44         .          .         .         .         .          .          .        .        .        .
    "A K I India Ltd."         7 2012 44         .          .         .         .         .          .          .        .        .        .
    "A K I India Ltd."         7 2013 44         .          .         .         .         .          .          .        .        .        .
    "A K I India Ltd."         7 2014 44 .52205396  .30712625 .21611154  .2912471  .2002324  .01587916  .04570101 .1781865 .2465108  .115554
    "A K I India Ltd."         7 2015 44  .2763325  .23164894  .3148936 .24414894  .3273936     -.0125 -.03324468 .1899247 .2681896 .1124139
    "A K I India Ltd."         7 2016 44 .25198808  .25186288  .3117238  .2640338  .3238947 -.01217089 -.03974168 .2280762 .2837639 .1442803
    "A K I India Ltd."         7 2017 44  .2825623   .2865047 .28169352  .2905942 .28578302 -.00408949  .02213134 .2250187 .2271142 .1160449
    "A K I India Ltd."         7 2018 44  .2776761    .333059 .27489713  .3017833  .2436214  .03127572  .06255144 .3011951  .269449  .129598
    "A K I India Ltd."         7 2019 44  .3218556  .29595426  .2348285  .3056288  .2445031 -.00967458  .06948109 .3377557 .2984064 .2072307
    "A K I India Ltd."         7 2020 44  .2578125  .21607143  .1857143  .3190476 .28869048 -.10297619 -.00555556 .8264322 .3948466 .2021737
    "A K I India Ltd."         7 2021 44  .3703462   .2990506 .28305203 .26353726  .2475387  .03551336  .08368495 .9442918 .4087403 .1830799
    "A K I India Ltd."         7 2022 44 .29813665  .30947265  .2931063  .2782278 .26186147  .03124483   .1147297 .7034526 .4203494 .1694909
    "A K Spintex Ltd."         8 2011 18  .1180946  .12772809 .15813953  .1255814 .15599284  .00214669 -.16135956 .2247052 .2491126 .1754886
    "A K Spintex Ltd."         8 2012 18 .17195006  .18154158 .18492223 .14705881  .1504395  .03448276 -.09702502 .2719578 .2850873 .2028704
    "A K Spintex Ltd."         8 2013 18 .21479714   .2173913 .20289855 .15126812 .13677536  .06612319  .29227054 .3097868 .3034298 .2243826
    "A K Spintex Ltd."         8 2014 18  .2011625   .2278191  .2389811 .14481969 .15598167  .08299943   .0638237 .3519206 .3175011 .2277378
    "A K Spintex Ltd."         8 2015 18 .17665686   .2241033 .26829267 .12883788 .17302726  .09526542   .0866571 .3261909 .3053296 .2288278
    "A K Spintex Ltd."         8 2016 18 .13716139  .22205065  .3011118 .12168005  .2007412   .1003706  .00339716 .3508026 .3083643 .2340993
    "A K Spintex Ltd."         8 2017 18 .14539275  .22235055  .3138897 .09904707  .1905862  .12330349 -.03032053 .3825423 .3037337 .2217646
    "A K Spintex Ltd."         8 2018 18 .14462505   .1748158  .2719357  .1020317  .1991516   .0727841  .01853092 .3347459 .2970477 .2394955
    "A K Spintex Ltd."         8 2019 18 .17608912  .23238973  .3265306  .1086241 .20276497  .12376564  .08009655 .4275655 .2796696 .2280781
    "A K Spintex Ltd."         8 2020 18 .17175213  .24424066  .3285618 .12681727 .21113846   .1174234  .15320957 .4390382 .2860536 .2195976
    "A K Spintex Ltd."         8 2021 18 .23708333   .2410506 .30862105  .1529337 .22050413  .08811692   .1563228 .6196606 .2828627 .2091845
    "A K Spintex Ltd."         8 2022 18 .13172746   .1822163 .28960395 .14908606 .25647372  .03313024  .10910129 .2884939 .2876807 .2029131
    "A M D Industries Ltd."    9 2011 36 .24933498  .14507909  .0981162  .1836989   .136736  -.0386198  .11389633 .1685547  .232511 .2305468
    "A M D Industries Ltd."    9 2012 36  .2483159   .2037194 .14756545  .1490984 .09294445    .054621  .13243777 .2574196 .2737391 .2547205
    "A M D Industries Ltd."    9 2013 36  .3083085  .21988033   .119475 .22300714  .1226018 -.00312681  .15498938 .1872582 .2505476 .2447623
    "A M D Industries Ltd."    9 2014 36  .2623424  .21615613 .14361845  .1796956 .10715792  .03646053  .15143964 .1800209  .259156 .2643724
    end

  • #2
    See an FAQ https://www.stata.com/support/faqs/d...ng-properties/

    In practice, you need careful code just in case you have missing values too, but the careful code includes rangestat from SSC.

    Code:
    webuse grunfeld
    rangestat (mean) mvalue, int(year 0 0) excludeself

    Comment


    • #3
      Is #2 capable of estimating peer variable for each xi variable for each company id i and each ind_id j distinctively.
      Last edited by Sartaj Hussain; 04 Sep 2023, 20:53.

      Comment


      • #4
        Yes. To do it by company just modify the code in #2 adding the -by(com_id)- option.

        Comment


        • #5
          It has to be for each company i in the industry j.

          Comment


          • #6
            Sorry, then it's not -by(com_id)-. Also, you said you want it separately for each year, so it's -by(industry_id year)-. There doesn't seem to be an industry variable in your example data set. Presumably your real data set has one and you will plug in the name of that variable where I have written industry_id. You do not have to specify a company identifier in the command, because within groups defined by industry_id and year, each company is just a separate single observation.

            Comment


            • #7
              In one of earlier posts, we used the following code instead of #2.

              Code:
               
               rangestat (mean) PIR = amean_idiosyncratic_rt, by(industry_type) excludeself ///     interval(amean_idiosyncratic_rt . .)

              Comment


              • #8
                The code you show in #7 does not restrict the calculations to observations in the same year.

                As for the difference in what appears in the -interval()- option, that makes no difference in how the calculation is done here. In the code in #2, -interval(year . .)- just says that year must be the same to be part of the calculation. In #6, we include year in the -by()- option: it's the same result either way.

                If what you want the peer observations to be is observations in the same industry and the same year, but excluding the self, there are several equivalent ways to do that. In my opinion, the simplest and most transparent way to code it is:
                Code:
                rangestat (mean) peer_var = var, by(industry year) interval(var . .) excludeself
                Last edited by Clyde Schechter; 05 Sep 2023, 09:34.

                Comment


                • #9
                  This looks fine. But how can we put calculation of peer_var for many x variables, say x1 to xn in the #8

                  Comment


                  • #10
                    Code:
                    foreach v of varlist x1-xn {
                        rangestat (mean) peer_`v' = `v', by(industry year) interval(`v', . .) excludeself
                    }
                    Note: The use of the wildcard abbrevation x1-xn presumes that these variables appear consecutively and in order in the data set. If that is not the case, this code will do the calculation on the wrong variables, so a different way of listing the actual x1, x2,...,xn variables would be needed. If they are not too numerous, just actually listing their names would be suitable.

                    Comment

                    Working...
                    X