Skip to content

Commit

Permalink
Match forecast and test set times in accuracy.gts()
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed Nov 23, 2021
1 parent 98178bc commit 3f444cf
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions R/accuracy-gts.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#' In-sample or out-of-sample accuracy measures for forecast grouped and
#' hierarchical model
#'
#'
#' 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 \code{keep.fitted =
#' TRUE} in the \code{\link[hts]{forecast.gts}}. All measures are defined and
#' discussed in Hyndman and Koehler (2006).
#'
#'
#' 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.
#'
#'
#' @param object An object of class \code{gts}, containing the forecasted
#' hierarchical or grouped time series. In-sample accuracy at the bottom level
#' returns when \code{test} is missing.
Expand All @@ -32,13 +32,13 @@
#' @keywords error
#' @method accuracy gts
#' @examples
#'
#'
#' 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)
#'
#'
#' @export
#' @export accuracy.gts
accuracy.gts <- function(object, test, levels, ..., f = NULL) {
Expand Down Expand Up @@ -79,6 +79,14 @@ accuracy.gts <- function(object, test, levels, ..., f = NULL) {
else {
fcasts <- unclass(aggts(object, levels, forecasts = TRUE))
x <- unclass(aggts(test, levels))
tspf <- tsp(fcasts)
tspx <- tsp(x)
start <- max(tspf[1], tspx[1])
end <- min(tspf[2], tspx[2])
start <- min(start, end)
end <- max(start, end)
fcasts <- window(fcasts, start = start, end = end)
x <- window(x, start = start, end = end)
res <- x - fcasts
}

Expand Down

0 comments on commit 3f444cf

Please sign in to comment.