tidyverts/fasster

How to specify new_data

mitchelloharawild opened this issue · 4 comments

Copied comments of 63f53d7 for more visibility.

Hi Mitchell, I have been your R package 'fable' and 'FASSTER' fan for a while. After FASSTER changes the way it incorporates newdata to the forecast function, my original code couldn't work.

Can you provide an example of how to include new_data into the forecast function (like how you do it to forecast the Australian electricity demand)?

The newdata I need to provide is "daytype" before the switching term %S% ( exactly like what you did for the Australian electricity forecast). I read your code and relevant information online regarding this change but still couldn't figure out how to do it by myself.

Appreciated,
Yijun (@yijunwang0805)

Hi Mitchell,

I found my coding error (forget to capitalize daytype). I am so sorry to interupt your work.

I want to thank you for your work! Your work has made my life ( and many others') much easier. I am really fortunate and excited to find your package :)

Thanks a lot!

Hi Yijun, thanks for your kind appreciation of my work. (and now glad you've figured out your issue - was mostly through my reply which I'll provide for you and others anyway!)

These changes should not have any impact on existing code, and how new_data is passed to the forecast function. The change to the tests has been made as tsibble::as_tsibble.list() has been made defunct, but as long as a tsibble is provided to new_data, everything should work as it has in the past.

library(fasster)
#> Loading required package: fabletools
library(tsibble)
library(tidyverse)
#> Registered S3 method overwritten by 'rvest':
#>   method            from
#>   read_xml.response xml2
ped_sthx <- pedestrian %>% 
  filter(
    Sensor == "Southern Cross Station",
    yearmonth(Date_Time) < yearmonth("2015 Apr")
  )

ped_sthx %>% 
  autoplot(Count)

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:tsibble':
#> 
#>     interval, new_interval
#> The following object is masked from 'package:base':
#> 
#>     date
fit <- ped_sthx %>% 
  # Add day type indicator for weekend or weekday
  mutate(day_type = wday(Date_Time) %in% c(1,7)) %>% 
  # Estimate the model
  model(
    mdl = fasster(Count ~ poly(1) + day_type %S% (trig("week", 8) + trig("day", 10)))
  )
fit
#> # A mable: 1 x 2
#> # Key:     Sensor [1]
#>   Sensor                 mdl      
#>   <chr>                  <model>  
#> 1 Southern Cross Station <FASSTER>

components(fit) %>% 
  autoplot()

# Things look okay.

# Create the future data to forecast. As the day_type variable comes from the data, this will need to be provided.
# new_data() is a convenient way to create a tsibble of future time periods.
future_data <- new_data(ped_sthx, n = 24*7*3) %>% 
  # Add the day_type variable
  mutate(day_type = wday(Date_Time) %in% c(1,7))

fit %>% 
  # Forecast the future_data
  forecast(new_data = future_data) %>%  
  # Plot the result
  autoplot(ped_sthx)

Created on 2019-10-08 by the reprex package (v0.2.1)

Hi Mitchell,

Thank you so much for your detailed example!

I learn new things from your example: the way you specify trig term with "week" and "day", how you draw components are really helpful!

(Really looking forward to more functions in FASSTER: i.e. CV, report) :)

Best Regards,
Yijun

From your comment on the components, I think the above model should be tweaked.
Rather than switching on the trig("week") seasonality, it should exist outside of the day_type switching.