Announcement

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

  • Loop a time-series regression to predict out-of-sample values

    Hello there,
    I am quite new to Stata and after desperately searching for a solution in this forum I really need your help.

    I have daily data for stock returns and would like to use a 10 year in sample period to predict the returns of the coming year (out of sample) and continue this process until the last date of the dataset is predicted. This would be like a rolling regression but with 1 year steps instead of daily steps.

    My data goes from 1/1/1964 till 1/1/2019. I would like to run a regression on stock returns in the period of 1/1/1964 until 1/1/1974 and create a new variable pred_ret that contains the predicted returns for the year 1975 on a daily basis. Next, I would like to use the period 1/1/1965 until 1/1/1975 and predict the daily returns of the year 1976 in the same variable pred_ret. So in the end I will have predicted returns until 2019 that are always based on the betas of a regression of the preceding 10 year period.

    As far as I read it in this forum, I will have to use the loop function to run the regression over a certain time and predict values for the next year.

    My data looks as following:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int Date double SP500 float(DMon DJan DSummer DToM lag1 movavmonth)
    1462    .5450369201135273 0 1 0 1    .6150561    .0914737
    1463   .09275823894395199 0 1 0 1    .5450369   .12144153
    1466   .22490980208971198 1 1 0 1   .09275824   .11148646
    1467  .026432346876929937 0 1 0 0    .2249098    .1174561
    1468    .4087262541685617 0 1 0 0  .026432347   .11290492
    1469    .3677427336290836 0 1 0 0   .40872625   .13604474
    1470  -.05245345096242126 0 1 0 0   .36774275   .15240423
    1473 -.026232454736759774 1 1 0 0  -.05245345   .16117047
    1474   .18351033877782877 0 1 0 0 -.026232455   .15130717
    1475   .36601086611797323 0 1 0 0   .18351033    .1529173
    1476  -.11749593208245183 0 1 0 0    .3660109   .15504064
    1477   .01305597276325544 0 1 0 0  -.11749593   .11964382
    1480  -.19610910127535547 1 1 0 0  .013055973    .1679611
    1481    .2744548418127024 0 1 0 0   -.1961091   .14879951
    1482    .5336764956149331 0 1 0 0   .27445483   .15508227
    1483   .07785752008458488 0 1 0 0    .5336765   .21350373
    1484   .02594682295907274 0 1 0 0   .07785752    .2065695
    1487 -.038911732012244515 1 1 0 0  .025946824    .1854712
    1488   .02593851357128715 0 1 0 0  -.03891173   .17425205
    1489   -.6114648795792405 0 1 0 0   .02593851    .1671895
    1490   .09130634266269673 0 1 0 0   -.6114649    .1304022
    1491    .4423111210359154 0 1 0 1   .09130634   .10546174
    1494  -.09090319318391851 1 0 0 1    .4423111   .10096062
    1495  -.11700228981167478 0 0 0 1   -.0909032   .09182425
    1496  -.16923391728775083 0 0 0 1   -.1170023   .08233213
    1497   .23425310035367483 0 0 0 0   -.1692339   .06441651
    1498   .32444386386294644 0 0 0 0    .2342531   .07386291
    1501  -.16857554075926928 1 0 0 0    .3244439   .06127061
    1502   .36274037822475447 0 0 0 0  -.16857554   .05032556
    1503   .30987500186526334 0 0 0 0    .3627404   .06452623
    1504  -.06448256199605328 0 0 0 0     .309875   .07980385
    1505  -.05160516243556825 0 0 0 0  -.06448256   .06853144
    1508 -.025821608174692114 1 0 0 0  -.05160516   .05972578
    1509  .012911637531657846 0 0 0 0  -.02582161    .0556521
    1510   .10321507552329169 0 0 0 0  .012911637   .05370935
    1511   .09022362833254678 0 0 0 0   .10321508   .06731499
    1515   .07726593924774057 1 0 0 0   .09022363  .034695674
    1516                    0 0 0 0 0   .07726594  .036824185
    1517   .24429841315724674 0 0 0 0           0  .035070654
    1518   -.3215643524049991 0 0 0 0    .2442984   .04855685
    1519   .23163051541643703 0 0 0 1  -.32156435  .032009095
    1522    .2182680402197652 1 0 0 1    .2316305   .05166679
    1523   .32012319648679916 0 0 0 1   .21826804   .05999685
    1524  -.19195219796224763 0 0 0 1    .3201232   .07238382
    1525 -.012812400104797079 0 0 0 0   -.1919522   .06757196
    1526    .3197547092171883 0 0 0 0   -.0128124  .072533384
    1529   .02554136888245668 1 0 0 0    .3197547   .07649962
    1530   .33137170688734285 0 0 0 0   .02554137   .07395171
    1531   .45702887218955274 0 0 0 0    .3313717    .0862098
    1532   .16453208903435704 0 0 0 0    .4570289   .11600049
    1533   .07583997281823922 0 0 0 0    .1645321     .106562
    1536                    0 1 0 0 0   .07583997    .1115713
    1537   .22718803301523074 0 0 0 0           0   .10599273
    1538   .07561059104284362 0 0 0 0   .22718804   .11176393
    1539  -.10082430902006141 0 0 0 0    .0756106   .11659404
    1540  -.48035106595272997 0 0 0 0  -.10082431   .11117804
    1543  .012672790801534462 1 0 0 0   -.4803511   .08304746
    1544  -.17752857707483716 0 0 0 0   .01267279   .07969628
    1545    .2408595893239726 0 0 0 0  -.17752858   .06800424
    1546     .265535963523251 0 0 0 0    .2408596   .07544032
    1550  -.06316301565919895 1 0 0 0   .26553595   .09320308
    1551  -.20237294786404786 0 0 0 1  -.06316301   .08538477
    1552   .32865027477694564 0 0 0 1  -.20237295   .07168202
    1553    .5788351564442354 0 0 0 1    .3286503   .07693832
    1554   .30068301469268804 0 0 0 1    .5788351   .08925793
    1557   .10001876365197467 1 0 0 0     .300683   .10842682
    1558  -.35052490667672453 0 0 0 0   .10001876    .1080064
    1559  .012542479326031373 0 0 0 0   -.3505249    .0861716
    1560  -.06271935099396053 0 0 0 0   .01254248    .0855526
    1561    .1880301459043782 0 0 0 0  -.06271935  .066786356
    1564  -.10023932991440215 1 0 0 0   .18803014   .04700751
    1565    .2754145542690814 0 0 0 0  -.10023933   .03964517
    1566   .12493505341715215 0 0 0 0   .27541456   .05087228
    1567    .1372525074207858 0 0 0 0   .12493505   .05682157
    1568   .43546694009461934 0 0 0 0   .13725251   .05253892
    1571  -.06209624424128384 1 0 0 0   .43546695   .10759732
    1572  .049678341498111456 0 0 0 0  -.06209625   .09911264
    1573  -.06210395816638141 0 0 0 0   .04967834   .09675863
    1574  -.13675765594977138 0 0 0 0  -.06210396   .09319783
    1575   -.7868610033383301 0 0 0 0  -.13675766    .0951393
    1578    -.502831989732509 1 0 0 0    -.786861   .03523344
    1579    .6907456035888876 0 0 0 0    -.502832  .009611278
    1580   -.2506329648503502 0 0 0 0    .6907456   .04057193
    1581   -.3015810353509959 0 0 0 1  -.25063297   .03205057
    1582    .8895617306367889 0 0 1 1    -.301581   .02754111
    1585   .37351014771604624 1 0 1 1    .8895617  .014364884
    1586    .5082081035194379 0 0 1 1   .37351015   .03146704
    1587   .22230589068534296 0 0 1 0    .5082081   .05313709
    1588    .1109722079237272 0 0 1 0    .2223059   .05869559
    1589   -.1850163933878988 0 0 1 0    .1109722   .07967273
    1592  -.12353058861142528 1 0 1 0   -.1850164  .071496524
    1593    .3208715456650392 0 0 1 0   -.1235306   .06220952
    1594  -.23438361946989042 0 0 1 0    .3208715   .07396688
    1595  -.13594514685972198 0 0 1 0   -.2343836   .06786942
    1596   .29636598807756503 0 0 1 0  -.13594514   .04917125
    1599   -.4696548026597588 1 0 1 0   .29636598  .034023948
    1600   -.5216730121241245 0 0 1 0   -.4696548  .010039246
    1601   .44731807234804744 0 0 1 0    -.521673 -.014129493
    1602    .3465325121284463 0 0 1 0    .4473181  .009025703
    1603    -.259787358955048 0 0 1 0    .3465325  .022519074
    end
    format %tdnn/dd/CCYY Date
    After reading several threads in this forum, I guess the code will be something like

    Code:
    gen pred_ret = .
    
    forval i = 1964/2019 {
    
        regress SP500 DMon DJan DToM DSummer lag1 movavmonth if year=`i'
    
        predict temp
    
        replace pred_ret = temp if year == `i'
    
        drop temp
    
    }
    Maybe I will also have to include some "local start" and "local end" values? I really don't know and I have no idea how to tell the loop that it should use a 10 year interval for the regression, predict the daily returns for the coming year and use another (one year forward) 10 year interval to predict the next year.
    I really hope this was relatively clear and would appreciate any help!!
    Thank you.

    Kind regards,
    Jannis

  • #2
    I think I came up with the solution myself:

    Code:
    gen pred_ret = .
    forval i = 1964/2019 {
    regress SP500 DMon DJan DToM DSummer lag1 if inrange(year,`i',`i'+10)
    predict temp
    replace pred_ret = temp if year == `i'+11
    drop temp
    }

    Comment


    • #3
      I think it is possible you have made a common mistake in your code in post #2.

      In post #1 you wrote
      I would like to run a regression on stock returns in the period of 1/1/1964 until 1/1/1974. ... So in the end I will have predicted returns until 2019 that are always based on the betas of a regression of the preceding 10 year period.
      Now, if you constructed your year varaible as
      Code:
      year =yofd(Date)
      then for the first time through the loop your regress command will be
      Code:
      regress SP500 DMon DJan DToM DSummer lag1 if inrange(year,1964,1974)
      which will include Date values of 1/1/1964 until 12/31/1974, an 11-year period.

      I think that you would more correctly say
      I would like to run a regression on stock returns in the period of 1/1/1964 until 12/31/1973.
      and then in your code
      Code:
      regress SP500 DMon DJan DToM DSummer lag1 if inrange(year,`i',`i'+9)
      predict temp
      replace pred_ret = temp if year == `i'+10

      Comment


      • #4
        Hello William,
        you're completely right. Thanks a lot for your help, I would not have realized this mistake!

        Kind regards,
        Jannis

        Comment

        Working...
        X