0

I am trying to use NNETAR to make forecast on data with seasonality (every 12 months); in the console it is returned a warning message:

Warning message:
Series too short for seasonal lags 

I kindly ask an help on how to get rid of this and be sure that the seasonality component has been taken in account

Below a simplified example which has same behavior:

library(lubridate)
library(tidyverse)
library(tsibble)
library(fable)
#
past <- tibble(yyyy_mm = seq(as.Date("1900-01-01"),as.Date("2020-05-01"),"month"),
              avg_d = runif(1445,1000000,2500000)) %>%
  mutate(yyyy_mm = yearmonth(yyyy_mm)) %>% 
  as_tsibble()
#
future <- tibble(yyyy_mm = seq(as.Date("2020-06-01"),as.Date("2020-12-01"),"month")) %>%
  mutate(yyyy_mm = yearmonth(yyyy_mm)) %>% 
  as_tsibble()
#
fit <- past %>% model(nnar = NNETAR(avg_d ~ AR(p=2,P=1,period=12)))
forecast(fit,future)

Thanks for support!

l_30
  • 1

1 Answers1

1

The warning is returned because the dataset future used for the argument new_data in forecast() is too short. To get rid of the warning and taking into account P = 1, the future dataset can be extended. Reproducible example ist attached.

library(lubridate)
#> 
#> Attache Paket: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(tidyverse)
library(tsibble)
#> 
#> Attache Paket: 'tsibble'
#> The following object is masked from 'package:lubridate':
#> 
#>     interval
library(fable)
#> Lade nötiges Paket: fabletools
#
past <- tibble(yyyy_mm = seq(as.Date("1900-01-01"),as.Date("2020-05-01"),"month"),
               avg_d = runif(1445,1000000,2500000)) %>%
  mutate(yyyy_mm = yearmonth(yyyy_mm)) %>% 
  as_tsibble()
#> Using `yyyy_mm` as index variable.

future <- tibble(yyyy_mm = seq(as.Date("2020-06-01"),as.Date("2020-12-01"),"month")) %>%
  mutate(yyyy_mm = yearmonth(yyyy_mm)) %>% 
  as_tsibble()
#> Using `yyyy_mm` as index variable.

fit <- past %>% model(nnar = NNETAR(avg_d ~ AR(p=2,P=1,period=12)))
forecast(fit,future)
#> Warning: Series too short for seasonal lags
#> # A fable: 7 x 4 [1M]
#> # Key:     .model [1]
#>   .model  yyyy_mm        avg_d    .mean
#>   <chr>     <mth>       <dist>    <dbl>
#> 1 nnar   2020 Jun sample[1000] 1758676.
#> 2 nnar   2020 Jul sample[1000] 1743311.
#> 3 nnar   2020 Aug sample[1000] 1766885.
#> 4 nnar   2020 Sep sample[1000] 1764609.
#> 5 nnar   2020 Okt sample[1000] 1744808.
#> 6 nnar   2020 Nov sample[1000] 1715543.
#> 7 nnar   2020 Dez sample[1000] 1738346.

# reason for the warning message: 
# inside forecast.NNETAR specials_nnetar() is called: 
P <- 1
period <- 12

# taken from specials_nnetar(): 
if (P > 0 && NROW(future) < period * P + 2) {
  rlang::warn("Series too short for seasonal lags")
  P <- 0
}
#> Warning: Series too short for seasonal lags

# extend future; NROW(future) must be equal to period * P + 2 = 14: 
future2 <- tibble(yyyy_mm = seq(as.Date("2020-06-01"),as.Date("2021-07-01"),"month")) %>%
  mutate(yyyy_mm = yearmonth(yyyy_mm)) %>% 
  as_tsibble()
#> Using `yyyy_mm` as index variable.

P <- 1
period <- 12

# taken from specials_nnetar(): 
if (P > 0 && NROW(future2) < period * P + 2) {
  rlang::warn("Series too short for seasonal lags")
  P <- 0
}
forecast(fit,future2)
#> # A fable: 14 x 4 [1M]
#> # Key:     .model [1]
#>    .model  yyyy_mm        avg_d    .mean
#>    <chr>     <mth>       <dist>    <dbl>
#>  1 nnar   2020 Jun sample[1000] 1726252.
#>  2 nnar   2020 Jul sample[1000] 1733855.
#>  3 nnar   2020 Aug sample[1000] 1750326.
#>  4 nnar   2020 Sep sample[1000] 1752966.
#>  5 nnar   2020 Okt sample[1000] 1759741.
#>  6 nnar   2020 Nov sample[1000] 1729142.
#>  7 nnar   2020 Dez sample[1000] 1746633.
#>  8 nnar   2021 Jan sample[1000] 1756297.
#>  9 nnar   2021 Feb sample[1000] 1738609.
#> 10 nnar   2021 Mrz sample[1000] 1748182.
#> 11 nnar   2021 Apr sample[1000] 1719133.
#> 12 nnar   2021 Mai sample[1000] 1718844.
#> 13 nnar   2021 Jun sample[1000] 1745875.
#> 14 nnar   2021 Jul sample[1000] 1730472.

Created on 2020-07-01 by the reprex package (v0.3.0)

Tim-TU
  • 408
  • 1
  • 4
  • 13
  • Thanks Tim91! clear the technical reason of raise of the warning and your proposed action to overcome the message. However I'm puzzled about the behavior; warning message and setting P to zero is ok when fitting initially the model (to check presence of enough lags for seasonality) but IMHO it should not occur when using the already fitted model to forecast. Am I got it wrong? – l_30 Jul 01 '20 at 17:16
  • This behaviour has been now been fixed: https://github.com/tidyverts/fable/commit/097ad50e8d16d99cb2da9b40ea798113002a4e8f Thanks for bringing up this issue. – Mitchell O'Hara-Wild Jul 02 '20 at 12:24