Search code examples
rtime-seriesforecasting

error in stl, series has less than two periods (erroneous?)


I have two years of monthly data but stl() seems to need a minimum of two years and one month.

Here are two simple examples:

Example 1 - returns

Error in stl(x, "periodic") : series is not periodic or has less than two periods

dat_24 <- cumsum(rnorm(24))
x_24 <- ts(dat_24, frequency = 12)
stl(x_24, "periodic")

Example 2 - returns forecast as expected

dat_25 <- cumsum(rnorm(25))
x_25 <- ts(dat_25, frequency = 12)
stl(x_25, "periodic")

Shouldn't I be able to get a forecast with only 24 numbers with frequency = 12?


Solution

  • It holds true for all kind of periodic series, be it a weekly or yearly or any other period. You have to have atleast 1 extra data point, in addition to atleast 2 complete cycles of the period.

    Here is an example for weekly series where you will get error too:

    dat_Weekly <- cumsum(rnorm(104,0,5))
    x_Weekly <- ts(dat_Weekly, frequency = 52, start = c(2013))
    stl(x_Weekly, "periodic")
    

    You can go through the original paper: here

    Below explanation might not be the best one, but I want to give a shot.

    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.