Avoiding seasonality assumption for stl() or decompose() in R - r

I have high frequency commodity price data that I need to analyze. My objective is to not assume any seasonal component and just identify a trend. Here is where I run into problems with R. There are two main functions that I know of to analyze this time series: decompose() and stl(). The problem is that they both take a ts object type with a frequency parameter greater than or equal to 2. Is there some way I can assume a frequency of 1 per unit time and still analyze this time series using R? I'm afraid that if I assume frequency greater than 1 per unit time, and seasonality is calculated using the frequency parameter, then my forecasts are going to depend on that assumption.
names(crude.data)=c('Date','Time','Price')
names(crude.data)
freq = 2
win.graph()
plot(crude.data$Time,crude.data$Price, type="l")
crude.data$Price = ts(crude.data$Price,frequency=freq)
I want frequency to be 1 per unit time but then decompose() and stl() don't work!
dim(crude.data$Price)
decom = decompose(crude.data$Price)
win.graph()
plot(decom$random[2:200],type="line")
acf(decom$random[freq:length(decom$random-freq)])
Thank you.

Both stl() and decompose() are for seasonal decomposition, so you must have a seasonal component. If you just want to estimate a trend, then any nonparametric smoothing method will do the job. For example:
fit <- loess(crude.data$Price ~ crude.data$Time)
plot(cbind(observed=crude.data$Price,trend=fit$fitted,random=fit$residuals),main="")

Related

R libraries forecast::auto.arima vs fable:ARIMA what's the differences?

The online documentation indicates that the algorithm under the hood is the same to estimate the (s)Arima models. During some tests, with a Kaggle dataset, I had different models: ARIMA function show me a sArima, auto.arima only Arima model.
auto.arima(tsbble_item1_store1$sales)
give
Best model: ARIMA(5,1,2)
and
tsbble_item1_store1 %>%
model(arima = ARIMA(sales))
give
# A mable: 1 x 2
# Key: store [1]
store arima
<dbl> <model>
1 1 <ARIMA(1,1,3)(0,0,2)[7]>
I have very different models. By the way, Arima's fable function shows me a better model, because it controls seasonality respect auto.arima function that doesn't, and the data show evident seasonality.
Does someone know the main differences in default parameters when the two functions try to estimate the model, because I didn't understand from docs?
Sorry if I had some mistakes
thank's in advance
Have nice day
MC
forecast::auto.arima() requires a ts object. That is a vector with some time series attributes including the seasonal frequency. When you just pass a numeric vector, as you have here, it assumes the seasonal frequency is 1 (as for annual data) and will not fit a seasonal ARIMA model.
On the other hand, the tsibble object contains a time index (in this case it looks like it is a date variable) and ARIMA() will use that index to determine what type of seasonality (if any) is present. With a date variable, it will select seasonal frequency of 7 to correspond to a time of week seasonality.
To get the same thing with forecast::auto.arima(), use
auto.arima(ts(tsbble_item1_store1$sales, frequency=7))

How to remove seasonality and trend from GDP time series data in R

I am doing a time series analysis to forecast the GDP for the next years and in order to get a good forecasting model I need to remove the trend and the seasonality.
I have used the seasonally adjusted data but it did not completely remove the trend and seasonality of the data. I am using the multiplicative method to remove trend and seasonality.
Seasonally adjusted GDP
decmopose_GDP <- decompose(GDP, 'multiplicative')
adjustGDP <- GDP/decmopose_GDP$seasonal
plot(adjustGDP)
Does anyone know any other method to remove trend and seasonality from the time series ?
You can try categorical variable of seasons and splines for time. For example, the model can be . In the model, X contains indicator variable of seasons and also splines for time (you can give them specific degree of freedom). Then the GDP with seasonality and time trend removed will be obtained, i.e., residuals of the model. The code can be as follows.
##Fitting a model with season and time variable
model1 <- lm(gdp ~ cat_season + ns(time, df = n))
##Extract the GDP without time trend
GDP_withouttrend <- resid(model1)
##Plot the GDP without trend
plot(GDP_withouttrend)
In case it may be useful for those who still read this old question, there are many R packages (e.g., stl)available to decompose a time series into seasonality, trend, and remainder. I believe what is being looked for here is the remainder component. Here I use a package called Rbeast developed by me as an example. Rbeast does time series decomposition and changepoint detection at the same time.
library(Rbeast)
Y = covid19$newcases # covi19 is a daily time series coming with the Rbeast package
# the 'seasonality'/periodicity is 7 days (a week)
o = beast(Y)
plot(o)
remainder = Y - o$trend$Y -o$season$Y
library(Rbeast)
Ylog = log( covid19$newcases+0.001) # treat covid19 using a multiplicative decomposition model
# the first datapoint is 0; adding 0.001 to nudge it a bit to avoid getting a inf from the log
o = beast(Ylog,freq=7)
plot(o)
remainder = exp( Ylog-o$trend$Y -o$season$Y )

Auto-ARIMA function in R giving odd results

I have a day level dataset for 3 years,
I ran auto.arima() in R on it for simple time series forecasting and it gave me a (2,1,2) model.
When I used this model to predict the variable for the next 1 year the plot became constant after a few days, which can't be correct
As I have a daily data for 3 years, and a frequency of 364 days, is ARIMA incapable of handling daily data with large frequencies?
Any help will be appreciated
This sounds like you are trying to forecast too far into the future.
The forecast for tomorrow is going to be accurate, but the forecast for the next day and the day after that are not going to be influenced much by the past data and they will therefore settle around some constant when trying to forecast too far into the future. "Too far into the future" probably means two or more time points.
Lets say you have data up until time point T+100, which you used to estimate your ARIMA(2,1,2) model. You can "forecast" the value for time T+1 by pretending you only have data until point T and use your ARIMA(2,1,2) model to forecast T+1. Then move ahead by one period in your data and pretend you only have data until time T+1 and "forecast" T+2. This way you can assess the forecasting accuracy of your ARIMA(2,1,2) model, for example by calculating the Mean Squared Error (MSE) of the "forecasts".

forecasting time series by updating in R

I'm working on time series with a monthly demand for 5 years in R. Currently, I'm using naive method to forecast 12 months (h=12)and it does work very well I want to forecast only for one month (h=1) (always with naive method) and then include this predicted value to time series and repeat this process 12 times. For example:
get forecast for January 2013
include this predicted value to time series
apply naive method for the new series
My time series is not stationary and has a trend and seasonality.
What I'm looking to do is to forecast using Naive but step by step (month by month) with updating my time series each step. How can I do that?
Surely you will get the same answer. The naive method uses the most recent observation as the forecast. So your first forecast will be equal to the last observation. Your second forecast will be equal to the first forecast, and so is also equal to the last observation. And so on.
In any case, what you describe is precisely how almost all time series forecasts work. It is called the recursive method of forecasting, where predicted values take the place of observations as you forecast further ahead. In the forecast package for R, all purely time series forecasts are created this way.

R: Generate a Seasonal ARIMA time-series model using parameters of existing data

I have a count time series data which I'm able to use to determine the parameters of the underlying stochastic process. For example say I have a SARIMA (p,d,q)(P,D,Q)[S] seasonal ARIMA model.
How do I use this to generate a new count time series data set?
Being even more specific: a SARIMA(1,0,1)(1,0,0)[12] - how can I generate a time series for a 10 year period for each month? (i.e., 120 points to estimate the number of 'counts'.)
Use simulate.Arima() from the forecast package. It handles seasonal ARIMA models whereas arima.sim() does not.
However, ARIMA models are not suitable for count time series as they assume the process is defined on the whole real line.

Resources