R时间序列的预测始终相同

时间:2018-11-19 10:42:42

标签: r forecasting

我有很多时间序列,我希望每个月都有一个为期10个月的预测。对于他们中的某些人来说,它是有效的,大多数情况下,我总是每个月都得到相同的预测。 时间序列包含每月数据。例如:

> 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

dput的输出为:

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)

分解时间序列时,我会得到季节性和趋势:

> 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"

但是预测总是相同的:

     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

数据为三年(2015年至2017年)。预测应该是在2018年的前10个月(这样我才能证明现实中的拟合有多好)。

我做了大约1000次预测(通过更改“数字”,我得到了另一个时间序列),并且我经常得到相同的值,有时点的预测是相同的,但是Lo和Hi值有一些变化,在某些情况下,我每个月的价值会有所不同。

我在某些情况下观察到了数据,但找不到原因,为什么在某些情况下预测是相同的,而在其他情况下则不是。尤其是因为我通过分解时间序列获得了理性和趋​​势。

整个代码是:

  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= ";")

1 个答案:

答案 0 :(得分:1)

由于数据是白噪声(没有趋势,季节和周期),因此您将获得相同的预测值,如果模型找到此类数据,它将简单地求平均值并为所有预测打印相同的结果,因此您将获得相同的预测。

通过这种方式,我认为您没有创建ex模型。有马 您应该先构建模型,然后进行预测。