【问题标题】:How can I write a loop for forecasting 100 different series using forecast package auto.arima loop?如何使用预测包 auto.arima 循环编写一个循环来预测 100 个不同的系列?
【发布时间】:2021-01-20 11:39:37
【问题描述】:

我的系列有 3 个不同的列,第一个标识第一个出口的 ID 标签,然后是时间标签,最后是测量值。

我需要为 100 个不同的系列(网点)创建预测。首先我需要对第一个出口的 ID 进行子集化,然后预测 arima 函数,最后为每个出口收集 7 天前的预测。此外,我的模型中还需要每小时、每周、每天的假人。所以我需要 xregs 到 auto.arima 程序。

但是,我无法使用可以为所有 100 个不同 ID 运行的循环创建下面的代码。

df11 <-subset(df10,ID==288)%>%select(Tag,Measure)
sales.xts <- xts(df11[ ,c(-1)],order.by = df11$Tag) 
sales.xts_m<-sales.xts["2020-07-22/2020-10-04"]
dummies<- xts(Seasonaldummies_all[,-1],order.by = Seasonaldummies_all$Tag)
dummies_hd_m<-dummies_hd["2020-07-22/2020-10-04"]
model<-auto.arima(sales.xts_m,xreg=dummies_hd_m, biasadj = TRUE,max.p=7,max.q=7,seasonal=FALSE,test=c("kpss"),lambda = "auto",num.cores=15,stationary = TRUE)

你能告诉我一个通过应用或循环函数快速完成这项工作的方法吗?

【问题讨论】:

  • 这正是 fable 包的设计目的。

标签: r time-series forecasting forecast


【解决方案1】:

如果您想使用forecast 包,您需要将您的数据转换为ts (mts) 对象。要做到这一点,将您的数据从长格式转换为宽格式(根据您在上面发布的图像,我假设您的数据是长格式的)。然后使用ts() 函数创建一个ts() 对象,见下例。

让我们生成一些示例 ts 数据

sales.xts_m <- ts(data.frame(AA = arima.sim(list(order=c(1,0,0), ar=.5), n=100, 
                                       mean = 12), 
                        AB = arima.sim(list(order=c(1,0,0), ar=.5), n=100, 
                                       mean = 12), 
                        AC = arima.sim(list(order=c(1,0,0), ar=.5), n=100, 
                                       mean = 11), 
                        BA = arima.sim(list(order=c(1,0,0), ar=.5), n=100, 
                                       mean = 10), 
                        BB = arima.sim(list(order=c(1,0,0), ar=.5), n=100, 
                                       mean = 14)), start = c(2000, 1), 
          frequency = 12)

nts <- ncol(sales.xts_m) # number of time series

h <- 12 # forecast horizon

示例 xreg

dummies_hd_m <- forecast::seasonaldummy(sales.xts_m[,1])

dummies_hd_m_future <- forecast::seasonaldummy(sales.xts_m[,1], h = h)

mylist <- list()

fc <- matrix(nrow = h, ncol = nts)

如果您需要保留模型--------

模型将在 mylist 中,并在 fc 中为每个 ts 预测点

for (i in 1:nts) {
  mylist[[i]] <- auto.arima(sales.xts_m[,i],xreg=dummies_hd_m, biasadj = TRUE,
                            max.p=7,max.q=7,seasonal= FALSE,test=c("kpss"),
                            lambda = "auto",num.cores=15,stationary = TRUE ) 
  fc[,i] <- forecast(mylist[[i]], h=h, xreg = dummies_hd_m_future)$mean
  
}

#ts names 
colnames(fc) <- colnames(sales.xts_m)

如果您不需要保留模型--------

fc <- matrix(nrow = h, ncol = nts)

for (i in 1:nts) {
  fc[,i] <- forecast(auto.arima(sales.xts_m[,i],xreg=dummies_hd_m, biasadj = TRUE,
                                max.p=7,max.q=7,seasonal=FALSE,test=c("kpss"),
                                lambda = "auto",num.cores=15,stationary = TRUE ), h=h, 
                     xreg = dummies_hd_m_future)$mean
  
}

#ts names 
colnames(fc) <- colnames(sales.xts_m)

如果您想在项目中使用 ML 模型

devtools::install_github("Akai01/caretForecast")

library(caretForecast)


nts <- ncol(sales.xts_m) # mumber of time series

h <- 12 # forecast horizon

fc <- matrix(nrow = h, ncol = nts)

示例:具有线性核的支持向量机。您只需更改 caret_method 参数即可使用其他模型,例如 caret_method = "ridge" 或 caret_method = "rf" 等。参考:https://github.com/Akai01/caretForecast

for (i in 1:nts) {
  fc[,i] <- forecast(ARml(sales.xts_m[,i], maxlag = 12, xreg = dummies_hd_m,
                          caret_method = "svmLinear", seasonal = FALSE ), 
                     h=h, xreg = dummies_hd_m_future)$mean
  
}

colnames(fc) <- colnames(sales.xts_m)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多