Announcement

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

  • Running Regressions Firmwise

    Hi All,
    I have an unbalancedpanel data of 100 companies with time frame from 2005-2019. Could any one help me how can I run regression for each firms and need to save the coefficiensts as well.
    I have tried with bysort command and Is there any other way I can run regression for each firm.






    Thankyou so much.
    Fadi

  • #2
    If it is a simple ordinary least squares regression that you need to run separately in each firm and save the coefficients, the simplest way to do that is with -rangestat-. -rangestat- was written by Robert Picard, Nick Cox, and Roberto Ferrer and is available from SSC. Install it and read the help file for details.

    Comment


    • #3
      You can install estout from SSC and use the -bysort- prefix, saving the estimates in the process. See example below:

      Code:
      *ssc install estout, replace
      webuse census, clear
      bys region: eststo: reg pop marriage, robust
      esttab est*, drop(_cons) mlab(none)
      Res.:

      Code:
      . esttab est*, drop(_cons) mlab(none)
      
      ----------------------------------------------------------------------------
                            (1)             (2)             (3)             (4)  
      ----------------------------------------------------------------------------
      marriage            124.4***        105.8***        77.13***        92.87**
                        (49.78)         (54.80)         (28.35)          (3.39)  
      ----------------------------------------------------------------------------
      N                       9              12              16              13  
      ----------------------------------------------------------------------------
      t statistics in parentheses
      * p<0.05, ** p<0.01, *** p<0.001
      Otherwise, see

      Code:
      help levelsof
      and

      Code:
      help foreach
      to store the groups in a local and loop using the -if- qualifier.


      Note: Crossed with #2 that suggests an alternative command.
      Last edited by Andrew Musau; 02 Feb 2022, 11:06.

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        If it is a simple ordinary least squares regression that you need to run separately in each firm and save the coefficients, the simplest way to do that is with -rangestat-. -rangestat- was written by Robert Picard, Nick Cox, and Roberto Ferrer and is available from SSC. Install it and read the help file for details.
        Thankyou so much.I have to run a pooled ols regression ,will this command work in that case.I am just curious to know will it be a problem to if i use by command

        Comment


        • #5
          Yes, a pooled ols regression can be handled firmwise with -rangestat-. You will not use the -bysort- prefix. You will use the -by()- option in the -rangestat- command. Do give it a try.

          Comment


          • #6
            Thanks again Clyde Schechter .
            rangestat lintrestincome_w lPF_w lPK_w lPL_w lLoan_asset_w lequity_asset_w ltotalbranch_w ltotalassets_w, interval(Year . 0) by(BANKS)
            -no result was getting while using this .I have tried with by command infront of rangetsat command ,but I am getting error while running.
            I am getting results when I am simply using the command ''by'' alone,is it okay to run a regression with that command only?

            Comment


            • #7
              By reading some of the posts I got the following command since they have applied that .If possible can you just look in to the command and help me on what is the problem with this command

              Code:
              rangestat (reg) lintrestincome_w lPF_w lPK_w lPL_w lLoan_asset_w lequity_asset_w ltotalbranch_w ltota
              > lassets_w, interval(lintrestincome_w lPF_w lPK_w lPL_w lLoan_asset_w lequity_asset_w ltotalbranch_w l
              > totalassets_w) by(BANKS)
              extra argument in interval() option: lPL_w
              r(198);

              Comment


              • #8
                rangestat lintrestincome_w lPF_w lPK_w lPL_w lLoan_asset_w lequity_asset_w ltotalbranch_w ltotalassets_w, interval(Year . 0) by(BANKS)
                That would not be the correct way to use -rangestat- to accomplish regressions by firm.

                First, since there is no example data, I cannot assure that there are no other errors in this code. But, to do a firm-wise regression, (not a rolling regression) the code would be
                Code:
                rangestat (reg) lintrestincome_w lPF_w lPK_w lPL_w lLoan_asset_w lequity_asset_w ltotalbranch_w ltotalassets_w, interval(Year . .) by(BANKS)
                If this does not resolve your problem, when posting back, please post example data using the -dataex- command and I will try to troubleshoot it.

                If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

                When asking for help with code, always show example data. When showing example data, always use -dataex-.

                Comment


                • #9
                  Thanks again .but still I am getting error while running it.Here I am attaching a part of my data.I am having one qurey is that is it okay to use the
                  Code:
                  by BANKS:reg lIntrestIncome_w lPF_w lPL_w lLoan_asset_w lequity_asset_w lTotalbranch_w lCapitaladequacyratio  lasset lPK_w
                  for getting a firmwise results.
                  [CODE]
                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input str1 Banks str21 Ownership double(TC TotalAssets intrestexpenses Operatingexpenses Paymentstoandprovisionsfore FixedAssets IntrestIncome) long noofemployees double Equitycapital
                  "A" "foreign bank"          2.4107000000000003     68.4206      .3454     2.0653     .6019     .3587     2.9934    26            44.7533
                  "A" "foreign bank"                      2.6662     67.4843      .3787     2.2875     .6395     .2178     3.7477    26 45.826100000000004
                  "A" "foreign bank"          4.6975999999999996     71.3945      .5299     4.1677     .7953     .1584     4.5242    27 46.103300000000004
                  "A" "foreign bank"                      3.4251     78.8407      .4324     2.9927     .7253     .3501     4.4544    26 47.084500000000006
                  "A" "foreign bank"          4.8652999999999995     81.0577      .7193      4.146     .9854      .639     4.5463    27 48.881600000000006
                  "A" "foreign bank"                       7.136     87.9771      .4324     6.7036    1.0365    2.8277     3.3009    28            53.6023
                  "A" "foreign bank"                       8.754    117.2519      .2709     8.4831    1.0346    2.0442     3.9915    30            58.9091
                  "A" "foreign bank"                      9.9498    114.4989      .5516     9.3982    1.2372    1.7904     5.0747    27  67.07249999999999
                  "A" "foreign bank"          11.486999999999998    198.2688     1.3403    10.1467    1.2683    1.0205     6.9074    32             75.062
                  "A" "foreign bank"                     18.3047    207.4335     1.6528    16.6519     1.303     .7351     7.9598    31            82.2748
                  "A" "foreign bank"          14.618500000000001    225.2591     1.6042    13.0143    1.2563    2.0095     7.9714    31            92.8617
                  "A" "foreign bank"                     16.3524     251.675     1.7213    14.6311     1.328    1.4424     8.0672    32            92.8729
                  "A" "foreign bank"                     19.7844    242.7238     1.7996    17.9848    1.3287     .9305     7.4239    32            98.0869
                  "A" "foreign bank"                     16.8888    212.7043     1.7748     15.114    1.5162     .8006     6.9207    31            89.2954
                  "A" "foreign bank"                     17.1758    250.2364     1.8366    15.3392    1.7188    1.0123     7.7901    31            97.3681
                  "B" "foreign bank"                    185.2602   1950.8457   137.9034    47.3568    8.2696    7.5946   149.8033    40            71.2214
                  "B" "foreign bank"                    115.0707    689.3158   101.0732    13.9975    3.2401    7.0739   111.3711    38            75.8034
                  "B" "foreign bank"                      51.166    793.1288    32.2716    18.8944    5.2876    6.7156    46.6471    36            77.7243
                  "B" "foreign bank"                     53.3545    576.7017    26.6688    26.6857     5.815    6.6196     64.727    39           106.9254
                  "B" "foreign bank"                     33.1686    655.4384    20.0201    13.1485    5.4067    6.5094    47.7986    40 123.28960000000001
                  "B" "foreign bank"                     33.1549    675.4537    20.0841    13.0708     6.926    6.2321    41.0989    41           130.9139
                  "B" "foreign bank"                     38.9585     949.141     24.383    14.5755    8.3646    7.0223    42.8091    43            139.132
                  "B" "foreign bank"          57.845600000000005   1219.4372    40.2757    17.5699     9.619    9.2151    76.0269    47           309.2061
                  "B" "foreign bank"                     93.9067    1637.755    69.4091    24.4976    13.892    6.9339   117.5819    51           327.4042
                  "B" "foreign bank"                    126.8535   2299.6308   102.2924    24.5611   12.0928    6.2902   149.5867    53           337.8588
                  "B" "foreign bank"                    162.3225   2656.5713   130.5383    31.7842   19.0079    8.1497   182.1622    69           351.4679
                  "B" "foreign bank"                    177.0478   2693.0871   138.5873    38.4605   21.4502    8.9912    185.235    70           368.6843
                  "B" "foreign bank"                    193.6426   3475.1443    153.379    40.2636   22.6456    9.3636   198.2442    78           384.5298
                  "B" "foreign bank"                    169.3838   3417.8669   125.6113    43.7725   23.9887   10.2425   181.1469    88            400.929
                  "B" "foreign bank"                    194.1901   2812.3225   142.9273    51.2628   29.3167    8.6864   192.2464    80           395.2414
                  "C" "public sector banks "           2891.6921  45144.9299  1821.5602  1070.1319  680.5363  731.9995  3185.5988 18917          2327.6509
                  "C" "public sector banks "  3225.5496000000003    55291.99  2189.7963  1035.7533  681.0983  742.7158  3767.2436 19134 3638.5305999999996
                  "C" "public sector banks "           4160.3525  67663.7413  3133.1177  1027.2348  620.7168 1056.3307  4883.8628 20379          4476.8288
                  "C" "public sector banks "           5656.4629  82939.3236  4498.8795  1157.5834  689.8806 1071.4676  6171.2159 20079          5221.0491
                  "C" "public sector banks "           6605.4997  97648.0078  5206.0613  1399.4384  873.9384 1109.7519  7364.7279 20457          5851.9496
                  "C" "public sector banks "           7336.5525  121699.213  5718.7209  1617.8316 1011.3792 1118.2746  8369.2002 20959          6752.9524
                  "C" "public sector banks "           9330.5263 151286.3609  6992.2242  2338.3021  1557.624 1148.2285 11014.6917 21227          8507.3866
                  "C" "public sector banks "          13052.0188 182934.5678 10360.6301  2691.3887 1834.4857  1197.731 15523.2776 22334          10506.613
                  "C" "public sector banks "          15527.3815 204373.1889 12569.2806  2958.1009 1985.9373 1251.5201 17435.6918 22557         11352.5164
                  "C" "public sector banks "          16892.0003 220434.2831 13435.3608  3456.6395 2245.0397 1309.6934 18746.6825 24569 11800.733400000001
                  "C" "public sector banks "          17252.4153 227096.4815 13538.2421  3714.1732 2307.2028 1405.4075 19716.1167 24231         12642.7794
                  "C" "public sector banks "          16661.1692 239825.3646 12986.4529  3674.7163 2130.8805 3255.8331 18884.9455 24137 14064.036600000001
                  "C" "public sector banks "          16437.9556 237037.8832 12373.3636   4064.592 2285.9372 3191.8503 17660.3936 23944 14296.400399999999
                  "C" "public sector banks "  15612.728799999999 252714.3103 11626.1027  3986.6261 2158.1041 3132.0863 16358.4926 23967         10268.3915
                  "C" "public sector banks "          15797.4844 248575.7709 11353.4834   4444.001 2529.8697 3538.2578 16864.2937 23210          9130.3177
                  "C" "foreign bank"                    443.1332   3020.8894   151.1767   291.9565  107.0315    68.995   269.7102  1586           301.1828
                  "C" "foreign bank"                    455.4849   3655.2484   154.4561   301.0288  118.8472   68.9173   259.2239  1773 398.77959999999996
                  "C" "foreign bank"                    506.1064   4043.6567   166.6977   339.4087  136.8241   56.4891   289.1454  1982           443.0952
                  "C" "foreign bank"                    455.7584   1216.7683    81.1735   374.5849  101.9923   93.0898    77.3812   733           481.0379
                  "C" "foreign bank"                    353.5627   1465.7538    32.1462   321.4165   86.2603   67.8762     63.822   857           481.0379
                  "C" "foreign bank"                    454.3371   1739.0241    38.1231    416.214  101.9856   45.5759    62.1933   870  624.3984999999999
                  "C" "foreign bank"                    563.1699    2133.672    80.6666   482.5033  107.5307   38.1836    78.3011   821           625.4004
                  "C" "foreign bank"           687.2864999999999   2540.1768   117.3322   569.9543  108.1438   43.1947    88.5896   623  760.4254000000001
                  "C" "foreign bank"                    764.9787   3276.0527   126.0678   638.9109  123.8807   45.4127   105.4227   648  760.4254000000001
                  "C" "foreign bank"                    892.1043   3816.9552   133.8263    758.278  127.9422   36.5839   146.0846   755          1004.6454
                  "C" "foreign bank"                    976.3198   4621.8767   147.8365   828.4833  123.9032   29.4879   203.9507   716          1004.6454
                  "C" "foreign bank"                   1168.4531    6126.291   110.0428  1058.4103  121.2088    32.328    272.797   612          1167.4268
                  "C" "foreign bank"                   1351.4903   7725.2608   125.8108  1225.6795  132.9453   28.0102   363.3676   643          1464.1334
                  "C" "foreign bank"                   1658.6975   8549.6745   146.1081  1512.5894  159.1008   31.6448   518.7308   672 1807.6453999999999
                  "D" "public sector banks "           2042.3809   32728.684  1212.9181   829.4628  539.5268  187.2873  2281.9595 13108          1837.0011
                  "D" "public sector banks "           2364.0865  40669.3389  1506.1508   857.9357  492.8883    192.75  2675.1224 14028          2893.9411
                  "D" "public sector banks "           2830.9724  47540.9977  1897.7861   933.1863  548.8025  192.3459  3315.3253 12981            3156.28
                  "D" "public sector banks "  3779.0847999999996  56592.3896  2869.9959   909.0889  509.3898  219.4634  4209.5643 13387          3249.2889
                  "D" "public sector banks "           4851.9698  68469.2076   3747.712  1104.2578  624.0607  335.2951  5374.6168 14255          3646.9934
                  "D" "public sector banks "           5527.6637  90342.4023  4178.1287   1349.535  824.0821  355.6567  6372.8657 14292          4410.0435
                  "D" "public sector banks "   6775.171399999999 108900.7195  5070.3074   1704.864  1104.219  317.5002  8291.2769 14098          6492.4162
                  "D" "public sector banks "   9383.655999999999 124545.3869  7579.4068  1804.2492 1149.9325  302.5531 11338.7283 15099          7479.3907
                  "D" "public sector banks "           11189.883 146298.9442  9152.6742  2037.2088 1286.9824  303.4814 12909.6921 16523          8441.1834
                  "D" "public sector banks "          12869.9163 167340.9251 10559.9811  2309.9352 1382.8539  433.9254  14297.315 20276          8737.4511
                  "D" "public sector banks "  14570.012200000001 185170.3486 11830.5712   2739.441 1697.9564 1263.5718 16368.6044 19799         10064.0872
                  "D" "public sector banks "  15239.146700000001 199961.7586 12313.7109  2925.4358 1734.7211 1368.4445 17634.6752 20016         10994.3893
                  "D" "public sector banks "          15947.7679 223099.3786 12495.7744  3451.9935 2096.7759 1530.7038 18027.4181 19380         11366.6596
                  "D" "public sector banks "          14985.5681 242171.3712 11639.7796  3345.7885 1857.7932 1473.2958  17974.538 19921         10818.2867
                  "D" "public sector banks "          15954.1404 249311.4154 12223.9915  3730.1489 2241.4955 1558.0848 18932.2205 20346 13165.125199999999
                  "E" "foreign bank"                    151.1247   3126.5477    40.5353   110.5894   50.6297   39.9365    94.3864    76           585.6421
                  "E" "foreign bank"                    286.1395   4908.4327   162.6745    123.465    63.701   46.1703   281.2705    79 1132.4070000000002
                  "E" "foreign bank"                    271.3008   5678.2565   137.5332   133.7676   70.9796   34.8683   307.0646    84          1173.9943
                  "E" "foreign bank"                    309.5855   5857.5431   171.5627   138.0228   73.6208   25.3667   284.4271    85          1230.4527
                  "E" "foreign bank"                    347.5483   5707.0087   188.8414   158.7069   78.9161   81.2366   290.7034    85          1278.0108
                  "E" "foreign bank"                    313.7794   6378.5046   164.8438   148.9356   77.6454   73.9159   245.5129    74 1315.0991000000001
                  "E" "foreign bank"          300.64189999999996    5352.017   158.0391   142.6028   73.4502   62.1921   287.5013    78          1373.2994
                  "E" "foreign bank"                       332.6   7719.1314   196.1623   136.4377   73.5947   53.3252   362.1589    78 1388.4985000000001
                  "F" "private sector banks "          1774.3597  37743.6908  1192.9808   581.3789  176.8548  518.4358  1924.1582  4761          2421.6019
                  "F" "private banks"         2624.6067000000003  49731.1167   1810.556   814.0507  240.2018  567.7131  2888.7904  6553 2885.6258000000003
                  "F" "private sector banks "          4207.9156  73257.2155  2993.3172  1214.5984  381.3461  673.1941  4461.6552  9980          3402.2072
                  "F" "private sector banks "          6574.8886 109577.8487  4419.9617  2154.9269  670.2491  922.8501  7005.3151 14739          8770.6904
                  "F" "private sector banks "         10007.4869 147722.0487  7149.2742  2858.2127  997.6625 1072.8873 10835.4856 20624         10214.7997
                  "F" "private sector banks "         10343.2484 180647.8519  6633.5261  3709.7223 1255.8219 1222.4199 11638.0215 21640         16044.6224
                  "F" "private sector banks " 13371.251100000001 242713.3716   8591.823  4779.4281 1613.9001 2273.1456 15154.8058 26341         18998.8255
                  "F" "private sector banks " 19984.001900000003 285627.7934 13976.9024  6007.0995 2080.1677  2259.325 21994.6474 31738         22808.5423
                  "F" "private sector banks "         24430.5486 340560.6584 17516.3111  6914.2375 2376.9825  2355.642 27182.5744 41752         33107.8599
                  "F" "private sector banks "         26590.2959 383244.8882  18689.522  7900.7739 2601.3494 2410.2106 30641.1554 42420         38220.4865
                  "F" "private sector banks "         30458.2051 461932.3942 21254.4595  9203.7456 3114.9697 2514.3105 35478.5977 42230          44676.515
                  "F" "private sector banks "         34255.8861 539821.0196 24155.0675 10100.8186 3376.0064 3523.1719 40988.0364 50135 53164.907300000006
                  "F" "private sector banks "         38648.9473 601467.6703  26449.042 12199.9053  3891.864 3746.8925 44542.1579 56617         55762.5418
                  "F" "private sector banks "         41152.9216 691329.5799 27162.5818 13990.3398 4312.9556 3971.6792 45780.3123 59819 63445.259600000005
                  "F" "private sector banks " 49111.004700000005 800996.5316  33277.597 15833.4077 4747.3218 4036.6358 54985
                  Last edited by Fadi Ansar; 03 Feb 2022, 04:43.

                  Comment


                  • #10
                    There could be various problems here. What you tried could be problematic if the number of predictors was similar to the number of observations. The variables in your regression call aren't all in your data example, so my experiments all ran into problems, but in principle


                    Code:
                    statsby, by(Banks) clear : regress y x1 x2 x3
                    should be more convenient than what you tried.

                    Comment


                    • #11
                      Thankyou so much.

                      Comment


                      • #12
                        Originally posted by Nick Cox View Post
                        There could be various problems here. What you tried could be problematic if the number of predictors was similar to the number of observations. The variables in your regression call aren't all in your data example, so my experiments all ran into problems, but in principle


                        Code:
                        statsby, by(Banks) clear : regress y x1 x2 x3
                        should be more convenient than what you tried.
                        Will this formula work if I need both bankwise and year wise results ,I was getting error when I have added Year as well in the command.Is there any way I can get the results for each banks and year in the sample.

                        Thanking you,

                        Comment


                        • #13
                          In principle something like


                          Code:
                          statsby, by(Banks year) clear : regress y x1 x2 x3
                          is legal, but I don't sense that your problems arise from illegal code. Back in #9 you were talking about a regression
                          Code:
                            
                           regress lIntrestIncome_w lPF_w lPL_w lLoan_asset_w lequity_asset_w lTotalbranch_w lCapitaladequacyratio  lasset lPK_w
                          i.e. one response variable and 8 predictors. That kind of regression (indeed any regression) is not going to work whenever you don't have enough data to fit it. It is not going to work well unless you have a sample size much bigger than the number of parameters estimated.

                          Comment


                          • #14
                            Thankyou so much@Nick Cox.
                            I am having an unbalanced panel data with a total 1290 observations.

                            Comment


                            • #15
                              The total number of observations is not what bites here. It is the number of observations in each subset you feed to a regression.

                              What was the last regression command you tried?

                              Comment

                              Working...
                              X