tidyverts / fasster

Forecasting with Additive Switching of Seasonality, Trend and Exogenous Regressors
https://fasster.tidyverts.org/
150 stars 15 forks source link

forecast not working as expected with %S% #55

Closed gacolitti closed 4 years ago

gacolitti commented 4 years ago

I can't seem to get forecast to work as expected when using the switching operator.

I expected this to work:

library(lubridate)
library(tsibble)
library(tsibbledata)
library(fasster)

data(vic_elec)

elec_fit <- vic_elec %>%
  dplyr::filter(year(Date) == 2012, month(Date) == 1) %>% 
  model(FASSTER(
    log(Demand) ~ 
      Holiday %S% (fourier(48, 16) + trend(1)) + 
      Temperature + I(Temperature^2)
  ))

future <- vic_elec %>%
  dplyr::filter(year(Date) == 2012, month(Date) == 2) 

elec_fit %>% 
  forecast(new_data = future)
#> Error: Problem with `mutate()` input `FASSTER(log(Demand) ~ Holiday %S% (fourier(48, 16) + trend(1)) + 
#>     Temperature + I(Temperature^2))`.
#> x subscript out of bounds
#> i Input `FASSTER(log(Demand) ~ Holiday %S% (fourier(48, 16) + trend(1)) + 
#>     Temperature + I(Temperature^2))` is `(function (object, ...) ...`.

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

mitchelloharawild commented 4 years ago

This is related to #48, the future data does not contain all levels of the switching operator and so the number of columns differs for forecasting.

mitchelloharawild commented 4 years ago

All fixed, thanks!

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
library(tsibble)
#> 
#> Attaching package: 'tsibble'
#> The following object is masked from 'package:lubridate':
#> 
#>     interval
library(tsibbledata)
library(fasster)
#> Loading required package: fabletools

data(vic_elec)

elec_fit <- vic_elec %>%
  dplyr::filter(year(Date) == 2012, month(Date) == 1) %>% 
  model(FASSTER(
    log(Demand) ~ 
      Holiday %S% (fourier(48, 16) + trend(1)) + 
      Temperature + I(Temperature^2)
  ))

future <- vic_elec %>%
  dplyr::filter(year(Date) == 2012, month(Date) == 2) 

elec_fit %>% 
  forecast(new_data = future)
#> # A fable: 1,392 x 7 [30m] <Australia/Melbourne>
#> # Key:     .model [1]
#>    .model Time                           Demand .mean Temperature Date      
#>    <chr>  <dttm>                         <dist> <dbl>       <dbl> <date>    
#>  1 "FASS… 2012-02-01 00:00:00 t(N(8.3, 0.0022)) 4213.        15.7 2012-02-01
#>  2 "FASS… 2012-02-01 00:30:00 t(N(8.3, 0.0031)) 4142.        15.5 2012-02-01
#>  3 "FASS… 2012-02-01 01:00:00 t(N(8.3, 0.0036)) 3877.        15.3 2012-02-01
#>  4 "FASS… 2012-02-01 01:30:00 t(N(8.2, 0.0041)) 3814.        15.2 2012-02-01
#>  5 "FASS… 2012-02-01 02:00:00 t(N(8.3, 0.0047)) 3948.        15.1 2012-02-01
#>  6 "FASS… 2012-02-01 02:30:00 t(N(8.3, 0.0053)) 3904.        15.0 2012-02-01
#>  7 "FASS… 2012-02-01 03:00:00 t(N(8.2, 0.0059)) 3707.        14.8 2012-02-01
#>  8 "FASS… 2012-02-01 03:30:00 t(N(8.2, 0.0068)) 3609.        14.4 2012-02-01
#>  9 "FASS… 2012-02-01 04:00:00 t(N(8.2, 0.0078)) 3582.        13.9 2012-02-01
#> 10 "FASS… 2012-02-01 04:30:00 t(N(8.2, 0.0088)) 3562.        13.6 2012-02-01
#> # … with 1,382 more rows, and 1 more variable: Holiday <lgl>

Created on 2020-08-18 by the reprex package (v0.3.0)

gacolitti commented 4 years ago

Awesome. Thank you!