使用 auto.arima 在 R 中预测多个时间序列

mes*_*i80 5 r time-series

我的数据集有以下 3 列:

date client_id sales
01/01/2012 client 1 $1000
02/01/2012 client 1 $900
...
...
12/01/2014 client 1 $1000
01/01/2012 client 2 $300
02/01/2012 client 2 $450
...
..
12/01/2014 client 2 $375
Run Code Online (Sandbox Code Playgroud)

等等其他 98 个客户(每个客户每月 24 个数据点)

我有多个客户(大约 100 个)...每个客户的数据采用时间序列格式(每月 24 个数据点)

如何使用 R 中的 auto.arima 自动预测所有 100 个客户的销售额?有 by 语句选项吗?还是我必须使用循环?

谢谢

J.R*_*.R. 5

您可以随时使用lapply()

lapply(tsMat, function(x) forecast(auto.arima(x)))
Run Code Online (Sandbox Code Playgroud)

一个小例子如下:

library(forecast)
#generate some time-series:
sales <- replicate(100, 
    arima.sim(n = 24, list(ar = c(0.8), ma = c(-0.2)), sd = sqrt(0.1)) 
)
dates <- seq(as.Date("2012/1/1"), by = "month", length.out=24)
df <- data.frame(date=rep(dates,100), client_id=rep(1:100,each=24), sales=c(sales))
#reshape and convert it to a proper time-series format like ts:
tsMat <- ts(reshape2::dcast(df, date~client_id), start=2012, freq=12)
#forecast by auto.arima:
output <- lapply(tsMat, function(x) forecast(auto.arima(x)))
Run Code Online (Sandbox Code Playgroud)