Error in seasonal_naive model

library(tsibble)
library(tidyverse)
library(tsibbledata)
library(fable)
library(forecast)

I am new to the tsibble package. I am trying to produce a forecast using a seasonal naive, SNAIVE(Y) model.

I am following the examples of the book "Forecasting: Principles and Practice" .

Also, i am using the global_economy dataset from the package tsibbledata

australia <- global_economy %>% filter(Country == "Australia")

australia_fit <- australia %>% 
  model(naive_seasonal = SNAIVE(GDP, lag("year")))

The lines of code above return me the following warning message:

Warning message:
1 error encountered for naive_seasonal
[1] Non-seasonal model specification provided, use RW() or provide a different lag specification.

I dont know what am i supposed to input in the lag specification.

Help would be appreciated!


Referred here by Forecasting: Principles and Practice, by Rob J Hyndman and George Athanasopoulos

Hi, and welcome!

A reproducible example, called a reprex is very helpful in attracting more helpful answers. Your code snippet comes close but violates the R zen of lazy evaluation. Here's what a reprex would look like.

library(tsibble)
library(tidyverse)
library(tsibbledata)
library(fable)
#> Loading required package: fabletools
library(forecast)
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> Registered S3 methods overwritten by 'forecast':
#>   method             from    
#>   fitted.fracdiff    fracdiff
#>   residuals.fracdiff fracdiff
#> 
#> Attaching package: 'forecast'
#> The following objects are masked from 'package:fabletools':
#> 
#>     GeomForecast, StatForecast
australia <- global_economy %>% filter(Country == "Australia")

australia_fit <- australia %>% 
  model(naive_seasonal = SNAIVE(GDP, lag("year")))
#> Warning: 1 error encountered for naive_seasonal
#> [1] Non-seasonal model specification provided, use RW() or provide a different lag specification.

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

The error is thrown by fable::model which takes a data structure such as a tstibble, model definitions, and an optional flag and produces a mable.

It's always a good idea to start with the example from the package function's help()

library(tsibble)
library(tidyverse)
library(tsibbledata)
library(fable)
#> Loading required package: fabletools
library(forecast)
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> Registered S3 methods overwritten by 'forecast':
#>   method             from    
#>   fitted.fracdiff    fracdiff
#>   residuals.fracdiff fracdiff
#> 
#> Attaching package: 'forecast'
#> The following objects are masked from 'package:fabletools':
#> 
#>     GeomForecast, StatForecast
# Training an ETS(M,Ad,A) model to Australian beer production
aus_production %>%
  model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A")))
#> # A mable: 1 x 1
#>   ets          
#>   <model>      
#> 1 <ETS(M,Ad,A)>

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

So isn't the obvious question is what is SNAIVE and what does it want?

Well, one possibility is something other than year.

Take a look at the formal arguments and see how your case differs from the example.

1 Like

The model you are trying to fit is seasonal, and you've specified that the seasonal period as "year".

If you look at the data you're trying to model, it is observed once every year (see the [1Y] in the output below).

The error is because a seasonal model should not be fitted to non-seasonal data. As there is only one data point every year, it does not make sense to fit a seasonal pattern to that one observation. You need at least two observations within a seasonal period to fit a seasonal pattern. The error is suggesting that you use a non-seasonal model such as a random walk (RW()).

library(fpp3)
australia <- global_economy %>% filter(Country == "Australia")
australia
#> # A tsibble: 58 x 9 [1Y]
#> # Key:       Country [1]
#>    Country   Code   Year          GDP Growth   CPI Imports Exports Population
#>    <fct>     <fct> <dbl>        <dbl>  <dbl> <dbl>   <dbl>   <dbl>      <dbl>
#>  1 Australia AUS    1960 18573188487.  NA     7.96    14.1    13.0   10276477
#>  2 Australia AUS    1961 19648336880.   2.49  8.14    15.0    12.4   10483000
#>  3 Australia AUS    1962 19888005376.   1.30  8.12    12.6    13.9   10742000
#>  4 Australia AUS    1963 21501847911.   6.21  8.17    13.8    13.0   10950000
#>  5 Australia AUS    1964 23758539590.   6.98  8.40    13.8    14.9   11167000
#>  6 Australia AUS    1965 25931235301.   5.98  8.69    15.3    13.2   11388000
#>  7 Australia AUS    1966 27261731437.   2.38  8.98    15.1    12.9   11651000
#>  8 Australia AUS    1967 30389741292.   6.30  9.29    13.9    12.9   11799000
#>  9 Australia AUS    1968 32657632434.   5.10  9.52    14.5    12.3   12009000
#> 10 Australia AUS    1969 36620002240.   7.04  9.83    13.3    12.0   12263000
#> # … with 48 more rows

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

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.