fable: ensemble, extract sigma

Hi,

A question based on
quantile_ensemble_talk/ensembles_NYCR_2020.Rmd at master · robjhyndman/quantile_ensemble_talk · GitHub

library(fpp3)
library(distributional)

cafe <- readRDS("cafe.rds")

auscafe <- cafe %>%
summarise(turnover = sum(turnover))

e <- auscafe %>%
filter(year(date) <= 2018) %>%
model(ETS = ETS(turnover),
ARIMA = ARIMA(turnover ~
pdq(d=1) + PDQ(D=1))
) %>%
forecast(h = "1 year") %>%
summarise(
turnover = dist_mixture(
turnover[1], turnover[2],
weights=c(0.5,0.5)),
.mean = mean(turnover)
) %>%
mutate(.model = "ENSEMBLE")

e

I can extract the mean forecast, i.e. including .mean, but how do I extract sigma (sd) of the forecast from the Ensemble? If I include

.sd = sd(turnover)

I get the following error

Error in `summarise()`:
! Problem while computing `.sd = sd(turnover)`.
ℹ The error occurred in group 1: date = 2019 Jan.
Caused by error in `as.double()`:
! Can't convert `x` <distribution> to <double>.
Run `e]8;;rstudio:run:rlang::last_error()rlang::last_error()e]8;;` to see where the error occurred.

The reason being I want to calculate the log-predictive density score.

many thanks,
Amarjit

Try h = 12 because strings won’t work.

The problem is that sd() is not a method in the distributional package. Instead you can use variance() and take the square root, like this.

auscafe %>%
  filter(year(date) <= 2018) %>%
  model(ETS = ETS(turnover),
        ARIMA = ARIMA(turnover ~
                        pdq(d=1) + PDQ(D=1))
  ) %>%
  forecast(h = "1 year") %>%
  summarise(
    turnover = dist_mixture(
      turnover[1], turnover[2],
      weights=c(0.5,0.5)),
    .mean = mean(turnover),
    .sd = sqrt(variance(turnover))
  ) %>%
  mutate(.model = "ENSEMBLE")
1 Like

The forecast() function from fabletools does allow character strings for the forecast horizon.

1 Like

Hi Rob,

Many Thanks!

Amarjit

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.