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:
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?
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. 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, i.e., Feb-16 to Jan-17, etc. The output should look like this
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.
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.
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:
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