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")
Related
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().
I want to know how to make 'seasonal' in stl function.
I guess 'trend' is diff time series.
and how to make seasonal??
in R
'stl' description is
which.cycle <- cycle(x)
z$seasonal <- tapply(z$seasonal, which.cycle, mean)[which.cycle]
and R documentation
fdrfourier Calculation of the false discovery rates (FDR) for periodic expression
backgroundData Generation of background expression set
ar1analysis Performs AR1 fitting
fourierscore Calculation of the Fourier score
AR and Fourier transform ??
A time series cannot be made seasonal, strictly speaking. I suspect you mean extracting the seasonality pattern using the stl() function in R.
Let's consider an example of a time series measuring the maximum recorded air temperature in Dublin, Ireland from 1941 to 2019.
Here is a graph of the original time series:
weatherarima <- ts(mydata$maxtp, start = c(1941,11), frequency = 12)
plot(weatherarima,type="l",ylab="Temperature in Celsius")
title("Maximum Air Temperature - Dublin")
The seasonal, trend, and random components can be extracted with stl() as follows:
stl_weather = stl(weatherarima, "periodic")
seasonal_stl_weather <- stl_weather$time.series[,1]
trend_stl_weather <- stl_weather$time.series[,2]
random_stl_weather <- stl_weather$time.series[,3]
plot(as.ts(seasonal_stl_weather))
title("Seasonal")
plot(trend_stl_weather)
title("Trend")
plot(random_stl_weather)
title("Random")
Seasonal
Trend
Random
As can be observed, there is clear seasonality in the weather data - given the change in temperature across seasons - but this was merely extracted from the series by using stl() - not created as such.
You might find the following to be informative: Extracting Seasonality and Trend from Data: Decomposition Using 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".
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.
I have a 10 year daily time series of air temperatures:
x <- c(rep((seq(-3,5,by=0.85)),365),NA)
I converted it to a time series object like this:
x <- ts(x, frequency=10, start=1)
and ran stlm
stlm(x, s.window=365, robust=TRUE, allow.multiplicative.trend=TRUE, level=0.95)
which produced the error
error in na.fail.default(as.ts(x)) : missing values in object
This is very strange, because meteorological time series are highly seasonal. What could I do to fix that? Is there a problem with the zeros?
Any help appreciated.
UPDATE: There was one missing value in my time series, which produced the error. The partial code
robust=TRUE, allow.multiplicative.trend=TRUE, level=0.95
produced another error and the arguments obviously cannot be used.
How can I decompose my time series adequately into season and trend in order to identify the trend which eventually changed during the 10 years?
you could also try using the dsa package, which is explicitly designed to handle daily data. You have to convert your data first to xts, but then, you'll be fine,
library(dsa); library(xts)
x <- c(rep((seq(-3,5,by=0.85)),365),NA) # I didn't change these strange looking data ;-)
# Converting to xts
dates <- seq(as.Date("2010-01-01"),length=length(x),by="days")
x <- xts(x, order.by=dates)
result <- dsa(x, fourier_number=24) # if no monthly recurring cycle is visible
# fourier_number can be reduced or left empty.
sa <- result$output[,1] # This is the seasonally adjusted series
xtsplot(result$output[,c(2,1)], names=c("original series", "seasonally adjusted series"))