Search code examples
rtime-seriesforecast

How does forecast() of a tslm() model decide how many periods to forecast?


What is happening under the hood here with the h values? If you could point me to the documentation that describes this I would really appreciate it! Thanks

library(forecast)
model <- tslm(USAccDeaths ~ trend + season)
forecast(model)                               # returns 12 months of forecasts
forecast(model, h=0)                          # returns  2 months of forecasts
forecast(model, h=1)                          # returns  1 month  of forecasts
forecast(model, h=2)                          # returns  2 months of forecasts (same as h=0)

Solution

  • The default forecast horizon is 10 months, not 12. h=0 is a bug, which I have fixed in https://github.com/robjhyndman/forecast/commit/4ed33c167822ba503bc4a6fdcac2b04cd5018459

    All other forecast horizons (1, 2, ...) are behaving as expected.

    library(forecast)
    #> Registered S3 method overwritten by 'quantmod':
    #>   method            from
    #>   as.zoo.data.frame zoo
    model <- tslm(USAccDeaths ~ trend + season)
    forecast(model)
    #>          Point Forecast    Lo 80     Hi 80    Lo 95     Hi 95
    #> Jan 1979       7557.275 6917.525  8197.025 6569.565  8544.985
    #> Feb 1979       6797.108 6157.358  7436.858 5809.399  7784.818
    #> Mar 1979       7575.608 6935.858  8215.358 6587.899  8563.318
    #> Apr 1979       7788.608 7148.858  8428.358 6800.899  8776.318
    #> May 1979       8637.608 7997.858  9277.358 7649.899  9625.318
    #> Jun 1979       9108.608 8468.858  9748.358 8120.899 10096.318
    #> Jul 1979       9966.108 9326.358 10605.858 8978.399 10953.818
    #> Aug 1979       9262.442 8622.692  9902.192 8274.732 10250.151
    #> Sep 1979       8213.608 7573.858  8853.358 7225.899  9201.318
    #> Oct 1979       8503.442 7863.692  9143.192 7515.732  9491.151
    forecast(model, h=0)
    #> Error in forecast.lm(model, h = 0): The forecast horizon must be at least 1.
    forecast(model, h=1)
    #>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
    #> Jan 1979       7557.275 6917.525 8197.025 6569.565 8544.985
    forecast(model, h=2)
    #>          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
    #> Jan 1979       7557.275 6917.525 8197.025 6569.565 8544.985
    #> Feb 1979       6797.108 6157.358 7436.858 5809.399 7784.818
    

    Created on 2022-08-31 by the reprex package (v2.0.1)