Page 1 of 1

Problems with a loop

Posted: Thu Oct 31, 2024 9:01 am
by ainhoa
Hi,

I have 24 series (p1, p2,...,p24), each with 1277 daily observations (from 2021:01:01 to 2024:06:30). I want to use observations from 2021:01:01 to 2023:12:31 (observations 1 to 1095) to estimate by OLS an AR(7) model and forecast from 2024:01:01 to 2024:06:30 (observations 1096 to 1277). By the way, I cannot use FORECAST for static forecast since I have the lagged dependent variable in the model (even with the "static" option the forecasts are dynamic). Besides, I want to save the forecasts in a file (24 columns, one for each series, and 182 lines, one for each day of the forecast period). At this last point I have problems. It only saves the forecast for the last series (p24). How could I save the forecasts for all the series in a file?
I have the following code:
*******************************************
all 1277
calendar(7) 2021:01:01
open data C:file.xls
data(org=col,format=xls) / p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20 p21 p22 p23 p24
compute start=2021:01:01,end=2023:12:31

dofor j = p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20 p21 p22 p23 p24
clear b0 b1 b2 b3 b4 b5 b6 b7 forecstatic
do i=0,181,1
linreg(define=eq,robusterrors,lags=4,lwindow=neweywest,noprint) j start+i end+i
# constant j{1 to 7}
compute b0(i+1) = %beta(1)
compute b1(i+1) = %beta(2)
compute b2(i+1) = %beta(3)
compute b3(i+1) = %beta(4)
compute b4(i+1) = %beta(5)
compute b5(i+1) = %beta(6)
compute b6(i+1) = %beta(7)
compute b7(i+1) = %beta(8)
compute forecstatic(i+1) = b0(i+1) + b1(i+1)*([series]j)(i+1095) + b2(i+1)*([series]j)(i+1094) + b3(i+1)*([series]j)(i+1093) + b4(i+1)*([series]j)(i+1092) + b5(i+1)*([series]j)(i+1091) + b6(i+1)*([series]j)(i+1090) $
+ b7(i+1)*([series]j)(i+1089)

open copy forecast.xls
copy(org=columns,dates,format=xls) / forecstatic

end do i
end dofor j
**************************************************************

Thanks,

Re: Problems with a loop

Posted: Fri Nov 08, 2024 6:19 pm
by TomDoan
FORECAST(STATIC,...) does static forecasts (i.e. a sequence of one step forecasts) whether or not you have lagged dependent variables. Note that you can also use UFORECAST to do forecasts (static or dynamic) for a single equation model and it's generally a bit easier to use. Note, however, that you have the COPY instruction inside the double loop. COPY(FORMAT=XLS,...) writes a full file, so each time you will be overwriting everything you did earlier. What exactly is it that you want in the spreadsheet at the end? 24 columns with forecasts over the forecast range?

Re: Problems with a loop

Posted: Thu Nov 14, 2024 3:27 am
by ainhoa
Thank you, Tom.
1. Yes, at the end I want to save a file containing 24 columns with 182 forecasts each. FORECAST(STATIC,...) does static forecasts, but if there are lagged dependent variables, it uses the actual data if the lag reaches back before the first forecast period, and uses the forecasted values if not. However, I want to use the actual data in all cases.
2. Related to this, I have another question. I try to estimate the same model, an AR(7), with lasso using a rolling window of 1095 observations and forecast the one-day-ahead value of the series (182 forecasts). Finally, I want to calculate MAE and RMSE forecast error criteria. I use the code from the example "lasso.rpf" as follows:

Code: Select all

all 1277
calendar(7) 2021:01:01
open data C:file.xls
data(org=col,format=xls) / p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 p11 p12 p13 p14 p15 p16 p17 p18 p19 p20 p21 p22 p23 p24
compute start=2021:01:01,end=2023:12:31

*First series, p1
compute N=182
compute start=2021:01:01,end=2023:12:31
clear fore error abserror

do j=0,181,1
linreg p1 start+j end+j
# constant p1{1 to 7}

***beginning of lasso
dec vect[series] plus(%nreg)
dec vect[series] minus(%nreg)
do k=1,%nreg
   set plus(k)  %regstart() %regend() = +%eqnxvector(0,t)(k)
   set minus(k) %regstart() %regend() = -%eqnxvector(0,t)(k)
end do k
cmom %regstart() %regend()
# plus minus p1

compute [symmetric] q = %xsubmat(%cmom,1,%ncmom-1,1,%ncmom-1)
compute [vector]    c = -1.0*%xsubmat(%cmom,%ncmom,%ncmom,1,%ncmom-1)
compute betasum=1.0
compute a=%fill(1,%ncmom-1,1.0)
lqprog(c=c,q=q,a=a,b=betasum) x
dec vector combined(%nreg)
ewise combined(i)=x(i)-x(i+%nreg)
***end of lasso

compute fore(j+1) = combined(1) + combined(2)*([series]p1)(j+1095) + combined(3)*([series]p1)(j+1094) + combined(4)*([series]p1)(j+1093) + combined(5)*([series]p1)(j+1092) + combined(6)*([series]p1)(j+1091) + combined(7)*([series]p1)(j+1090) + combined(8)*([series]p1)(j+1089)
compute error(j+1) = ([series]p1)(j+1096) - fore(j+1)
compute abserror(j+1) = abs(error(j+1))
compute mae = %sum(abserror)/N
set sq = error**2
compute mse = %sum(sq)/N
compute rmse = (mse)^(0.5)
end do j

print(nodates) / fore error 
write mae
write rmse

When running the code, vectors "plus" and "minus" are not the ones I want. The start and end of the vectors should be %regstart() and %regend(), which is the range of the regresion (which is moving with the rolling window of 1095 observations).
For the first window (j=0) it is ok, since it takes from 2021:01:01 to 2023:12:31 (1095 lines). However, for the second window (j=1) it takes from 2021:01:01 to 2024:01:01 (1096 lines, while it should be from 2021:01:02 to 2024:01:01 ), for the third window (j=2) it takes from 2021:01:01 to 2024:01:02 (1097 lines, while it should be from 2021:01:03 to 2024:01:02),...
When I run the code for separate windows I have the right vectors but ONLY when I clear the memory after running each window, so I guess the problem is related with some information in the memory that distort the results.

I would appreciate some help in solving this problem.