Forecast using auto.arima with help of dplyr groupby function

2k views Asked by At

I need to forecast the demand of some products (10 products) in 100 stores for 150 days. In this I need to groupby PRODUCT and STORE, and fit a arima model and forecast it. Also some products may have less stores. I need to use auto.arima as there are 10000 subsets. I have written a code which computes fit but am not able to forecast it.

data <- read.csv("data.csv")
dat <- data.frame(data)
library(dplyr)
library(forecast)
model_fit <- group_by(dat, PRODUCT,STORE) %>% do({fit=auto.arima(.$DEMAND)})

Till here the code works fine with some warnings like "Unable to fit final model using maximum likelihood. AIC value approximated". I hope its ok, pls let me know if not and why.

Now I need to forecast it into a column Forecast I am new to R, so by online material I felt this would work.

dat[,"Forecast"] <- NULL
model_fit <- group_by(dat, PRODUCT,STORE) %>% do({fit=auto.arima(.$DEMAND) Forecast = forecast(fit)})
write.csv(dat,"Forecast.csv",row.names = FALSE)

This part is not working. Please let me know the problem of this code. Thanks.

1

There are 1 answers

2
Eric On

FYI, you'll get more/better/faster answers if you state a simple, reproducible example (I don't have access to data.csv, so I can't run what you have exactly).

Here's some example input that I think captures the main idea of your problem:

> df <- data_frame(g = c(1, 1, 1, 1, 2, 2, 2, 2), v = c(1, 2, 3, 4, 1, 4, 9, 16))
> df
Source: local data frame [8 x 2]

  g  v
1 1  1
2 1  2
3 1  3
4 1  4
5 2  1
6 2  4
7 2  9
8 2 16

It also helps if you state exactly what error message you're getting. My guess is that you're getting something along the lines of "results are not data frames", like I do here:

> df %>% group_by(g) %>% do(forecast(auto.arima(.$v), h=3))
Error: Results are not data frames at positions: 1, 2

I believe your problem is that you're not returning a data frame within the do() statement, and maybe you also want to return the $mean value.

In the example I gave, to create a forecast for each group g, you can do the following:

> df %>% group_by(g) %>% do(data.frame(v_hat = forecast(auto.arima(.$v), h=3)$mean))
Source: local data frame [6 x 2]
Groups: g

  g  v_hat
1 1  6
2 1  7
3 1  8
4 2 31
5 2 37
6 2 43