Announcement

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

  • Rolling regression beta value

    Hello everyone, this problem has been bothering me for a while. I've tried different sets of code, but it still fails. While it might be simple for many, I really don't know what the issue is. I'm seeking help. 😢

    I want to estimate monthly beta for each firm (PERMNO) using a rolling regression with a twelve-month window, similar to the illustration below:
    Click image for larger version

Name:	S__3964936.jpg
Views:	1
Size:	187.2 KB
ID:	1725866


    My code is as follows:

    gen beta_EPU = .
    levelsof PERMNO, local(PERMNO_values)

    foreach PERMNO in `PERMNO_values' {
    bysort PERMNO: gen beta_EPU = .
    rolling, window(12): regress mr d_EPU
    replace beta_EPU = _b[d_EPU] if _n == 12
    }

    However, after execution, all the beta_EPU values are still missing. I'm not sure what went wrong, hope someone can help me. Thank you!

  • #2
    -rolling- was designed to work with a single time series, not panel data. It is difficult to work around these limitations.

    Fortunately, there is a much simpler way to do this:
    Code:
    rangestat (reg) mr d_EPU, by(PERMNO) interval(date 0 11)
    -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 18, 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.

    Comment


    • #3
      After using the rangestat command, I was able to successfully obtain the desired results! I'm truly grateful 🙏
      I've also installed dataex, so I'll remember to use it for future posts.

      Clyde Schechter I want to express my sincere thanks! While it might have been a small effort on your part, you really helped me a lot.! Sending angelic blessings your way 🕊️

      Comment


      • #4
        Are you calculating beta from future observations? If so, be sure you are following some theory. Conventionally, betas are calculated from past observations. In the following code, I show finding beta from both past and future observations using asreg that is much faster.
        Code:
        ssc install asreg
        
        * Rolling window of past 12 periods
        . asreg invest mvalue , by(company) window(year 12)
        
        . list company year _b_mvalue in 1/10
        
             +----------------------------+
             | company   year   _b_mvalue |
             |----------------------------|
          1. |       1   1935           . |
          2. |       1   1936           . |
          3. |       1   1937   .04135324 |
          4. |       1   1938   .05411168 |
          5. |       1   1939   .05233687 |
             |----------------------------|
          6. |       1   1940   .06102699 |
          7. |       1   1941   .06942586 |
          8. |       1   1942   .05101677 |
          9. |       1   1943   .05052367 |
         10. |       1   1944   .05623158 |
             +----------------------------+
        
        
        * Rolling window of future 12 periods
        
        . asreg invest mvalue , by(company) window(year 0 11)
        
        . list company year _b_mvalue in  1/10
        
             +----------------------------+
             | company   year   _b_mvalue |
             |----------------------------|
          1. |       1   1935   .08628562 |
          2. |       1   1936   .05810075 |
          3. |       1   1937   .05062214 |
          4. |       1   1938   .07981734 |
          5. |       1   1939   .01747126 |
             |----------------------------|
          6. |       1   1940   .05969137 |
          7. |       1   1941   .11869181 |
          8. |       1   1942   .22531085 |
          9. |       1   1943   .29493615 |
         10. |       1   1944   .28797581 |
             +----------------------------+
        Last edited by Attaullah Shah; 07 Sep 2023, 04:16.
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #5
          Attaullah Shah makes a good point. I'd like to clarify that the reason that I used -interval(date 0 11)-, which gives a forward 12 day window, is that -rolling, window(12)- produces that result. Since O.P. had used -rolling, window(12)- in her attempts, I replicated that with -rangestat-. Even at the time it struck me as a slightly odd choice, but I assumed that she has her reasons to do so.

          Comment


          • #6
            Attaullah Shah Thank you for the reminder! I didn't express myself well in the question,and there was an error in the initial code. Indeed, I wanted to calculate betas from past observations. Later, the code I used is as follows:

            Code:
            rangestat (reg) mr d_EPU, by(PERMNO) interval(year -1 -1)
            drop reg_nobs reg_r2 reg_adj_r2 b_cons se_`x' se_cons
            replace b_`x' = abs(b_`x')
            It seems that the program runs without any issues. In the future, if I have the same need, I will also try the "asreg" you provided. Thanks again!

            Comment

            Working...
            X