Search code examples
rtime-seriesforecastingintermittent

Croston method in R vs. Croston by hand


I am having trouble relating how forecasts are calculated in the R packages forecast::croston and tsintermittent::crost. I understand the concept of croston, such as in the example posted here (www.robjhyndman.com/papers/MASE.xls), but the output from the R packages produces very different results.

I used the values from the Excel example (by R. Hyndman) in the following code:

library (tsintermittent)
library (forecast)
x=c(0,1,0,11,0,0,0,0,2,0,6,3,0,0,0,0,0,7,0,0,0,0) # from Hyndman Excel example
x_crost = crost(x,h=5, w=0.1, init = c(1,1) ) # from the tsintermittent package
x_croston=croston(x,h=5, alpha = 0.1) # from the forecast package
x_croston$fitted
y=data.frame(x,x_crost$frc.in,x_croston$fitted)
y
plot(x_croston)
lines(x_croston$fitted, col="blue")
lines(x_crost$frc.in,col="red")
x_crost$initial
x_crost$frc.out # forecast
x_croston$mean # forecast

The forecast from the Excel example is 1.36, crost gives 1.58 and croston gives 1.15. Why are they not the same? Also note that the in-sample (fitted) values are very different.


Solution

  • For crost in the tsintermittent package you need a second flag to not optimise the initial values: init.opt=FALSE, so the command should be:

    crost(x,w=0.1,init=c(2,2),init.opt=FALSE)
    

    Setting only init=c(2,2) will only set the initial values for the optimiser to work from. Also note that the time series that Rob Hyndman has in his example has two additional values in the beggining (see column B), so x should be:

    x=c(0,2,0,1,0,11,0,0,0,0,2,0,6,3,0,0,0,0,0,7,0,0,0,0)
    

    Running these two commands produces the same values as in the excel example.