Title: | Degenerate Hierarchical Time Series Reconciliation |
---|---|
Description: | Takes the MinT implementation of [hts: Hierarchical and Grouped Time Series]<https://cran.r-project.org/package=hts> and adapts it to allow degenerate hierarchical structures. Instead of the "nodes" argument, this function takes an S matrix which is more versatile in the structures it allows. For a demo, see [Degenerate Hierarchical Time Series Reconciliation With The Minimum Trace Algorithm in R]<doi:10.15488/17729>. The MinT algorithm is based on [Optimal Forecast Reconciliation for Hierarchical and Grouped Time Series Through Trace Minimization]<doi:10.1080/01621459.2018.1448825>. |
Authors: | Louis Steinmeister [aut, cre], Markus Pauly [aut] |
Maintainer: | Louis Steinmeister <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.1.0 |
Built: | 2025-01-10 06:18:49 UTC |
Source: | https://github.com/lsteinmeister/htsdegenerater |
accuracy.gts
accuracy.gts(fcasts, actuals)
accuracy.gts(fcasts, actuals)
fcasts |
forecasts to be evaluated |
actuals |
actuals to compare the forecasts against |
Averaged error measures across all time series in matrix form.
Bottom-up reconciliation
BU(fcasts, S)
BU(fcasts, S)
fcasts |
forecasts to be reconciled |
S |
structure matrix representing the hierarchical structure of the hts |
reconciled forecasts
Using the method of Wickramasuriya et al. (2019), this function (based on Hyndman et al.'s hts library) combines the forecasts at all levels of a hierarchical time series and works for degenerate hierarchies.
MinT( fcasts, Smat, residual, covariance = c("shr", "sam", "custom"), nonnegative = FALSE, cov.type = "complete.obs", cov.matrix = NULL )
MinT( fcasts, Smat, residual, covariance = c("shr", "sam", "custom"), nonnegative = FALSE, cov.type = "complete.obs", cov.matrix = NULL )
fcasts |
a vector or a matrix (rows = horizon, columns = ts columns) of forecasts |
Smat |
a structure matrix detailing the hierarchical structure of the hts. Make sure that the order of the rows align with the order of the forecasts. |
residual |
a matrix of in-sample residuals (columns = ts columns) |
covariance |
should a shrinkage estimator or the sample estimator be used? alternatively, a custom covariance matrix can be passed (additionally requires the cov.matrix argument) |
nonnegative |
not implemented yet. |
cov.type |
specify how the covariance matrix should be computed (default = complete observations). Note that pairwise.complete.obs may not yield a positive definite matrix! |
cov.matrix |
specify in case a custom covariance matrix should be used |
reconciled forecasts
[hts: Hierarchical and Grouped Time Series]<https://cran.r-project.org/package=hts> [Optimal Forecast Reconciliation for Hierarchical and Grouped Time Series Through Trace Minimization]<doi:10.1080/01621459.2018.1448825> [Degenerate Hierarchical Time Series Reconciliation With The Minimum Trace Algorithm in R]<doi:10.15488/17729>
# Set the seed for reproducibility set.seed(123) # Create a sequence of 120 numbers x <- seq(1, 120) # Generate the columns AA <- sin(x*pi/6) + rnorm(120, 0, 1) # Sine component with random noise AB <- 0.05*x + rnorm(120, 0, 0.5) # Linear component B <- cos(x*pi/6)+ rnorm(120, 0, 1) # Cosine component # Combine the columns into a matrix matrix <- cbind(AA, AB, B) hts = ts(matrix, frequency = 12) # Define S matrix S <- rbind(c(1,1,1), c(1,1,0), diag(1,3)) rownames(S) <-c("Total", "A", "AA", "AB", "B") colnames(S) <- c("AA", "AB", "B") # Aggregate hts on all levels hts.complete <- ts(t(S %*% t(hts)), frequency = 12) # Fit a model to the time series hts.models = lapply(hts.complete, function(c.ts) forecast::ets(c.ts)) # Fit a model to the time series hts.models = lapply(hts.complete, function(c.ts) forecast::ets(c.ts)) # Generate predictions based on this model hts.forecasts = sapply(hts.models, function(mdl) forecast::forecast(mdl, h = 1)$mean) # Extract residuals hts.residuals = sapply(hts.models, function(mdl) mdl$residuals) # Compute reconciled forecasts MinT(fcasts = hts.forecasts, Smat = S, residual = hts.residuals)
# Set the seed for reproducibility set.seed(123) # Create a sequence of 120 numbers x <- seq(1, 120) # Generate the columns AA <- sin(x*pi/6) + rnorm(120, 0, 1) # Sine component with random noise AB <- 0.05*x + rnorm(120, 0, 0.5) # Linear component B <- cos(x*pi/6)+ rnorm(120, 0, 1) # Cosine component # Combine the columns into a matrix matrix <- cbind(AA, AB, B) hts = ts(matrix, frequency = 12) # Define S matrix S <- rbind(c(1,1,1), c(1,1,0), diag(1,3)) rownames(S) <-c("Total", "A", "AA", "AB", "B") colnames(S) <- c("AA", "AB", "B") # Aggregate hts on all levels hts.complete <- ts(t(S %*% t(hts)), frequency = 12) # Fit a model to the time series hts.models = lapply(hts.complete, function(c.ts) forecast::ets(c.ts)) # Fit a model to the time series hts.models = lapply(hts.complete, function(c.ts) forecast::ets(c.ts)) # Generate predictions based on this model hts.forecasts = sapply(hts.models, function(mdl) forecast::forecast(mdl, h = 1)$mean) # Extract residuals hts.residuals = sapply(hts.models, function(mdl) mdl$residuals) # Compute reconciled forecasts MinT(fcasts = hts.forecasts, Smat = S, residual = hts.residuals)
Structural Scaling reconciliation
strucScaling(fcasts, Smat, weights = rowSums(Smat))
strucScaling(fcasts, Smat, weights = rowSums(Smat))
fcasts |
forecasts to be reconciled |
Smat |
structure matrix representing the hierarchical structure of the hts |
weights |
use the default for structural scaling and a vector of the residual variances for variance scaling |
reconciled forecasts