Reputation: 11
I am using a beta-t-egarch package and unfortunately it does not have rolling forecast, so I would like to manually make it in R. The basic idea for the loop is that it runs the code then makes forecast, keep the forecasted value in forecast data.frame and goes back, re-estimates the model, forecast and add that value to forecast data. frame and etc until loops finishes. My problem in the loop is that it only forecasts for one day and does not re-estimate the model and forecast again.
I looked on the web and I could only construct this code:
start = 1
finish = 65
for (k in start:finish) {
WIGcomp1<- tegarch(WIG$r)
forecast <- predict(WIGcomp1, n.ahead = 1)
forecast[k-start+1]
}
My data start from:
> head(WIG)
Date WIG r
5233 2014-01-02 51865.89 0.0112776612
5234 2014-01-03 51497.81 -0.0071220662
5235 2014-01-07 50444.78 -0.0206600098
5236 2014-01-08 50482.93 0.0007559867
5237 2014-01-09 49753.03 -0.0145638931
5238 2014-01-10 49796.50 0.0008733342
> tail(WIG)
Date WIG r
5293 2014-03-28 51831.67 0.003646887
5294 2014-03-31 52373.47 0.010398813
5295 2014-04-01 52571.51 0.003774173
5296 2014-04-02 52761.31 0.003603819
5297 2014-04-03 52376.18 -0.007326249
5298 2014-04-04 52660.68 0.005417159
Structure of the data is:
'data.frame': 66 obs. of 3 variables:
$ Date: Date, format: "2014-01-02" "2014-01-03" "2014-01-07" "2014-01-08" ...
$ WIG : num 51866 51498 50445 50483 49753 ...
$ r : num 0.011278 -0.007122 -0.02066 0.000756 -0.014564 ...
If anything needs to be amended or added please let me know and I will gladly do it.Thank you in advance.
Upvotes: 1
Views: 1932
Reputation: 18612
The main issue with your code is that you are calculating the same forecast over and over, because rather than using k
to isolate a moving window, you are just using the entire set of data each time. In this scenario, there is only one possibly forecast to make, and it uses your entire data vector (WIGcomp1<- tegarch(WIG$r)
). For example, consider this data set, and a slightly modified version of your code:
library(betategarch)
##
set.seed(1234)
Data <- data.frame(
Date=seq.Date(
from=as.Date("2014-01-02"),
by="day",
length.out=66),
WIG=rnorm(66,mean=51000,sd=1500),
r=rnorm(66,mean=0,sd=.005))
##
##
your.fun <- function(){
start = 1
finish = 65
Forecast <- NULL
for (k in start:finish) {
WIGcomp1<- tegarch(Data$r)
Forecast[k] <- predict(WIGcomp1, n.ahead = 1)
##forecast[k-start+1]
}
Forecast
}
##
> your.fun()
[1] 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116
[10] 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116
[19] 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116
[28] 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116
[37] 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116
[46] 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116
[55] 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116 0.003424116
[64] 0.003424116 0.003424116
Since you aren't using a moving window of source data, it just makes the same prediction over and over. An alternative solution would be to use a moving subset of data to make successive 1 step ahead predictions from a moving window that spans n
days (n = 10
in my example below). This is representative of a situation in which each day you get a new observed value, and use that data point and the previous 9 days of data to predict tomorrow's value.
rolling.forecast <- function(Df=Data,Var="r",Window=10,nAhead=1){
##
fCast <- rollapplyr(
Df[,Var], width=Window, function(x){
predict(
tegarch(x),
n.ahead=nAhead)
})
##
newData <- cbind(
Df,
Forecast=c(
rep(NA,Window-1),
fCast))
##
return(newData)
}
##
> NewData <- rolling.forecast()
> head(NewData,10)
Date WIG r Forecast
1 2014-01-02 49189.40 -5.693039e-03 NA
2 2014-01-03 51416.14 6.839136e-03 NA
3 2014-01-04 52626.66 6.647824e-03 NA
4 2014-01-05 47481.45 1.682364e-03 NA
5 2014-01-06 51643.69 3.446419e-05 NA
6 2014-01-07 51759.08 -2.277344e-03 NA
7 2014-01-08 50137.89 -1.832620e-03 NA
8 2014-01-09 50180.05 3.241433e-03 NA
9 2014-01-10 50153.32 1.035135e-02 NA
10 2014-01-11 49664.94 -7.669921e-04 0.003737219
> tail(NewData)
Date WIG r Forecast
61 2014-03-03 51984.88 -0.003157046 0.255080969
62 2014-03-04 54823.49 -0.007566441 0.011048830
63 2014-03-05 50947.86 -0.003180499 0.012665863
64 2014-03-06 49995.55 0.001131508 0.002204160
65 2014-03-07 50988.59 0.005068452 0.047949976
66 2014-03-08 53665.63 0.001263751 0.001458638
When you do this, you will of course only be able to generate N - Window + 1
new values; where N
is the number of observations in your data, and Window
is the span of the rolling window of data. Since I used the function rollapplyr
which has a default align
value of right
(adapted from the more general function rollapply
in the package zoo
), the first Window-1 = 9
observations are set to NA
.
Upvotes: 2