Passing different forecasting method to hierarchical time series forecast in R?

守給你的承諾、 提交于 2019-12-05 14:04:37
user1569317

I'm posting because after a closer look at the hts documentation (insert well-deserved RTFM here), I think I found a work-around using the combinef() function from hts, which can be used to optimally combine forecasts outside of the forecast.gts() environment. I'll leave it up for a while before accepting as an answer so others can tell me if I'm wrong.

fh <- 8

library(hts)
library(MAPA)
set.seed(1)

x <- ts(rpois(365, lambda=0.05), frequency=365, start=2014)
y <- ts(rpois(365, lambda=0.07), frequency=365, start=2014)

my_hts <- hts(data.frame(x,y))

ally <- aggts(my_hts)

allf <- matrix(NA, nrow = fh, ncol = ncol(ally))

for(i in 1:ncol(ally)){
    allf[,i] <- mapafor(ally[,i], 
                        mapaest(ally[,i], outplot=0), 
                        fh = fh, 
                        outplot=0)$outfor
}

allf <- ts(allf)

y.f <- combinef(allf, my_hts$nodes, weights=NULL, keep="bottom")

#here's what the non-reconciled, bottom-level MAPA forecasts look like
print(allf[1,1:2])

 Series 1   Series 2
1 0.1343304 0.06032574

#here's what the reconciled MAPA bottom-level forecasts look like
#notice that they're different
print(y.f[1,])

[1] 0.06030926 0.07402938

"Is there a clever way to pass different forecasting methods to forecast.gts() without having to tear up the code?" - Almost certainly not. forecast.gts() does not allow you to plug in forecast methods along the lines of the family parameter to glm() and similar.

It will likely be easiest to run the MAPA forecasts and then reimplement the optimal combination yourself. It's really not all that hard, I did it myself a few times before the hts package came along. Look at the references in the hts package; most of them are available as preprints on Rob Hyndman's website.

One key problem in combining the optimal combination approach with intermittent demand forecasts is that the optimal combination may well yield negative forecasts. These may be "optimal" in the GLS sense used here, but they make no sense for demands. My solution was to use pcls() in the mgcv package for the actual reconciliation, in order to constrain the solution (i.e., the bottom level reconciled forecasts) to be nonnegative.

I have run successfully in the past small experiments using MAPA forecasts and combinef. Stephan's point about the negative forecasts remains an issue. You might be interested to look into iMAPA in the tsintermittent package that is specifically designed to produce forecasts for intermittent demand time series. In contrast to MAPA that one implements Croston's method, SBA and exponential smoothing to choose from in the various temporal aggregation levels.

I would be very interested in learning more about your results. Perhaps a relevant research was presented in last year's ISF by E. Spiliotis et al. Examining the effect of temporal aggregation on forecasting accuracy for hierarchical energy consumption time series.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!