Announcement

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

  • Monthly rolling regressions - recording residuals

    Hi All,

    Thanks for reading.

    I currently have daily time series data for the periods from July 2010 to March 2019. I am looking to run a regression for every month against 3 independent variables in order to record the daily residual values.

    I am attempting to follow these instructions: "Regressions are conducted every month for each stock with its idiosyncratic risk for the particular month represented by the standard deviation of the regression residuals."

    So after the daily residuals are recorded using data from the first month (Jul 2010), data from the second month (Aug 2010) is regressed against the second month independent variables and again the residuals are recorded and so on until the last month (Mar 2019).... if my understanding is correct.

    Any help would be greatly appreciated.

    Thanks in advance.

    Alex

  • #2
    Show us your variable names please to get specific advice. You can't expect us to know what you are and if we guess at what they might be that is more work for all.

    See 12.2 in https://www.statalist.org/forums/help#stata for more specific details about how to do this. You were asked to read this before posting.

    Comment


    • #3
      Apologies Nick,

      This is an example of the data and variables i am using.
      Dependent Independent Variables
      X Mkt SMB HML
      01/07/2010 -0.92% -0.40% -0.31% -0.43%
      02/07/2010 -0.89% -0.50% -0.31% -0.43%
      06/07/2010 3.23% 0.33% -2.00% 0.08%
      07/07/2010 3.92% 3.17% 0.06% 0.45%
      08/07/2010 -1.05% 1.00% 0.57% 0.00%
      09/07/2010 -0.82% 0.81% 0.65% 0.58%
      12/07/2010 -1.50% -0.11% -1.19% 0.05%
      13/07/2010 0.62% 1.76% 1.62% 0.65%
      14/07/2010 0.39% -0.04% -0.16% -0.76%
      15/07/2010 -0.50% 0.02% -0.87% -0.10%
      16/07/2010 -2.99% -2.94% -0.56% -0.96%
      19/07/2010 -0.14% 0.54% -0.15% -0.19%
      20/07/2010 5.55% 1.23% 0.65% 0.14%
      21/07/2010 -2.06% -1.30% -0.38% -0.42%
      22/07/2010 1.69% 2.37% 1.25% 0.54%
      23/07/2010 0.82% 1.05% 1.47% -0.23%
      26/07/2010 -1.01% 1.23% 0.87% 0.11%
      27/07/2010 -1.49% -0.23% -0.33% 0.35%
      28/07/2010 0.22% -0.82% -0.90% -0.03%
      29/07/2010 -0.39% -0.36% 0.32% 0.67%
      30/07/2010 1.56% 0.06% 0.03% -0.22%
      02/08/2010 0.97% 2.08% -0.54% 0.15%
      03/08/2010 -0.49% -0.55% -0.41% -0.51%

      I understand how to run the regression for each month however doing this manually is very time consuming, is there a way to repeat the regression for each month automatically?

      Thanks again.

      Comment


      • #4
        You didn't follow 12.2 in https://www.statalist.org/forums/help#stata which as said gives more specific details about how to do this. No display format known to me shows % so I am unclear whether these data are even in Stata yet.

        But it appears that you have a daily date variable, which you haven't named. Something like this might work and naturally you need to use your actual variable name if it's not date.

        Code:
        gen mdate = mofd(date) 
        rangestat (reg) X Mkt SMB XML, int(mdate 0 0) 
        gen res = X - b_cons - b_Mkt * Mkt - b_SMB * SMB - b_XML * XML
        where rangestat must be installed using

        Code:
        ssc install rangestat
        If you're being graded on this, know that many teachers will squawk at naming your outcome or response variable X.....

        Comment


        • #5
          Hi Nick,

          I was using Dataex however as you rightly say, i needed to reformat it since my original post was incorrect. There are plenty of decimal places and it looked unorganised.


          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double(MktRF SMB HML SPGUS)
                          .0031                -.0058   .012199999999999999    .016566958122415487
                          .0013                -.0025                 .0052    .005205975554544828
                           .004                 .0008   .009399999999999999  -.0027020941229445317
                          .0033                  .004                 .0001   .0020320614134170523
                          .0013                -.0013                -.0021   -.009013068949980552
                           -.01                -.0019                -.0131   -.012278308321965414
                          .0085                 .0027  .0034000000000000002    .021178637200734096
                          .0024  .0028000000000000004                 .0005    .005635707844907412
           -.011200000000000002                -.0013                 -.004    -.01367406411118588
                          .0126                 .0043                -.0016    .004318181818180111
                         -.0098                -.0059                 .0025   .0031681375876955187
                         -.0174  .0040999999999999995                -.0115   -.007895330475978346
           -.021400000000000002  .0055000000000000005  -.008199999999999999    -.04092769440654609
                          .0037                 -.002  .0034000000000000002    .024182076813653808
          -.0045000000000000005                -.0009                -.0046   -.015277777777778168
                          .0053                 .0023                 .0016    .002820874471085588
                         -.0118                -.0067                 .0046   -.007969995311766676
                         -.0097                -.0004 -.0045000000000000005   .0030718336483956943
                          .0139                 -.002                 .0091    .014840989399291571
                          .0121                -.0042                 .0066  -.0011606313834729987
                         -.0049                 .0011                -.0038   -.005577504066928696
           -.031400000000000004                -.0015  -.011899999999999999    -.04136480486095284
                          .0029                -.0005                -.0006    -.02242808386152806
                         -.0079                 .0005                -.0036    .014214463840402036
            .013300000000000001                 .0032                 .0059    .019178755839684536
          -.0017000000000000001                 .0025                 .0029   .0033775633293102736
                          .0113  .0070999999999999995  .0007000000000000001     .02236114450589155
          -.0007000000000000001                 .0097  .0014000000000000002   .0023518344308570693
                          .0175 -.0028000000000000004                 .0102    .017362740497413043
                          .0049                 .0011                 .0012    .008071955719560098
                          .0062                 .0011                 .0001   -.007320979180966414
           .0028000000000000004                 .0005                 .0048    .012214796035950659
                         -.0005                -.0004   .005699999999999999   -.003187613843349424
                         -.0124                 .0027  -.009399999999999999    -.02215623572407266
            .009399999999999999 -.0034000000000000002                 .0053   -.004438215370238696
                         -.0013                 .0024  .0028000000000000004     .00422336931017786
                          .0013 -.0055000000000000005  .0039000000000000003  -.0032710280373809963
                           .012   .012199999999999999  .0007000000000000001    .006329113924050889
                          .0032  .0060999999999999995                 .0018   -.003494060097834706
                          .0009  .0017000000000000001                 .0009   .0046750818139333905
                          .0037                 .0009                 .0023  -.0004653327128882889
                          .0143                  .004  .0060999999999999995    .010474860335190739
                          .0002                 .0008                 .0026   -.022114720110568142
                          .0021                 .0025                 -.001     .01036513545347506
                          .0053                 .0005                 .0069   .0051294007927256136
                          .0043                -.0009                -.0011    .009046624913013224
                         -.0002                 .0003 -.0028000000000000004  -.0027586206896546006
                         -.0002                -.0031                -.0048  -.0009220839096408806
           .0078000000000000005 -.0014000000000000002                 .0059    .021227503461011432
            .005699999999999999                 .0004  .0034999999999999996 -.00022593764121425775
                          -.001 -.0017000000000000001  -.005699999999999999   -.005423728813558015
                         -.0062                 -.006                -.0063   .0063621904112660665
                          .0067                 .0089                 .0001   -.000903138405957904
                           .008  .0034999999999999996                 .0036    .007231638418079944
                          -.006                -.0064   .008199999999999999   -.002243661655823015
                         -.0026                -.0048 -.0028000000000000004    -.04519901056892528
                          .0006                -.0008                 .0031   -.019076778144137108
                          .0059 -.0007000000000000001                 .0019    .026890756302528462
                          .0004                 .0029 -.0045000000000000005   .0002338087444422321
                         -.0036                 -.005                 .0015   -.003973819541841839
                          .0075                -.0001                 .0079    .002102180239377981
                          .0095   .010700000000000001                 .0092    .014509906323188448
                          .0019                 .0023                 .0046   -.011782853185594733
                         -.0048                 .0021                 .0024   -.022901847699136658
                          .0031 -.0028000000000000004  .0039000000000000003    .004771257470716449
                          .0066                -.0012  .0017000000000000001     .01402759219605183
                          .0023                 .0027                 .0006  -.0063450539652750056
           .0007000000000000001                 .0016                -.0004   -.004732550177097217
                          .0124                 .0087                 .0141  -.0009589916963196788
                          .0011                 .0059                -.0004  -.0019097862740464111
           -.015300000000000001                  .008                -.0183  -.0014375517487547518
           .0028000000000000004                -.0085                -.0009    -.01025541339051388
                          .0088                 .0066   .005600000000000001    .015396836783824707
                         -.0006  .0070999999999999995                  .002   -.003803266951166552
                          .0037   .008100000000000001                 .0049   .0035597287006200007
            .006999999999999999                 .0037   .005699999999999999    .004732708086318937
                         -.0043                 .0046                -.0031   -.012046818503657231
           -.023399999999999997                 .0011                -.0141    -.01721019111323867
            .005600000000000001                -.0074                 .0074    -.00851753524549741
                          .0134                 .0046                 .0046    .006364111301786556
                         -.0172                -.0097                -.0092   -.005856528623628111
            .013600000000000001   .006500000000000001  .0060999999999999995     .01150678510169285
                          -.025                -.0058  -.009000000000000001  -.0065506976744191155
                         -.0075                -.0074                -.0063    -.00317995854669877
                         -.0327                -.0027  -.009000000000000001   -.020802563600781077
                         -.0175  -.013500000000000002                -.0048    -.02923807894079349
                          .0447                 .0097   .011699999999999999     .05145680391147584
          -.0017000000000000001                 .0116                -.0015    .002682119432203377
                          .0163                 .0148                 .0019    .018296077617775252
           -.011200000000000002                 .0032                 -.003   .0007090795781333303
                         -.0192                -.0027                -.0076   -.013662694610772786
                          .0011                 .0004  -.005600000000000001    .008246435162699472
                         -.0141                -.0024                -.0096   -.009403063583814642
                         -.0063                -.0097                 .0003   -.015570418186240543
           -.039900000000000005                -.0089                -.0116    -.03557433687330185
                          .0144                -.0044                 .0158     .03404889884762625
           -.011899999999999999                 .0033                -.0091   -.021555319465078427
                         -.0003  -.004699999999999999                 .0054    .003027205770690221
          -.0034999999999999996                 .0083                 .0038   .0030180090840264506
                          .0345                 .0067                 .0161     .03118496855345716
          end

          Thank you very much, the code you posted worked excellently as i am sure you know already

          Have a great day!

          Comment


          • #6
            Hi again Nick,

            Following on from this, is it possible to repeat this process for 100+ dependent variables without needing to copy the residuals manually? i.e would it be possible to run through a list of dependent variables and store the residuals in the same order?

            Thanks very much.

            Alex

            Comment


            • #7
              You can also try asreg for rolling and by-group regressions. In your specification, the regressions do not seem to be rolling, rather they are by-group regression. The grouping variable is the month. So

              Code:
              ssc install asreg 
               gen month = mofd(date)  bys month: asreg  X Mkt SMB XML, fit
              Option fit will report fitted values and residuals, no matter 10 varaiables or 100.
              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


              • #8
                #6 With rangestat, you would need to loop over dependent variables. Say

                Code:
                foreach Y in U V W X {
                    rangestat (reg) `Y' Mkt SMB XML, int(mdate 0 0)
                    gen res`Y' = X - b_cons - b_Mkt * Mkt - b_SMB * SMB - b_XML * XML
                    rename (reg_* b_* se_*) `Y'=
                }
                I doubt that there's a command in the Stataverse that does otherwise unless you program it for that specific purpose. If you don't care about results other than residuals, then you can drop them.

                Comment


                • #9
                  Hi Attaullah,

                  Thanks very much for your response. I notice that your suggestion will be extremely useful for the second stage of my analysis when i will be using the Fama-MacBeth cross sectional analysis approach.

                  With regards to the current problem, the solution Nick proposed earlier is precisely what i was looking for however my goal is to get this to repeat for the next dependent variable in the list. Perhaps i am not understanding the solution very well?

                  For example, the regressions on the daily data for SGPASX are completed for each month and the daily residuals are recorded. The process is then repeated for SHBLON, PLDUS... and so on.

                  Can asreg be used for this if i define the dependent variables in a list? if so, how would i approach this using this awesome tool.

                  Thanks
                  Date SGPASX SHBLON PLDUS MGRASX MktRF SMB HML
                  01/07/2010 -0.0027 -0.0297 -0.0009 -0.0152 -0.004 -0.0031 -0.0043
                  02/07/2010 0.0054 0.0049 -0.0282 0 -0.005 -0.0031 -0.0043
                  06/07/2010 0.0027 0.0239 -0.0224 -0.0077 0.0033 -0.02 0.0008
                  07/07/2010 0 0.0323 0.0665 -0.0117 0.0317 0.0006 0.0045
                  08/07/2010 0 0.0331 0.0274 0.0157 0.01 0.0057 0
                  09/07/2010 0.0107 -0.0099 0.0078 0 0.0081 0.0065 0.0058
                  12/07/2010 0.0132 0.0147 0.0053 -0.0078 -0.0011 -0.0119 0.0005
                  13/07/2010 0.0026 0.0075 0.0247 -0.0039 0.0176 0.0162 0.0065
                  14/07/2010 0.0182 0.0033 -0.017 0.0392 -0.0004 -0.0016 -0.0076
                  15/07/2010 -0.0102 -0.0003 -0.0205 0 0.0002 -0.0087 -0.001
                  16/07/2010 -0.0026 -0.0121 -0.0558 0 -0.0294 -0.0056 -0.0096
                  19/07/2010 -0.0104 0.0047 0.0348 -0.0038 0.0054 -0.0015 -0.0019
                  20/07/2010 0.0262 -0.0049 0.0101 0 0.0123 0.0065 0.0014
                  21/07/2010 -0.0077 0.007 -0.0641 0 -0.013 -0.0038 -0.0042
                  22/07/2010 -0.0206 0.0346 0.02 -0.0076 0.0237 0.0125 0.0054
                  23/07/2010 0.021 0.002 0.0196 0.0343 0.0105 0.0147 -0.0023
                  26/07/2010 -0.0077 0.0057 0.0479 -0.0037 0.0123 0.0087 0.0011
                  27/07/2010 0 0.0059 0.0069 0 -0.0023 -0.0033 0.0035
                  28/07/2010 0.0104 -0.0101 -0.0045 0.0111 -0.0082 -0.009 -0.0003
                  29/07/2010 -0.0282 0.0102 -0.0147 -0.0293 -0.0036 0.0032 0.0067
                  30/07/2010 0 -0.0148 0.0305 0 0.0006 0.0003 -0.0022
                  02/08/2010 0.0132 0.014 0.0441 0.0226 0.0208 -0.0054 0.0015
                  03/08/2010 0.0078 0.0079 -0.0131 0.0037 -0.0055 -0.0041 -0.0051
                  04/08/2010 0.0052 -0.002 0.0093 0.0073 0.0074 0.0043 -0.001
                  05/08/2010 -0.0026 -0.0074 -0.0054 -0.0037 -0.0022 -0.0082 0.0013
                  06/08/2010 -0.0103 -0.001 -0.0074 -0.0073 -0.0036 -0.0017 -0.0035
                  09/08/2010 -0.0104 0.0081 0.0191 -0.0222 0.0062 0.0062 0.0005

                  Comment


                  • #10
                    Hi Nick,

                    After a bit of work with Varlists i have made it work! Looks great and thank you very much! No more manual regressions :D

                    I appreciate all your help today. Thank you.

                    Comment


                    • #11
                      Here is the code you can use for the specification in #9
                      Code:
                      gen month = mofd(date)
                      
                      * Start a loop for dependent variables
                      
                      foreach var of varlist SGPASX SHBLON PLDUS MGRASX {
                      
                      * Estimate regressions in each month
                          bys month: asreg `var' MktRF SMB HML, fit
                      
                      * Rename the estimated residuals and append the variable names to the residual variables name
                          ren _residual res_`var'
                      
                      * Drop all uncessary regressions coefficients
                          drop _*
                        }
                      
                      . list month res_SGPASX res_SHBLON res_PLDUS res_MGRASX
                           +------------------------------------------------------------+
                           |  month   res_SGPASX   res_SHBLON    res_PLDUS   res_MGRASX |
                           |------------------------------------------------------------|
                        1. | 2010m7   -.00710118   -.02893251    .00947212   -.02260977 |
                        2. | 2010m7    .00138129     .0064605   -.01584412   -.00702392 |
                        3. | 2010m7    .00207392    .00736367   -.02129098    .00368618 |
                        4. | 2010m7   -.00525742    .00278118    .00788378   -.01112195 |
                        5. | 2010m7   -.00319024    .02560706    .00936851    .01021797 |
                           |------------------------------------------------------------|
                        6. | 2010m7    .01659821   -.01808083   -.00489728    .01036904 |
                        7. | 2010m7    .01418754    .00619942    .01327965     .0006544 |
                        8. | 2010m7     .0063025   -.00325014    -.0087502    .00008953 |
                        9. | 2010m7    .00775164    .00352592   -.01508663    .02087558 |
                       10. | 2010m7   -.01172017   -.00740885   -.01624759     .0024329 |
                           |------------------------------------------------------------|
                       11. | 2010m7   -.00500643    .00985205     .0039156   -.01058789 |
                       12. | 2010m7   -.01487968   -.00220392    .02689541   -.00918074 |
                       13. | 2010m7    .02417553   -.01441613   -.01224883   -.00300714 |
                       14. | 2010m7   -.00854667    .01447805   -.03569125   -.00333914 |
                       15. | 2010m7   -.02097472    .01749669   -.02507351   -.00715446 |
                           |------------------------------------------------------------|
                       16. | 2010m7    .01471841     .0000541   -.00207761    .01822672 |
                       17. | 2010m7   -.01005696   -.00248218    .02498101   -.00854836 |
                       18. | 2010m7    .00613683    .00167331    .01628294     .0128683 |
                       19. | 2010m7    .01308402   -.01102839    .01669144    .01878602 |
                       20. | 2010m7   -.01668223    .00909234   -.00319706   -.01045241 |
                           |------------------------------------------------------------|
                       21. | 2010m7   -.00299419   -.01678134    .03163461   -.00518086 |
                       22. | 2010m8   -.00167779    .00123513    .00123705   -.00230534 |
                       23. | 2010m8    .00484442    .00417535    .00078846    .00001241 |
                       24. | 2010m8    .00662949   -.00770486   -.00647881    .01153317 |
                       25. | 2010m8    .00209647   -.00136295   -.00144415    .00272581 |
                           |------------------------------------------------------------|
                       26. | 2010m8    -.0092684   -.00291997    .00134615   -.00437348 |
                       27. | 2010m8   -.00262419    .00657731     .0045513   -.00759256 |
                           +------------------------------------------------------------+
                      And please note that my code in #7 does exactly what Nick's code in #4 does. Due to formatting issues, the code was shown on one line. It should have been shown like this


                      Code:
                      ssc install asreg
                      gen month = mofd(date)
                      bys month: asreg X Mkt SMB XML, fit
                      You mentioned that you would estimate Fama and MacBeth regressions. There is a built-in option in asreg for estimating these regressions. See these resources

                      https://fintechprofessor.com/stata-p...sion-in-stata/

                      https://fintechprofessor.com/2018/12...dure-in-stata/

                      https://fintechprofessor.com/2019/05...sreg-in-stata/
                      Last edited by Attaullah Shah; 29 May 2019, 15:04.
                      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

                      Working...
                      X