Search code examples
rforecasting

Trying to auto.arima forecast volume of multiple time series


I am trying to forecast multiple timeseries using auto.arima with the output of units in a dataframe.

I have this sample data set:

Name <- c(rep("Tom",24),
  rep("Jake",12),
  rep("Jill",12))
Product <- c(rep("Orange",12),rep("Apples",12),
     rep("Orange",12),rep("Apples",12))
Quarter <- c(rep(c("2013-03-31","2013-06-30","2013-09-30","2013-12-31",
     "2014-03-31","2014-06-30","2014-09-30","2014-12-31",
     "2015-03-31","2015-06-30","2015-09-30","2015-12-31"),4))
Units <- c(1200,1500,1800,2600,1300,1500,1700,2600,1300,1700,1900,2700,300,400,800,900,
     250,400,600,700,200,300,0,0,100,250,500,600,90,200,400,450,50,0,0,0,1200,0,0,0,
     1500,1700,1800,2600,1600,1900,2000,3000)
data <- cbind(Name,Product,Quarter,Units)
data <- as.data.frame(data)
data$Name_Product <- paste(data$Name,data$Product)
data <- select(data,Quarter,Units,Name_Product)
data <- spread(data,Name_Product,Units)
data2 <- ts(data[,-1], frequency=4, start=2013)

I am trying to forecast the batch data above by Name and Product using the following script:

h <- 12
ns <- ncol(data)

arimaforecast <- matrix(NA, nrow=h, ncol=ns)
for (i in 1:ns){
   arimaforecast[,i] <- forecast(auto.arima(data2[,i]), h=h,)$mean}

My problem is that I keep getting the error "Error in '[.default'(data2, , i) : subscript out of bounds" and am unable to create a forecast similar to using the forecast() function from Rob Hyndman's batch forecasting approach but using the auto.arima function

I'm not sure where to go from here or if I am using the correct method. Any help is appreciated, thanks!


Solution

  • Change this line of your code:

    ns <- ncol(data)
    

    to:

    ns <- ncol(data2)
    

    Your loop was cycling through the 5 columns in your original data.frame instead of the four from your new time series matrix.