A blog by Rob J Hyndman 

Twitter Gplus RSS

Forecasting annual totals from monthly data

Published on 15 May 2013

This ques­tion was posed on cross​val​i​dated​.com:

I have a monthly time series (for 2009–2012 non-​​stationary, with sea­son­al­ity). I can use ARIMA (or ETS) to obtain point and inter­val fore­casts for each month of 2013, but I am inter­ested in fore­cast­ing the total for the whole year, includ­ing pre­dic­tion inter­vals. Is there an easy way in R to obtain inter­val fore­casts for the total for 2013?

I’ve come across this prob­lem before in my con­sult­ing work, although I don’t think I’ve ever pub­lished my solu­tion. So here it is.

If x is your monthly time series, then you can con­struct annual totals as follows.

y <- filter(x,rep(1,12), sides=1) # Total of last 12 months

To get the fore­casts of the annual totals:

fit <- auto.arima(y)

The last fore­cast is for the total of the next year.

Note that diff(y,lag=1) is the same as diff(x,lag=12). So, pro­vided D>1, if an ARIMA(p,d,q)(P,D,Q)12 is appro­pri­ate for the x series, then an ARIMA(p,d+1,q)(P,D-1,Q)12 is appro­pri­ate for the y series. How­ever, auto.arima may not choose the equiv­a­lent mod­els because the fil­ter­ing and dif­fer­enc­ing will lead to dif­fer­ent num­bers of obser­va­tions. To take advan­tage of this result, and use all the avail­able data as effi­ciently as pos­si­ble, the fol­low­ing code is bet­ter, assum­ing d=D=1 is appro­pri­ate for x:

fit <- auto.arima(x,d=1,D=1)
fit$arma[c(6,7)] <- c(2,0)
fit <- Arima(y,model=fit)

Related Posts:

12 Comments  comments 
  • Richard War­nung

    Thanks for post­ing this solu­tion. If we replace x by a data set from the fpp pack­age then this could by fancy, right? If I want to use this as an exer­cise, would you rec­om­mend some other data set?

    • http://robjhyndman.com/ Rob J Hyndman

      Yes, you could do this with the fancy data from the fpp pack­age. How­ever, you would need to model the log­a­rithms, not the raw data.

  • Anthony

    Pro­fes­sor Hyn­d­man,
    How can we plot MAPE or MASE vs dif­fer­ent lead times?

    • http://robjhyndman.com/ Rob J Hyndman

      plot(1:h, mape) for exam­ple. You will need to com­pute the mape vec­tor yourself.

      • Anthony

        Thanks. I will find the val­ues of MAPE for dif­fer­ent h using accu­racy() and con­struct a vec­tor.

  • Fraj

    Pro­fes­sor Hyndman,

    First of all, I must thank you very much for your valu­able work.

    I have 3000 monthly time series (sales rev­enue by agent) each with 30 obser­va­tions, each series has dif­fer­ent behaviour

    I am loop­ing through all of them using ets to get a 6 months fore­cast but some­times the fore­cast is just lin­ear while the series is very random!!

    the ques­tion is:
    1. how to choose between ets and auto.arima in a loop? Can I loop and com­pare BIC and/​or AIC and choose the fit with the low­est BIC/​AIC?
    2. why some­times the fore­cast is just linear?

    • http://robjhyndman.com/ Rob J Hyndman

      1. You can’t com­pare AIC/​BIC between ets and auto.arima. See http://​rob​jhyn​d​man​.com/​h​y​n​d​s​i​g​h​t​/aic/ for details.

      2. The point fore­casts are means of future dis­tri­b­u­tions. It is com­mon for the means to be lin­ear or flat. See http://​rob​jhyn​d​man​.com/​h​y​n​d​s​i​g​h​t​/​f​l​a​t​-​f​o​r​e​c​asts/

      • Fraj

        thanks a lot for your prompt reply, really appre­ci­ated.
        the ts are very dif­fer­ent, some of them has only trend, or only sea­son­al­ity and some has trend & sea­son­al­ity or even it can be ran­dom walk.…
        any suggestion/​advice on how to approch this in a batch process?


        • http://robjhyndman.com/ Rob J Hyndman

          I would choose either ets or auto.arima and stick with that for all series. I’ve had bet­ter results with ets when applied to large num­bers of time series of sales data.

          • Fraj

            thanks again, I’ll give it a try with either func­tion.
            if not well work­ing, can I share 10 series (csv, matrix 30X10) with you for testing?

            Appre­ci­ated, thanks

          • Fraj Lazreg

            Prof. Hyn­d­man,

            I have a very high level in both ts fore­cast­ing and pro­gram­ming. am try­ing to make good use out of your R fore­cast­ing pack­age using cou­ple of thous­hand of ts (with/​without trend and/​or with/​without seasonality).

            i have used dummy data to test a batch script with ets and auto.arima but like explained above some fore­cast are just lin­ear (avg of future dis­tru­bu­tions like you mentioned)

            my ques­tions are:

            1. why ets and auto.arima are giv­ing lin­ear fore­cast while HW(add) and HW(mul) are not?

            2. how to avoid get­ting lin­ear forecast?

            appre­ci­ate if you could have a look at this ts:


            mts[,1]=c(t,fetsmean) mts[,2]=c(t,farimamean)
            mts[,3]=c(t,fhwamean) mts[,4]=c(t,fhwmmean)

            best regards & thanks a lot

          • http://robjhyndman.com/ Rob J Hyndman