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.
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:
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:
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: