I have a dataset where sales values of multiple products are available. Some products have data starting from Jan 2014, but some products have data starting later than that and its varying for various products. I have tried to convert the series into a pivot table with each product Id as the colummn name and then converted the dataframe to a TS startig from Jan 2014. But, the products which start later than Jan 14, for example July 14 have the first 6 values as NA. The forecast code which uses a for loop and forecast function from the forecast package is unable to forecast all the product's sales because of NA and I get NaN or 0s as the output for these products.

My questions:

  1. Is there some other method which can be used in my case? I am thinking of dealing with individual product's data seperately and convert it to a TS based on its start date. But I am not sure how to do that.

Sample code:

df <- data.frame(Product_Id = rep(1:100, each = 50), 
                     Date = seq(from = as.Date("2014/1/1"), to = as.Date("2018/2/1") , by = "month"), 
                     Sales = rnorm(100, mean = 50, sd= 20))

df <- df[-c(251:256, 301:312, 2551:2562, 2651:2662, 2751:2762) ,]

df_new2 <- df %>% select(Product_Id, Date, Sales) %>% spread(Product_Id, Sales)
#Convert to a time series
df_new2 <- ts(df_new2, start=c(2014,1), frequency =12)

#loop to perform a forecast of all the contracts together
fcast2 <- matrix(NA,nrow=h,ncol=ns)
for(i in 1:ncol(df_new2)){
  fcast2[,i] <- forecast(df_new2[,i], h=12, robust = TRUE, lambda = "auto", biasadj = TRUE)$mean

fcast2 is unable to produce the output of all the products. The language I am using is R.

Any idea or suggestion would be appreciated.

  • $\begingroup$ I don't see why would missing values within the first few entries cause problems. I tried with dummy data with missing values, as expected it throw a warning message : Missing values encountered. Using longest contiguous portion of time series , but eventually it worked $\endgroup$ – DS_UNI Mar 7 at 14:08
  • $\begingroup$ can you share the Data? or the code for that matter? $\endgroup$ – DS_UNI Mar 7 at 14:09
  • $\begingroup$ Hey, edited the question for further clarification $\endgroup$ – user10579790 Mar 7 at 14:15
  • $\begingroup$ I think the reason for NA or 0s can be that I am converting the dataframe to a Timeseries using ts. The start date for all the products is taken as Jan 2014. So for the one's where start date should be July 14, it might create a bug. I am not sure though how to create seperate time series for different variables. $\endgroup$ – user10579790 Mar 7 at 14:27
  • $\begingroup$ It's hard to say without seeing basedata and df, can you maybe share the output of dput(basedata) and dput(df)? $\endgroup$ – DS_UNI Mar 7 at 14:36

The sample data also worked with me, however try running this to see where in the loop the problem is happening, this way if you encounter any errors, R throws the error and the loop keeps going

# the loop
ns <- ncol(df_new2)
h <- 12
fcast2 <- matrix(NA,nrow=h,ncol=ns)

for(i in 1:ncol(df_new2)){
  fcast2[,i] <- try(forecast(df_new2[,i], h=12, robust = TRUE, lambda = "auto", biasadj = TRUE)$mean)
  • $\begingroup$ Hey, I have added a sample code to explain the problem $\endgroup$ – user10579790 Mar 8 at 11:10
  • $\begingroup$ are you getting any error messages? $\endgroup$ – DS_UNI Mar 8 at 13:10
  • $\begingroup$ please see the edited answer again. $\endgroup$ – DS_UNI Mar 8 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.