Search code examples
rtime-serieshierarchical-dataforecasting

Hierarchical data forecast using GTS


I am running into an error when using GTS to specify two hierarchy groups. The error is:

Error in colnames<-(*tmp*, value = unlist(labels[levels])) :
length of 'dimnames' [2] not equal to array extent

I've recreated the issue with the below code. In this example, there are two hierarchies State/County and Industry/Sub-industry/product. The State/County is constant in this example - this may or may not be true in my actual data set.

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")

If I use a different method such as bu, the forecast function returns successfully but any subsequent operations such as allts fails with the same error.

fc3 <- forecast(gy3, h = 6, method = "bu", fmethod="ets")
allts(fc3)

Solution

  • I have used gts a few times, but don't claim to know everything about the package. But hopefully this will help.

    I think what is happening is that your gts object is specifying a hierarchy that doesn't exist in your data. The first vector in your list is c(2,2) which would indicate that State/County is a hierarchy level. However, all of the states and counties are the same in your data, so this isn't really a hierarchy.

    Then your second hierarchy consists of c(1,1,3) but Industry is always constant, leaving you with just Sub-Industry and Product changing.

    In summary, this does not look like a hierarchical series. I noticed that the labels in gy3 have many NA's

    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"
    

    Tracing through the code, this seems to cause an issue with aggts

    aggts(gy3)
    Error in `colnames<-`(`*tmp*`, value = unlist(labels[levels])) : 
    length of 'dimnames' [2] not equal to array extent
    

    aggts is called when you specify a comb method, which requires a hierarchical series. bu does not fail, because it is bottoms-up, which does not require a hierarchy.

    Long-story short, one way to correct your code would be to specify a vector of c(6, 3) encompassing the sub-industry in one group and product in the other.

    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
    

    This works too

    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