robjhyndman / forecast

Forecasting Functions for Time Series and Linear Models
http://pkg.robjhyndman.com/forecast
1.12k stars 342 forks source link

Theta Forecast Error when n <= frequency #762

Closed cbojer closed 5 years ago

cbojer commented 5 years ago

I'm currently using the forecasting package to automatically batch forecast a large number of daily time series where I'm only considering the weekly seasonality, i.e. frequency = 7. Here I ran into an issue with the thetaf function with new/small time series (n <= frequency), producing the following error:

Error in if (seasonal) { : missing value where TRUE/FALSE needed

The problem in particular happens as the seasonal variable in the function is assigned an NA value due to trying to access an element of the ACF value vector that doesn't exist. This could easily be remedied by including a check of whether n <= frequency before conducting the seasonal check.

A reproducible example:

# Produces the error
ts(c(35, 24, 30, 26, 40, 32, 27), frequency = 7) %>% thetaf(h = 7)

# Setting frequency = 1 makes it work, since this means seasonal = FALSE
ts(c(35, 24, 30, 26, 40, 32, 27), frequency = 1) %>% thetaf(h = 7)

# Adding one more observation to the vector makes it work:
ts(c(35, 24, 30, 26, 40, 32, 27, 29), frequency = 7) %>% thetaf(h = 7)

#  auto.arima and ets handles the case fine
ts(c(35, 24, 30, 26, 40, 32, 27), frequency = 7) %>% ets()
ts(c(35, 24, 30, 26, 40, 32, 27), frequency = 7) %>% auto.arima()
robjhyndman commented 5 years ago

Thanks. Fixed in https://github.com/robjhyndman/forecast/commit/91a39e884de9fe871e83d1eb8799d260507d6283