rtime-seriesforecast

Forecast::ets throwing 'lastseason' error with weekly time series length > 52 ... bug?


I think I stumbled upon a bug?

I have a generic forecasting process that runs ets on many different time series. Yes, I know that forecast::ets is not intended to work on data with frequency greater than 24, but at least it does not throw an error. But I found a strange example...

library(forecast) #version 8.23.0

ts1 <- structure(c(0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01
), tsp = c(2018.99452054795, 2019.99109823446, 52.1785714285714
), class = "ts")

window(ts1, end = 2019.99) |> ets()  # works great, time series of length 52

ts1 |> ets() # throws error, time series of length 53

The time series is pretty boring, just a bunch of 0.01 measurements made on a weekly basis.

I'm not sure why having a time series of length > 52 causes this error. Interestingly, changing the last measurement also causes the error to go away.

structure(c(0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
            0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
            0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
            0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 
            0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.01, 0.02 # <- different
), tsp = c(2018.99452054795, 2019.99109823446, 52.1785714285714
), class = "ts") |> ets() # time series length of 53, works fine

I tried to investigate the source of the error: Error in if (is.na(lastseason)) { : argument is of length zero.

I searched the source code in both forecast::ets and https://github.com/robjhyndman/forecast/blob/master/R/ets.R but I was unable to find any reference to lastseason.

I'm guessing lastseason = NULL is what's causing the error, but I don't know where that line of code is coming from, or how to avoid it.

Perhaps I should send a bug report to the maintainer of the forecast package?

I just want ts1 |> ets() to work. Any ideas are greatly appreciated!


Solution

  • This is being caused by the non-integer seasonality. When you pass a time series that is constant, ets() by-passes some of the checks to return an essentially null model. Changing the last value meant the time series was no longer constant. I've now fixed the problem on the dev version (https://github.com/robjhyndman/forecast/)