Error in Predict for ARMA GARCH model
r
Solution
This issue seems to be duplicate with an older post which does not contain answer:R error when using predict() function with class = fGarch
The error stems from the case where either `(i - 1)` or `(i - u2)` becomes negative, so the index is something like -1:2 which is not allowed.
After checking the predict method for the fitted object via `getMethod("predict","fGARCH")`, it looks like the error happens here (irrelevant parts omitted):
a_vec <- rep(0, (n.ahead))
u2 <- length(ar)
a_vec[1] = ar[1] + ma[1]
if ((n.ahead - 1) > 1) {
for (i in 2:(n.ahead - 1)) {
a_vec[i] <- ar[1:min(u2, i - 1)] * a_vec[(i - 1):(i - u2)]
}
}
So as `i` is always larger than 1, the error happens because
`(i - u2) < 0 <==> i < u2 <==> i < length(ar)`
Does this make any sense? For me it doesn't, as it looks like if your model's ar part is larger than 2, this always produces an error.
The code is bit weird also because `a_vec[i]` is scalar and
`ar[1:min(u2, i - 1)] * a_vec[(i - 1):(i - u2)] + ...` can be a vector which length is greater than 1.
EDIT:
There is either a bug in the prediction function or there's undocumented restrictions which kind of models can be predicted, as even the example from the `fGarch`'s manual gives error if it is slightly modified:
set.seed(123)
fit = garchFit(~arma(2,0)+garch(1,1), data = garchSim(), trace = FALSE)
predict(fit, n.ahead = 4)
meanForecast meanError standardDeviation
1 -7.512452e-04 0.004161189 0.004161189
2 -1.107497e-03 0.003958535 0.003878321
3 2.617933e-04 0.003782362 0.003665391
4 6.264252e-05 0.003616971 0.003507209
Warning message:
In a_vec[i] <- ar[1:min(u2, i - 1)] * a_vec[(i - 1):(i - u2)] + :
number of items to replace is not a multiple of replacement length
Based on the Changelog of fGarch package it seems that this issue was corrected several years ago, but apparently it has resurfaced, or was never properly fixed:
2009-11-05 chalabi
* R/methods-predict.R: small changes in predict,fGARCH-method to
correct its output when n.ahead=1 since addition of conditional
errors.
I would suggest you to contact the maintainer of the package.
Problem
I have data of an one year interest rate for a period over roughly five years. I would like to create a model for this interest rate and I have come to the conclusion that an ARMA(3,2) with a GARCH(1,1) is appropriate. I therefore use the following code below to get my estimates. ``` > stibor1ydarmagarch=garchFit(formula=~arma(3,2)+garch(1,1), data=stibor1yd, cond.dist="std", trace=FALSE) ``` This works fine and I get nice estimates. However when it comes to predicting, I get an error. Does someone have a clue why I get the error and how to solve it? ``` > predict(stibor1ydarmagarch, n.ahead=10) Error in a_vec[(i - 1):(i - u2)] : only 0's may be mixed with negative subscripts ```