Chapter 13 Some practical forecasting issues

I have two questions to ask. The first one is that when I add a prompt model to the composite model and run the second section of code below, R reports an error: Error in UseMethod ("generate"): "generate" does not have a method applicable to the target object of "fbl_prophet". Can't this model be used in combination models to generate prediction intervals? Is there an alternative method?
The second question is: When I use a combination model to predict my monthly case data, the prediction interval generated by the ETS model contains negative numbers, which is illogical because the number of cases cannot be negative. How should this be resolved?
Very confused and looking forward to your answer!

auscafe <- aus_retail |>
  filter(stringr::str_detect(Industry, "Takeaway")) |>
  summarise(Turnover = sum(Turnover))
train <- auscafe |>
  filter(year(Month) <= 2013)
STLF <- decomposition_model(
  STL(log(Turnover) ~ season(window = Inf)),
  ETS(season_adjust ~ season("N"))
)
cafe_models <- train |>
  model(
    ets = ETS(Turnover),
    stlf = STLF,
    arima = ARIMA(log(Turnover)),
   prophet = prophet(log(Turnover) ~ season(period = 4, order = 2,type = "multiplicative")),
  ) |>
  mutate(combination = (ets + stlf + arima+prophet ) / 4)
cafe_fc <- cafe_models |>
  forecast(h = "5 years")


cafe_futures <- cafe_models |>
  # Generate 1000 future sample paths
  generate(h = "5 years", times = 1000) |>
  # Compute forecast distributions from future sample paths
  as_tibble() |>
  group_by(Month, .model) |>
  summarise(
    dist = distributional::dist_sample(list(.sim))
  ) |>
  ungroup() |>
  # Create fable object
  as_fable(index = Month, key = .model,
           distribution = dist, response = "Turnover")


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

No-one has written a generate() function for prophet. The fable.prophet package is simply a wrapper to the prophet package to enable these models to be used in the fable framework. But the prophet package does not include methods for simulation. It could, however, be used in ensemble models by sampling from the forecast distributions.

Most models assume a sample space of the whole real line. If you want to restrict the forecast distributions to be on the positive real line, you could model the log of the variable of interest. Alternatively, you could truncate the distributions at zero and re-scale the remaining part of the distribution.

1 Like

Your answer is very comprehensive and has provided me with reference opinions. Thank you. Indeed, the Prophet model can be used in a set of models, and the model generates prediction intervals by sampling from the predicted distribution.

So this model cannot use generate function to generate prediction intervals. right?

I can solve the problem of negative numbers in the prediction interval of the ETS model in the combination model through ETS (log (variable)), and The mean predicted by the model is still the mean that has not been converted by the log function,right?

You proposed :Alternatively, you could truncate the distributions at zero and re-scale the remaining part of the distribution. Could you write a code example? I really don't know how to operate it specifically.

Thank you again for your selfless dedication

Here's an example:

library(fpp3)
#> ── Attaching packages ─────────────────────────────────────── fpp3 0.5.0.9000 ──
#> βœ” tibble      3.2.1          βœ” tsibble     1.1.4     
#> βœ” dplyr       1.1.4          βœ” tsibbledata 0.4.1.9000
#> βœ” tidyr       1.3.1          βœ” feasts      0.3.2.9000
#> βœ” lubridate   1.9.3          βœ” fable       0.3.4.9000
#> βœ” ggplot2     3.5.1          βœ” fabletools  0.4.2
#> ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
#> βœ– lubridate::date()    masks base::date()
#> βœ– dplyr::filter()      masks stats::filter()
#> βœ– tsibble::intersect() masks base::intersect()
#> βœ– tsibble::interval()  masks lubridate::interval()
#> βœ– dplyr::lag()         masks stats::lag()
#> βœ– tsibble::setdiff()   masks base::setdiff()
#> βœ– tsibble::union()     masks base::union()
prices |> 
  filter(!is.na(eggs)) |> 
  model(ets = ETS(eggs)) |> 
  forecast(h = 40) |> 
  mutate(eggs = distributional::dist_truncated(eggs, lower = 0)) |> 
  autoplot(prices |> filter(!is.na(eggs)))

Created on 2024-06-02 with reprex v2.1.0

Note that truncation causes the mean to change, and in this case it changes from a downward trend to an upward trend.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.