R时间序列预测总是一样的

R time series forecast is always the same

我有很多时间序列,我想要对每个时间序列进行十个月的预测。对于其中一些它有效,对于大多数人来说,我每个月总是得到相同的预测。 时间序列包含月度数据。例如:

> 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年前十个月的(这样我就可以证明现实中的拟合度有多好)。

我做了大约 1000 次预测(通过更改 "number" 我得到了另一个时间序列)并且我经常得到相同的值,有时点预测是相同的,但是 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= ";")

你得到相同的预测值,因为你的数据是白噪声(没有趋势、季节和周期),如果模型找到这样的数据,它会简单地取平均值并为所有预测打印相同的结果,因此你得到相同的结果预测。

顺便说一句,我认为你没有创建模型 ex。华宇。 你应该先建立模型然后预测。