简体   繁体   English

借助dplyr groupby函数使用auto.arima进行预测

[英]Forecast using auto.arima with help of dplyr groupby function

I need to forecast the demand of some products (10 products) in 100 stores for 150 days. 我需要预测150天中100家商店中某些产品(10种产品)的需求。 In this I need to groupby PRODUCT and STORE, and fit a arima model and forecast it. 在这种情况下,我需要对PRODUCT和STORE进行分组,并拟合Arima模型并对其进行预测。 Also some products may have less stores. 另外,某些产品的商店可能更少。 I need to use auto.arima as there are 10000 subsets. 我需要使用auto.arima,因为有10000个子集。 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". 直到此处,该代码还可以通过一些警告(例如“无法使用最大似然来拟合最终模型。AIC值近似”)正常运行。 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. 现在,我需要将其预测到“我是R的新手”一栏中,因此通过在线材料,我认为这是可行的。

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). 仅供参考,如果您声明一个简单的,可复制的示例,您将获得更多/更好/更快的答案(我没有访问data.csv的权限,因此我无法运行您拥有的功能)。

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. 我相信您的问题是您没有在do()语句中返回数据帧,也许您还想返回$ mean值。

In the example I gave, to create a forecast for each group g, you can do the following: 在我给出的示例中,要为每个组g创建一个预测,您可以执行以下操作:

> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM