Error in decompose function

I have a time series(xts object) which has data recorded hourly for 15 years. I want to decompose the time series and observe trend, seasonality etc. When I use the decompose or stl function to do so, I get the error that there are less than 2 periods. I've used the wo function in seastests package to confirm seasonality in the data. I've used findfrequency function to check if the time series I created has the correct frequency and it shows 24 which looks correct. What might be wrong here? TIA.

The solution is to have as few as a single observation more than the period being tested. Not at all obvious from the documentation.

1 Like

The xts object has time stamps to the second, so it will interpret the frequency of hourly data as 1/3600 and decompose() does not know how to handle frequencies less than 1. There are two choices.

  1. Redefine the object to have seasonality of 24 so it picks up the daily pattern (but not a weekly or annual pattern if they exist). It will make a mess of the time axis, but it will give you a decomposition. To do this, simply use
decompose(ts(xts.obj, frequency=24))
  1. A nicer approach, but slightly more work, would be to use the feasts package which uses tsibble objects.
library(feasts)
df <- tsbox::ts_tsibble(xts_obj)
decomp <- df %>% model(STL(Wind)) %>% components()
decomp %>% autoplot()

This will automatically identify that it is hourly data and the decomposition will show the daily, weekly and annual patterns (the latter only if the series is long enough).

2 Likes

Hi,
Thank you for your response. In the second approach, what needs to be passed as argument to the STL function? Do I use the vector with only the values there?

I'm not sure if I got you. Could you please elaborate?

Listen to Prof. Hyndman in all things. You'll find the tsibble and related fpp3 text very helpful.

When I ran into the same problem, I ran across this advice, which worked

Let’s say you have exactly 2 years of monthly sales data (24 data points) for an ice-cream company and your goal is to find out seasonality in it.
First thing STL would do is consume the data for 2 or more years (24 or 36 or 48 months) in order to calculate Seasonality, Trend etc. In this case we have exactly 24 data points. Now, STL would required atleast an additional data point ON which it would predict the seasonality. Since STL has already used your first 24 data points in learning the monthly seasonality, the next data point is absolutely required to extend what has been predicted earlier.
In other words, first 24 data points are being used to check out the seasonality while the next data points (greater than 24) will follow the previously calculated seasonal patterns of two years. For January sales, you might see a dip in the STL plot.

If 24 is absolutely the most you have, pick a reasonable mean to add.

1 Like

The STL argument is the name of a column in your data set. See 3.6 STL decomposition | Forecasting: Principles and Practice (3rd ed) for examples

2 Likes

Thanks, I'll have a look

Oh okay. Thanks for the help.

Hi,
After running the code, I found the following error:

decomp <- df_temp %>% model(STL(value)) %>% autoplot()
Error: Objects of type mdl_df/tbl_df/tbl/data.frame not supported by autoplot.

Any package that I should install or any other way to plot my result? Just to confirm the result I obtained in decomp is a 1x1 mable.

Sorry, I left out the components() function. I've updated the code now.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.