简体   繁体   English

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

[英]Applying models to multiple time-series

Let's say I have multiple time series for which I want forecasts.假设我有多个时间序列需要预测。 If I have the appropriate time-series object for each, I could fit (for the sake of example) an ARIMA model and so on.如果我为每个对象设置了合适的时间序列对象,我就可以拟合(例如)一个 ARIMA 模型等等。 But, I know there must be an easy way to automate this process when all of the series are in one xts object (leaving aside the fact that different variables might require different ARIMA models; that's probably a question for another time).但是,我知道当所有系列都在一个xts对象中时,必须有一种简单的方法来自动化这个过程(撇开不同变量可能需要不同 ARIMA 模型的事实;这可能是另一个问题)。

Some sample data as an xts object (daily revenue for six different businesses):一些示例数据作为 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"))

I can extract one time-series from this object...我可以从这个对象中提取一个时间序列......

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

and model it however (for the sake of example, fit an ARIMA model):并对其进行建模(例如,拟合 ARIMA 模型):

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

and make forecasts并做出预测

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

But what if I want to do this for each of the 6 companies in this ts object?但是如果我想对这个ts对象中的 6 个公司中的每一个都这样做呢?

When fitting standard regression models, I've used by() to do something similar with subsets of the data.在拟合标准回归模型时,我使用 by() 对数据子集进行了类似的操作。 But applying that methodology here doesn't seem to work:但是在这里应用这种方法似乎不起作用:

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

returns an error about sequence length:返回关于序列长度的错误:

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

Is there any easy way to apply a time-series model to multiple time-series at once and extract relevant information?有没有什么简单的方法可以将时间序列模型一次应用于多个时间序列并提取相关信息? Ultimately what I'm looking to do is put the forecasts for each of these time series into one data.frame or matrix (but it would be great to be able to do the same thing with intermediate steps in the modeling process, such as auto.arima() output for each time-series)...最终我想要做的是将这些时间序列中的每一个的预测放入一个 data.frame 或矩阵中(但能够在建模过程中的中间步骤中做同样的事情会很棒,例如自动每个时间序列的.arima()输出)...

Simply use lapply here:只需在此处使用lapply

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

If you want to use different order parameter for each time serie, you can use Map or mapply :如果要为每个时间序列使用不同的顺序参数,可以使用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)

EDIT get order using auto.arima fom forecast package:使用 auto.arima fom forecast编辑获取订单:

Note I am rarely use this package, so I am not sure of the final result.注意我很少使用这个包,所以我不确定最终的结果。 I show here just the idea of using lapply :我在这里展示的只是使用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