Wednesday, March 27, 2019

r - Forecasting using rugarch package


I want to do one step ahead in-sample forecasts. My data can be found here. This is just a data frame with the date as the rownames.


I specify my model and do the fit and show the plots with


library(rugarch)
model<-ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(0, 0), include.mean = FALSE),
distribution.model = "norm")

modelfit<-ugarchfit(spec=model,data=mydata)

plot(modelfit)

Now I want to do one-step-ahead in sample forecasts of my cond. mean and cond. volatility.


I therefore use the ugarchfit command:


ugarchforecast(modelfit,n.ahead=1,data=mydata)

But this is only one value for the last date. So I want to have this for every data starting from the beginning and up to my final values. These should be 1-step-ahead forecasts which use the specified model parameters and my data. How can I get this?



Answer



You want to set the parameter n.roll to the number of n.ahead, n.roll rolling forecasts you want. (The n.ahead parameter controls how many steps ahead you want to forecast for each roll date.) Thus by setting n.roll to a number almost equal to your sample size, and critically setting the out.sample parameter almost equal to your sample size, you're telling the method to take a specified fit and treat the in sample data as out of sample data, and thereby roll the forecast n.roll times, n.ahead times forward each time.


This will do it:



spec = getspec(modelfit);
setfixed(spec) <- as.list(coef(modelfit));
forecast = ugarchforecast(spec, n.ahead = 1, n.roll = 2579, data = mydata[1:2580, ,drop=FALSE], out.sample = 2579);
sigma(forecast);
fitted(forecast)

No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...