mitchelloharawild / fable.prophet

fable extension for the prophet forecasting procedure
https://pkg.mitchelloharawild.com/fable.prophet
55 stars 8 forks source link

Bare prophet and fable.prophet giving very different results #12

Closed adrfantini closed 4 years ago

adrfantini commented 4 years ago

Hi and thank you for the package! As per SO question, I have been unable to use the fable interface for prophet, probably due to my own lack of understanding. In any case here is an example:


library(readr)
library(fable)
library(dplyr)
library(prophet)
library(fable.prophet)
library(feasts)
library(tidyr)

### READ DATA (about 4 months, hourly data)

download.file("https://srv-file7.gofile.io/download/BESqZA/gh_example.csv", method = "wget", destfile = "gh_example.csv")
d = read_csv("gh_example.csv") %>%
    mutate(ds = d$ds[1] + 0:(nrow(.)-1)*3600) %>% #regolarise timeseries, as some times are very slighly wrong
    as_tsibble(index = ds)

### FORECAST WITH PROPHET

# Define model

m = prophet::prophet(
    growth = "linear",
    yearly.seasonality = FALSE,
    weekly.seasonality = FALSE,
    daily.seasonality = FALSE,
    seasonality.mode = "multiplicative"
) %>%
    add_seasonality("daily" , period = 1, fourier.order = 10, prior.scale = 10) %>%
    add_seasonality("weekly", period = 7, fourier.order = 10, prior.scale = 10)

# Fit model

m = m %>% fit.prophet(d %>% as.data.frame)
future_df = m %>% make_future_dataframe(periods = 24*7, freq = 3600) # 7 day forecast with 1h freq

# Forecast model

fc = m %>% predict(future_df)

# Visualise forecast

m %>% plot(fc, uncertainty = FALSE) # plot time series and forecast, static

prophet

With Fable:

fit <- d %>%
  model(
    prophet = prophet(y ~
        growth("linear") +
        season(name = "daily" , period = 1, order = 10, prior_scale = 10, type = "multiplicative") +
        season(name = "weekly", period = 7, order = 10, prior_scale = 10, type = "multiplicative")
        )
    )

# Forecast model

fc_fable <- fit %>%
  forecast(h = "1 week")

# Visualise forecast

fc_fable %>% autoplot(d) # Completely different from bare Prophet!

fable

Clearly something here is very different in the settings, but I can't figure it out.

mitchelloharawild commented 4 years ago

I'm having trouble downloading the files from your reprex.

You are not authorized to download this file.

Despite this, I think I know what the issue is. There's a difference in how seasonal periods are specified with fable and prophet. In fable, a numerical input is relative to the data's frequency (in your case 15 minutes), but in prophet a numerical input is relative to 1 day (the assumed data frequency from Facebook's application).

With fable, seasonal periods can be specified with natural language, which minimises the chance of user error. Try setting period = "day" (and week) and see if this works for you.

fit <- d %>%
  model(
    prophet = prophet(y ~
        growth("linear") +
        season(name = "daily" , period = "day", order = 10, prior_scale = 10, type = "multiplicative") +
        season(name = "weekly", period = "week", order = 10, prior_scale = 10, type = "multiplicative")
        )
    )

I think the docs should be updated to make this difference clearer.

adrfantini commented 4 years ago

This data is hourly (unlike my SO data):

# A tsibble: 2,772 x 2 [1h] <UTC>

So what you say still applies: fable

Bingo! Thanks!

adrfantini commented 4 years ago

(feel free to reopen if you want use this issue to update the docs)

mitchelloharawild commented 4 years ago

Updated docs, thanks.