将移动平均一次应用于多个时间序列

时间:2019-11-14 13:25:20

标签: r

我有一个数据集,其中包含52个星期内100种产品的需求,我需要预测下一个时期每种产品的需求。我想测试几种方法(例如移动平均线,ARIMA等),并比较预测准确性。 100种产品的每个时间序列都存储在一个称为Order_volume_ts的mts对象中。当我现在使用以下代码运行移动平均线时,它当然会给我一条错误消息

Arima(Order_volume_ts, order=c(0,0,1))
Error in stats::arima(x = x, order = order, seasonal = seasonal, include.mean = include.mean,  : 
  only implemented for univariate time series

我通过使用for()公式将移动平均值应用于每个时间序列并将预测结果存储在单独的数据框中来创建变通方法。即使代码可以工作,我还是觉得必须有一种更简单的方法来做到这一点?

Results <- data.frame("Material_Number"="x","Forecast"=0)  #Creating a dummy data frame to store results
Results$Material_Number <- as.character(Results$Material_Number)
for(K in seq(100)) {
    MA_model <- Arima(Order_volume_ts[,K], order=c(0,0,1)) 
    MA_FC <- forecast(MA_model, h=1)   #Create forecast for next period
    Results[K,1] <- colnames(Order_volume_ts)[K]   #Store name of material in Results data frame
    Results[K,2] <- MA_FC$mean[1]   #Store forecast for next period in Results data frame
  }

以下是两种产品的一些示例数据(尚未使用ts格式):

    Material_A            Material_B
 1:                  0                100
 2:                  5                400
 3:                  1                  0
 4:                  2                  0
 5:                  5                800
 6:                  0                  0
 7:                  1                  0
 8:                  1                100
 9:                  2                300
10:                  9                600
11:                  1                200
12:                  5                  0
13:                  4                  0
14:                  1                  0
15:                  5                400
16:                  3                500
17:                  2                300
18:                  0                600
19:                  2                400
20:                  3                500
21:                  3                100
22:                  1                200
23:                  6                  0
24:                  1                100
25:                  5                  0
26:                  5                200
27:                  0                100
28:                  4                  0
29:                  1                200
30:                  3                  0
31:                  4                200
32:                  2                300
33:                  1                  0
34:                  2                400
35:                  1                  0
36:                  2                  0
37:                  5                500
38:                  1                100
39:                  1                700
40:                  6                  0
41:                  1                  0
42:                 14                100
43:                  0                400
44:                  1                100
45:                  4                  0
46:                  9                100
47:                  5                  0
48:                  7                200
49:                  2                400
50:                  2                  0
51:                  2                  0
52:                  0                  0

ts是使用

创建的
Order_volume_ts <- ts(Order_volume_vector, start=c(2016,1), end=c(2016,52), frequency=52)

0 个答案:

没有答案