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:
- It adds a 13th element when all I really want is to get a rolling mean of 12; and,
- 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.
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