Title: | Temporal Hierarchical Forecasting |
---|---|
Description: | Methods and tools for generating forecasts at different temporal frequencies using a hierarchical time series approach. |
Authors: | Rob Hyndman [aut, cre, cph], Nikolaos Kourentzes [aut, cph] |
Maintainer: | Rob Hyndman <[email protected]> |
License: | GPL-3 |
Version: | 0.3 |
Built: | 2024-11-09 03:06:53 UTC |
Source: | https://github.com/robjhyndman/thief |
Weekly demand of Accident & Emergency departments in the UK, from 7 November 2010 to 7 June 2015.
AEdemand
AEdemand
An object of class ts
.
library(ggplot2) autoplot(AEdemand, xlab="Year", ylab="Demand ('000)") + ggtitle("Accident & Emergency Demand in the UK") ## Not run: # Demonstration of the adjustment of all temporal aggregates # using Total Emergency Admissions total <- AEdemand[,12] totalagg <- tsaggregates(total) plot(totalagg, main="Total Emergency Admissions") # Base forecasts base <- list() for(i in 1:5) base[[i]] <- forecast(auto.arima(totalagg[[i]])) base[[6]] <- forecast(auto.arima(totalagg[[6]]), h=2) # Reconciled forecasts reconciled <- reconcilethief(base) main <- paste(names(totalagg)," (k=", 52/unlist(lapply(reconciled,frequency)),")",sep="") par(mfrow=c(2,3)) for(i in 6:1) { ylim <- range(base[[i]]$mean, base[[i]]$x, reconciled[[i]]$mean) plot(base[[i]], main=main[i], fcol='white', plot.conf=FALSE, ylim=ylim, xlim=c(2010.5,2017.5)) polygon(c(2015.45, 2020, 2020, 2015.45), c(0, 0, 1e5, 1e5), col='grey', border=FALSE) lines(base[[i]]$mean, col='red', lty=2) lines(reconciled[[i]]$mean, col='blue') } ## End(Not run)
library(ggplot2) autoplot(AEdemand, xlab="Year", ylab="Demand ('000)") + ggtitle("Accident & Emergency Demand in the UK") ## Not run: # Demonstration of the adjustment of all temporal aggregates # using Total Emergency Admissions total <- AEdemand[,12] totalagg <- tsaggregates(total) plot(totalagg, main="Total Emergency Admissions") # Base forecasts base <- list() for(i in 1:5) base[[i]] <- forecast(auto.arima(totalagg[[i]])) base[[6]] <- forecast(auto.arima(totalagg[[6]]), h=2) # Reconciled forecasts reconciled <- reconcilethief(base) main <- paste(names(totalagg)," (k=", 52/unlist(lapply(reconciled,frequency)),")",sep="") par(mfrow=c(2,3)) for(i in 6:1) { ylim <- range(base[[i]]$mean, base[[i]]$x, reconciled[[i]]$mean) plot(base[[i]], main=main[i], fcol='white', plot.conf=FALSE, ylim=ylim, xlim=c(2010.5,2017.5)) polygon(c(2015.45, 2020, 2020, 2015.45), c(0, 0, 1e5, 1e5), col='grey', border=FALSE) lines(base[[i]]$mean, col='red', lty=2) lines(reconciled[[i]]$mean, col='blue') } ## End(Not run)
Plots all temporal aggregations of a time series
## S3 method for class 'tsaggregates' plot(x, series = "all", ...) ## S3 method for class 'tsaggregates' autoplot(object, series = "all", ...)
## S3 method for class 'tsaggregates' plot(x, series = "all", ...) ## S3 method for class 'tsaggregates' autoplot(object, series = "all", ...)
x |
|
series |
The indexes of the series to plot. By default, all series are plotted. |
... |
Other arguments passed to |
object |
|
Rob J Hyndman
deathagg <- tsaggregates(USAccDeaths) plot(deathagg, series=c(1,2,4,6)) library(ggplot2) autoplot(deathagg)
deathagg <- tsaggregates(USAccDeaths) plot(deathagg, series=c(1,2,4,6)) library(ggplot2) autoplot(deathagg)
Takes forecasts of time series at all levels of temporal aggregation and combines them using the temporal hierarchical approach of Athanasopoulos et al (2016).
reconcilethief(forecasts, comb = c("struc", "mse", "ols", "bu", "shr", "sam"), mse = NULL, residuals = NULL, returnall = TRUE, aggregatelist = NULL)
reconcilethief(forecasts, comb = c("struc", "mse", "ols", "bu", "shr", "sam"), mse = NULL, residuals = NULL, returnall = TRUE, aggregatelist = NULL)
forecasts |
List of forecasts. Each element must be a time series of forecasts, or a forecast object. The number of forecasts should be equal to k times the seasonal period for each series, where k is the same across all series. |
comb |
Combination method of temporal hierarchies, taking one of the following values:
|
mse |
A vector of one-step MSE values corresponding to each of the forecast series. |
residuals |
List of residuals corresponding to each of the forecast models.
Each element must be a time series of residuals. If |
returnall |
If |
aggregatelist |
(optional) User-selected list of forecast aggregates to consider |
List of reconciled forecasts in the same format as forecast
.
If returnall==FALSE
, only the most disaggregated series is returned.
Rob J Hyndman
# Construct aggregates aggts <- tsaggregates(USAccDeaths) # Compute forecasts fc <- list() for(i in seq_along(aggts)) fc[[i]] <- forecast(auto.arima(aggts[[i]]), h=2*frequency(aggts[[i]])) # Reconcile forecasts reconciled <- reconcilethief(fc) # Plot forecasts before and after reconcilation par(mfrow=c(2,3)) for(i in seq_along(fc)) { plot(reconciled[[i]], main=names(aggts)[i]) lines(fc[[i]]$mean, col='red') }
# Construct aggregates aggts <- tsaggregates(USAccDeaths) # Compute forecasts fc <- list() for(i in seq_along(aggts)) fc[[i]] <- forecast(auto.arima(aggts[[i]]), h=2*frequency(aggts[[i]])) # Reconcile forecasts reconciled <- reconcilethief(fc) # Plot forecasts before and after reconcilation par(mfrow=c(2,3)) for(i in seq_along(fc)) { plot(reconciled[[i]], main=names(aggts)[i]) lines(fc[[i]]$mean, col='red') }
Takes a time series as input and produces forecasts using the temporal hierarchical approach of Athanasopoulos et al (2016).
thief(y, m = frequency(y), h = m * 2, comb = c("struc", "mse", "ols", "bu", "shr", "sam"), usemodel = c("ets", "arima", "theta", "naive", "snaive"), forecastfunction = NULL, aggregatelist = NULL, ...)
thief(y, m = frequency(y), h = m * 2, comb = c("struc", "mse", "ols", "bu", "shr", "sam"), usemodel = c("ets", "arima", "theta", "naive", "snaive"), forecastfunction = NULL, aggregatelist = NULL, ...)
y |
Time series input |
m |
Seasonal period |
h |
Forecast horizon |
comb |
Combination method of temporal hierarchies, taking one of the following values:
|
usemodel |
Model used for forecasting each aggregation level:
|
forecastfunction |
User-defined function to be used instead of |
aggregatelist |
User-selected list of forecast aggregates to consider |
... |
Arguments to be passed to the time series modelling function
(such as |
This function computes the temporal aggregates of y
using
tsaggregates
, then calculates all forecasts using the model function
specified by usemodel
or forecastfunction
, and finally reconciles the
forecasts using reconcilethief
. The reconciled forecasts of y
are returned.
forecast object.
Rob J Hyndman and Nikolaos Kourentzes
## Not run: # Select ARIMA models for all series using auto.arima() z <- thief(AEdemand[,12], usemodel='arima') plot(z) # Use your own function ftbats <- function(y,h,...){forecast(tbats(y),h,...)} z <- thief(AEdemand[,12], forecastfunction=ftbats) plot(z) ## End(Not run)
## Not run: # Select ARIMA models for all series using auto.arima() z <- thief(AEdemand[,12], usemodel='arima') plot(z) # Use your own function ftbats <- function(y,h,...){forecast(tbats(y),h,...)} z <- thief(AEdemand[,12], forecastfunction=ftbats) plot(z) ## End(Not run)
Produces all temporal aggregations for frequencies greater than 1
tsaggregates(y, m = frequency(y), align = c("end", "start"), aggregatelist = NULL)
tsaggregates(y, m = frequency(y), align = c("end", "start"), aggregatelist = NULL)
y |
Univariate time series of class |
m |
Integer seasonal period |
align |
Indicates how the aggregates are to be aligned:
either with the |
aggregatelist |
User-selected list of aggregates to consider. |
A list of time series. The first element is the series 'y', followed by series with increasing levels of aggregation. The last element is the "annual" series (i.e., the series aggregated over all seasons).
Rob J Hyndman
tsaggregates(USAccDeaths)
tsaggregates(USAccDeaths)