Announcement

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

  • "Asreg" rolling window regressions with monthly data

    Hi everyone:

    I have a question regarding the asreg command written by @Attaullah Shah. Since I am an absolute beginner and new in this forum, my first question is: Is it possible to estimate rolling window regressions with that command for data that is reported monthly?

    I tried running a few example regressions with that command (daily data which if found online) - it worked perfectly well within an impressively short amount of time. My own data, however, is an unbalanced panel monthly data set structured in the long format in Stata.

    My original date variable looks like follows: 201501, 201502, 201503, and so forth. Beyond that I have a clear Identifier variable for my panel structure and variety of further variables.

    I created my monthly date variables by using the numdate command:

    numdate monthly mdate = _date, pattern(YM) topyear(2050)

    By doing this, my data looks like: 2015m1, 2015m2, 2015m3, and so forth.

    When running the asreg command, all regression estimates are missing values:

    bys Identifier: asreg Y X1 X2 X3, wind(mdate 12)

    For each Identifier, there is more than 10 years of monthly data available. So my aim was to calculate for each Identifier regression results that are based on one year of monthly data for each point in time (month).

    Any suggestions are greatly appreciated. Thank you for your help in advance!

    Best,
    Dennis

  • #2
    Hi Dennis Mueller , and welcome to Statalist!

    I haven't used asreg (SSC), but in order for others to help you, it will be super helpful if you could share a sample of your data using Stata's dataex command. If you need more help using dataex, there is a YouTube tutorial here (feel free to watch it at 2x speed, and you can probably stop after first 6 minutes).

    Code:
    bysort Identifier:  gen n = _n
    ssc install dataex  // if not already installed
    dataex Identifier Y X1 X2 X3 mdate _date if n<=5  // include other variables that you think will be helpful
    * dataex defaults to sharing 100 obs  (the above will share the 1st 5 obs of 20 different ID's)
    * The dataex output will be ugly to read (for people) but makes it very easy for Stata to read
    Last edited by David Benson; 02 Jan 2019, 16:13.

    Comment


    • #3
      Hi David, thank you very much for your fast reply. Also, thanks for the hint regarding the dataex command, I will use it for further posts.

      Luckily, by adjusting and re-running my code right now, in order to provide a meaningful insight in my data set, my problem disappeared.

      I now use:

      bys Identifier: asreg Y X1 X2 X3 [...] , window(mdate 12) min(12)

      ...setting a minimum for the observations and thus creating a kind of rolling regression window with a "fixed length" of 12 months, not starting directly when enough observations for the calculation are available. I know that this shouldn't be the solution for my previous error, however, it does run perfectly now. I maybe incorporated too many variables for the testing purpose with regards to my defined window earlier.

      Best,
      Dennis

      Comment


      • #4
        Glad you found the solution (and thanks for posting it so that others with similar problem can use it to solve their own!)

        Comment


        • #5
          Dennis Mueller I am happy that the command worked for you. However, I would appreciate if you could share the data and the exact code that you said generated missing values. That would help me in identifying any possible coding omission. If you are comfortable with sharing the data (actual or fabricated), you can send it through a dropbox link in a private message in this forum.
          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


          • #6
            Attaullah Shah I am happy to send you a private message regarding my asreg error!

            Comment


            • #7
              After re-evaluating my problem with @Attaullah Shah, I can confirm that it did not arise due to any issues with the asreg command.

              I anybody is having similar issues, I did estimate:

              bys Fund_ISIN: asreg ER MKT SMB HML MOM , window(mdate 12) min(12)

              So I estimated the classical Fama-French four-factor alpha (respective intercept of the equation). My fault was, I used (just for trying out) window(mdate 4) first, where statistics don't agree with regarding # of variables / obs. As soon as I changed the window to - for example - 12, everything went fine.

              Comment


              • #8
                Hello Attaullah Shah, I am also encountering a problem with the asreg function.

                I have used it to estimate idiosyncratic volatility, which is simply the standard deviation of the residual. I want to run my regression on a rolling basis of 24 months.

                My code is:
                bys permo : asreg Excess_USD_w MKT SMB HML, wind(ymdate 24) se fit *(I also drop all observations less than 24.)
                bys permo: egen IVOL=sd (residuals)

                However, when I look at my data, I only get one value for each permo, instead different values for the rolling basis.

                I would appreciate if you could help me figure out what is the problem here. Thank you kindly in advance!

                Comment


                • #9
                  #8 is a variant on https://www.statalist.org/forums/for...rolling-window

                  Kate: We appreciate that you want an answer, but please don't ask the same question in different places. Or, if this is a different question, please (a) give a cross-reference in each thread (b) explain what the difference is.

                  Comment


                  • #10
                    Nick Cox I understanding what you mean, however, it is different to that on the other forum since I skip the step in the middle to find the residuals and do that through the asreg program instead. In addition, it is better explained here, just as you suggested.

                    I apologise for the confusion.

                    Comment


                    • #11
                      My code is:
                      bys permo : asreg Excess_USD_w MKT SMB HML, wind(ymdate 24) se fit *(I also drop all observations less than 24.)
                      bys permo: egen IVOL=sd (residuals)

                      However, when I look at my data, I only get one value for each permo, instead different values for the rolling basis.
                      Please note that egen does not estimate statistics in a rolling window. You can use asrol for this, so changing your code to

                      Code:
                      ssc install asrol
                      bys permo : asreg Excess_USD_w MKT SMB HML, wind(ymdate 24) se fit min(24)
                      bys permo: asrol residuals, gen(IVOL) stat(sd) window(ymdate 24) min(24)
                      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


                      • #12
                        Originally posted by Attaullah Shah View Post
                        Please note that egen does not estimate statistics in a rolling window. You can use asrol for this, so changing your code to

                        Code:
                        ssc install asrol
                        bys permo : asreg Excess_USD_w MKT SMB HML, wind(ymdate 24) se fit min(24)
                        bys permo: asrol residuals, gen(IVOL) stat(sd) window(ymdate 24) min(24)
                        Thank you very much Attaullah Shah. Then, I will just always use the min function. I appreciate your help.

                        Comment


                        • #13
                          Originally posted by Attaullah Shah View Post
                          Please note that egen does not estimate statistics in a rolling window. You can use asrol for this, so changing your code to

                          Code:
                          ssc install asrol
                          bys permo : asreg Excess_USD_w MKT SMB HML, wind(ymdate 24) se fit min(24)
                          bys permo: asrol residuals, gen(IVOL) stat(sd) window(ymdate 24) min(24)
                          Just a follow-up question: Is it necessary to use min(24) to generate IVOL? I ask because I then have many missing observations. Whereas, when I compute without min(24), as you can see through the variable IVOL_G, many more observations are present. Thank you in advance for your help.




                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input str6 permo double Date float(ymdate _Nobs_IVOL) double(_residuals IVOL IVOL_G)

                          "13259U" 16891 554 . . . .
                          "13259U" 16919 555 . . . .
                          "13259U" 16952 556 . . . .
                          "13259U" 16982 557 . . . .
                          "13259U" 17013 558 24 -.053328748384746744 . .
                          "13259U" 17044 559 24 -.08492014588617035 . .022338491400416386
                          "13259U" 17073 560 24 .049396282159429365 . .0702274295169198
                          "13259U" 17105 561 24 -.012148787775128624 . .058001872448014156
                          "13259U" 17135 562 24 -.013995624237627274 . .050482637370450556
                          "13259U" 17164 563 24 .0016441575774402972 . .046260294731879745
                          "13259U" 17197 564 24 .03437884799175206 . .04678403070082647
                          "13259U" 17225 565 24 .09191750052567091 . .05663333627765756
                          "13259U" 17255 566 24 .0037643537441928027 . .0529804665142747
                          "13259U" 17286 567 24 -.07006175242727036 . .054884161478410076
                          "13259U" 17317 568 24 -.00877322641113927 . .05207800390800316
                          "13259U" 17346 569 24 .016407108834407484 . .05006093916802455
                          "13259U" 17378 570 24 -.01454601084073728 . .04802210385519978
                          "13259U" 17409 571 24 -.025643675021741093 . .04647851013059361
                          "13259U" 17437 572 24 -.00013829001338982322 . .0448145812389794
                          "13259U" 17470 573 24 -.060258121178526664 . .04538991770082343
                          "13259U" 17500 574 24 -.0559556547968835 . .04539141362589485
                          "13259U" 17531 575 24 .0006962450465672898 . .044136075721801525
                          "13259U" 17562 576 24 -.12500101597459867 . .05021362907720539
                          "13259U" 17591 577 24 .1271443130139132 . .05856867359361576
                          "13259U" 17622 578 24 -.08143298721910654 . .059177326592297644
                          "13259U" 17652 579 24 .022235066484589866 . .05824802986044777
                          "13259U" 17682 580 24 .005759573696555206 . .057025883678188
                          "13259U" 17713 581 24 .0011700504536449746 .05582765736878367 .05582765736878367
                          "13259U" 17744 582 24 -.007008572552528555 .055077820087510554 .055077820087510554
                          "13259U" 17773 583 24 -.1403431071139683 .059410244129173465 .059410244129173465
                          "13259U" 17805 584 24 -.05143339113569187 .058521923201852045 .058521923201852045
                          "13259U" 17836 585 24 -.004783145708325776 .058557198631202054 .058557198631202054
                          "13259U" 17864 586 24 .0677202259682817 .060931194276499956 .060931194276499956
                          "13259U" 17897 587 24 -.03545889753988826 .061057575624194484 .061057575624194484
                          "13259U" 17927 588 24 .3025783186157863 .08847495009981339 .08847495009981339
                          "13259U" 17955 589 24 -.16284934359722958 .09196562270329522 .09196562270329522
                          "13259U" 17987 590 24 .03113445673992493 .09234296321181824 .09234296321181824
                          "13259U" 18017 591 24 .2069109235893152 .10151854602943496 .10151854602943496
                          "13259U" 18046 592 24 -.16890278410336862 .10724406024408067 .10724406024408067
                          "13259U" 18078 593 24 .05732681305022816 .10794429905759488 .10794429905759488
                          "13259U" 18109 594 24 -.07902266461773721 .10899906108685246 .10899906108685246
                          "13259U" 18140 595 24 .13312543725464962 .11259691343931509 .11259691343931509
                          "13259U" 18170 596 24 .03605969375713497 .11284691756397695 .11284691756397695
                          "13259U" 18200 597 24 -.0026238924739372532 .11210166688804357 .11210166688804357
                          "13259U" 18231 598 24 -.04234242539210836 .11182337513301754 .11182337513301754
                          "13259U" 18262 599 24 .022175973920607436 .11188358347611053 .11188358347611053
                          "13259U" 18319 601 . . . .11043110575441062
                          "13259U" 18352 602 . . . .11141571768233917
                          "13259U" 18382 603 . . . .11426850131047281
                          "13259U" 18413 604 . . . .11739790913719599
                          "13259U" 18443 605 . . . .1207872569266718
                          "13259U" 18473 606 . . . .12443640402175764
                          "13259U" 18505 607 . . . .12213343346328932
                          "13259U" 18535 608 . . . .12490115562438237
                          "13259U" 18564 609 . . . .1293511078363022
                          "13259U" 18596 610 . . . .13405517820942148
                          "13259U" 18627 611 . . . .1388118120232458
                          "13259U" 18658 612 . . . .11383645833767225
                          "13259U" 18686 613 . . . .10509115060947827
                          "13259U" 18717 614 . . . .11137994586294064
                          "13259U" 18746 615 . . . .09190761754839112
                          "13259U" 18778 616 . . . .06906972888916209
                          "13259U" 18808 617 . . . .07321516812789206
                          "13259U" 18837 618 . . . .06524556774446127
                          "13259U" 18870 619 . . . .03438905941274818
                          "13259U" 18900 620 . . . .03254540182635676
                          "13259U" 18931 621 . . . .04562139766532283
                          "13259U" 18961 622 . . . .
                          "13259U" 18991 623 . . . .
                          "13259U" 19023 624 24 .052935538185583136 . .
                          "13259U" 19052 625 24 -.021948557107469503 . .05295105158473715
                          end

                          Comment


                          • #14
                            min(24) is for you to use if and only if you don't think results based on fewer values are interesting or useful or intelligible or worthwhile or some such.

                            Specifically, if time windows are incomplete then it's harder to interpret results unless you know where values are missing (or -- subtly different -- absent), although not difficult to keep track of that too.

                            You have to look upstream and downstream to see what people in your field do and also imagine that someone might grumble at your work because they object to incomplete windows or windows with small numbers of present non-missing values.

                            Comment


                            • #15
                              Nick Cox Thank you for your response and feedback. I completely understand that consistent time windows are important. However, in the context of idiosyncratic volatility, the standard deviation of regression residuals, is it possible to estimate the residuals on a rolling window of 24 months with min(24), and since they are done that way, the standard deviation does not need to be estimated through a rolling window?

                              I end up losing two years of observations with the regression, and then another two years idiosyncratic volatility and I am not sure if that is okay. Thank you.

                              Comment

                              Working...
                              X