Hierarchical time series forecasting using Fable in R - r

I am doing hierarchical time series forecasting using fable. I am using optimal reconciliation method to reconcile the forecast. Here is the example code.
agg_sw <- df %>%
aggregate_key(productcategory/brand/sku, sales = sum(sales))
#Fit the model
ets_fit <- agg_sw %>%
model(ets = ETS(sales)) %>%
reconcile(ols = min_trace(ets, method = "ols"))
# Forecast
fc <- forecast(ets_fit,h= "1 year")
Is it possible to use different forecasting method at each level(eg:sku/brand/product) and reconcile? If so, kindly let me know how to do it.

Related

How to get multiple-steps ahead forecast with STL model in fable-r?

My purpose is forecast multiple-step without re-estimation. And I will update new observation to next forecast.
I did not using fit and apply forecast(h=7) because this function using fitted value to forecast next observation.
I used following codes to get 1-step ahead forecast with stretch_tsibble to do it.
library(fable)
library(dplyr)
library(tsibble)
library(feasts)
us_accidental_deaths <- as_tsibble(USAccDeaths)
stretch_dt <- us_accidental_deaths %>%
stretch_tsibble(.init = 60, .step = 1)
fit_train <- stretch_dt %>%
# keep same estimate period with each .id
filter_index(. ~ '1977 Dec') %>%
model(stl_ets_mod = decomposition_model(
STL(value, ~ season(window = 12)),
ETS(season_adjust ~ season("N")),
SNAIVE(season_year)
),
arima_mod = ARIMA(value))
It's ok when I refit ARIMA model
fit_train %>%
select(arima_mod) %>%
refit(stretch_dt) %>%
forecast(h = 1)
But I met error when I refit STL model.
fit_train %>%
select(stl_ets_mod) %>%
refit(stretch_dt) %>%
forecast(h = 1)
Many thanks !!!
The error you are getting is
! no applicable method for 'refit' applied to an object of class "c('decomposition_model', 'model_combination')"
refit() is not available for all models.
It is not clear how a refit should work for an STL decomposition. The STL components are specific to the data set used for training. If the model is applied to a different data set, potentially of a different length, what should the components be?

When ets() is used, why R is not responding and crashes?

I am trying to find the best model to forecast the average monthly rainfall of a particular region.
So far I have used a a seasonal naive method and SARIMA. But when trying to run ets(), R crashes without producing an output.
I tend to use fable and fabletools. The followup of forecast. Using package fpp3 loads all the needed packages for working with tsibbles, dplyr and date objects.
I don't have any issues running any forecasts methods on your data. I tried both fable and forecast and get the same outcomes. See code below.
# load your data
df1 <- readxl::read_excel("datasets/Copy.xlsx")
colnames(df1) <- c("date", "rainfall")
library(fpp3)
fit <- df1 %>%
mutate(date = yearmonth(date)) %>%
as_tsibble() %>%
model(ets = ETS(rainfall))
report(fit)
Series: rainfall
Model: ETS(M,N,A)
Smoothing parameters:
alpha = 0.002516949
gamma = 0.0001065384
Initial states:
l[0] s[0] s[-1] s[-2] s[-3] s[-4] s[-5] s[-6] s[-7] s[-8] s[-9] s[-10]
86.7627 -77.53686 -57.90353 -18.72201 86.57944 150.0896 166.8125 60.45602 -39.25331 -55.94238 -68.85851 -70.52719
s[-11]
-75.19377
sigma^2: 0.1109
AIC AICc BIC
2797.766 2799.800 2850.708
Using forecast:
library(forecast)
fit <- forecast::ets(ts(df1[, 2], frequency = 12))
fit
ETS(M,N,A)
Call:
forecast::ets(y = ts(df1[, 2], frequency = 12))
Smoothing parameters:
alpha = 0.0025
gamma = 1e-04
Initial states:
l = 86.7627
s = -77.5369 -57.9035 -18.722 86.5794 150.0896 166.8125
60.456 -39.2533 -55.9424 -68.8585 -70.5272 -75.1938
sigma: 0.333
AIC AICc BIC
2797.766 2799.800 2850.708

Forecasting future observations based off of lowest RMSE models

I asked this question over at RStudio community and received no answer so I figured I'd give it a go here. My question pertains to what budugulo asked here Select models with lowest RMSE but I'm wondering how I can go further and use the models with the best predictive capability against the test data and apply it across the entire original hierarchical dataset to get future observations.
I understand how to forecast into the future with one individual time series, but I'm trying to forecast a hierarchical dataset that would require too much time to forecast the best models onto all of the original time series individually to forecast future observations. Is there a way to fit the best models (using lowest RMSE) onto the original time series in a hierarchical dataset to forecast future observations 3 years into the future (2020)? I tried using refit() but to no avail.
Hopefully the code below will help towards answering my question.
library(tidyverse)
library(tsibble)
library(fable)
library(fpp3)
fit <- tourism %>%
filter(Quarter <= yearquarter("2015 Q1")) %>%
model(
ets = ETS(Trips),
arima = ARIMA(Trips)
)
fc <- fit %>%
forecast(new_data = filter(tourism, Quarter > yearquarter("2015 Q1")))
bestrmse <- accuracy(fc, tourism) %>%
group_by(Region, State, Purpose) %>%
filter(RMSE == min(RMSE)) %>%
select(.model:Region)
bestfits <- fit %>%
pivot_longer(cols=ets:arima, names_to = ".model", values_to = "fit") %>%
right_join(bestrmse) %>%
mutate(.model = "best") %>%
pivot_wider(Region:Purpose, names_from = ".model", values_from = "fit") %>%
as_mable(key = c(Region, State, Purpose), model = best)
#Apply 'best' models from bestfits onto original non-trained/non-tested time series and
#forecast future observations into 2020.

Issues with forecasting model fit

I want to forecast rainfall in Albury. I have a data set holding rainfall in mm from 2009 to 2016. Besides rainfall the data set holds other variables: date, windgustspeed, windspeed, humidity, maximum temperature, and pressure. All of these have in earlier tests shown to have influence on rainfall.
I have tried forecasting rainfall the next year using arima, vector autogressive, tslm, snaive, naive, mean, rw models. All of them seem only to forecast a mean and do not catch all.
Code used for model fitting
albury_fit <- albury %>%
model(
naive = NAIVE(Rainfall),
drift = RW(Rainfall ~ drift()),
mean = MEAN(Rainfall),
seasonal_naive = SNAIVE(Rainfall),
tslm = TSLM(Rainfall ~trend()),
arima = ARIMA(Rainfall)
)
albury_fc <- albury_fit %>%
forecast(h = '1 year')
plot of model fit
Code for checking model performance for arima, all models have the same output as arima
albury%>%
model(ARIMA(Rainfall)) %>%
gg_tsresiduals()
output from the code above
So the bottom line is, that the models do not perform well, but I don't know how to fix it.
I hope that someone can help me :)

Adding theta model with fable forecasting estimates

I want to use theta model implemented in Forecast package inside my fable forecasting model. This what I am trying to do.
library(forecast)
library(tidyverse)
library(fable)
library(tsibble)
library(fabletools)
tourism_aus <- tourism %>%
summarise(Trips = sum(Trips))
tourism_aus
fit <- tourism_aus %>%
model(
ets = ETS(Trips),
arima = ARIMA(Trips),
theta = forecast::thetaf(Trips)
) %>%
mutate(
average = (ets + arima + theta) / 3
)
fit
fit %>%
forecast(h = "2 years") %>%
autoplot(tourism_aus, level = 95, alpha = 0.5)
I am having this error message,
Failure in thetaf(Trips) : Objekt 'Trips' not found
Is there any way I can use theta method inside fable?
Models from the forecast package use a different interface, and so are not compatible with the model() function used by fable. The theta model will be added to fable in the next release.
You can create a fable yourself, by using the forecast output of forecast::thetaf() to identify an appropriate distribution. This can be useful for plotting, accuracy evaluation and reconciliation, however ensembling requires models to use the fable interface.
Update:
The THETA() model has now been added to fable.

Resources