Announcement

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

  • Rolling replications when using rolling command with panel data

    I have a more general question that I am struggling to find a direct answer to about the functionality of -rolling- when it is used with panel data. I am trying to run a regression using -xtivreg2- for a moving window of dates. I had assumed that -rolling- would accomplish this easily, but the rolling replications have me a bit confused. As other posts have noted, it seems that after the regression is estimated for the entire panel for the given window, -rolling- goes on to run the same regression specification for each group (in my case each country) of the panel. Furthermore, it appears these rolling replication regression coefficients are saved and not the coefficients of the regression for the entire panel for a given timeframe, which is what I am ultimately interested in.

    I don't know if I just missed something in the documentation to suppress this, if this is just what -rolling- is always meant to do with panel data, or if there is some element of my regression specification that prompts this. In case it is helpful for addressing the question, here is what I am running:

    Code:
    rolling _b _se, window(4) start(1986q1) end(1996q1) clear: xtivreg2 infl (einfl ugap = L(0/1).r L(2/3).infl L(1/2).ugap L(1/2).ygap) linfl if peg==1, fe cluster(iso) robust

  • #2
    I think it would be fairer to say that -rolling- isn't meant to do anything with panel data. It is designed to work with single time-series data, and panel data just confuses it. It even has some safeguards against this built in:
    Code:
    . webuse grunfeld, clear
    
    . xtset
    
    Panel variable: company (strongly balanced)
     Time variable: year, 1935 to 1954
             Delta: 1 year
    
    . rolling _b, window(5) clear: xtreg mvalue kstock, fe
    rolling is not allowed with xtreg
    r(198);
    The reason you do not get a similar error message using -xtivreg2- is that the latter is a user-written command and it does not declare its properties to be an -xt- command, so -rolling- doesn't realize it is being snookered.

    When you have panel data, but want to moving-time-window analyses that apply across, rather than within, panels, you have to use other tools. I recommend using -rangerun- instead of -rolling- here. -rangerun- is written by Robert Picard, and is available from SSC. It has a lengthy help file with many worked examples. Moving window analyses are just one type of task it can do: it is a very flexible, multi-purpose command. To use -rangerun-, you must also install -rangestat-, written by Robert Picard, Nick Cox, and Roberto Ferrer, also available from SSC.

    Here is an example, using the StataCorp grunfeld data set and a simple -xtreg, fe- to illustrate the general approach:
    Code:
    webuse grunfeld, clear
    
    capture program drop one_regression
    program define one_regression
        xtset company year
        xtreg mvalue kstock, fe
        gen b = _b[kstock]
        gen se = _se[kstock]
        exit
    end
    
    rangerun one_regression, interval(year -5 -1)
    If your data set is very large, you will want to speed up the performance. As shown above, the same regression is carried out for every observation in the data set having the same year. Clearly it is really only necessary to do this once and then copy the results. In the help file you will see the technique for doing this demonstrated. Find "Additional examples" in the help file. The very first of these, "Controlling the sample: Median salary of non-teammates" shows how to do this.

    Added: Just to be clear, the -rangerun- example I have shown is for a 5 year moving window.
    Last edited by Clyde Schechter; 09 Jun 2023, 11:30.

    Comment


    • #3
      Thank you so much for the straightforward answer, Clyde. I knew that the command was intended for time-series data, but I didn't realize that it had to be a single time series. I appreciate your suggestions on how to get around this and your sample code. I will look into the documentation for rangerun.

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        I think it would be fairer to say that -rolling- isn't meant to do anything with panel data. It is designed to work with single time-series data, and panel data just confuses it. It even has some safeguards against this built in:
        Code:
        . webuse grunfeld, clear
        
        . xtset
        
        Panel variable: company (strongly balanced)
        Time variable: year, 1935 to 1954
        Delta: 1 year
        
        . rolling _b, window(5) clear: xtreg mvalue kstock, fe
        rolling is not allowed with xtreg
        r(198);
        The reason you do not get a similar error message using -xtivreg2- is that the latter is a user-written command and it does not declare its properties to be an -xt- command, so -rolling- doesn't realize it is being snookered.

        When you have panel data, but want to moving-time-window analyses that apply across, rather than within, panels, you have to use other tools. I recommend using -rangerun- instead of -rolling- here. -rangerun- is written by Robert Picard, and is available from SSC. It has a lengthy help file with many worked examples. Moving window analyses are just one type of task it can do: it is a very flexible, multi-purpose command. To use -rangerun-, you must also install -rangestat-, written by Robert Picard, Nick Cox, and Roberto Ferrer, also available from SSC.

        Here is an example, using the StataCorp grunfeld data set and a simple -xtreg, fe- to illustrate the general approach:
        Code:
        webuse grunfeld, clear
        
        capture program drop one_regression
        program define one_regression
        xtset company year
        xtreg mvalue kstock, fe
        gen b = _b[kstock]
        gen se = _se[kstock]
        exit
        end
        
        rangerun one_regression, interval(year -5 -1)
        If your data set is very large, you will want to speed up the performance. As shown above, the same regression is carried out for every observation in the data set having the same year. Clearly it is really only necessary to do this once and then copy the results. In the help file you will see the technique for doing this demonstrated. Find "Additional examples" in the help file. The very first of these, "Controlling the sample: Median salary of non-teammates" shows how to do this.

        Added: Just to be clear, the -rangerun- example I have shown is for a 5 year moving window.
        Hello Clyde.
        I recently ran the command you provided, the example using -xtreg, fe. However, I'm puzzled as to why all the coefficients are the same across companies and years. I attempted to include a "by(company)" command after "interval(year -5 -1)", but then it fails to run (doesn't calculate "b" or "se"). Any thoughts on this? Thanks in advance.

        Comment


        • #5
          Bruno Paisani

          #4 seems very incomplete as a question. Are you using exactly the same data and exactly the same code as in #2? If not, we may need to know much more about your real data and your real code. I suspect that you need an expert on panel models to advise, and that's not me.

          But I can make three points.

          1. xtreg as a panel regression necessarily uses data from all available panels unless instructed otherwise. Running it from rangerun has a side-effect that results are repeated as new variables for all panels. This isn't a problem; it just means that your results are repeated with no obvious advantage to you. You used a rather general tool which lacks the intelligence to see that it's repeating itself.

          As a extreme demonstration with the Grunfeld data the rangerun version repeats the instruction -- fit the panel regression with all available data -- again and again.

          Code:
          . xtreg mvalue kstock, fe
          
          Fixed-effects (within) regression               Number of obs     =        200
          Group variable: company                         Number of groups  =         10
          
          R-squared:                                      Obs per group:
               Within  = 0.1417                                         min =         20
               Between = 0.4545                                         avg =       20.0
               Overall = 0.2388                                         max =         20
          
                                                          F(1, 189)         =      31.21
          corr(u_i, Xb) = 0.3967                          Prob > F          =     0.0000
          
          ------------------------------------------------------------------------------
                mvalue | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
          -------------+----------------------------------------------------------------
                kstock |   .5510551   .0986341     5.59   0.000       .35649    .7456203
                 _cons |   929.5804   35.56927    26.13   0.000     859.4167    999.7442
          -------------+----------------------------------------------------------------
               sigma_u |  1262.9068
               sigma_e |   323.7247
                   rho |  .93834456   (fraction of variance due to u_i)
          ------------------------------------------------------------------------------
          F test that all u_i=0: F(9, 189) = 256.49                    Prob > F = 0.0000
          
          .
          . rangerun one_regression, interval(year . .)
            (using rangestat version 1.1.1)
          
          . ds
          company  year     invest   mvalue   kstock   time     b        se
          
          . l b se
          
               +---------------------+
               |        b         se |
               |---------------------|
            1. | .5510551   .0986341 |
            2. | .5510551   .0986341 |
            3. | .5510551   .0986341 |
            4. | .5510551   .0986341 |
            5. | .5510551   .0986341 |
               |---------------------|
            6. | .5510551   .0986341 |
            7. | .5510551   .0986341 |
            8. | .5510551   .0986341 |
            9. | .5510551   .0986341 |
           10. | .5510551   .0986341 |
               |---------------------|
           11. | .5510551   .0986341 |
           12. | .5510551   .0986341 |
           13. | .5510551   .0986341 |
           14. | .5510551   .0986341 |
           15. | .5510551   .0986341 |

          The output is truncated here but is all the same all through the data, as it should be.

          2. by(company) with rangerun splits calculations so that they are entirely separate by the variable concerned. I am no kind of expert on panel models, but I guess that means running a panel regression separately for each panel. If that makes sense and is what you really want, I confirm that the code runs but doesn't produce any new results. . What to blame there is not clear to me. I suspect you're trying that because you're misunderstanding the point made under #1.

          3. Rolling regressions seem quite fashionable. It's often true, however, that fitting quite complicated models to short runs of data is optimistic, or rather naive, both statistically and scientifically (in this case perhaps economically). But that's for experts to judge.
          Last edited by Nick Cox; 15 May 2024, 02:46.

          Comment

          Working...
          X