将模型应用于多个时间序列 [英] Applying models to multiple time-series

查看:36
本文介绍了将模型应用于多个时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. 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 对象(六家不同企业的每日收入):

Some sample data as an xts object (daily revenue for six different businesses):

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

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

and model it however (for the sake of example, fit an ARIMA model):

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

并进行预测

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

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

But what if I want to do this for each of the 6 companies in this ts object?

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

When fitting standard regression models, I've used by() to do something similar with subsets of the data. 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

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

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)...

推荐答案

只需在这里使用 lapply:

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

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

If you want to use different order parameter for each time serie, you can use Map or mapply:

## 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 使用 auto.arima fom forecast 包获取订单:

EDIT get order using auto.arima fom forecast package:

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

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:

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

这篇关于将模型应用于多个时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆