Search code examples
rtime-seriesforecasting

Use Croston model with R hts package


I think this is similar post to this post passing forecasting method to hts

The example given there is for MAPE package. Is it possible to use the croston function in the forecast package in a similar way?

I simply tried

all_ts <- aggts(bts)
  allf <- matrix(NA, nrow = 3, ncol = ncol(all_ts))
  for(i in 1:ncol(all_ts)){
       allf[,i] <- croston(all_ts[,i],h = 3)  
  }

But that gives me an error saying number of items to replace is not a multiple of replacement length.


Solution

  • Look at the structure of the object returned by croston. It is not a simple vector. The following code is an example that works.

    library(hts)
    nodes <- list(2, c(3, 2))
    abc <- ts(5 + matrix(sort(rnorm(500)), ncol = 5, nrow = 100))
    bts <- hts(abc, nodes)
    all_ts <- aggts(bts)
    allf <- matrix(NA, nrow = 3, ncol = ncol(all_ts))
    for(i in 1:ncol(all_ts)){
      allf[,i] <- croston(all_ts[,i],h = 3)$mean
    }
    y.f <- combinef(allf, bts$nodes)