R HTS 包:combinef 和 aggts 不适用于 gts 对象

R HTS package: combinef and aggts not working with gts object

我正在尝试将 R hts 包中的 combinef 和 aggts 函数应用于时间序列矩阵,以便在整个层次结构中获得一组优化的预测。我 运行 每个月都使用相同的代码,没有问题,现在在升级到 hts 包 v4.5 后看到错误。

可重现的示例(如果需要,我可以离线共享数据文件)

    #Read in forecast data for all levels of hierarchy#

fcast<-read.csv("SampleHierarchyForecast.csv", header = TRUE, check.names = FALSE)

#Convert to time series#

fcast<-ts(fcast, start = as.numeric(2010.25) + (64)/12, end = as.numeric(2010.25) + (75)/12, f= 12)

#Create time series of only the bottom level of the hierarchy#
index<-c()

fcastBottom<-fcast
for (i in 1:length(fcastBottom [1,]))
{
    if(nchar(colnames(fcastBottom)[i])!=28)
    index[i]<-i
    else
    index[i]<-0
}
fcastBottom<-fcastBottom[,-index]

#Create grouped time series from the bottom level forecast #
GtsForecast <- gts(fcastBottom, characters = list(c(12,12), c(4)), gnames = c("Category", "Item", "Customer", "Category-Customer"))
#Use combinef function to optimally combine the full hierarchy forecast using the groups from the full hierarchy gts#
combo <- combinef(fcast, groups = GtsForecast$groups)
*Warning message:
In mapply(rep, as.list(gnames), times, SIMPLIFY = FALSE) :
  longer argument not a multiple of length of shorter*
traceback()
2: stop("Argument fcasts requires all the forecasts.")
1: combinef(fcast, groups = GtsForecast$groups)

comebinef() 函数调用 gts() 时有一个小错误。现在我已将其修复为 github。因此,在更新 the development version.

后,您可以 运行 您自己的代码而不会遇到任何问题

或者,如果您不想安装最新版本,则需要稍微调整一下代码。

combo <- combinef(fcast, groups = GtsForecast$groups, keep = "bottom")
combo <- ts(combo, start = as.numeric(2010.25) + (64)/12, 
            end = as.numeric(2010.25) + (75)/12, f = 12)
colnames(combo) <- colnames(fcastBottom)
newGtsForecast <- gts(combo, characters = list(c(12,12), c(4)), 
                  gnames = c("Category", "Item", "Customer", 
                             "Category-Customer"))
Aggregate <- aggts(newGtsForecast)

希望对您有所帮助。