繁体   English   中英

将模型应用于多个时间序列

[英]Applying models to multiple time-series

假设我有多个时间序列需要预测。 如果我为每个对象设置了合适的时间序列对象,我就可以拟合(例如)一个 ARIMA 模型等等。 但是,我知道当所有系列都在一个xts对象中时,必须有一种简单的方法来自动化这个过程(撇开不同变量可能需要不同 ARIMA 模型的事实;这可能是另一个问题)。

一些示例数据作为 xts 对象(六个不同业务的每日收入):

library(xts)

ts <- structure(c(534L, 549L, 636L, 974L, 848L, 895L, 1100L, 1278L, 
1291L, 1703L, 1532L, 533L, 619L, 642L, 939L, 703L, 759L, 1213L, 
1195L, 1153L, 1597L, 1585L, 649L, 597L, 628L, 924L, 703L, 863L, 
1261L, 1161L, 1212L, 1616L, 1643L, 583L, 694L, 611L, 891L, 730L, 
795L, 1242L, 1210L, 1159L, 1501L, 1702L, 513L, 532L, 580L, 917L, 
978L, 947L, 1227L, 1253L, 1121L, 1697L, 1569L, 646L, 636L, 516L, 
869L, 980L, 937L, 1173L, 1203L, 1204L, 1511L, 1640L), .Dim = c(11L, 
6L), .Dimnames = list(NULL, c("Americas_Globe", "Americas_Lucky", 
"Americas_Star", "Asia_Star", "EuroPac_Globe", "EuroPac_Lucky"
)), index = structure(c(1367384400, 1367470800, 1367557200, 1367643600, 
1367730000, 1367816400, 1367902800, 1367989200, 1368075600, 1368162000, 
1368248400), tzone = "", tclass = c("POSIXlt", "POSIXt")), .indexCLASS = c("POSIXlt", 
"POSIXt"), tclass = c("POSIXlt", "POSIXt"), .indexTZ = "", tzone = "", class = c("xts", 
"zoo"))

我可以从这个对象中提取一个时间序列......

ts.amerglob <- ts[,1] #Extract the "Americas_Global company time-series

并对其进行建模(例如,拟合 ARIMA 模型):

ts.ag.arima <- arima(ts.amerglob, order=c(0,1,1))

并做出预测

ts.ag.forecasts <- forecast.Arima(ts.ag.arima, h=5)

但是如果我想对这个ts对象中的 6 个公司中的每一个都这样做呢?

在拟合标准回归模型时,我使用 by() 对数据子集进行了类似的操作。 但是在这里应用这种方法似乎不起作用:

co.arima <- by(ts, ts[,1:6],
    function(x) arima(x, order=c(1,0,1)))

返回关于序列长度的错误:

error in tapply(seq_len(11L), list(INDICES = c(534L, 549L, 636L, 974L,  : 
  arguments must have same length

有没有什么简单的方法可以将时间序列模型一次应用于多个时间序列并提取相关信息? 最终我想要做的是将这些时间序列中的每一个的预测放入一个 data.frame 或矩阵中(但能够在建模过程中的中间步骤中做同样的事情会很棒,例如自动每个时间序列的.arima()输出)...

只需在此处使用lapply

res <- lapply(dat.ts,arima,order=c(1,0,1))

如果要为每个时间序列使用不同的顺序参数,可以使用Mapmapply

## generate a random list of orders
orders <- lapply(seq_len(ncol(dat.ts)),function(x)sample(c(0,1),3,rep=T))
## for each serie compute its arima with its corresponding order
Map(function(x,ord)arima(x,ord),as.list(dat.ts),orders)

使用 auto.arima fom forecast编辑获取订单:

注意我很少使用这个包,所以我不确定最终的结果。 我在这里展示的只是使用lapply的想法:

orders <- lapply(dat.ts,function(x){
             mod <- auto.arima(x)
             mod$arma[c(1, 6, 2, 3, 7, 4, 5)][1:3]
 })
$Americas_Globe
[1] 0 1 0
$Americas_Lucky
[1] 0 1 0
$Americas_Star
[1] 0 1 1
$Asia_Star
[1] 0 1 0
$EuroPac_Globe
[1] 0 1 0
$EuroPac_Lucky
[1] 0 1 0

暂无
暂无

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

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