Initializing The Dataframe For Storing Return Forecasts
Initializing The Dataframe For Storing Return Forecasts
1. Outer Loop:
o for (i in 1:n): This loop iterates over each test observation from 1 to n.
2. Initialize Forecast Value:
o forecast_value <- Coefficients[1]: Start with the intercept (constant term)
from the AR(10) model.
3. Inner Loop:
o for (j in 1:10): This loop iterates over the 10 lags.
4. Boundary Check:
o if (i + 10 - j > n) break: This ensures we do not access out-of-bounds
indices in the test data. If the index i + 10 - j exceeds n, the loop breaks.
5. Update Forecast Value:
o forecast_value <- forecast_value + Coefficients[j + 1] *
Test_Return_New_df[i + 10 - j, 2]: Update the forecast value by adding the
product of the j-th coefficient and the corresponding lagged return. Here's how it
works:
▪ Coefficients[j + 1]: Get the coefficient for the j-th lag (plus 1 because
Coefficients[1] is the intercept).
▪ Test_Return_New_df[i + 10 - j, 2]: Access the return value at the
appropriate lag for the i-th forecast. i + 10 - j calculates the correct
index in the Test_Return_New_df dataframe.
6. Assign Forecast Value:
o AR_10_Return_Forecast[i, 2] <- forecast_value: Assign the calculated
forecast value to the Forecast column of AR_10_Return_Forecast for the i-th
observation.
This process generates the forecasted return for each test observation using the fitted AR(10)
model. Once all forecasts are generated, you can transform these return forecasts into price
forecasts, as described in the next part of the code.