r holtwinters predict - r

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)

Related

Time Series and MA-model look equal in R

I am using the forecast package of R and I created a MA(1) model by using the ARIMA function. I plotted the time series itself ($x variable of the ma_model), the model itself ($fitted variable of the ma_model) and the residuals (residuals variable of the ma_model). Strangely the time series looks equal to the model altough there are nonegative residuals. Here is the code that I used:
library(forecast)
ma_model<-Arima(ts(generationData$Price[1:200]), order=c(0,1,0))
plot(ma_model$fitted, main = "Fitted")
plot(ma_model$x, main = "X")
plot(ma_model$residuals, main = "Residuals")
Here is the result
Basically the model can't be equal to the real time series especially when having residuals. Can anyone explain this to me? I'd appreciate every comment.
Update: I tried to use the order=c(0,0,20) so I have a MA(20) or AR(20) model (I am not sure which parameters stands for MA and AR). Now the fitted curve and the original time series look quite equal (but not exactly equal). Is this possible and usual? I'd appreciate every further comment.
Any comments on this issue?
I am not sure about your output, but from the code it seems that you just took the difference in the model, not the MA.
I think it should be order=c(0,0,1) instead of order=c(0,1,0) for building the MA model.

Weekly forecasts with holidays

I use Hyndman's forecast package to produce a somewhat accurate tbats forecast at the weekly level, but I have significant errors on holidays. How can I include holidays in the model? Also, Arima has been shown to fit my weekly data poorly. So holidays would have to be added in a non-arima way.
I have seen two solutions. One https://robjhyndman.com/hyndsight/dailydata/ shows how to add holidays as dummy variables with fourier terms. The problem is dummy variables take the form of 1 or 0. I know that different holidays have different effects that a 1 or 0 would not capture. Black Friday, for example, is very different from Chinese New Year.
Another solution is have seen is here https://robjhyndman.com/hyndsight/forecast7-part-2/ where covariate nnetr change is used as an alternative to auto.arima with seasonal dummy variables. The problem is I don't see how to write the R code to input my holidays. An example would be useful.
The benchmark for time series modeling for use by official statistics agencies is x13-arima-seats by the US Census bureau. It deals with seasonal effects as well as with "parametric" holidays including, say, the Chinese New Year as well as Easter.
The functionality is available in R via the seasonal package which installs and uses the underlying x13-arima-seats binary.
And there is also a full-feature interactive website giving access to most-if-not-all features.
Have you read about Facebook's prophet package?
Haven't used it but from reading the documentation, it seems like a quick implementation that also accounts for holidays:
https://cran.r-project.org/web/packages/prophet/prophet.pdf
Implements a procedure for forecasting time series data based on
an additive model where non-linear trends are fit with yearly and weekly
seasonality, plus holidays [...]
https://cran.r-project.org/web/packages/prophet/vignettes/quick_start.html
The following did everything I needed it to do.
k=23
#forecast holidays
#bool list of future holidays
holidayf <- c(0,0,0,0,0,1,0,0,0,1,1,1,1,1,0,0,0)
h <- length(holidayf)
#given holidays
holiday <- df[,2]
y <- ts(df[,1],start = 2011,frequency = 52)
z <- fourier(y, K=k)
zf <- fourier(y, K=k, h=h)
fit <- auto.arima(y, xreg=cbind(z,holiday), seasonal=FALSE)
fc <- forecast(fit, xreg=cbind(zf,holidayf), h=h)
fc %>% autoplot()
summary(fit)
To solve the problem of different holidays having different effect, I simply added additional holiday dummy variables. For example, you can make a vector of good holidays and a vector of bad holidays and cbind them then put them in xreg. I did not show this in the above code, but it is straight forward.

Daily temperature simulation/forecast with Arima/HW

I have a time series of daily temperature from 1960 to 2015, and would like to forecast for 2016. My goal is to use a simple method while still be able to capture the trend and seasonality of the data over time. (I actually only care about the weather condition from May to August, but not sure if I can just use data from May to August in the past years to get a convincing forecast, so I am forecasting for a year)
I tried auto.arima, it suggested an order of (2,0,1), but the results seemed pretty bad (see plot)[forecasts from arima][1]
In addition, I tried HoltWinters smoothing method and got a seemingly reasonable result. However I don't know if this method is good at forecasting temperature.
[forecast from HW][2]
I too am hesitant to provide an answer without more information; however, I will suggest that for someone without a lot of experience in time series forecasting, the auto.arima function in the "forecast" package is excellent. It has a built in optimization that searches for the best ARIMA order (p,d,q). Here is some example code:
install.packages("forecast")
library(forecast)
set.seed(1234)
tsdatav <- (seq(1:300) + rnorm(300, 1000, 10))
myts <- ts(tsdatav, frequency = 365, start = c(2017, 6))
mytsfit <- auto.arima(myts)
mytsfit #to my example data, fit an ARIMA(5,1,0) with drift
mytsforecast <- forecast(mytsfit, 50, level = c(80, 95))
plot(mytsforecast)
Notice that in the forecast function, you can set how many periods you want to forecast out as well as confidence intervals (in addition to the point forecast).
Duke has an excellent website on ARIMA forecasting at https://people.duke.edu/~rnau/411arim.htm
Again, this is just one suggestion. There are many forecasting approaches that work better than others given problem specifics.

GARCH parameter estimation and forecast in R with rugarch package

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.

neural network time series prediction tsDyn nnetTS

I'm using tsDyn package to predict time series data in R. there is a function in this package called nnetTs. However when I try to predict, it just gives me 1 output and does not provide x steps ahead forecast. See eblow for the code:
library("tsDyn")
set.seed(1234)
mod.nnet <- nnetTs(log(lynx), m=2, size=3,steps=12)
mod.nnet
predict(mod.nnet,steps=12)
here is the output (as noted above, I'm just getting 1 single output and not 12 steps ahead prediction). I'm not sure what the issue is, I read the documentation, I'm stuck.
Time Series:
Start = 1935
End = 1935
Frequency = 1
[1] 7.80263
Any help would be greatly appreicated
You should run
predict(mod.nnet,n.ahead=12)
at the last line. The argument for selecting forecast horizon is n.ahead not steps.

Resources