Announcement

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

  • Rolling Regression ( with window being a variable instead of time periods)

    Dear All,

    I am familiar with the commands of asreg and rolling in Stata to make rolling regressions (many of the forum posts were quite helpful). However, I could not find a lot regarding rolling regressions with the window being a variable instead of a time value. For instance, if I want the window to be a variable (for example, income) between x and y values in an unbalanced panel (instead of time series). Any recommendation? Thank you very much!

    Code:
     * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float country1 int year str2 isic double(Wages OutputINDSTAT4 ValueAdded) float(coeff_diversi gdp_percapita)
    218 1963 "16"     305556    4944444     777778  .5130082 4880.8784
    578 1963 "17"   36539985  162399935   68599973  .7106571  19577.57
    826 1963 "28" 1119999552 4339998264 2015999194  .7767401 15766.834
     56 1963 "23"   31799934  840759907   69199850  .5630758 14371.427
    710 1963 "15"  114799954 1139599544  324799870  .7362456  8701.798
    348 1963 "16"    4599659   34923339   10221465  .6535436         .
    188 1963 "26"     801509    3924528    2701887  .4628521  5419.053
    554 1963 "28"   33370690  126530533   53949282  .6389886 18497.617
    528 1963 "17"  172375819  950276499  262430875   .609821 17149.121
    170 1963 "20"    3777778   21222222    9777778  .6257223  4280.184
    410 1963 "16"    4615385  103076923   64615385   .758609 1722.5104
    392 1963 "21"  283333333 2605555556  913888889  .7718055  8275.577
    608 1963 "16"    5625990   64698883   28897130  .6826711   3093.64
    710 1963 "23"   13999994  118999952   46199982  .7362456  8701.798
    840 1963 "36"  3.040e+09  1.057e+10  5.630e+09  .7825109  20620.72
    826 1963 "16"   83999966  839999664  307999877  .7767401 15766.834
    792 1963 "16"   12333333  182222222   47777778  .6656876  5975.373
    218 1963 "28"     627778    3777778    1333333  .5130082 4880.8784
    710 1963 "22"   48999980  132999947   85399966  .7362456  8701.798
    170 1963 "24"   20111111  188555556   97333333  .6257223  4280.184
    170 1963 "16"    4000000   56222222   39444444  .6257223  4280.184
    792 1963 "17"   63111111  466666667  162222222  .6656876  5975.373
    788 1963 "20"     628571    3285714    1357143 .48436305 2256.2856
    188 1963 "20"    1009811    7849057    4181132  .4628521  5419.053
    590 1963 "36"    1345000    5560000    2490000  .5293286  5135.478
    288 1963 "20"    7380282   28408451   17042254  .6085891  2951.635
    792 1963 "15"   38333333  636666667  165555556  .6656876  5975.373
    840 1963 "27"  7.400e+09  3.341e+10  1.439e+10  .7825109  20620.72
    250 1963 "27"          . 5572547665 1492588853  .5855801  14469.21
    470 1963 "17"     268800    2699200     624400  .4270359 2409.7773
    792 1963 "27"   13888889  1.400e+08   50000000  .6656876  5975.373
     56 1963 "17"  198200357 1204819326  350799698  .5630758 14371.427
    470 1963 "22"     579600    1092001     772800  .4270359 2409.7773
    788 1963 "28"     976190    6190476    1761905 .48436305 2256.2856
     36 1963 "21"   70560011  395360063  169120027  .7586986 19166.545
    288 1963 "17"     676056    4112676    1901408  .6085891  2951.635
    364 1963 "21"     409241    2724752    1149835 .45206895 13145.432
    372 1963 "17"   26878922  140000176   49839242  .4762844 12042.435
    124 1963 "33"   66758521  241072439  120536219  .7681732 18873.299
     56 1963 "15"  170999977 2914399565  651600525  .5630758 14371.427
    348 1963 "20"   20102215  146933560   52385009  .6535436         .
    348 1963 "25"   13160136  114991482   40459966  .6535436         .
    348 1963 "21"    8901193   80068143   31090290  .6535436         .
    392 1963 "27"  794444444 7580555556 2091666667  .7718055  8275.577
    752 1963 "24"  106703575  498723230  247428579  .7406743 18558.988
    554 1963 "24"   14752626  116797415   38376293  .6389886 18497.617
    250 1963 "21"          . 1054068956  359728958  .5855801  14469.21
    246 1963 "27"   21248567  228124808   43751312  .6219669  12082.25
    702 1963 "27"     359334    3593339    1633336  .4753499  6015.885
    170 1963 "28"   15444444   90555556   43444444  .6257223  4280.184
    410 1963 "33"     423077    3076923    1230769   .758609 1722.5104
    800 1963 "15"    5164217  113251995   11621728 .21304467 1027.2823
    364 1963 "23"      66007     528053     264026 .45206895 13145.432
    608 1963 "20"   12530614   86435662   35546027  .6826711   3093.64
    218 1963 "24"    1472223   12333338    6000000  .5130082 4880.8784
    124 1963 "15"  787194232 5878458694 1835859340  .7681732 18873.299
    528 1963 "23"   27072158  560773709   69889279   .609821 17149.121
    608 1963 "28"    9206165   79786765   27362769  .6826711   3093.64
     36 1963 "15"  288960046 2279200365  679840109  .7586986 19166.545
    826 1963 "23"  117599953 2099999160  307999877  .7767401 15766.834
    710 1963 "26"   47599981  194599922  107799957  .7362456  8701.798
    410 1963 "26"    8461538   58461538   28461538   .758609 1722.5104
    608 1963 "17"   15343609  129909220   46798006  .6826711   3093.64
    152 1963 "24"   18495684  160295931   86313194  .4500653  6423.558
    288 1963 "16"     422535   20507042   16492958  .6085891  2951.635
    792 1963 "24"   14222222  122222222   51111111  .6656876  5975.373
    364 1963 "27"     646865   14521452    3234323 .45206895 13145.432
    528 1963 "15"  298342834 3646409778  616022315   .609821 17149.121
    372 1963 "24"    9519756   70001191   25760897  .4762844 12042.435
    800 1963 "16"     455271    2861683    1058799 .21304467 1027.2823
    300 1963 "20"    3736895   31337672    9995343  .6587666   9035.88
    288 1963 "28"     873239   10070423    2492958  .6085891  2951.635
     36 1963 "25"   77280012  328160053  141120023  .7586986 19166.545
    410 1963 "21"    3846154   46923077   17692308   .758609 1722.5104
    792 1963 "36"    1000000    7222222    3444444  .6656876  5975.373
     36 1963 "24"  105280017  744800119  331520053  .7586986 19166.545
    800 1963 "20"     680386    2799944    1147977 .21304467 1027.2823
    170 1963 "23"    4666667  1.050e+08   28222222  .6257223  4280.184
    348 1963 "24"   42546848  507666099  189097104  .6535436         .
    410 1963 "25"    5800000   56153846   15576923   .758609 1722.5104
     36 1963 "33"   15680003   48160008   26880004  .7586986 19166.545
     36 1963 "27"  249760040 1562400250  507360081  .7586986 19166.545
    710 1963 "28"   97999961  390599844  172199931  .7362456  8701.798
    170 1963 "27"    3222222   82888889   26222222  .6257223  4280.184
    218 1963 "22"    1166667    5888889    3222222  .5130082 4880.8784
    554 1963 "26"   23053585  100112070   44216164  .6389886 18497.617
    300 1963 "15"   30429005  381004305   85664634  .6587666   9035.88
    608 1963 "15"   39381929  561064621  247287825  .6826711   3093.64
    840 1963 "25"  2.370e+09  9.130e+09  4.650e+09  .7825109  20620.72
    246 1963 "17"   48125143  181250128   81250314  .6219669  12082.25
    800 1963 "24"     329413    4110178     615988 .21304467 1027.2823
    702 1963 "22"    4573340   17313359    9800014  .4753499  6015.885
    710 1963 "17"   47599981  289799884  116199954  .7362456  8701.798
    826 1963 "26"  671999731 2239999104 1259999496  .7767401 15766.834
    376 1963 "26"   17666667   96333333   57666667  .6215691  11281.71
    608 1963 "25"    5621131   48090961   22170236  .6826711   3093.64
    364 1963 "17"   20039604  184313663   87619010 .45206895 13145.432
    124 1963 "26"  202129968  825209501  435784793  .7681732 18873.299
    554 1963 "25"   17088574   69522271   33216351  .6389886 18497.617
    590 1963 "21"     684000    5550000    1950000  .5293286  5135.478
    end
    With the "desired" regression being

    Coeff_diversi (measure of dispersion)= beta_0+ beta_1*gdp_per_capita i. country1 (country fixed effects), robust with a window og 10000 in gdp_per_capita

    Thanks!



  • #2
    I don't get what you want here. A rolling regression window refers to a period of time. How is 10000 in gdp_per_capita a period of time? What am I missing?

    Comment


    • #3
      I want a regression with a rolling income window. I want to see how coefficients (betas) change over different ranges of gdp per capita. Perhaps, I miswrote the question. My apologies.

      Comment


      • #4
        All right. To be honest, I'm not sure this approach makes a lot of sense statistically, but I'm not going to dive into that. I think the following code does what you ask for:

        Code:
        capture program drop one_regression
        program define one_regression
            capture regress coeff_diversi gdp_percapita i.country1, robust
            if c(rc) == 0 {
                gen b = _b[gdp_percapita]
                gen se = _se[gdp_percapita]
            }
            else if inlist(c(rc), 2000, 2001) { // DO NOT CHANGE THESE NUMBERS!!!!!
                continue
            }
            else {
                gen error_code = c(rc)
            }
            exit
        end
        
        rangerun one_regression, interval(gdp_percapita -10000 10000)
        -rangerun- is by Robert Picard and Nick Cox. It is available from SSC. Using it requires also having -rangestat- installed. -rangestat- is by Robert Picard, Nick Cox, and Roberto Ferrer. It is also available from SSC.

        The above code picks out, for each observation in your data set, all other observations where gdp_percapita lies between 10000 below and 10000 above that observation's gdp_percapita value and does the regression on those, storing the coefficient and its standard error back in the data set. (You didn't say which outputs you wanted, but it seemed most likely that you are interested in the coefficient, and you should have some measure of the uncertainty also, so I settled on the standard error as the simplest.)

        If by a 10000 window you meant from 5000 below to 5000 above, just change those numbers in the -rangerun- command; everything else can be left the same.

        If you wanted to do these separately by year, or by isic, or both, -rangerun- has a -by()- option which you can use accordingly to achieve that.

        In the event that for some observation there simply aren't enough other observations within the window to do the regression, this code will skip over those without any error message and will just leave the b and se results as missing value for that observation. If any other error arises during the regression command, the program will create a new variable containing the error code that was generated.

        Added: I notice that when this runs on your example data, the coefficients are very small numbers, typically on the order of 0.0001. You might want to consider rescaling the gdp_percapita variable, changing it from dollars (assuming that's the current unit) to thousands of dollars. That way the coefficients will be more like "normal" numbers. (If you do that, you have to rescale the window numbers in the -interval()- option of -rangerun- too.)
        Last edited by Clyde Schechter; 29 Mar 2022, 14:44.

        Comment


        • #5
          Thank you very much. I am trying your sintax in a couple of minutes Let me give you a little bit of context in a way to clarify my reasoning behind. An existing literature states that the coefficient (that measures specialization) changes according to the GDP levels (higher or lower). As a consequence, I am trying to test that hypothesis with this empirical strategy

          Comment


          • #6
            I get it. I suppose it's a reasonable first step.

            Comment


            • #7
              The code takes a while to run or is it my program?

              Comment


              • #8
                If your data set is large, it's going to take a very long time to run. It's doing a separate regression keyed to each observation in the data set. If you have 25,000 observations, that's 25,000 regressions. That's going to be slow.

                Comment


                • #9
                  Thank you, how do I see the coefficients? It seems that it finished running but I cannot find stored coefficients or a new variable with coefficients grouped
                  Last edited by Hugo Rocha; 29 Mar 2022, 18:41.

                  Comment


                  • #10
                    When I ran the code in #4 with the example data in #1, the resulting data set had 2 new variables, located at the end: b and se. These are the coefficient and the standard error. Here's the output for the first five observations:

                    Code:
                    . list in 1/5
                    
                         +--------------------------------------------------------------------------------------------------------+
                         | country1   year   isic       Wages   OutputI~4   ValueAd~d   coeff_~i   gdp_pe~a          b         se |
                         |--------------------------------------------------------------------------------------------------------|
                      1. |      218   1963     16      305556     4944444      777778   .5130082   4880.878   .0000262   1.95e-20 |
                      2. |      578   1963     17    36539985   1.624e+08    68599973   .7106571   19577.57   .0000164   3.18e-19 |
                      3. |      826   1963     28   1.120e+09   4.340e+09   2.016e+09   .7767401   15766.83   .0000164   1.68e-19 |
                      4. |       56   1963     23    31799934   8.408e+08    69199850   .5630758   14371.43   .0000164   1.24e-19 |
                      5. |      710   1963     15   1.148e+08   1.140e+09   3.248e+08   .7362456   8701.798   .0001531   1.17e-17 |
                         +--------------------------------------------------------------------------------------------------------+

                    Comment


                    • #11
                      I am trying the code tomorrow again ( I am preparing a class for tomorrow). Thank you so much, Professor Schechter!

                      Comment

                      Working...
                      X