Search code examples
rforecasting

How to get x-points of forecast results in R?


I've used r to get some forecast result.

library(forecast)
fc<-forecast(fit.ets)
fc

I got a result like this

Points Forecast Lo 80 Hi 80 Lo 95 Hi 95
19.5,  1.8895   xxx    xxx   xxx  xxx
20.0    xxxx    xxx    xxx   xxx  xxx
...

I want to get the Points Column to plot my data, how can I get this column?


Solution

  • You haven't copied and pasted correctly. The output is like this:

         Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
    19.5         1.8895   xxx   xxx   xxx   xxx
    20.0           xxxx   xxx   xxx   xxx   xxx
    ...
    

    The left hand column gives the time periods. The second column gives the "Point Forecast" -- that is, the estimated mean or median of each future observation.

    If you just want the future times, you can get them using the time() function:

    time(fc$mean)
    

    Contrary to what you say in the comments, ets() does not change the x-axis scale, it simply fits a model. When you pass that model to forecast.ets(), the resulting times are a continuation of the sequence of time periods from the data that you provide.

    For example:

    > USAccDeaths
           Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
    1973  9007  8106  8928  9137 10017 10826 11317 10744  9713  9938  9161  8927
    1974  7750  6981  8038  8422  8714  9512 10120  9823  8743  9129  8710  8680
    1975  8162  7306  8124  7870  9387  9556 10093  9620  8285  8466  8160  8034
    1976  7717  7461  7767  7925  8623  8945 10078  9179  8037  8488  7874  8647
    1977  7792  6957  7726  8106  8890  9299 10625  9302  8314  8850  8265  8796
    1978  7836  6892  7791  8192  9115  9434 10484  9827  9110  9070  8633  9240
    
    
    > library(forecast)
    > ets.fit <- ets(USAccDeaths)
    > fc <- forecast(ets.fit)
    > fc
             Point Forecast   Lo 80   Hi 80  Lo 95   Hi 95
    Jan 1979         8252.1  7945.0  8559.2 7782.4  8721.8
    Feb 1979         7473.7  7162.8  7784.6 6998.3  7949.2
    Mar 1979         8281.0  7903.8  8658.3 7704.0  8858.0
    Apr 1979         8527.1  8107.5  8946.6 7885.4  9168.7
    ....    
    Nov 1980         8794.6  7960.6  9628.6 7519.1 10070.1
    Dec 1980         9072.3  8195.5  9949.0 7731.4 10413.2
    
    
    > time(fc$mean)
            Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep
    1979 1979.0 1979.1 1979.2 1979.3 1979.3 1979.4 1979.5 1979.6 1979.7
    1980 1980.0 1980.1 1980.2 1980.3 1980.3 1980.4 1980.5 1980.6 1980.7
            Oct    Nov    Dec
    1979 1979.8 1979.8 1979.9
    1980 1980.8 1980.8 1980.9