Search code examples
rforecastingforecast

Error in hw(train): The time series should have frequency greater than 1 (forecast library)


What does this mean? My timeSeries has a frequency of 365, doesn't it? What I'm trying to do is make 3 years of daily forecasts, one day at a time. To put it differently, I'd like to get a forecast for the next day, 365*3 times.

library(forecast)
df = read.csv("./files/all_var_df.csv")

ts = as.timeSeries(df[, c(1, 2)])
train = as.timeSeries(ts[0:3285, ])
validation = ts[3285:4380]

fit_hw <- hw(train)
fit2_hw <- hw(validation, model=fit_hw)
onestep_hw <- fitted(fit2_hw)

Error in hw(train): The time series should have frequency greater than 1.

Here is some info that might help you answer:

class(train)
> [1] "timeSeries"

head(train, 3)
> 2005-01-01 101634.4 
> 2005-01-02 106812.5 
> 2005-01-03 119502.8 

length(train)
> [1] 3285

Solution

  • Without actually seeing your data I can only speculate. I can, however, recreate this problem using available datasets in R. In the library(fpp2) in R, the dataset ausair contains "Total annual air passengers (in millions) including domestic and international aircraft passengers of air carriers registered in Australia. 1970-2016."

    Reading in this dataset as a ts (air <- window(ausair, start = 1990)), we get the following:

    Time Series:
    Start = 1990 
    End = 2016 
    Frequency = 1 
     [1] 17.55340 21.86010 23.88660 26.92930 26.88850 28.83140 30.07510 30.95350
     [9] 30.18570 31.57970 32.57757 33.47740 39.02158 41.38643 41.59655 44.65732
    [17] 46.95177 48.72884 51.48843 50.02697 60.64091 63.36031 66.35527 68.19795
    [25] 68.12324 69.77935 72.59770
    

    I will now use the hw() function to train with:

    fc <- hw(air, seasonal = "additive")
    

    This gives the following error:

    Error in hw(air, seasonal = "additive") : 
      The time series should have frequency greater than 1.
    

    What has happened here is that each datapoint corresponds to a whole year. So the Holt-Winters method is unable to find seasonality. The seasonal portion of the HW method follows the following equation:

    Where the term represents the seasonality, and m the frequency. It doesn't make sense to talk about a repetitive seasonal pattern if there is only 1 data point in a time period.

    The fix to your problem is in the way you define your time series object with ts(). One argument of the time series function is frequency. Without seeing your data I can't say what that value should be set to. Here is a site explaining the term frequency. It will be dependent on what seasonality your data exhibits. Does it repeat a seasonal pattern every week? Every quarter? If there is no seasonal pattern, you can switch to the function holt() which only uses an exponential decay term and trend term to find a pattern and will not give your error.