[英]Using forecast.gts (package hts) with external regressor and parallel processing
[英]Hierarchical data forecast using GTS
使用 GTS 指定兩個層次結構組時遇到錯誤。 錯誤是:
colnames<-
(*tmp*
, value = unlist(labels[levels])) 中的錯誤:
'dimnames' [2] 的長度不等於數組范圍
我用下面的代碼重新創建了這個問題。 在此示例中,有兩個層次結構 State/County 和 Industry/Sub-industry/product。 在這個例子中,州/縣是恆定的——這在我的實際數據集中可能是也可能不是。
y3 <- ts(matrix(rnorm(25),ncol=5,nrow=5))
blnames3 <- paste(rep("CA",5), # State
rep("AL",5), # County
rep("O",5), # Industry
c("P","Q","Q","P","R"), # Sub-industry
c("514","807","514","807","807"), # product
sep="")
colnames(y3) <- blnames3
head(y3)
gy3 <- gts(y3, characters=list(c(2,2),c(1,1,3)))
fc3 <- forecast(gy3, h = 6, method = "comb", fmethod="ets")
如果我使用不同的方法(例如 bu),則預測函數會成功返回,但任何后續操作(例如 allts)都會失敗並出現相同的錯誤。
fc3 <- forecast(gy3, h = 6, method = "bu", fmethod="ets")
allts(fc3)
我已經使用過幾次gts
,但不要聲稱了解有關該軟件包的所有信息。 但希望這會有所幫助。
我認為正在發生的事情是您的gts
對象指定了一個在您的數據中不存在的層次結構。 您列表中的第一個向量是c(2,2)
,它表示州/縣是一個層次結構級別。 但是,數據中的所有州和縣都相同,因此這並不是真正的層次結構。
然后您的第二個層次結構由c(1,1,3)
組成,但行業始終不變,只剩下子行業和產品在變化。
總之,這看起來不像一個分層系列。 我注意到gy3
中的標簽有很多NA
的
gy3$labels
$G1
[1] "G1/CA"
$<NA>
[1] "G1/CAAL"
$<NA>
[1] "G1/O"
$<NA>
[1] "G1/OP" "G1/OQ" "G1/OR"
$<NA>
[1] "G1/OP514" "G1/OQ807" "G1/OQ514" "G1/OP807" "G1/OR807"
$<NA>
[1] "G1/CAO"
$<NA>
[1] "G1/CAOP" "G1/CAOQ" "G1/CAOR"
$<NA>
[1] "G1/CAOP514" "G1/CAOQ807" "G1/CAOQ514" "G1/CAOP807" "G1/CAOR807"
$<NA>
[1] "G1/CAALO"
$<NA>
[1] "G1/CAALOP" "G1/CAALOQ" "G1/CAALOR"
跟蹤代碼,這似乎會導致aggts
出現問題
aggts(gy3)
Error in `colnames<-`(`*tmp*`, value = unlist(labels[levels])) :
length of 'dimnames' [2] not equal to array extent
當您指定需要分層系列的comb
方法時,將調用aggts
。 bu
不會失敗,因為它是自下而上的,不需要層次結構。
長話短說,糾正代碼的一種方法是指定c(6, 3)
的向量,其中包含一組中的子行業和另一組中的產品。
gy3 <- gts(y3, characters=c(6, 3))
fc3 <- forecast(gy3, h = 6, method = "comb", fmethod="ets")
fc3
Grouped Time Series
4 Levels
Number of groups at each level: 1 3 2 5
Total number of series: 11
Number of observations in each historical series: 5
Number of forecasts per series: 6
Top level series of forecasts:
Time Series:
Start = 6
End = 11
Frequency = 1
[1] -0.5835628 -0.5835628 -0.5835628 -0.5835628 -0.5835628 -0.5835628
這也有效
fc3 <- forecast(gy3, h = 6, method = "bu", fmethod="ets")
allts(fc3)
Time Series:
Start = 6
End = 11
Frequency = 1
Total G1/CAALOP G1/CAALOQ G1/CAALOR G2/514 G2/807 CAALOP514 CAALOQ807 CAALOQ514 CAALOP807 CAALOR807
6 -0.6227325 0.0344344 -0.8919916 0.2348247 -0.02331766 -0.5994149 0.2459208 -0.6227531 -0.2692384 -0.2114864 0.2348247
7 -0.6227325 0.0344344 -0.8919916 0.2348247 -0.02331766 -0.5994149 0.2459208 -0.6227531 -0.2692384 -0.2114864 0.2348247
8 -0.6227325 0.0344344 -0.8919916 0.2348247 -0.02331766 -0.5994149 0.2459208 -0.6227531 -0.2692384 -0.2114864 0.2348247
9 -0.6227325 0.0344344 -0.8919916 0.2348247 -0.02331766 -0.5994149 0.2459208 -0.6227531 -0.2692384 -0.2114864 0.2348247
10 -0.6227325 0.0344344 -0.8919916 0.2348247 -0.02331766 -0.5994149 0.2459208 -0.6227531 -0.2692384 -0.2114864 0.2348247
11 -0.6227325 0.0344344 -0.8919916 0.2348247 -0.02331766 -0.5994149 0.2459208 -0.6227531 -0.2692384 -0.2114864 0.2348247
示例中的blnames3
指定錯誤。 列名只被賦予了一個州和一個縣,無法形成層次結構。 您需要提供至少兩個州和另外兩個縣來構建分組時間序列。
我猜你已經看過Rob關於如何在gts()
函數中指定characters
參數的帖子。 現在,我根據 Rob 的示例在行業/子行業層次結構中再添加一個名為“產品”的級別。
下面的代碼表明有兩個州,每個州有兩個縣,兩個行業分別由三個和兩個子行業組成。 此外,每個子行業生產四種產品。
y3 <- ts(matrix(rnorm(300),ncol=60,nrow=5))
blnames3 <- paste0(rep(c("CA", "NY"), each = 30), # State
rep(c("AL", "LA", "CL", "ES"), each = 15), # County
rep(c("O", "O", "O", "C", "C"), 12), # Industry
rep(c("p", "q", "r", "p", "q"), 12), # Sub-industry
rep(504:507, 15)) # Product
colnames(y3) <- blnames3
gy3 <- gts(y3, characters=list(c(2,2),c(1,1,3)))
fc3 <- forecast(gy3, h = 6, method = "comb", fmethod="ets")
aggts(fc3)
希望它有點幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.