GARCH parameter estimation and forecast in R with rugarch package - r

I have a problem with parameter estimation and forecast for a GARCH model.
I have a time series of volatilities, starting in 1996 and ending in 2009.
I tried to estimate the parameters with the ugarchspec and ugarchfit function:
garch1.1 <- ugarchspec(variance.model=list(model="sGARCH", garchOrder=c(1,1)),mean.model=list(armaOrder=c(0,0)),distribution="std")
garch1.1fit <- ugarchfit(spec=garch1.1,data=RV)
The results seemed to be okay, so I went on with the forecast.
I wanted to use the ugarchforecast or ugarchroll function. But when I tried to do it, I recognized that they work with the wrong date. For example, If I try to do a simple forecast like
forecast <- ugarchforecast(garch1.1fit,n.ahead=2)
I get the following results:
0-roll forecast [T0=1979-04-05 01:00:00]:
Series Sigma
T+1 5.373e-05 3.733e-05
T+2 5.373e-05 3.762e-05
So my problem is: why does R say that T0=1979? This cant be correct as my data starts in 1996 and ends in 2009.
When I had a look at the residuals from garch1.1fit, the date is also wrong.
What's the problem here?

I am not sure what object do you use as RV, but I assume it is a numeric vector. Package rugarch works better with xts objects supported by xts package.
Following code should do the job:
require(xts)
time <- #put here time vector from your data
RV.xts <- na.omit(xts(x = RV, order.by = time))
and then your code with changed object RV for new one RV.xts:
garch1.1 <- ugarchspec(variance.model=list(model="sGARCH", garchOrder=c(1,1)),
mean.model=list(armaOrder=c(0,0)),
distribution="std")
garch1.1fit <- ugarchfit(spec=garch1.1,data=RV.xts)
forecast <- ugarchforecast(garch1.1fit,n.ahead=2)
The code i provided does two things: first it makes an xts object using time. This object will tell your ugarchfit() function what is the time of this model. Second, it omits possible NA data, which function ugarchfit() do not handle.
Make sure if object xts connected dates correctly by checking:
head(RV.xts)
tail(RV.xts)

I think you did not specify date for your ugarch model. Note that R "Date" class is coded in the number of days from the day 1970-01-01.
Following code may help to understand the concept:
as.Date("1970-01-01")
as.numeric(as.Date("1970-01-01"))
as.Date("1970-01-10")
as.numeric(as.Date("1970-01-10"))
As the date is not specified for ugarch model, your data seems to have the number of observations to fill the 1970-1979 years (probably weekends are excluded), and the prediction starts after that period.

Related

Time Series Forecasting using Support Vector Machine (SVM) in R

I've tried searching but couldn't find a specific answer to this question. So far I'm able to realize that Time Series Forecasting is possible using SVM. I've gone through a few papers/articles who've performed the same but didn't mention any code, instead explained the algorithm (which I didn't quite understand). And some have done it using python.
My problem here is that: I have a company data(say univariate) of sales from 2010 to 2017. And I need to forecast the sales value for 2018 using SVM in R.
Would you be kind enough to simply present and explain the R code to perform the same using a small example?
I really do appreciate your inputs and efforts!
Thanks!!!
let's assume you have monthly data, for example derived from Air Passengers data set. You don't need the timeseries-type data, just a data frame containing time steps and values. Let's name them x and y. Next you develop an svm model, and specify the time steps you need to forecast. Use the predict function to compute the forecast for given time steps. That's it. However, support vector machine is not commonly regarded as the best method for time series forecasting, especially for long series of data. It can perform good for few observations ahead, but I wouldn't expect good results for forecasting eg. daily data for a whole next year (but it obviously depends on data). Simple R code for SVM-based forecast:
# prepare sample data in the form of data frame with cols of timesteps (x) and values (y)
data(AirPassengers)
monthly_data <- unclass(AirPassengers)
months <- 1:144
DF <- data.frame(months,monthly_data)
colnames(DF)<-c("x","y")
# train an svm model, consider further tuning parameters for lower MSE
svmodel <- svm(y ~ x,data=DF, type="eps-regression",kernel="radial",cost=10000, gamma=10)
#specify timesteps for forecast, eg for all series + 12 months ahead
nd <- 1:156
#compute forecast for all the 156 months
prognoza <- predict(svmodel, newdata=data.frame(x=nd))
#plot the results
ylim <- c(min(DF$y), max(DF$y))
xlim <- c(min(nd),max(nd))
plot(DF$y, col="blue", ylim=ylim, xlim=xlim, type="l")
par(new=TRUE)
plot(prognoza, col="red", ylim=ylim, xlim=xlim)

X-13 Arima Seats with weekly data does not work

I am using R and I have weekly data (all in all 660 obeservations) and I want to use X-13 Arima-Seats from the seasonal package to seasonally adjust my data. I store my data in a ts object:
library(lubridate)
x <- ts(data, freq=365.25/7, start=decimal_date(ymd("2004-02-01")))
library(seasonal)
x_sa <- seas(x)
However, I get the error:
Error: X-13 run failed
Errors:
- Seasonal period too large. See Section 2.7 of the Reference Manual on program limits
- Expected argument name or "}" but found ".1785714285714"
- Time series could not be read due to previously found errors
- Expected specification name but found "}"
- Specify series before user-defined adjustments
- Need to specify a series to identify outliers
I also tried a shorter period of time, but the error is still the same.
I would average your weekly data by month and run the following ts object:
ts(data, freq=12, start=c(2004,2))
You'll lose some data granularity converting to months instead of weeks, but then the seasonal package will at least be able to process your data.
Try STL (Seasonal and Trend decomposition using Loess). You can use it with any type of seasonality, not only monthly and quarterly.
It has automatic decomposition mstl(). So for your data the formula is:
x_sa <- mstl(x)
There are tuning parameters for the function t.window and s.window with help of with you are able to control how rapidly the trend-cycle and seasonal components can change.
More details you can get from book of Rob J Hyndman and George Athanasopoulos "Forecasting: Principles and Practice". In section "Time series decomposition".

Negative values in timeseries when removing seasonal values with HoltWinters (R)

i'm new to R, so I'm having trouble with this time series data
For example (the real data is way larger)
data <- c(7,5,3,2,5,2,4,11,5,4,7,22,5,14,18,20,14,22,23,20,23,16,21,23,42,64,39,34,39,43,49,59,30,15,10,12,4,2,4,6,7)
ts <- ts(data,frequency = 12, start = c(2010,1))
So if I try to decompose the data to adjust it
ts.decompose <- decompose(ts)
ts.adjust <- ts - ts.decompose$seasonal
ts.hw <- HoltWinters(ts.adjust)
ts.forecast <- forecast.HoltWinters(ts.hw, h = 10)
plot.forecast(ts.forecast)
But for the first values I got negative values, why this is happening?
Well, you are forecasting the seasonally adjusted time series, and of course the deseasonalized series ts.adjust can already contain negative values by itself, and in fact, it actually does.
In addition, even if the original series contained only positive values, Holt-Winters can yield negative forecasts. It is not constrained.
I would suggest trying to model your original (not seasonally adjusted) time series directly using ets() in the forecast package. It usually does a good job in detecting seasonality. (And it can also yield negative forecasts or prediction intervals.)
I very much recommend this free online forecasting textbook. Given your specific question, this may also be helpful.

Detect the order of seasonality of a daily time series

I am working with some daily time series (unequally spaced) problem and want to detect the order of seasonality (and/or the frequency of the data if necessary).
I know there is seasonality from the time series plot and ACF plot. The features of seasonality is obvious. My code looks like the following:
plot(mydates, mydata, type="l")
Acf(mydata)
I tried to fit the data using auto.arima, but It returns a non-seasonal fit.
auto.arima(mydata)
Series: mydata, ARIMA(1,0,1) with zero mean, Coefficients: ....
I also tried function nsdiffs, and it doesn't work either.
nsdiffs(mydata)
Error in nsdiffs(tslist[[1]]) : Non seasonal data
nsdiffs(ts(mydata, frequency=90))
0
I technically cannot use the ts function because I don't really know the frequency of my data (which is what I intend to find out). But I tested anyway, using some random guess of the frequency. It returns 0 every time.
Could anyone help me with this?
Thank you!

r holtwinters predict

I am using R for sometime now and some days ago I found a very interesting function which made a prediction on a given time series. It just took the data from the known time series and applied it on a given period, but it kept the pattern. The problem is that I lost it. I am sure it was a sort of HoltWinters. I am trying two days to find something, but till now without success. Could someone please give me a hand on this!
Just use predict:
# Assuming you have some timeseries data in myts
hw <- HoltWinters(myts)
predict(hw, 10) # predict 10 periods ahead
You can use forecast.HoltWinters
#Model creation
fit <- HoltWinters(ts.data,gamma=FALSE)
#Load forecast package
require(forecast)
#Apply model into forecast
forecast(fit)

Resources