Combination of forecasts

Hello all,

I've done several forecasts using R. I've used ETS, ARIMA and VAR methods. Now, I'd like to combine the results using an average of means and confidence interval's limits.

This is a simple example:

library(forecast)

#Data
gini=c(21.8,23.3,23.4,25.5,27.1,31.2,34.9,35.2,39.7,28.4,32.2,42.1,46.2);

#As TS
Xt=ts(gini,start=2003,end=2015,frequency=1);

#ETS
ets.gini=forecast::ets(Xt);
forecast.ets.gini=forecast::forecast.ets(ets.gini,h=6,level=95,PI=TRUE);
plot(forecast.ets.gini,lwd=3,main="",col="black",xlab="Year",ylab="Gini",
     xlim=c(2000,2025),ylim=c(0,100),panel.first=grid(),showgap=FALSE,
     shaded=TRUE,shadebars=FALSE,shadecols="grey75",fcol="grey50",flwd=3)

#ARIMA
arima.gini=forecast::auto.arima(Xt);
forecast.arima.gini=forecast::forecast(arima.gini,h=6,level=95);
plot(forecast.arima.gini,lwd=3,main="",col="black",xlab="Year",ylab="Gini",
     xlim=c(2000,2025),ylim=c(0,100),panel.first=grid(),showgap=FALSE,
     shaded=TRUE,shadebars=FALSE,shadecols="grey75",fcol="grey50",flwd=3)

#Combination
mean.gini=(forecast.ets.gini$mean+forecast.arima.gini$mean)/2;
lower.gini=(forecast.ets.gini$lower+forecast.arima.gini$lower)/2;
upper.gini=(forecast.ets.gini$upper+forecast.arima.gini$upper)/2;

How could I make a plot with this last combined forecast equal to the ones done for ETS and ARIMA?

Thank you in advance for any help!

I believe you'll find forecastHybrid package very useful for this purpose. See below:

library(forecast)

#Data
gini <- c(21.8, 23.3, 23.4, 25.5, 27.1, 31.2, 34.9, 35.2, 39.7, 28.4, 32.2, 42.1, 46.2)

#As TS
Xt <- ts(data = gini,
         start = 2003,
         end = 2015,
         frequency = 1)

#ETS
ets.gini <- ets(y = Xt)
forecast.ets.gini <- forecast.ets(object = ets.gini,
                                  h = 6,
                                  level = 95,
                                  PI = TRUE)
plot(x = forecast.ets.gini,
     lwd = 3,
     main = "",
     col = "black",
     xlab = "Year",
     ylab = "Gini",
     xlim = c(2000,2025),
     ylim = c(0, 100),
     panel.first = grid(),
     showgap = FALSE,
     shaded = TRUE,
     shadebars = FALSE,
     shadecols = "grey75",
     fcol = "grey50",
     flwd = 3)


#ARIMA
arima.gini <- auto.arima(Xt)
forecast.arima.gini <- forecast(object = arima.gini,
                                h = 6,
                                level = 95)
plot(x = forecast.arima.gini,
     lwd = 3,
     main = "",
     col = "black",
     xlab = "Year",
     ylab = "Gini",
     xlim = c(2000, 2025),
     ylim = c(0, 100),
     panel.first = grid(),
     showgap = FALSE,
     shaded = TRUE,
     shadebars = FALSE,
     shadecols = "grey75",
     fcol = "grey50",
     flwd = 3)


#Combination
library(forecastHybrid)
#> Loading required package: thief

hybrid_model <- hybridModel(y = Xt,
                            models = c("ae"),
                            weights = "equal")
#> Fitting the auto.arima model
#> Fitting the ets model
hybrid_model_forecast <- forecast(object = hybrid_model,
                                  h = 6,
                                  level = 95,
                                  PI.combination = "mean")

plot(x = hybrid_model_forecast,
     lwd = 3,
     main = "",
     col = "black",
     xlab = "Year",
     ylab = "Gini",
     xlim = c(2000, 2025),
     ylim = c(0, 100),
     panel.first = grid(),
     showgap = FALSE,
     shaded = TRUE,
     shadebars = FALSE,
     shadecols = "grey75",
     fcol = "grey50",
     flwd = 3)

Created on 2019-04-09 by the reprex package (v0.2.1)

Note that these forecasts indeed match what you've calculated manually:

> mean.gini <- (forecast.ets.gini$mean + forecast.arima.gini$mean) / 2
> lower.gini <- (forecast.ets.gini$lower + forecast.arima.gini$lower) / 2
> upper.gini <- (forecast.ets.gini$upper + forecast.arima.gini$upper) / 2
> 
> cbind(mean.gini,
+       lower.gini,
+       upper.gini)
Time Series:
Start = 2016 
End = 2021 
Frequency = 1 
     mean.gini lower.gini upper.gini
2016  46.19979   34.02411   58.37547
2017  46.19979   28.91875   63.48084
2018  46.19979   24.95775   67.44184
2019  46.19979   21.58144   70.81814
2020  46.19979   18.57375   73.82583
2021  46.19979   15.82419   76.57540
> 
> hybrid_model_forecast
     Point Forecast    Lo 95    Hi 95
2016       46.19979 34.02411 58.37547
2017       46.19979 28.91875 63.48084
2018       46.19979 24.95775 67.44184
2019       46.19979 21.58144 70.81814
2020       46.19979 18.57375 73.82583
2021       46.19979 15.82419 76.57540

Hope this helps.

Note: I see you're using ; at the end of the lines. These are not necessary in R.

3 Likes

Thank you for your answer @Yarnabrina. The package forecastHybrid is interesting. However, I've been checking the options and I think it doesn't allow the inclusion of VAR forecasts.

Is there any option to build the plot manually?

Thanks again!

Okay, I didn't actually notice that. Then, I can't provide you a good solution.

A manual plotting can be somewhat like the following:

library(forecast)

#Data
gini <- c(21.8, 23.3, 23.4, 25.5, 27.1, 31.2, 34.9, 35.2, 39.7, 28.4, 32.2, 42.1, 46.2)

#As TS
Xt <- ts(data = gini,
         start = 2003,
         end = 2015,
         frequency = 1)

#ETS
ets.gini <- ets(y = Xt)
forecast.ets.gini <- forecast.ets(object = ets.gini,
                                  h = 6,
                                  level = 95,
                                  PI = TRUE)

#ARIMA
arima.gini <- auto.arima(Xt)
forecast.arima.gini <- forecast(object = arima.gini,
                                h = 6,
                                level = 95)

#combination
mean.gini <- (forecast.ets.gini$mean + forecast.arima.gini$mean) / 2
lower.gini <- (forecast.ets.gini$lower + forecast.arima.gini$lower) / 2
upper.gini <- (forecast.ets.gini$upper + forecast.arima.gini$upper) / 2

#plot
plot(x = Xt,
     xlim = c(2000, 2025),
     ylim = c(0, 100),
     lwd = 3,
     main = "",
     col = "black",
     xlab = "Year",
     ylab = "Gini")
polygon(x = c(2016:2021, 2021:2016),
        y = c(lower.gini, rev(x = upper.gini)),
        col = "lightgrey",
        border = "lightgrey")
lines(x = 2016:2021,
      y = mean.gini,
      lwd = 2,
      col = "darkgrey")

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

Does this help? :thinking:

1 Like

Thank you very much for your answer! This solves my problem!

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.