[英]How to forecast and fit the optimal model for multiple time series?
I want to do batch forecasting among multiple series, for example, if I want to forecast time series with IDs that end with 1(1,11,21,31...), how can I do that?我想在多个序列之间进行批量预测,例如,如果我想预测 ID 以 1(1,11,21,31...) 结尾的时间序列,我该怎么做?
Since you did not provide detailed information, I was not sure which forecasting method you want to use hence I give here an example of a univariate time series model:由于您没有提供详细信息,因此我不确定您要使用哪种预测方法,因此我在这里给出了一个单变量时间序列 model 的示例:
Load required packages:加载所需的包:
library(forecast)
library(dplyr)
We use example data from Rob Hyndman:我们使用来自 Rob Hyndman 的示例数据:
dta <- read.csv("https://robjhyndman.com/data/ausretail.csv")
Now change the column names:现在更改列名:
colnames(dta) <- c("date", paste0("tsname_", seq_len(ncol(dta[,-1]))))
Select timeseries which end with 1:以 1 结尾的 Select 时间序列:
dta_ends_with1 <- dplyr::select(dta, dplyr::ends_with("1"))
Create a ts object:创建一个ts object:
dta_ends_with1 <- ts(dta_ends_with1, start = c(1982,5), frequency = 12)
Specify how many steps ahead you want to forecast, here I set it to 6 steps ahead,指定要预测提前多少步,这里我设置为提前6步,
h <- 6
Now we prepare a matrix to save the forecast:现在我们准备一个矩阵来保存预测:
fc <- matrix(NA, ncol = ncol(dta_ends_with1), nrow = h)
Forecasting loop.预测循环。
for (i in seq_len(ncol(dta_ends_with1))) {
fc[,i] <- forecast::forecast(forecast::auto.arima(dta_ends_with1[,i]),
h = h)$mean
}
Set the column names:设置列名:
colnames(fc) <- colnames(dta_ends_with1)
head(fc)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.