I had an old post that perhaps was not clear. As is advised by some of you, I decided to create a new one that is more to the point.
I want to estimate out-of-sample forecasts for a variable called BUS using predictors s1-s5.
I have nearly 500 predictors but I try to simplify the issue here by showing only 5 predictors.
As you see from the data structure below, BUS is a quarterly variable but s1-s5 are weekly variables (i.e. BUS is updated on a quarterly basis while s1-s5 are updated every week).
My aim is to run a lasso regression every week and make an out-of-sample prediction for the quarterly variable BUS. These out-of-sample predictions are then updated every week when new observations of s1-s5 become available. Ultimately, I want to see whether the out-of-sample predictions become more accurate throughout the quarter (i.e. from week 1 to week 12 of the quarter).
The variable "series" takes value 1 to 12 consistent with the weeks (i.e. series=1 is for weekly data available at week 1, series=2 is for weekly data available at week 2, etc.)
I think I managed to make predictions but I believe they are in-sample predictions as follows:
Code:
gen prediction=. levelsof series, local(levels) foreach x of local levels { lasso linear BUS s1 s2 s3 s4 s5 if series==`x' predict temp if series==`x', postselection replace prediction=temp if series==`x' drop temp }
The problem is that I am not sure how to make these predictions out-of-sample? More specifically, the business cycle analyst can see the predictors s1-s5 in real-time as at week 1 for example but can not see the quarterly variable BUS in real-time as it is only available at the end of the quarter. Therefore, I want to estimate the parameters of the model using data from weeks 1 up till (and without) data from week 1 of the current quarter and then use the predictors in week 1 of the current quarter to make the forecast out-of-sample. Similarly, I want to estimate the parameters of the model using data from weeks 2 up till (and without) data from week2 of the current quarter and then use the predictors in week 2 of the current quarter to make the forecast out-of-sample, and so on.
Here is an example of my time-series data
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(BUS s1 s2 s3 s4 s5 series) int year byte(quarter month week) float quarter_date 5.28 1.4280612 -1.7400645 2.1785972 .6041555 0 1 1987 1 1 1 108 7.32 -.3261277 -2.8215795 -.23142336 7.937459 .3825918 1 1987 2 1 1 109 6.13 .706113 -3.8739974 1.460365 .1957696 .24385256 1 1987 3 1 1 110 9.83 .4272946 -3.122698 1.1591737 2.161951 .3692954 1 1987 4 1 1 111 5.02 1.432191 -3.5722666 2.432849 .5817058 0 1 1988 1 1 1 112 9.75 .17579167 -1.486287 1.0115452 1.0553348 .28939685 1 1988 2 1 1 113 7.31 1.411383 -3.54055 1.7815936 -.020459617 .610453 1 1988 3 1 1 114 8.85 1.715589 -1.0885705 2.654196 .3305015 0 1 1988 4 1 1 115 9.04 1.8187963 -2.506453 2.738661 .4680699 0 1 1989 1 1 1 116 7.42 1.8606597 -2.459989 2.78734 .3364668 0 1 1989 2 1 1 117 5.51 1.887378 1.6824546 2.860331 .0734062 0 1 1989 3 1 1 118 2.54 1.911596 2.3742833 2.920467 .09255037 0 1 1989 4 1 1 119 9.35 .36569595 .9221073 1.4366348 .6418874 .25642812 1 1990 1 1 1 120 4.82 2.0296164 -1.8166744 3.0796354 .0996016 0 1 1990 2 1 1 121 2.53 2.0968032 1.9894003 3.134805 .18245694 0 1 1990 3 1 1 122 -2.42 3.010042 4.6542344 4.4679475 5.588562 .10877172 1 1990 4 1 1 123 .49 3.114446 7.097381 3.351834 .7527507 -.0597137 1 1991 1 1 1 124 6.31 3.216622 9.544506 6.088016 7.100855 -.11968945 1 1991 2 1 1 125 5.17 4.0031595 5.090754 4.819216 .7430318 -.064995535 1 1991 3 1 1 126 3.06 1.7156185 -.3905046 1.8559965 -.3724067 .1004606 1 1991 4 1 1 127 6.93 1.9902998 1.9397452 2.1061885 -.7137325 .14847873 1 1992 1 1 1 128 6.95 2.597939 2.390985 .7381327 -4.080062 .5012339 1 1992 2 1 1 129 6.33 1.6087084 -1.3339542 1.9718102 .5833408 .14864297 1 1992 3 1 1 130 7.74 1.7855448 .8567627 1.962867 .7399028 .26191464 1 1992 4 1 1 131 5.28 -.312875 -2.0297842 -.6567536 .7758612 .4881686 2 1987 1 1 2 108 7.32 -1.5230926 -2.703133 -3.0387714 2.954472 .22246388 2 1987 2 1 2 109 6.13 -.6086997 -3.5352764 -1.9099023 .026646543 .2183693 2 1987 3 1 2 110 9.83 -1.0030841 -3.710979 -.8193175 1.0371368 .2992593 2 1987 4 1 2 111 5.02 -1.315071 -5.708353 -1.81234 .5035552 .23610453 2 1988 1 1 2 112 9.75 -.6764717 -1.3538846 -2.5990624 -.758627 .1763391 2 1988 2 1 2 113 7.31 -.21421558 -2.4105334 .40100315 1.1111029 .23137583 2 1988 3 1 2 114 8.85 -.49769095 -.08228127 1.0387636 .5207426 .1803996 2 1988 4 1 2 115 9.04 -.014957066 -1.9950553 -.58217704 .8293411 .22317313 2 1989 1 1 2 116 7.42 -.8647903 -.7270581 -.8096528 .901518 .1744625 2 1989 2 1 2 117 5.51 .8862366 2.6689715 1.2430537 -.29651818 .16048166 2 1989 3 1 2 118 2.54 .05685891 3.506664 -.1775705 -1.1541252 .20539308 2 1989 4 1 2 119 9.35 -1.5025678 .8159853 -1.1917896 -.5318023 .11380605 2 1990 1 1 2 120 4.82 1.5260097 -1.1706387 -.9492483 .24037273 .15399224 2 1990 2 1 2 121 2.53 .9257024 -.4118942 .7638614 -2.0135093 .15347226 2 1990 3 1 2 122 -2.42 1.1741554 1.1157335 .8788975 .4426497 .1168849 2 1990 4 1 2 123 .49 3.525193 6.711821 5.894926 2.0596566 -.1692952 2 1991 1 1 2 124 6.31 4.2774143 8.058287 9.068833 1.0657045 -.1169197 2 1991 2 1 2 125 5.17 3.225771 .4149646 5.304836 -.31201684 -.10806065 2 1991 3 1 2 126 3.06 .6311643 -2.970027 -.28448343 -.07355707 .15270847 2 1991 4 1 2 127 6.93 .8370029 .5811588 -.950491 2.069583 .11863983 2 1992 1 1 2 128 6.95 .8845069 1.4056878 -1.5377135 -.4812104 .114008 2 1992 2 1 2 129 6.33 .51111126 -4.1659355 -1.9837395 .09373852 .3755253 2 1992 3 1 2 130 7.74 -.3481121 -1.0498939 -.3189843 .066026814 .10963735 2 1992 4 1 2 131 5.28 -2.356725 -2.1076612 -2.7361324 .2479541 .22487326 3 1987 1 1 3 108 7.32 -2.2176385 -2.59371 -3.308432 -.8266953 .13695842 3 1987 2 1 3 109 6.13 -1.988106 -3.5369556 -2.5531945 1.8277788 .1750264 3 1987 3 1 3 110 9.83 -1.8028708 -3.756836 -2.467215 1.5251398 .1382897 3 1987 4 1 3 111 5.02 -1.60548 -5.638969 -2.2708828 .28224888 .2874467 3 1988 1 1 3 112 9.75 -2.2246215 -1.3491007 -1.7797307 .06242354 .13456556 3 1988 2 1 3 113 7.31 -2.578337 -2.410444 -1.8754367 1.4670404 .12353268 3 1988 3 1 3 114 8.85 -.7692303 -.09574382 -1.9727464 .4673429 .11508931 3 1988 4 1 3 115 9.04 -.9678542 -1.995568 -1.9843154 -.5841132 .29625088 3 1989 1 1 3 116 7.42 -.6068677 -.6276433 -2.032895 -.3696545 .12134252 3 1989 2 1 3 117 5.51 -.12857853 2.771389 -1.5378385 .317264 .11938269 3 1989 3 1 3 118 2.54 -1.0707742 3.595633 -2.094334 .035563562 .1235524 3 1989 4 1 3 119 9.35 -.970474 .8063396 -1.1299096 .6624953 .13118356 3 1990 1 1 3 120 4.82 .2601256 -1.0888479 -1.6639595 .6947708 .1191846 3 1990 2 1 3 121 2.53 .30467215 -.40419185 -1.7502427 -.43394426 .1235137 3 1990 3 1 3 122 -2.42 .9444076 1.1760539 -.2948909 .7812307 .13087627 3 1990 4 1 3 123 .49 3.9914865 6.568848 6.704494 1.595678 -.1509655 3 1991 1 1 3 124 6.31 5.012939 7.920979 8.347696 -.54813856 -.12451165 3 1991 2 1 3 125 5.17 5.038057 .3071753 7.27375 -.9620001 -.11965105 3 1991 3 1 3 126 3.06 1.2079372 -3.035961 -.2178535 -.05455993 .15887807 3 1991 4 1 3 127 6.93 .06978367 .6693566 -.8070132 1.7806284 .14646508 3 1992 1 1 3 128 6.95 -.1376001 1.3361707 -1.2892662 .38877955 .1481647 3 1992 2 1 3 129 6.33 -.6247261 -4.2310996 -1.1468972 .3107327 .13287802 3 1992 3 1 3 130 7.74 -1.0327399 -1.0610285 -1.2286204 .1454983 .15739937 3 1992 4 1 3 131 5.28 -1.0315892 -1.9988308 -2.6316936 .6735969 .13540626 4 1987 1 1 4 108 7.32 -.9939098 -2.511362 -2.58485 .4599007 .15181194 4 1987 2 1 4 109 6.13 -1.5999373 -3.6620574 -2.831921 1.808131 .20369446 4 1987 3 1 4 110 9.83 -2.7039974 -3.832227 -3.109755 1.0882541 .1632594 4 1987 4 1 4 111 5.02 -3.543656 -5.493874 -2.1929116 1.3907964 .1679988 4 1988 1 1 4 112 9.75 -1.9179015 -1.2556508 -2.625051 1.0341737 .1740451 4 1988 2 1 4 113 7.31 -1.778265 -2.495931 -2.4760535 1.4696044 .17356914 4 1988 3 1 4 114 8.85 -1.2345937 -.22844014 -2.127411 1.1983488 .15559915 4 1988 4 1 4 115 9.04 -.9724296 -2.0516148 -2.1667652 .8662595 .14979433 4 1989 1 1 4 116 7.42 -1.5865642 -.7517704 -1.934539 -.001189246 .15478706 4 1989 2 1 4 117 5.51 -1.353096 2.647187 -1.8039092 -.07529771 .15468076 4 1989 3 1 4 118 2.54 -.4403289 3.6720455 -1.8033653 .14847729 .158337 4 1989 4 1 4 119 9.35 -.7641429 .8248587 -1.5446472 .3296993 .13763453 4 1990 1 1 4 120 4.82 -.3110338 -1.201032 -1.2366627 .017878672 .14217184 4 1990 2 1 4 121 2.53 -.0579773 -.37944755 -1.5019845 .16272673 .14685132 4 1990 3 1 4 122 -2.42 .551628 1.1874413 -.4475872 1.1742166 .1394521 4 1990 4 1 4 123 .49 3.9375834 6.587499 7.437414 .2495805 -.1574398 4 1991 1 1 4 124 6.31 4.6535735 8.093565 8.163906 -.8019937 -.13311808 4 1991 2 1 4 125 5.17 4.676563 .3106449 6.918107 -.8722724 -.1107249 4 1991 3 1 4 126 3.06 1.301191 -2.932334 -.5840098 -.06370235 .166544 4 1991 4 1 4 127 6.93 -.06733606 .9889328 -1.319969 .2210396 .14780125 4 1992 1 1 4 128 6.95 -.25309724 1.299411 -1.0797026 .2594811 .1547733 4 1992 2 1 4 129 6.33 -.437004 -4.1473575 -1.1806424 .2261984 .18456896 4 1992 3 1 4 130 7.74 -.8715544 -.8834404 -1.0527567 .24967523 .16514884 4 1992 4 1 4 131 5.28 -1.0928361 -2.1836462 -2.6183126 .8119413 .13540626 5 1987 1 2 1 108 7.32 -.9907247 -3.33188 -2.753484 .6022525 .1520763 5 1987 2 2 1 109 6.13 -1.7747288 -3.991014 -2.996839 2.509792 .2036956 5 1987 3 2 1 110 9.83 -3.0593615 -3.507675 -3.167567 1.2470064 .1632594 5 1987 4 2 1 111 end format %tq quarter_date
0 Response to I have in-sample predictions after lasso regressions but can not make them out-of-sample
Post a Comment