X-13 Arima Seats with weekly data does not work - r

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".

Related

how to handle non-periodic timeseries in bfast (R)

my problem is the following : I have a Landsat NDVI time series that is non-periodic/doesn't have a homogenous frequency. However, the error code I receive is
Error in stl(Yt, "periodic") : series is not periodic or has less than two periods
after having tried to convert my data into a timeseries without explicitely setting a frequency :
test_timeseries <-ts(test$nd, start = c(1984,4), end = c(2011,10)). when I try to calculate the frequency or deltat with the help of the functions frequency() or deltat(), it both leads to 1 - which I don't understand , as I have non-periodic data for nearly every month and not only once a year.
So my question is, how to set the frequency in this case and how to deal with this circumstance of non-periodicity ? It seems like, without setting a frequency, I cannot use the function bfast().
sorry if the answer is obvious, I'm very new to timeseries analyses.
Please read the help file. It helps. In this case, it describes the following argument.
season : the seasonal model used to fit the seasonal component and detect seasonal breaks (i.e. significant phenological change). There are three options: "dummy", "harmonic", or "none" where "dummy" is the model proposed in the first Remote Sensing of Environment paper and "harmonic" is the model used in the second Remote Sensing of Environment paper (See paper for more details) and where "none" indicates that no seasonal model will be fitted (i.e. St = 0 ). If there is no seasonal cycle (e.g. frequency of the time series is 1) "none" can be selected to avoid fitting a seasonal model.
So set season = "none" in bfast().

ERROR in R: decompose(y) : time series has no or less than 2 periods

I have a time series data of daily transactions, starting from 2017-06-28 till 2018-11-26.
The data looks like this:
I am interested to use decompose() or stl() function in R. But I am getting
error:
decompose(y) : time series has no or less than 2 periods
when I am trying to use decompose()
and
Error in stl(y, "periodic") :
series is not periodic or has less than two periods
when I am trying to use stl().
I have understood that I have to specify the period, but I am not able to understand what should be the period in my case? I have tried with the following toy example:
dat <- cumsum(rnorm(51.7*10))
y <- ts(dat, frequency = 517)
plot.ts(y)
stl(y, "periodic")
But I couldn't succeed. Any help will be highly appreciated.
The frequency parameter reflects the number of observations before the seasonal pattern repeats. As your data is daily, you may want to set frequency equal to 7 or 365.25 (depending on your business seasonality).
Of course, the larger the business seasonality, the more data you need (i.e. more than 2 periods) in order to decompose your time series. In your case, you set the frequency to 517, but have data available for less than two periods. Thus, the seasonal decomposition cannot happen.
For more info, please see: Rob Hyndman's Forecasting Principles and Practice book

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.

remove seasonality from weekly time series data

I need to decompose a series to remove seasonality. The series has 2 columns date and volume.
This is what my time series object looks like:
salestsDec <- ts(salests, frequency=52, start=c(2010, 1), end=c(2014,12))
I ran the decompose() function on a 'ts' object.
salests = sales[, c(1,6)]
View(salests)
salestsDec <- ts(salests, frequency=52, start=c(2010, 1), end=c(2014,12))
salestsDec <- decompose(salestsDec, type=c("additive"))
plot(salestsDec)
Upon, running the decompose() function, I get a list of 6 components, observed, trend, seasonal, random for both date and volume. I should only be seeing, observed, trend, seasonal and random component for Volume in my plot.
I've attached an image of what the plot looks like.
Moreover, when I try to remove seasonal component from the series, I am getting an error. It appears that it's the same underlying issue.
Error:
Error in salests - salestsDec$seasonal :
non-numeric argument to binary operator
In addition: Warning message:
Incompatible methods ("Ops.data.frame", "Ops.ts") for "-"
Seasonal decomposition of time series by loess can help.
stl(salestsDec,"periodic")

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