Problems with a loop
Posted: Thu Oct 31, 2024 9:01 am
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,
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,