简体   繁体   English

建立滚动平均预测

[英]Building a rolling mean forecast

I have a series of intermittent demand called parts (sample below) and I want to develop a rolling mean forecast of a training set and a test set.我有一系列称为部分的间歇性需求(下面的示例),我想开发一个训练集和测试集的滚动平均预测。 My code is below as well.我的代码也在下面。 the series fitmean calculates the rolling mean, but there are two problems:系列 fitmean 计算滚动平均值,但有两个问题:

  1. It adds a 13th element when all I really want is to get a rolling mean of 12;当我真正想要的是滚动平均值为 12 时,它添加了第 13 个元素; and,和,
  2. The dates go from Jun 2016 to Jun 2017, so when I subtract testparts I only get the 6 values for Jan to Jun 2017.日期从 2016 年 6 月到 2017 年 6 月,所以当我减去测试部分时,我只得到 2017 年 1 月到 6 月的 6 个值。

Is there a way to (1) remove the 13th element at the end of fitmean, and (2) change the dates so they match with testparts?有没有办法(1)删除 fitmean 末尾的第 13 个元素,以及(2)更改日期以便它们与测试部分匹配?

Thank you.谢谢你。

library(forecast,zoo)
parts<-matrix(c(0,0,0,0,0,0,2,0,0,0,0,0,3,0,0,0,0,0,1,0,0,7,0,0),nrow=24,ncol=1)
parts<-ts(parts,f=12,start=c(2016,1))
maemean<-matrix(NA,nrow=12,ncol=1)
  trainparts<-window(parts,end=c(2016,12))
  testparts<-window(parts,start=c(2017,1),end=c(2017,12))
  fitmean<-round(rollapply(parts, width=12, by = 1, FUN = mean))
  maemean<-abs(fitmean-testparts)

Jan-16  0
Feb-16  0
Mar-16  0
Apr-16  0
May-16  0
Jun-16  0
Jul-16  2
Aug-16  0
Sep-16  0
Oct-16  0
Nov-16  0
Dec-16  0
Jan-17  3
Feb-17  0
Mar-17  0
Apr-17  0
May-17  0
Jun-17  0
Jul-17  1
Aug-17  0
Sep-17  0
Oct-17  7
Nov-17  0
Dec-17  0

Clarification:澄清:

The above list should break down to a training set from Jan-16 to Dec-16 and a test set from Jan-17 to Dec-17.上面的列表应该分解为从 1 月 16 日到 12 月 16 日的训练集和从 1 月 17 日到 12 月 17 日的测试集。 What I want to do is use a rolling mean so the average of Jan-16 to Dec-16 (rounded, which is 0) becomes the forecast for Jan-17, and so on, ie, Feb-16 to Jan-17, etc. The output should look like this我想要做的是使用滚动平均值,因此 1 月 16 日到 12 月 16 日的平均值(四舍五入,即 0)成为 1 月 17 日的预测,依此类推,即 2 月 16 日到 1 月 17 日,等输出应该是这样的

Jan-17  0
Feb-17  0
Mar-17  0
Apr-17  0
May-17  0
Jun-17  0
Jul-17  0
Aug-17  0
Sep-17  0
Oct-17  0
Nov-17  1
Dec-17  1

Unfortunately, I get this with 13 vice 12 elements.不幸的是,我得到了 13 个副 12 个元素。

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2016                       0   0   0   0   0   0   0
2017   0   0   0   1   1   1                        

1) width = list(...) Removing all the irrelevant code from the question and changing the rollapply line we have this where -seq(12) is a vector of offsets instructing rollapply to pass the first prior, second prior, ... twelfth prior values to mean at each point. 1) width = list(...)从问题中删除所有不相关的代码并更改rollapply行,其中-seq(12)是偏移向量,指示rollapply通过第一个优先级,第二个优先级,.. . 第十二个先验值在每个点上的意思。

library(zoo)

# test data
parts <- matrix(c(0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1, 0, 0, 7, 0, 0), 
  nrow = 24, ncol = 1)
parts <- ts(parts, freq = 12, start = c(2016, 1))

round(rollapply(parts, list(-seq(12)), FUN = mean))

giving:给予:

     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2017   0   0   0   0   0   0   0   0   0   0   1   1

2) rollsumr Another approach would be to take a rolling sum of width 13 and then subtract off the current value and divide by 12: 2) rollsumr另一种方法是取宽度为 13 的滚动总和,然后减去当前值并除以 12:

round((rollsumr(parts, 13) - parts) / 12)
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 2017   0   0   0   0   0   0   0   0   0   0   1   1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM