You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用HTS包开展层次预测时遭遇colMeans维度错误求助

层次预测(hts包)报错排查:'x' must be an array of at least two dimensions

问题概述

作为预测领域新手,尝试用HTS包做层次预测,构建两个层级结构后执行以下代码:

forecast_middle_out_lum_arima <- forecast(train_hierarchy_lumpy, h = 7, method = "mo", fmethod = "arima", level = 1)
accuracy(forecast_bottom_up_lum_arima, test_hierarchy_lumpy, levels = 2)

持续收到报错:

Error in base::colMeans(x, na.rm = na.rm, dims = dims, ...) : 'x' must be an array of at least two dimensions

数据集仅含单年份周度数据,尝试生成周度预测,可复现代码如下:

library(tidyverse)
library(forecast)
library(hts)
States_df<-c("VIC","TAS","SA~","WA~","NSW","ACT","QLD","NT~")
SKUs<-c("Pro1","Pro2","Pro3","Pro4","Pro5","Pro6")
df_fc<-data.frame(S.no=seq(1,8689),Year=2022,week=sample(1:52,8689,replace = TRUE),state=sample(States_df,8689,replace = TRUE),
              SKU=sample(SKUs,8689,replace = TRUE))
df_fc<-df_fc%>%mutate(Combo=paste(state,SKU,sep = ""))%>%mutate(Sale=sample(10:50,8689,replace=TRUE))
grouped_df_fc<-df_fc%>%group_by(Year,week,Combo)%>%summarise(Qty=sum(Sale,na.rm = TRUE))%>%arrange(Year,week)
pivoted_df_fc<-grouped_df_fc%>%pivot_wider(names_from =Combo,values_from = Qty,values_fill = 0 )
time_series_df_fc<-ts(pivoted_df_fc[,3:50],start = c(2022,1),frequency = 52)
colnames(time_series_df_fc)<-colnames(pivoted_df_fc[,3:50])
hierarchy_df_fc<-hts(time_series_df_fc,characters = c(3,4))
train_hierarchy_df_fc<-window(hierarchy_df_fc,end=c(2022,47))
test_hierarchy_df_fc<-window(hierarchy_df_fc,start=c(2022,48))
forecast_middle_df_fc<-forecast(train_hierarchy_df_fc,h=5,method = "mo",fmethod = "arima",level = 1)
accuracy(forecast_middle_df_fc,test_hierarchy_df_fc,levels = 2)

错误原因及修正方案

1. forecast函数level参数误用

hts包的forecast.hts方法中,level参数的作用是指定需要生成预测的层级编号,而非普通forecast函数中的置信区间水平。你设置level=1,意味着仅对第1层级(state层级)生成预测,但后续accuracy调用指定levels=2(需要底层state+SKU节点的预测值),导致预测结果与测试集的维度不匹配,触发报错。

2. Middle-out方法缺少nodes参数

使用method="mo"(中层向外预测)时,必须通过nodes参数指定作为预测起点的中层节点,否则无法正确执行该预测逻辑,也会导致预测结果结构异常。

修正后的代码

library(tidyverse)
library(forecast)
library(hts)
States_df<-c("VIC","TAS","SA~","WA~","NSW","ACT","QLD","NT~")
SKUs<-c("Pro1","Pro2","Pro3","Pro4","Pro5","Pro6")
df_fc<-data.frame(S.no=seq(1,8689),Year=2022,week=sample(1:52,8689,replace = TRUE),state=sample(States_df,8689,replace = TRUE),
              SKU=sample(SKUs,8689,replace = TRUE))
df_fc<-df_fc%>%mutate(Combo=paste(state,SKU,sep = ""))%>%mutate(Sale=sample(10:50,8689,replace=TRUE))
grouped_df_fc<-df_fc%>%group_by(Year,week,Combo)%>%summarise(Qty=sum(Sale,na.rm = TRUE))%>%arrange(Year,week)
pivoted_df_fc<-grouped_df_fc%>%pivot_wider(names_from =Combo,values_from = Qty,values_fill = 0 )
time_series_df_fc<-ts(pivoted_df_fc[,3:50],start = c(2022,1),frequency = 52)
colnames(time_series_df_fc)<-colnames(pivoted_df_fc[,3:50])
hierarchy_df_fc<-hts(time_series_df_fc,characters = c(3,4))
train_hierarchy_df_fc<-window(hierarchy_df_fc,end=c(2022,47))
test_hierarchy_df_fc<-window(hierarchy_df_fc,start=c(2022,48))

# 修正forecast调用:去掉错误的level参数,添加nodes指定中层节点,如需置信区间用flevel
forecast_middle_df_fc <- forecast(train_hierarchy_df_fc, h=5, method = "mo", fmethod = "arima", 
                                  nodes = get_nodes(train_hierarchy_df_fc, level = 1), # 指定第1层级为中层起点
                                  flevel = c(95)) # 可选:设置置信区间水平(如95%)

# 调用accuracy,可查看指定层级的精度
accuracy(forecast_middle_df_fc, test_hierarchy_df_fc, levels = 2)

额外说明

  • 若需要对所有层级生成预测,可直接省略level参数(默认对所有层级生成预测)。
  • 单年份47周的样本量偏小,可能影响ARIMA模型的拟合效果,若条件允许建议增加历史数据。

内容的提问来源于stack exchange,提问作者Umair Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 07:48:12