Search code examples
rforecasting

R time series forecast is always the same


I have a lot of time series and i want a forecast for every single one for ten months. For some of them it works, for the most i always get the same forecast for every month. The time series consists monthly data. For example:

> ts(Menge[Nummer==8 & Jahr>2014 & Index<61 ], frequency=12)
  Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1   6 225   0 114  21  25   5 256   1   6   1   8
2  13  35 180 215  20  48  20  31 283 130   3   1
3  53  31   0 142  60  76  10  28 298  29   5  14

The output of dput is:

dput(Menge[Nummer==8 & Jahr>2014 & Index<61 ])
c(6, 225, 0, 114, 21, 25, 5, 256, 1, 6, 1, 8, 13, 35, 180, 215, 
20, 48, 20, 31, 283, 130, 3, 1, 53, 31, 0, 142, 60, 76, 10, 28, 
298, 29, 5, 14)

When i decompose the time series i get seasonality and a trend:

> decompose(ts(Menge[Nummer==8 & Jahr>2014 & Index<61 ], frequency=12))
$x
  Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1   6 225   0 114  21  25   5 256   1   6   1   8
2  13  35 180 215  20  48  20  31 283 130   3   1
3  53  31   0 142  60  76  10  28 298  29   5  14

$seasonal
         Jan        Feb        Mar        Apr        May        Jun        Jul        Aug        Sep        Oct
1 -35.142361 -30.496528  25.065972 106.899306 -32.163194 -10.371528 -57.725694  76.336806  78.878472   4.295139
2 -35.142361 -30.496528  25.065972 106.899306 -32.163194 -10.371528 -57.725694  76.336806  78.878472   4.295139
3 -35.142361 -30.496528  25.065972 106.899306 -32.163194 -10.371528 -57.725694  76.336806  78.878472   4.295139
         Nov        Dec
1 -63.100694 -62.475694
2 -63.100694 -62.475694
3 -63.100694 -62.475694

$trend
       Jan      Feb      Mar      Apr      May      Jun      Jul      Aug      Sep      Oct      Nov      Dec
1       NA       NA       NA       NA       NA       NA 55.95833 48.33333 47.91667 59.62500 63.79167 64.70833
2 66.29167 57.54167 59.91667 76.83333 82.08333 81.87500 83.25000 84.75000 77.08333 66.54167 65.16667 68.00000
3 68.75000 68.20833 68.70833 65.12500 61.00000 61.62500       NA       NA       NA       NA       NA       NA

$random
           Jan          Feb          Mar          Apr          May          Jun          Jul          Aug
1           NA           NA           NA           NA           NA           NA    6.7673611  131.3298611
2  -18.1493056    7.9548611   95.0173611   31.2673611  -29.9201389  -23.5034722   -5.5243056 -130.0868056
3   19.3923611   -6.7118056  -93.7743056  -30.0243056   31.1631944   24.7465278           NA           NA
           Sep          Oct          Nov          Dec
1 -125.7951389  -57.9201389    0.3090278    5.7673611
2  127.0381944   59.1631944    0.9340278   -4.5243056
3           NA           NA           NA           NA

$figure
 [1] -35.142361 -30.496528  25.065972 106.899306 -32.163194 -10.371528 -57.725694  76.336806  78.878472   4.295139
[11] -63.100694 -62.475694

$type
[1] "additive"

attr(,"class")
[1] "decomposed.ts"

But the forecast is always the same:

     Point Forecast     Lo 80   Hi 80     Lo 95    Hi 95
Jan 4       68.41899 -47.17701 184.015 -108.3698 245.2078
Feb 4       68.41899 -47.17701 184.015 -108.3698 245.2078
Mar 4       68.41899 -47.17701 184.015 -108.3698 245.2078
Apr 4       68.41899 -47.17701 184.015 -108.3698 245.2078
May 4       68.41899 -47.17701 184.015 -108.3698 245.2078
Jun 4       68.41899 -47.17701 184.015 -108.3698 245.2078
Jul 4       68.41899 -47.17701 184.015 -108.3698 245.2078
Aug 4       68.41899 -47.17701 184.015 -108.3698 245.2078
Sep 4       68.41899 -47.17701 184.015 -108.3698 245.2078
Oct 4       68.41899 -47.17701 184.015 -108.3698 245.2078

The data are three years (2015-2017). The forecast should be for the first ten months of 2018 (so that i can prove how good is the fitting in reality).

I did about 1000 forecasts (by changing "number" i get another time series) and very often i just got the same values, sometimes the point forecast is the same, but the Lo and Hi values change a little bit, in some cases i get different values for every month.

I observed the data in some cases but can't find a reason, why in some cases the forecasts are the same and in other cases not. Especially because i get saisonality and trends by decomposing the time series.

The whole code is:

  setwd("Z:/Bestellvorschlag/Lagerdrehung") #workspace festlegen
    x= read.csv("Daten Aufbereitet.csv", header=TRUE, sep=";")  #read the data
    attach(x)
    library(forecast)
    Zeilenanzahl<-length(x[,1]) #number of rows
    AnzahlArtikel<-x[Zeilenanzahl,1] #number of articles
    ForecastMatrix<-matrix(0,9*AnzahlArtikel,8) #i want nine forecasts for every article
    #with the columns Nummer, Monat,Forecast, lower80, lower 95, upper 80, upper 95, Menge



    for (i in 1:AnzahlArtikel) { #do it for all numbers; each number is another product

    #extract mean(point forecast), lower und upper bounds
    TS<- ts(Menge[Nummer==i & Jahr>2014 & Index<61 ], frequency=12)
    mean<-unlist(forecast(TS,9)[2])
    upper<-unlist(forecast(TS,9)[5])
    lower<-unlist(forecast(TS,9)[6])

    #write the data in a matrix
    for (j in 1:9) {
      ForecastMatrix[9*(i-1)+j,1]<-i
      ForecastMatrix[9*(i-1)+j,2]<-j
      ForecastMatrix[9*(i-1)+j,3]<-mean[j]
      ForecastMatrix[9*(i-1)+j,4]<-lower[j]
      ForecastMatrix[9*(i-1)+j,5]<-lower[9+j]
      ForecastMatrix[9*(i-1)+j,6]<-upper[j]
      ForecastMatrix[9*(i-1)+j,7]<-upper[9+j]
      ForecastMatrix[9*(i-1)+j,8]<-Menge[Nummer==i & Jahr==2018 & Monat==j] #the real value
    }
    }
#write the data in a .csv
write.table(ForecastMatrix, file = "Forecastmatrix.csv", sep= ";")

Solution

  • you are getting same forecast value because your data is white noise (no trend, season and cycle), if the model find such data it will simply take avg and print same result for all forecast, therefore you are getting same forecast.

    By the way i think you didn't create model ex. arima. You should first build model then forecast.