Batch forecasting in R returning same data without variation

Hi all,
I am following the methodology provided by this professor here: https://robjhyndman.com/hyndsight/batch-forecasting/

Here, I am trying to get the forecast of multiple products at once, which has mostly similar trend, same features in real world. So, we can use same forecasting method for all the products.
So, I am trying to run a loop and do the same forecast technique for all the 50 products that I have.
Input: 2 years of data, total 104 weeks.
column 1: has the weeks, column 2 onwards the sales qty.

Issue: Code ran without error. But except 2 or 3 products, rest all the cases, the forecasted value for all 13 weeks is literally same. no variation, only constant value, it is definitely not correct.

please review the code and let me know, why the forecasted value is constant every week?Also, how do I add the holtwinters() in the code.

Here is the reprex code:


#set directory
setwd("d:\\Users\\DemandPlanning")

# load required packages
library(readr)
library(ggplot2)
library(forecast)
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#> Registered S3 method overwritten by 'quantmod':
#>   method            from
#>   as.zoo.data.frame zoo
#> Registered S3 methods overwritten by 'forecast':
#>   method             from    
#>   fitted.fracdiff    fracdiff
#>   residuals.fracdiff fracdiff
library(reprex)

Sales <- read.csv("DIY_Top 50_SKU_Sales.csv",header=FALSE)
Sales <- ts(Sales[,-1],f=52)


ns <- ncol(Sales)
h <- 13
fcst <- matrix(NA,nrow=h,ncol=ns)
for(i in 1:ns)
  fcst[,i] <- forecast(Sales[,i],h=h)$mean

write(t(fcst),file="SalesFcstTop50.csv",sep=",",ncol=ncol(fcst))

Created on 2019-11-11 by the reprex package (v0.3.0)

I do not see anything wrong with your code and I was able to run it against some data I invented without any problem. I suggest you plot the individual fits as I did at the end or my code below and see if the constant result looks plausible.

library(forecast)
#> Warning: package 'forecast' was built under R version 3.5.3
set.seed(1)
Sales <- data.frame(W = 1:104, 
                 D1 = 23 + 1:104 * 0.01 + rnorm(104), 
                 D2 = 16 + 1:104 * 0.05 + rnorm(104))

Sales <- ts(Sales[,-1],f=52)


ns <- ncol(Sales)
h <- 13
fcst <- matrix(NA,nrow=h,ncol=ns)
for(i in 1:ns)
  fcst[,i] <- forecast(Sales[,i],h=h)$mean

#Plot one model
i = 1
FIT <- forecast(Sales[,i],h=h)
plot(FIT)


i = 2
FIT <- forecast(Sales[,i],h=h)
plot(FIT)

Created on 2019-11-11 by the reprex package (v0.3.0.9000)

Thanks for your reply. Yes, the code is running as per expectation. I plotted the graph as per code provided by you. Now, my doubt is: Why there is no variation in the forecasted plot?
The forecasted value is same across all the time frame. Does that mean my data is wrong? In my case: the data is correct: It is a real data which has some seasonality as well. Here is the snapshot of the plot. See the forecasted part. Could you suggest any alternative so that I can get a proper forecast with seasonal pattern showing up?

The constant value of the forecast looks reasonable given the data. I have never done forecasting, so I cannot provide useful guidance on doing a more elaborate analysis. There are whole books available on line but I do not want to recommend any particular resource since I have not done this sort of analysis.

Starting a new thread that includes some of your data and that asks about including seasonality in the analysis might attract the attention of the right person. I would say, though, that the seasonality in the plot you posted is not strong enough to be obvious to me.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.