R HTS包:combinef和aggts不能与gts对象一起使用

时间:2015-07-13 23:59:10

标签: r hierarchy forecasting

我正在尝试将R hts包中的combinef和aggts函数应用于时间序列矩阵,以便在层次结构中获得一组优化的预测。我每个月都运行相同的代码而没有问题,现在我在升级到hts package 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)

1 个答案:

答案 0 :(得分:1)

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)

希望它有所帮助。