Title: | Hierarchical and Grouped Time Series |
---|---|
Description: | Provides methods for analysing and forecasting hierarchical and grouped time series. The available forecast methods include bottom-up, top-down, optimal combination reconciliation (Hyndman et al. 2011) <doi:10.1016/j.csda.2011.03.006>, and trace minimization reconciliation (Wickramasuriya et al. 2018) <doi:10.1080/01621459.2018.1448825>. |
Authors: | Rob Hyndman [aut] (Package creator), Alan Lee [aut] (Fast computation using recursive methods), Earo Wang [aut, cre], Shanika Wickramasuriya [aut] (Reconciliation via trace minimization) |
Maintainer: | Earo Wang <[email protected]> |
License: | GPL (>= 2) |
Version: | 6.0.3 |
Built: | 2024-10-28 05:21:31 UTC |
Source: | https://github.com/earowang/hts |
This package presents functions to create, plot and forecast hierarchical and grouped time series. In forecasting hierarchical and grouped time series, the base methods implemented include ETS, ARIMA and the naive (random walk) models. Forecasts for grouped time series are calibrated using bottom-up and optimal combination methods. Forecasts for hierarchical time series are distributed in the hierarchy using bottom-up, top-down, middle-out and optimal combination methods. Three top-down methods are available: the two Gross-Sohl methods and the forecast-proportion approach of Hyndman, Ahmed, and Athanasopoulos (2011).
Rob J Hyndman, Alan Lee, Earo Wang and Shanika L Wickramasuriya with contributions from Roman A Ahmed and Han Lin Shang to earlier versions of the package
G. Athanasopoulos, R. A. Ahmed and R. J. Hyndman (2009) Hierarchical forecasts for Australian domestic tourism, International Journal of Forecasting, 25, 146-166.
R. J. Hyndman, R. A. Ahmed, G. Athanasopoulos and H.L. Shang (2011) Optimal combination forecasts for hierarchical time series. Computational Statistics and Data Analysis, 55(9), 2579–2589. https://robjhyndman.com/publications/hierarchical/
Hyndman, R. J., Lee, A., & Wang, E. (2016). Fast computation of reconciled forecasts for hierarchical and grouped time series. Computational Statistics and Data Analysis, 97, 16-23. https://robjhyndman.com/papers/hgts7.pdf
Wickramasuriya, S. L., Athanasopoulos, G., & Hyndman, R. J. (2018). Forecasting hierarchical and grouped time series through trace minimization. Journal of the American Statistical Association, to appear https://robjhyndman.com/papers/mint.pdf
Returns a range of summary measures of the forecast accuracy. The function
measures out-of-sample forecast accuracy based on (holdout data - forecasts)
and in-sample accuracy at the bottom level when setting keep.fitted =
TRUE
in the forecast.gts
. All measures are defined and
discussed in Hyndman and Koehler (2006).
## S3 method for class 'gts' accuracy(object, test, levels, ..., f = NULL)
## S3 method for class 'gts' accuracy(object, test, levels, ..., f = NULL)
object |
An object of class |
test |
An object of class |
levels |
Return the specified level(s), when carrying out out-of-sample |
... |
Extra arguments to be ignored |
f |
Deprecated. Please use |
MASE calculation is scaled using MAE of in-sample naive forecasts for non-seasonal time series, and in-sample seasonal naive forecasts for seasonal time series.
Matrix giving forecast accuracy measures.
ME |
Mean Error |
RMSE |
Root Mean Square Error |
MAE |
Mean Absolute Error |
MAPE |
Mean Absolute Percentage Error |
MPE |
Mean Percentage Error |
MASE |
Mean Absolute Scaled Error |
Rob J Hyndman and Earo Wang
R. J. Hyndman and A. Koehler (2006), Another look at measures of forecast accuracy, International Journal of Forecasting, 22, 679-688.
hts
, plot.gts
,
forecast.gts
, accuracy
data <- window(htseg2, start = 1992, end = 2002) test <- window(htseg2, start = 2003) fcasts <- forecast(data, h = 5, method = "bu") accuracy(fcasts, test) accuracy(fcasts, test, levels = 1)
data <- window(htseg2, start = 1992, end = 2002) test <- window(htseg2, start = 2003) fcasts <- forecast(data, h = 5, method = "bu") accuracy(fcasts, test) accuracy(fcasts, test, levels = 1)
The time series from selected levels of a hierarchical/grouped time series or a forecasted hierarchical/grouped time series are returned as a multivariate time series.
aggts(y, levels, forecasts = TRUE)
aggts(y, levels, forecasts = TRUE)
y |
An object of class |
levels |
Integer(s) or string(s) giving the specified level(s). |
forecasts |
If |
Earo Wang
aggts(htseg1, levels = c(0, 2)) aggts(infantgts, levels = "State")
aggts(htseg1, levels = c(0, 2)) aggts(infantgts, levels = "State")
The time series from all levels of a hierarchical/grouped time series or a forecasted hierarchical/grouped time series are returned as a multivariate time series.
allts(y, forecasts = TRUE)
allts(y, forecasts = TRUE)
y |
An object of class |
forecasts |
If |
Rob J Hyndman
allts(htseg1)
allts(htseg1)
Using the methods of Hyndman et al. (2016) and Hyndman et al. (2011), this function optimally combines
the forecasts at all levels of a hierarchical time series. The
forecast.gts
calls this function when the comb
method
is selected.
combinef( fcasts, nodes = NULL, groups = NULL, weights = NULL, nonnegative = FALSE, algorithms = c("lu", "cg", "chol", "recursive", "slm"), keep = c("gts", "all", "bottom"), parallel = FALSE, num.cores = 2, control.nn = list() )
combinef( fcasts, nodes = NULL, groups = NULL, weights = NULL, nonnegative = FALSE, algorithms = c("lu", "cg", "chol", "recursive", "slm"), keep = c("gts", "all", "bottom"), parallel = FALSE, num.cores = 2, control.nn = list() )
fcasts |
Matrix of forecasts for all levels of the hierarchical time series. Each row represents one forecast horizon and each column represents one time series from the hierarchy. |
nodes |
If the object class is |
groups |
If the object class is |
weights |
A numeric vector. The default is |
nonnegative |
Logical. Should the reconciled forecasts be non-negative? |
algorithms |
An algorithm to be used for computing reconciled
forecasts. See |
keep |
Return a |
parallel |
Logical. Import parallel package to allow parallel processing. |
num.cores |
Numeric. Specify how many cores are going to be used. |
control.nn |
A list of control parameters to be passed on to the block principal pivoting algorithm. See 'Details'. |
The control.nn
argument is a list that can supply any of the following components:
ptype
Permutation method to be used: "fixed"
or "random"
. Defaults to "fixed"
.
par
The number of full exchange rules that may be tried. Defaults to 10.
gtol
The tolerance of the convergence criteria. Defaults to sqrt(.Machine$double.eps)
.
Return the (non-negative) reconciled gts
object or forecasts at the bottom
level.
Alan Lee, Rob J Hyndman, Earo Wang and Shanika L Wickramasuriya
Hyndman, R. J., Ahmed, R. A., Athanasopoulos, G., & Shang, H. L. (2011). Optimal combination forecasts for hierarchical time series. Computational Statistics and Data Analysis, 55(9), 2579–2589. https://robjhyndman.com/publications/hierarchical/
Hyndman, R. J., Lee, A., & Wang, E. (2016). Fast computation of reconciled forecasts for hierarchical and grouped time series. Computational Statistics and Data Analysis, 97, 16–32. https://robjhyndman.com/publications/hgts/
Wickramasuriya, S. L., Turlach, B. A., & Hyndman, R. J. (to appear). Optimal non-negative forecast reconciliation. Statistics and Computing. https://robjhyndman.com/publications/nnmint/
# hts example ## Not run: h <- 12 ally <- aggts(htseg1) allf <- matrix(NA, nrow = h, ncol = ncol(ally)) for(i in 1:ncol(ally)) allf[,i] <- forecast(auto.arima(ally[,i]), h = h)$mean allf <- ts(allf, start = 51) y.f <- combinef(allf, get_nodes(htseg1), weights = NULL, keep = "gts", algorithms = "lu") plot(y.f) ## End(Not run) ## Not run: h <- 12 ally <- abs(aggts(htseg2)) allf <- matrix(NA, nrow = h, ncol = ncol(ally)) for(i in 1:ncol(ally)) allf[,i] <- forecast(auto.arima(ally[,i], lambda = 0, biasadj = TRUE), h = h)$mean b.f <- combinef(allf, get_nodes(htseg2), weights = NULL, keep = "bottom", algorithms = "lu") b.nnf <- combinef(allf, get_nodes(htseg2), weights = NULL, keep = "bottom", algorithms = "lu", nonnegative = TRUE) ## End(Not run) # gts example ## Not run: abc <- ts(5 + matrix(sort(rnorm(200)), ncol = 4, nrow = 50)) g <- rbind(c(1,1,2,2), c(1,2,1,2)) y <- gts(abc, groups = g) h <- 12 ally <- aggts(y) allf <- matrix(NA,nrow = h,ncol = ncol(ally)) for(i in 1:ncol(ally)) allf[,i] <- forecast(auto.arima(ally[,i]),h = h)$mean allf <- ts(allf, start = 51) y.f <- combinef(allf, groups = get_groups(y), keep ="gts", algorithms = "lu") plot(y.f) ## End(Not run)
# hts example ## Not run: h <- 12 ally <- aggts(htseg1) allf <- matrix(NA, nrow = h, ncol = ncol(ally)) for(i in 1:ncol(ally)) allf[,i] <- forecast(auto.arima(ally[,i]), h = h)$mean allf <- ts(allf, start = 51) y.f <- combinef(allf, get_nodes(htseg1), weights = NULL, keep = "gts", algorithms = "lu") plot(y.f) ## End(Not run) ## Not run: h <- 12 ally <- abs(aggts(htseg2)) allf <- matrix(NA, nrow = h, ncol = ncol(ally)) for(i in 1:ncol(ally)) allf[,i] <- forecast(auto.arima(ally[,i], lambda = 0, biasadj = TRUE), h = h)$mean b.f <- combinef(allf, get_nodes(htseg2), weights = NULL, keep = "bottom", algorithms = "lu") b.nnf <- combinef(allf, get_nodes(htseg2), weights = NULL, keep = "bottom", algorithms = "lu", nonnegative = TRUE) ## End(Not run) # gts example ## Not run: abc <- ts(5 + matrix(sort(rnorm(200)), ncol = 4, nrow = 50)) g <- rbind(c(1,1,2,2), c(1,2,1,2)) y <- gts(abc, groups = g) h <- 12 ally <- aggts(y) allf <- matrix(NA,nrow = h,ncol = ncol(ally)) for(i in 1:ncol(ally)) allf[,i] <- forecast(auto.arima(ally[,i]),h = h)$mean allf <- ts(allf, start = 51) y.f <- combinef(allf, groups = get_groups(y), keep ="gts", algorithms = "lu") plot(y.f) ## End(Not run)
Methods for forecasting hierarchical or grouped time series.
## S3 method for class 'gts' forecast( object, h = ifelse(frequency(object$bts) > 1L, 2L * frequency(object$bts), 10L), method = c("comb", "bu", "mo", "tdgsa", "tdgsf", "tdfp"), weights = c("wls", "ols", "mint", "nseries"), fmethod = c("ets", "arima", "rw"), algorithms = c("lu", "cg", "chol", "recursive", "slm"), covariance = c("shr", "sam"), nonnegative = FALSE, control.nn = list(), keep.fitted = FALSE, keep.resid = FALSE, positive = FALSE, lambda = NULL, level, FUN = NULL, xreg = NULL, newxreg = NULL, parallel = FALSE, num.cores = 2, ... )
## S3 method for class 'gts' forecast( object, h = ifelse(frequency(object$bts) > 1L, 2L * frequency(object$bts), 10L), method = c("comb", "bu", "mo", "tdgsa", "tdgsf", "tdfp"), weights = c("wls", "ols", "mint", "nseries"), fmethod = c("ets", "arima", "rw"), algorithms = c("lu", "cg", "chol", "recursive", "slm"), covariance = c("shr", "sam"), nonnegative = FALSE, control.nn = list(), keep.fitted = FALSE, keep.resid = FALSE, positive = FALSE, lambda = NULL, level, FUN = NULL, xreg = NULL, newxreg = NULL, parallel = FALSE, num.cores = 2, ... )
object |
Hierarchical or grouped time series object of class
|
h |
Forecast horizon |
method |
Method for distributing forecasts within the hierarchy. See details |
weights |
Weights used for "optimal combination" method:
|
fmethod |
Forecasting method to use for each series. |
algorithms |
An algorithm to be used for computing the combination
forecasts (when |
covariance |
Type of the covariance matrix to be used with
|
nonnegative |
Logical. Should the reconciled forecasts be non-negative? |
control.nn |
A list of control parameters to be passed on to the block principal pivoting algorithm. See 'Details'. |
keep.fitted |
If |
keep.resid |
If |
positive |
If |
lambda |
Box-Cox transformation parameter. |
level |
Level used for "middle-out" method (only used when |
FUN |
A user-defined function that returns an object which can be
passed to the |
xreg |
When |
newxreg |
When |
parallel |
If |
num.cores |
If |
... |
Other arguments passed to |
Base methods implemented include ETS, ARIMA and the naive (random walk) models. Forecasts are distributed in the hierarchy using bottom-up, top-down, middle-out and optimal combination methods.
Three top-down methods are available: the two Gross-Sohl methods and the
forecast-proportion approach of Hyndman, Ahmed, and Athanasopoulos (2011).
The "middle-out" method "mo"
uses bottom-up ("bu"
) for levels
higher than level
and top-down forecast proportions ("tdfp"
)
for levels lower than level
.
For non-hierarchical grouped data, only bottom-up and combination methods are possible, as any method involving top-down disaggregation requires a hierarchical ordering of groups.
When xreg
and newxreg
are passed, the same covariates are
applied to every series in the hierarchy.
The control.nn
argument is a list that can supply any of the following components:
ptype
Permutation method to be used: "fixed"
or "random"
. Defaults to "fixed"
.
par
The number of full exchange rules that may be tried. Defaults to 10.
gtol
The tolerance of the convergence criteria. Defaults to sqrt(.Machine$double.eps)
.
A forecasted hierarchical/grouped time series of class gts
.
In-sample fitted values and resiuals are not returned if method = "comb"
and nonnegative = TRUE
.
Earo Wang, Rob J Hyndman and Shanika L Wickramasuriya
Athanasopoulos, G., Ahmed, R. A., & Hyndman, R. J. (2009). Hierarchical forecasts for Australian domestic tourism, International Journal of Forecasting, 25, 146-166.
Hyndman, R. J., Ahmed, R. A., Athanasopoulos, G., & Shang, H. L. (2011). Optimal combination forecasts for hierarchical time series. Computational Statistics and Data Analysis, 55(9), 2579–2589. https://robjhyndman.com/publications/hierarchical/
Hyndman, R. J., Lee, A., & Wang, E. (2016). Fast computation of reconciled forecasts for hierarchical and grouped time series. Computational Statistics and Data Analysis, 97, 16–32. https://robjhyndman.com/publications/hgts/
Wickramasuriya, S. L., Athanasopoulos, G., & Hyndman, R. J. (2019). Optimal forecast reconciliation for hierarchical and grouped time series through trace minimization. Journal of the American Statistical Association, 114(526), 804–819. https://robjhyndman.com/publications/mint/
Wickramasuriya, S. L., Turlach, B. A., & Hyndman, R. J. (to appear). Optimal non-negative forecast reconciliation. Statistics and Computing. https://robjhyndman.com/publications/nnmint/
Gross, C., & Sohl, J. (1990). Dissagregation methods to expedite product line forecasting, Journal of Forecasting, 9, 233–254.
hts
, gts
,
plot.gts
, accuracy.gts
forecast(htseg1, h = 10, method = "bu", fmethod = "arima") ## Not run: forecast( htseg2, h = 10, method = "comb", algorithms = "lu", FUN = function(x) tbats(x, use.parallel = FALSE) ) ## End(Not run)
forecast(htseg1, h = 10, method = "bu", fmethod = "arima") ## Not run: forecast( htseg2, h = 10, method = "comb", algorithms = "lu", FUN = function(x) tbats(x, use.parallel = FALSE) ) ## End(Not run)
Get nodes/groups from an hts/gts object
get_groups(y) get_nodes(y)
get_groups(y) get_nodes(y)
y |
An hts or gts object series. |
Method for creating grouped time series.
gts(y, groups, gnames = rownames(groups), characters) is.gts(xts) ## S3 method for class 'gts' print(x, ...) ## S3 method for class 'gts' summary(object, ...)
gts(y, groups, gnames = rownames(groups), characters) is.gts(xts) ## S3 method for class 'gts' print(x, ...) ## S3 method for class 'gts' summary(object, ...)
y |
A matrix or multivariate time series contains the bottom level series. |
groups |
Group matrix indicates the group structure, with one column
for each series when completely disaggregated, and one row for each grouping
of the time series. It allows either a numerical matrix or a matrix
consisting of strings that can be used for labelling. If the argument
|
gnames |
Specify the group names. |
characters |
A vector of integers, or a list containing vectors of
integers, indicating the segments in which bottom level names can be read in
order to construct the corresponding grouping matrix and its labels. A
|
xts |
|
x |
|
... |
Extra arguments passed to |
object |
|
bts |
Multivariate time series contains the bottom level series |
groups |
Information about the groups of a grouped time series |
labels |
Information about the labels that are used for plotting. |
Earo Wang and Rob J Hyndman
Hyndman, R. J., Ahmed, R. A., Athanasopoulos, G., & Shang, H. L. (2011). Optimal combination forecasts for hierarchical time series. Computational Statistics and Data Analysis, 55(9), 2579–2589. https://robjhyndman.com/publications/hierarchical/
hts
, accuracy.gts
,
forecast.gts
, plot.gts
# Example 1 illustrating the usage of the "groups" argument abc <- ts(5 + matrix(sort(rnorm(1600)), ncol = 16, nrow = 100)) sex <- rep(c("female", "male"), each = 8) state <- rep(c("NSW", "VIC", "QLD", "SA", "WA", "NT", "ACT", "TAS"), 2) gc <- rbind(sex, state) # a matrix consists of strings. gn <- rbind(rep(1:2, each = 8), rep(1:8, 2)) # a numerical matrix rownames(gc) <- rownames(gn) <- c("Sex", "State") x <- gts(abc, groups = gc) y <- gts(abc, groups = gn) # Example 2 with two simple hierarchies (geography and product) to show the argument "characters" bnames1 <- c("VICMelbAA", "VICMelbAB", "VICGeelAA", "VICGeelAB", "VICMelbBA", "VICMelbBB", "VICGeelBA", "VICGeelBB", "NSWSyndAA", "NSWSyndAB", "NSWWollAA", "NSWWollAB", "NSWSyndBA", "NSWSyndBB", "NSWWollBA", "NSWWollBB") bts1 <- matrix(ts(rnorm(160)), ncol = 16) colnames(bts1) <- bnames1 x1 <- gts(bts1, characters = list(c(3, 4), c(1, 1))) # Example 3 with a non-hierarchical grouped time series of 3 grouping variables (state, age and sex) bnames2 <- c("VIC1F", "VIC1M", "VIC2F", "VIC2M", "VIC3F", "VIC3M", "NSW1F", "NSW1M", "NSW2F", "NSW2M", "NSW3F", "NSW3M") bts2 <- matrix(ts(rnorm(120)), ncol = 12) colnames(bts2) <- bnames2 x2 <- gts(bts2, characters = c(3, 1, 1))
# Example 1 illustrating the usage of the "groups" argument abc <- ts(5 + matrix(sort(rnorm(1600)), ncol = 16, nrow = 100)) sex <- rep(c("female", "male"), each = 8) state <- rep(c("NSW", "VIC", "QLD", "SA", "WA", "NT", "ACT", "TAS"), 2) gc <- rbind(sex, state) # a matrix consists of strings. gn <- rbind(rep(1:2, each = 8), rep(1:8, 2)) # a numerical matrix rownames(gc) <- rownames(gn) <- c("Sex", "State") x <- gts(abc, groups = gc) y <- gts(abc, groups = gn) # Example 2 with two simple hierarchies (geography and product) to show the argument "characters" bnames1 <- c("VICMelbAA", "VICMelbAB", "VICGeelAA", "VICGeelAB", "VICMelbBA", "VICMelbBB", "VICGeelBA", "VICGeelBB", "NSWSyndAA", "NSWSyndAB", "NSWWollAA", "NSWWollAB", "NSWSyndBA", "NSWSyndBB", "NSWWollBA", "NSWWollBB") bts1 <- matrix(ts(rnorm(160)), ncol = 16) colnames(bts1) <- bnames1 x1 <- gts(bts1, characters = list(c(3, 4), c(1, 1))) # Example 3 with a non-hierarchical grouped time series of 3 grouping variables (state, age and sex) bnames2 <- c("VIC1F", "VIC1M", "VIC2F", "VIC2M", "VIC3F", "VIC3M", "NSW1F", "NSW1M", "NSW2F", "NSW2M", "NSW3F", "NSW3M") bts2 <- matrix(ts(rnorm(120)), ncol = 12) colnames(bts2) <- bnames2 x2 <- gts(bts2, characters = c(3, 1, 1))
Method for creating hierarchical time series.
hts(y, nodes, bnames = colnames(y), characters) is.hts(xts) ## S3 method for class 'hts' print(x, ...) ## S3 method for class 'hts' summary(object, ...)
hts(y, nodes, bnames = colnames(y), characters) is.hts(xts) ## S3 method for class 'hts' print(x, ...) ## S3 method for class 'hts' summary(object, ...)
y |
A matrix or multivariate time series contain the bottom level series. |
nodes |
A list contains the number of child nodes associated with each
level, which indicates the hierarchical structure. The default is a simple
hierarchy with only 2 levels (i.e. total and bottom). If the argument
|
bnames |
The names of the bottom time series. |
characters |
Integers indicate the segments in which the bottom level
names can be read in order to construct the corresponding node structure and
its labels. For instance, suppose one of the bottom series is named
"VICMelb" referring to the city of Melbourne within the state of Victoria.
Then |
xts |
|
x |
|
... |
Extra arguments passed to |
object |
|
bts |
Multivariate time series containing the bottom level series |
nodes |
Information about the nodes of a hierarchical time series |
labels |
Information about the labels that are used for plotting. |
Earo Wang and Rob J Hyndman
Hyndman, R. J., Ahmed, R. A., Athanasopoulos, G., & Shang, H. L. (2011). Optimal combination forecasts for hierarchical time series. Computational Statistics and Data Analysis, 55(9), 2579–2589. https://robjhyndman.com/publications/hierarchical/
gts
, accuracy.gts
,
forecast.gts
, plot.gts
# Example 1 # The hierarchical structure looks like 2 child nodes associated with level 1, # which are followed by 3 and 2 sub-child nodes respectively at level 2. nodes <- list(2, c(3, 2)) abc <- ts(5 + matrix(sort(rnorm(500)), ncol = 5, nrow = 100)) x <- hts(abc, nodes) # Example 2 # Suppose we've got the bottom names that can be useful for constructing the node # structure and the labels at higher levels. We need to specify how to split them # in the argument "characters". library(hts) abc <- ts(5 + matrix(sort(rnorm(1000)), ncol = 10, nrow = 100)) colnames(abc) <- c("A10A", "A10B", "A10C", "A20A", "A20B", "B30A", "B30B", "B30C", "B40A", "B40B") y <- hts(abc, characters = c(1, 2, 1))
# Example 1 # The hierarchical structure looks like 2 child nodes associated with level 1, # which are followed by 3 and 2 sub-child nodes respectively at level 2. nodes <- list(2, c(3, 2)) abc <- ts(5 + matrix(sort(rnorm(500)), ncol = 5, nrow = 100)) x <- hts(abc, nodes) # Example 2 # Suppose we've got the bottom names that can be useful for constructing the node # structure and the labels at higher levels. We need to specify how to split them # in the argument "characters". library(hts) abc <- ts(5 + matrix(sort(rnorm(1000)), ncol = 10, nrow = 100)) colnames(abc) <- c("A10A", "A10B", "A10C", "A20A", "A20B", "B30A", "B30B", "B30C", "B40A", "B40B") y <- hts(abc, characters = c(1, 2, 1))
These are simulated data. htseg1
has three levels with a total of 8
series each of length 10. htseg2
has four levels with a total of 17
series each of length 16.
Objects of class hts
.
R. J. Hyndman, R. A. Ahmed, G. Athanasopoulos and H.L. Shang (2011) Optimal combination forecasts for hierarchical time series. Computational Statistics and Data Analysis, 55(9), 2579–2589. https://robjhyndman.com/publications/hierarchical/
plot(htseg1)
plot(htseg1)
These are infant mortality counts. This data set is an example of
gts
, where the total infant mortality count in Australia can be first
disaggregated by sex then by state, or vice versa.
Objects of class gts
.
R. J. Hyndman, R. A. Ahmed, G. Athanasopoulos and H.L. Shang (2011) Optimal combination forecasts for hierarchical time series. Computational Statistics and Data Analysis, 55(9), 2579–2589.
plot(infantgts)
plot(infantgts)
Using the method of Wickramasuriya et al. (2019), this function combines the
forecasts at all levels of a hierarchical or grouped time series. The
forecast.gts
calls this function when the MinT
method
is selected.
MinT( fcasts, nodes = NULL, groups = NULL, residual, covariance = c("shr", "sam"), nonnegative = FALSE, algorithms = c("lu", "cg", "chol"), keep = c("gts", "all", "bottom"), parallel = FALSE, num.cores = 2, control.nn = list() )
MinT( fcasts, nodes = NULL, groups = NULL, residual, covariance = c("shr", "sam"), nonnegative = FALSE, algorithms = c("lu", "cg", "chol"), keep = c("gts", "all", "bottom"), parallel = FALSE, num.cores = 2, control.nn = list() )
fcasts |
Matrix of forecasts for all levels of a hierarchical or grouped time series. Each row represents one forecast horizon and each column represents one time series of aggregated or disaggregated forecasts. |
nodes |
If the object class is hts, a list contains the number of child nodes referring to hts. |
groups |
If the object is gts, a gmatrix is required, which is the same as groups in the function gts. |
residual |
Matrix of insample residuals for all the aggregated and
disaggregated time series. The columns must be in the same order as
|
covariance |
Type of the covariance matrix to be used. Shrinking
towards a diagonal unequal variances ( |
nonnegative |
Logical. Should the reconciled forecasts be non-negative? |
algorithms |
Algorithm used to compute inverse of the matrices. |
keep |
Return a gts object or the reconciled forecasts at the bottom level. |
parallel |
Logical. Import parallel package to allow parallel processing. |
num.cores |
Numeric. Specify how many cores are going to be used. |
control.nn |
A list of control parameters to be passed on to the block principal pivoting algorithm. See 'Details'. |
The control.nn
argument is a list that can supply any of the following components:
ptype
Permutation method to be used: "fixed"
or "random"
. Defaults to "fixed"
.
par
The number of full exchange rules that may be tried. Defaults to 10.
gtol
The tolerance of the convergence criteria. Defaults to sqrt(.Machine$double.eps)
.
Return the reconciled gts
object or forecasts at the bottom
level.
Shanika L Wickramasuriya
Wickramasuriya, S. L., Athanasopoulos, G., & Hyndman, R. J. (2019). Optimal forecast reconciliation for hierarchical and grouped time series through trace minimization. Journal of the American Statistical Association, 114(526), 804–819. https://robjhyndman.com/publications/mint/
Wickramasuriya, S. L., Turlach, B. A., & Hyndman, R. J. (to appear). Optimal non-negative forecast reconciliation. Statistics and Computing. https://robjhyndman.com/publications/nnmint/
Hyndman, R. J., Lee, A., & Wang, E. (2016). Fast computation of reconciled forecasts for hierarchical and grouped time series. Computational Statistics and Data Analysis, 97, 16–32. https://robjhyndman.com/publications/hgts/
hts
, gts
,
forecast.gts
, combinef
# hts example ## Not run: h <- 12 ally <- aggts(htseg1) n <- nrow(ally) p <- ncol(ally) allf <- matrix(NA, nrow = h, ncol = p) res <- matrix(NA, nrow = n, ncol = p) for(i in 1:p) { fit <- auto.arima(ally[, i]) allf[, i] <- forecast(fit, h = h)$mean res[, i] <- na.omit(ally[, i] - fitted(fit)) } allf <- ts(allf, start = 51) y.f <- MinT(allf, get_nodes(htseg1), residual = res, covariance = "shr", keep = "gts", algorithms = "lu") plot(y.f) y.f_cg <- MinT(allf, get_nodes(htseg1), residual = res, covariance = "shr", keep = "all", algorithms = "cg") ## End(Not run) ## Not run: h <- 12 ally <- abs(aggts(htseg2)) allf <- matrix(NA, nrow = h, ncol = ncol(ally)) res <- matrix(NA, nrow = nrow(ally), ncol = ncol(ally)) for(i in 1:ncol(ally)) { fit <- auto.arima(ally[, i], lambda = 0, biasadj = TRUE) allf[,i] <- forecast(fit, h = h)$mean res[,i] <- na.omit(ally[, i] - fitted(fit)) } b.f <- MinT(allf, get_nodes(htseg2), residual = res, covariance = "shr", keep = "bottom", algorithms = "lu") b.nnf <- MinT(allf, get_nodes(htseg2), residual = res, covariance = "shr", keep = "bottom", algorithms = "lu", nonnegative = TRUE, parallel = TRUE) ## End(Not run) # gts example ## Not run: abc <- ts(5 + matrix(sort(rnorm(200)), ncol = 4, nrow = 50)) g <- rbind(c(1,1,2,2), c(1,2,1,2)) y <- gts(abc, groups = g) h <- 12 ally <- aggts(y) n <- nrow(ally) p <- ncol(ally) allf <- matrix(NA,nrow = h,ncol = ncol(ally)) res <- matrix(NA, nrow = n, ncol = p) for(i in 1:p) { fit <- auto.arima(ally[, i]) allf[, i] <- forecast(fit, h = h)$mean res[, i] <- na.omit(ally[, i] - fitted(fit)) } allf <- ts(allf, start = 51) y.f <- MinT(allf, groups = get_groups(y), residual = res, covariance = "shr", keep = "gts", algorithms = "lu") plot(y.f) ## End(Not run)
# hts example ## Not run: h <- 12 ally <- aggts(htseg1) n <- nrow(ally) p <- ncol(ally) allf <- matrix(NA, nrow = h, ncol = p) res <- matrix(NA, nrow = n, ncol = p) for(i in 1:p) { fit <- auto.arima(ally[, i]) allf[, i] <- forecast(fit, h = h)$mean res[, i] <- na.omit(ally[, i] - fitted(fit)) } allf <- ts(allf, start = 51) y.f <- MinT(allf, get_nodes(htseg1), residual = res, covariance = "shr", keep = "gts", algorithms = "lu") plot(y.f) y.f_cg <- MinT(allf, get_nodes(htseg1), residual = res, covariance = "shr", keep = "all", algorithms = "cg") ## End(Not run) ## Not run: h <- 12 ally <- abs(aggts(htseg2)) allf <- matrix(NA, nrow = h, ncol = ncol(ally)) res <- matrix(NA, nrow = nrow(ally), ncol = ncol(ally)) for(i in 1:ncol(ally)) { fit <- auto.arima(ally[, i], lambda = 0, biasadj = TRUE) allf[,i] <- forecast(fit, h = h)$mean res[,i] <- na.omit(ally[, i] - fitted(fit)) } b.f <- MinT(allf, get_nodes(htseg2), residual = res, covariance = "shr", keep = "bottom", algorithms = "lu") b.nnf <- MinT(allf, get_nodes(htseg2), residual = res, covariance = "shr", keep = "bottom", algorithms = "lu", nonnegative = TRUE, parallel = TRUE) ## End(Not run) # gts example ## Not run: abc <- ts(5 + matrix(sort(rnorm(200)), ncol = 4, nrow = 50)) g <- rbind(c(1,1,2,2), c(1,2,1,2)) y <- gts(abc, groups = g) h <- 12 ally <- aggts(y) n <- nrow(ally) p <- ncol(ally) allf <- matrix(NA,nrow = h,ncol = ncol(ally)) res <- matrix(NA, nrow = n, ncol = p) for(i in 1:p) { fit <- auto.arima(ally[, i]) allf[, i] <- forecast(fit, h = h)$mean res[, i] <- na.omit(ally[, i] - fitted(fit)) } allf <- ts(allf, start = 51) y.f <- MinT(allf, groups = get_groups(y), residual = res, covariance = "shr", keep = "gts", algorithms = "lu") plot(y.f) ## End(Not run)
Method for plotting grouped or hierarchical time series and their forecasts.
## S3 method for class 'gts' plot(x, include, levels, labels = TRUE, col = NULL, color_lab = FALSE, ...)
## S3 method for class 'gts' plot(x, include, levels, labels = TRUE, col = NULL, color_lab = FALSE, ...)
x |
An object of class |
include |
Number of values from historical time series to include in the plot of forecasted group/hierarchical time series. |
levels |
Integer(s) or string(s) giving the specified levels(s) to be plotted |
labels |
If |
col |
Vector of colours, passed to |
color_lab |
If |
... |
Other arguments passing to |
Rob J Hyndman and Earo Wang
Hyndman, R. J., Ahmed, R. A., Athanasopoulos, G., & Shang, H. L. (2011). Optimal combination forecasts for hierarchical time series. Computational Statistics and Data Analysis, 55(9), 2579–2589. https://robjhyndman.com/publications/hierarchical/
plot(htseg1, levels = c(0, 2)) plot(infantgts, include = 10, levels = "State") plot(infantgts, include = 10, levels = "State", col = colours()[100:107], lty = 1:8, color_lab = TRUE)
plot(htseg1, levels = c(0, 2)) plot(infantgts, include = 10, levels = "State") plot(infantgts, include = 10, levels = "State", col = colours()[100:107], lty = 1:8, color_lab = TRUE)
This function returns the summing matrix for a hierarchical or grouped time series, as defined in Hyndman et al. (2011).
smatrix(xts)
smatrix(xts)
xts |
Hierarchical or grouped time series of class |
A numerical matrix.
Earo Wang
Hyndman, R. J., Ahmed, R. A., Athanasopoulos, G., & Shang, H. L. (2011). Optimal combination forecasts for hierarchical time series. Computational Statistics and Data Analysis, 55(9), 2579–2589. https://robjhyndman.com/publications/hierarchical/
smatrix(htseg1)
smatrix(htseg1)
Extracts a subset of the time series from a grouped time series object.
## S3 method for class 'gts' window(x, ...)
## S3 method for class 'gts' window(x, ...)
x |
An object of class |
... |
All other arguments are passed to |
Rob J Hyndman
window(htseg2, start = 2000, end = 2001)
window(htseg2, start = 2000, end = 2001)