Announcement

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

  • Calculating rolling/recursive forecast

    Hi,

    I am trying to calculate one-step-ahead rolling/recursive forecast for my dataset. Was reading through the stata PDF help files and going through the examples but there seems to be an error.

    The stata help file is found here: https://www.stata.com/manuals13/tsrolling.pdf

    I downloaded the ibm dataset and followed the instructions found in page 7 to automatically calculate the forecast values apart from using rolling/recursive estimation to find the constant and slope coefficient:

    Click image for larger version

Name:	007.JPG
Views:	1
Size:	110.2 KB
ID:	1435987



    These are the results:

    Click image for larger version

Name:	008.JPG
Views:	1
Size:	109.0 KB
ID:	1435988


    I'm not sure what's wrong, and would appreciate any help..

    Cheers.

  • #2
    Another related question is: if i run one-step-ahead forecast using: rolling _b, recursive window(129) clear: regress rpce time2, my prior stored variables such as rpce and time2, along with the data set no longer appears in the Variables window, with only "start", "end", "_b_time2" and "_b_cons" left.
    Last edited by Benson Tay; 24 Mar 2018, 05:32.

    Comment


    • #3
      Please read and act on https://www.statalist.org/forums/help#stata - which explains that you should not post screenshots.

      Here your image is virtually unreadable on any device accessible to me but I will hazard a wild guess that your punctuation around

      Code:
      `if'
      is wrong in at least one instance. You can see this documented at [U] 18.3.1 which is bundled with your Stata as the .pdf documentation People sometimes use the same quotation mark to both left and right, but that's wrong.

      If this doesn't answer your question then please show your code as CODE as requested. It's your code that needs to be crystal clear, not that in the manual. The link above applies on that. Incidentally, I doubt that there is a difference but https://www.stata.com/manuals/tsrolling.pdf is in general a more up-to-date source (for Stata 15.1, as I write).

      I've not used this forecasting functionality myself, so that's likely to be as far as I can go.

      Comment


      • #4
        Note that you can replicate the example using rangestat (from SSC). It will be substantially more efficient (faster) than rolling and you do not have to figure out how to merge back the results with the original data.

        Code:
        use http://www.stata-press.com/data/r13/ibm, clear
        tsset t
        
        * create vars with last and next period values
        gen L_ibm  = L.ibm
        gen L_spx  = L.spx
        gen actual = F.ibm
        
        * recursive regression and generate forecast
        rangestat (reg) ibm L_ibm L_spx, interval(t . 0)
        gen forecast = b_cons + b_L_ibm*ibm + b_L_spx*spx
        
        * correlation using at least 20 periods
        corr actual forecast if t>=20
        and the results (match those in the manual):
        Code:
        . corr actual forecast if t>=20
        (obs=474)
        
                     |   actual forecast
        -------------+------------------
              actual |   1.0000
            forecast |  -0.0957   1.0000

        Comment


        • #5
          Hi Robert,

          Thank you. I used rangestat as suggested but couldn't get the same results i have manually derived, so there must be something wrong with the way im doing it.

          Basically, im trying to fit a simple linear model of: rpce = const + b_time2 + u

          I have observations of rpce over 132 periods, with the intention to do one-step-ahead forecasts of rpce for periods 129 - 132 recursively and evaluate the forecast errors.

          ie:

          When forecasting rpce(t=129), the model is estimated from observations in t = 1-128
          ... forecasting rpce(t=130), the model is estimated from observations in t = 1-129
          ... and so on.

          These are what i have input:

          Code:
          gen L_time2 = L.time2
          gen actual = F.rpce
          rangestat (reg) rpce L_time2, interval(time2 . 0)
          gen forecast = b_cons + b_L_time2*time2
          Appreciate your help on this.

          Cheers.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Please read and act on https://www.statalist.org/forums/help#stata - which explains that you should not post screenshots.

            Here your image is virtually unreadable on any device accessible to me but I will hazard a wild guess that your punctuation around

            Code:
            `if'
            is wrong in at least one instance. You can see this documented at [U] 18.3.1 which is bundled with your Stata as the .pdf documentation People sometimes use the same quotation mark to both left and right, but that's wrong.

            If this doesn't answer your question then please show your code as CODE as requested. It's your code that needs to be crystal clear, not that in the manual. The link above applies on that. Incidentally, I doubt that there is a difference but https://www.stata.com/manuals/tsrolling.pdf is in general a more up-to-date source (for Stata 15.1, as I write).

            I've not used this forecasting functionality myself, so that's likely to be as far as I can go.
            Apologies on the screenshots. Will not post them again.

            Thanks for your help too.

            Comment


            • #7
              The problem punctuation around the local macro is a consequence of copying code from Stata's PDF documentation, as suggested in the portion of that very documentation Nick cited in post #2.

              Here is the line of code in question, as copied from the PDF documentation for tsrolling cited in post #1:

              regress ibm L.ibm L.spx ‘if’

              Note that the local macro if is surrounded by characters known typographically as the left and right single quotation marks. Unfortunately, the keyboard characters used in Stata to expand a local macro are those known typographically as the grave accent and the apostrophe. Hence Stata did not recognize the sequence "left apostrophe i f right apostrophe" as a local macro to be expanded, and instead tried to interpret it as a variable name.

              It is regrettable that Stata documentation does not hew more closely to Stata syntax. This would have been less of a problem before Stata 11, but since then the availability of documentation as PDFs invites reuse of code from the documentation.

              Comment


              • #8
                re: #5

                With recursive regressions, the sample is different from one observation to the next so you must perform a new regression for each observation in the data. rangestat performs all the regressions is one pass (and does this very efficiently) and regression results for each observation are stored in new variables (number of observations, R2, adjR2, coefficients, and standard errors).

                Results for a given observation can easily be checked using standard Stata commands that target the subsample that is appropriate for that observation. If these do not match, then either you are mis-specifying your rangestat call or the commands you use to manually check results. You do not provide a data example nor how you manually derived results so I can't comment on what's going on. To move things along, here's a made up data example that may reflect your data (I assume that you have panel data) and I implemented the problem you describe in #5 using rangestat. I then manually check at time 128 for both panels and results match what rangestat calculated.

                Code:
                clear
                set seed 3123
                set obs 2
                gen long panel = _n
                expand 200
                bysort panel: gen time2 = _n
                gen rpce = runiform()
                
                tsset panel time2
                
                * create vars with last and next period values
                gen L_time2  = L.time2
                gen actual = F.rpce
                
                * recursive regression and generate forecast
                rangestat (reg) rpce L_time2, interval(time2 . 0) by(panel)
                gen forecast = b_cons + b_L_time2*time2
                
                * spot check at time 128 in both panel
                regress rpce L_time2 if panel == panel[128] & inrange(time2, ., time2[128])
                dis _b[_cons] + _b[L_time2] * time2[128]
                list panel time2 reg_nobs b_L_time2 b_cons forecast in 128
                
                regress rpce L_time2 if panel == panel[328] & inrange(time2, ., time2[328])
                dis _b[_cons] + _b[L_time2] * time2[328]
                list panel time2 reg_nobs b_L_time2 b_cons forecast in 328
                and here are the results from both spot checks:
                Code:
                . * spot check at time 128 in both panel
                . regress rpce L_time2 if panel == panel[128] & inrange(time2, ., time2[128])
                
                      Source |       SS           df       MS      Number of obs   =       127
                -------------+----------------------------------   F(1, 125)       =      1.11
                       Model |  .084429569         1  .084429569   Prob > F        =    0.2937
                    Residual |  9.49191768       125  .075935341   R-squared       =    0.0088
                -------------+----------------------------------   Adj R-squared   =    0.0009
                       Total |  9.57634725       126  .076002756   Root MSE        =    .27556
                
                ------------------------------------------------------------------------------
                        rpce |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                     L_time2 |  -.0007033    .000667    -1.05   0.294    -.0020234    .0006168
                       _cons |   .4886408   .0491949     9.93   0.000      .391278    .5860035
                ------------------------------------------------------------------------------
                
                . dis _b[_cons] + _b[L_time2] * time2[128]
                .39861729
                
                . list panel time2 reg_nobs b_L_time2 b_cons forecast in 128
                
                     +--------------------------------------------------------------+
                     | panel   time2   reg_nobs    b_L_time2      b_cons   forecast |
                     |--------------------------------------------------------------|
                128. |     1     128        127   -.00070331   .48864076   .3986173 |
                     +--------------------------------------------------------------+
                
                . 
                . regress rpce L_time2 if panel == panel[328] & inrange(time2, ., time2[328])
                
                      Source |       SS           df       MS      Number of obs   =       127
                -------------+----------------------------------   F(1, 125)       =      0.02
                       Model |  .001467223         1  .001467223   Prob > F        =    0.8979
                    Residual |  11.0909003       125  .088727202   R-squared       =    0.0001
                -------------+----------------------------------   Adj R-squared   =   -0.0079
                       Total |  11.0923675       126  .088034663   Root MSE        =    .29787
                
                ------------------------------------------------------------------------------
                        rpce |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                     L_time2 |  -.0000927    .000721    -0.13   0.898    -.0015196    .0013342
                       _cons |   .4672931   .0531773     8.79   0.000     .3620486    .5725376
                ------------------------------------------------------------------------------
                
                . dis _b[_cons] + _b[L_time2] * time2[328]
                .45542571
                
                . list panel time2 reg_nobs b_L_time2 b_cons forecast in 328
                
                     +--------------------------------------------------------------+
                     | panel   time2   reg_nobs    b_L_time2      b_cons   forecast |
                     |--------------------------------------------------------------|
                328. |     2     128        127   -.00009271   .46729313   .4554257 |
                     +--------------------------------------------------------------+
                
                .

                Comment


                • #9
                  @Robert Picard Hi Robert, can I ask you a question about -rangestat-? I would like to do month-by-month predictions by using the current month's excess return and market return to predict next month's excess return which is the successive out-of-sample prediction of an existing variable. Do you possibly know how to do that by using -rangestat-? I will be very grateful if you can provide some comments!

                  Here is my data:


                  Code:
                  * Example generated by -dataex-. To install: ssc install dataex
                  clear
                  input float(P1 double_P2 exret_m) double mktrf float ym
                  1 1 -.0010275785 -.050399999999999806 252
                  1 1  -.037058156  .005600000000000034 253
                  1 1    .05429238   .03560000000000003 254
                  1 1    .00736598 -.020999999999999925 255
                  1 1   .019176433 .0010999999999999938 256
                  1 1   -.04318568 -.023600000000000055 257
                  1 1   -.03732651 -.015399999999999959 258
                  1 1   -.11005977  -.07029999999999965 259
                  1 1   -.14016296   -.0716999999999999 260
                  1 1     .0392986  .049200000000000306 261
                  1 1  -.032197274   .03359999999999989 262
                  1 1   -.04497838 -.036500000000000164 263
                  1 1   -.02502831 -.032400000000000234 264
                  1 1   -.07564716 -.058599999999999826 265
                  1 1  -.029289363 -.018699999999999953 266
                  1 1    .03762526   .03270000000000013 267
                  1 1   -.03818964  -.03989999999999978 268
                  1 1   -.05297857 -.030900000000000177 269
                  1 1   -.05291183 -.031900000000000144 270
                  1 1    .03681149   .11140000000000044 271
                  1 1    .00208415  .012900000000000059 272
                  1 1    .11712262   .11299999999999968 273
                  1 1    .04921977   .04669999999999991 274
                  1 1 -.0002171315  .005500000000000017 275
                  1 1     .1605438   .03599999999999973 276
                  1 2  -.010518644 -.050399999999999806 252
                  1 2   -.02133877  .005600000000000034 253
                  1 2    .07478558   .03560000000000003 254
                  1 2    .02834966 -.020999999999999925 255
                  1 2   .024349524 .0010999999999999938 256
                  1 2   -.02187504 -.023600000000000055 257
                  1 2  -.034823272 -.015399999999999959 258
                  1 2    -.0904793  -.07029999999999965 259
                  1 2   -.09354436   -.0716999999999999 260
                  1 2    .06053396  .049200000000000306 261
                  1 2   .011907714   .03359999999999989 262
                  1 2   -.03529753 -.036500000000000164 263
                  1 2  -.022417257 -.032400000000000234 264
                  1 2   -.05457042 -.058599999999999826 265
                  1 2  -.028812805 -.018699999999999953 266
                  1 2   .031537414   .03270000000000013 267
                  1 2  -.031380806  -.03989999999999978 268
                  1 2   -.05122279 -.030900000000000177 269
                  1 2   -.03342192 -.031900000000000144 270
                  1 2    .04020997   .11140000000000044 271
                  1 2    .02557412  .012900000000000059 272
                  1 2    .13551117   .11299999999999968 273
                  1 2    .10932016    .0466999999999999 274
                  1 2    .02675929  .005500000000000017 275
                  1 2    .11422492   .03599999999999973 276
                  1 3  -.009699522 -.050399999999999806 252
                  1 3   -.00794358  .005600000000000034 253
                  1 3    .07254252   .03560000000000003 254
                  1 3   .028720394 -.020999999999999925 255
                  1 3   .025490524 .0010999999999999938 256
                  1 3  -.036586907 -.023600000000000055 257
                  1 3   -.04110854 -.015399999999999959 258
                  1 3   -.08340014  -.07029999999999965 259
                  1 3   -.09454712   -.0716999999999999 260
                  1 3    .07413716  .049200000000000306 261
                  1 3    .02301753   .03359999999999989 262
                  1 3   -.02434069 -.036500000000000164 263
                  1 3   -.03753273 -.032400000000000234 264
                  1 3   -.06126577 -.058599999999999826 265
                  1 3  -.013710598 -.018699999999999953 266
                  1 3    .04548332   .03270000000000013 267
                  1 3  -.025108384  -.03989999999999978 268
                  1 3   -.04767511 -.030900000000000177 269
                  1 3  -.020784933 -.031900000000000144 270
                  1 3    .07024763   .11140000000000044 271
                  1 3   .033434305  .012900000000000059 272
                  1 3    .13846287   .11299999999999968 273
                  1 3    .09373194   .04669999999999991 274
                  1 3   .023181343  .005500000000000017 275
                  1 3    .08609963   .03599999999999973 276
                  1 4    -.0376755 -.050399999999999806 252
                  1 4   .012637936  .005600000000000034 253
                  1 4     .0685721   .03560000000000003 254
                  1 4  .0019762507 -.020999999999999925 255
                  1 4   .023337957 .0010999999999999938 256
                  1 4   -.04084831 -.023600000000000055 257
                  1 4  -.033578657 -.015399999999999959 258
                  1 4    -.0822666  -.07029999999999965 259
                  1 4   -.07110452   -.0716999999999999 260
                  1 4    .07507407  .049200000000000306 261
                  1 4   .029575463   .03359999999999989 262
                  1 4    -.0261962 -.036500000000000164 263
                  1 4   -.04086727 -.032400000000000234 264
                  1 4   -.05303835 -.058599999999999826 265
                  1 4  -.021864437 -.018699999999999953 266
                  1 4    .05066933   .03270000000000013 267
                  1 4   -.04682332  -.03989999999999978 268
                  1 4  -.033823945 -.030900000000000177 269
                  1 4   -.02913457 -.031900000000000144 270
                  1 4    .10521629   .11140000000000044 271
                  1 4   .024716226  .012900000000000059 272
                  1 4    .14531672   .11299999999999968 273
                  1 4     .0719162    .0466999999999999 274
                  1 4   .015658902  .005500000000000017 275
                  1 4    .04475749   .03599999999999973 276
                  end
                  format %tm ym
                  Last edited by Jae Li; 18 Jun 2018, 07:04.

                  Comment


                  • #10
                    Hello! I`m trying to calculate the cumulative abnormal return for different windows of time (2 days, 5 days and 30 days). The thing is i need to use the 90 days prior to some specific dates to predict the abnormal return for the 30 days following that same date.
                    I`ve been trying to find how to do it in the internet but haven`t been able to find an answer.
                    If someone could please help me it would be great.

                    Thanks

                    Comment

                    Working...
                    X