Hi,
I need to do a rolling forecast for various forecasting horizons (1,4 and 20 quarters). My approach is to make a for loop and then extract the respective observations from the dynamic forecast series. I need to do some manipulation with that data afterwards, which I think will be easier to do in matrix form, therefore I want to save the data in a matrix. Unfortunately I only get NAs with my little program:
' rolling forecast loop
for !i = 1 to !length-!window+1
' set sample to estimation period
smpl @first+!i-1 @first+!i+!window-2
' estimate equation
eq1.ls fx c money interest cpi gdp
' reset sample to forecast period
smpl @first+!i+!window-1 @last
' make forecasts
eq1.forecast(f=na) tmp_yhat
' extract observations according to forecast horizon
forecastmatrix(!i,1) =@elem(tmp_yhat,"@first+!i+!window-1")
forecastmatrix(!i,2) =@elem(tmp_yhat,"@first+!i+!window+2")
forecastmatrix(!i,3) =@elem(tmp_yhat,"@first+!i+!window+18")
next
I have been trying so many things in the last couple of days, searched this forum for hints, but nothing worked. Help would be greatly appreciated, as I'm getting more and more desperate. Thanks!
rolling forecast matrix problem
Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt
-
EViews Glenn
- EViews Developer
- Posts: 2682
- Joined: Wed Oct 15, 2008 9:17 am
Re: rolling forecast matrix problem
What's going on is that @elem, as currently supported, does not allow you to do anything but provide a single offset to the @first or @last keyword. Thus, all of your offsets are being interpreted as @first+!i, hence your NAs.
I've put a feature request in for extending the functionality of @elem to match that of smpl statements, the latter of which allows for full expression offsets. We've looked at this addition and don't anticipate any problems with adding the feature to @elem. It should show up in a patch shortly.
For now, you can resolve your offsets to replacement variables, as in
!offset1 = !i+!window-1
!offset2 = !i+!window+2
!offset3 = !i+!window+18
and then use
@elem(tmp_yhat, "@first+!offset1)
@elem(tmp_yhat, "@first+!offset2)
@elem(tmp_yhat, "@first+!offset3)
as desired. I'm very glad you posted your question. Sorry about the inconvenience.
I've put a feature request in for extending the functionality of @elem to match that of smpl statements, the latter of which allows for full expression offsets. We've looked at this addition and don't anticipate any problems with adding the feature to @elem. It should show up in a patch shortly.
For now, you can resolve your offsets to replacement variables, as in
!offset1 = !i+!window-1
!offset2 = !i+!window+2
!offset3 = !i+!window+18
and then use
@elem(tmp_yhat, "@first+!offset1)
@elem(tmp_yhat, "@first+!offset2)
@elem(tmp_yhat, "@first+!offset3)
as desired. I'm very glad you posted your question. Sorry about the inconvenience.
Re: rolling forecast matrix problem
Thanks for your response Glenn. Unfortunately, that doesn't seem to be the only problem as I still just get NAs in my matrix.
I tried to narrow it down, so I put @last in the argument for the observation. tmp_yhat gets overwritten every time through the loop, so I expected the matrix to be filled with differing values. Surprisingly that was not the case. Instead, half of the matrix was filled with NAs and the period from 2000q1 to 2009y4 (the last subsample period) was filled with the same value, i.e. the very last value tmp_yhat. Even more surprisingly, when I put in @last-1, the matrix was NOT completly filled with NAs (as would be expected, because the last series of tmp_yhat just contains one value at the very end), but instead the period from 2000q1 to 2009q4 was filled with the value of tmp_yhat(-1).
I'm really confused now. Maybe I did something wrong beforehand, so I'm posting the complete code this time:
PS: the line which copies tmp_yhat to yhat is just there to see if the right values are written into the matrix, once it works.
I tried to narrow it down, so I put @last in the argument for the observation. tmp_yhat gets overwritten every time through the loop, so I expected the matrix to be filled with differing values. Surprisingly that was not the case. Instead, half of the matrix was filled with NAs and the period from 2000q1 to 2009y4 (the last subsample period) was filled with the same value, i.e. the very last value tmp_yhat. Even more surprisingly, when I put in @last-1, the matrix was NOT completly filled with NAs (as would be expected, because the last series of tmp_yhat just contains one value at the very end), but instead the period from 2000q1 to 2009q4 was filled with the value of tmp_yhat(-1).
I'm really confused now. Maybe I did something wrong beforehand, so I'm posting the complete code this time:
Code: Select all
' create workfile
wfcreate singapur q 1980 2009
' fetch data series
read(t=xls, b4, s=sheet1, t) "\\myfiles\cl306\Profiles_Do_Not_Delete\campusts\Desktop\Dissertation Data\Singapur\quarterly.xls" fx money interest cpi gdp
' ESTIMATE REGRESSION AND ROLLING FORECAST
'set window size (ADJUST HERE FOR DIFFERENT FORECAST PERIODS, default is 1990q1 to 2009q4)
!window = 40
' get number of observations
!length = @obsrange
' declare matrix to store results
matrix(!length-!window+1,3) forecastmatrix
' declare equation for estimation
equation eq1
' declare series to store results
series yhat ' point estimates
series tmp_yhat ' temporary series that gets overwritten each time through the loop
' rolling forecast loop
for !i = 1 to !length-!window+1
' set sample to estimation period
smpl @first+!i-1 @first+!i+!window-2
' estimate equation
eq1.ls fx c money interest cpi gdp
' reset sample to forecast period
smpl @first+!i+!window-1 @last
' make forecasts
eq1.forecast(f=na) tmp_yhat
' copy data in current forecast sample
yhat = tmp_yhat
' read out elements according to forecast horizon
!offset1 = !i+!window-1
!offset2 = !i+!window+2
!offset3 = !i+!window+18
forecastmatrix(!i,1) =@elem(tmp_yhat2,"@first+!offset1")
forecastmatrix(!i,2) =@elem(tmp_yhat2,"@first+!offset2")
forecastmatrix(!i,3) =@elem(tmp_yhat2,"@first+!offset3")
next
-
EViews Glenn
- EViews Developer
- Posts: 2682
- Joined: Wed Oct 15, 2008 9:17 am
Re: rolling forecast matrix problem
It's hard for me to be certain what's going on since I don't have your data. I do notice that you are doing the @elem on tmp_yhat2. Is that what really what you want?
If that's not it, I will note that the first thing that I did was to build a simple forecast loop example and to use the @last as your indicated, and that seemed to work fine. So I think that there's something else going on...
One thing I was going to suggest prior to my first posting on this was that you might want to just use output to series as in the examples that we provide and then use stom to move your series into a matrix once you're done.
If that's not it, I will note that the first thing that I did was to build a simple forecast loop example and to use the @last as your indicated, and that seemed to work fine. So I think that there's something else going on...
One thing I was going to suggest prior to my first posting on this was that you might want to just use output to series as in the examples that we provide and then use stom to move your series into a matrix once you're done.
Re: rolling forecast matrix problem
A bit late, but still: thanks for the help, stom did the trick!
-
EViews Glenn
- EViews Developer
- Posts: 2682
- Joined: Wed Oct 15, 2008 9:17 am
Re: rolling forecast matrix problem
I didn't suggest it since I thought that the first approach solved the problem in my test case and I figured you wouldn't be interested since this required a rewrite of your program. But since your program wasn't working anyway :)
Who is online
Users browsing this forum: No registered users and 2 guests
