我正在尝试执行以下操作,但是我会在最终的列表方式中使用函数(#4以下 - #1-3只是问题的设置):
(1):数据:
library(plyr); library(xts); library(forecast)
set.seed(1234)
data <- data.frame(
date = seq(as.Date('2014-05-01'), length = 31, by = '1 day'),
segment = c(rep('High', 31), rep('Med', 31), rep('Low', 31)),
orders = sample(50:100, 93, replace=TRUE),
revenue = sample(1500:3000, 93, replace=TRUE))
设置训练和测试时段:
train_dates <- as.Date('2014-05-01') + 0:19
test_dates <- as.Date('2014-05-21') + 0:10
(2):通过“segment”将数据帧拆分为列表,并使列表中的每个元素成为XTS对象,以便我们可以在预测中使用它:
xtsFunc <- function(x) {
as.xts(x[,3:4], order.by=x$date)
}
# Training and test lists:
train_list <- dlply(subset(data, date %in% train_dates), .(segment), xtsFunc)
test_list <- dlply(subset(data, date %in% test_dates), .(segment), xtsFunc)
(3):现在,我想在“train_list”对象上使用auto.arima()。这很容易:
arimaFunc <- function(x){
auto.arima(x[,'revenue'], xreg=x[,'orders'])
}
train_arimas <- llply(train_list, arimaFunc)
(4):但现在我们遇到了问题,列表中的第4部分。我想采用我刚刚为每个细分市场的培训日期创建的arima模型,并使用forecast.Arima()根据我们在测试日期已经拥有的订单数据来预测未来的收入数字*
如何将“train_arimas”中的模型应用到“test_list”中各自的段,使用[,'orders']的未来值(例如来自“test_list”)作为“预测中的xreg参数” .Arima()“?
我的尝试肯定没有正确使用“申请”:
forecastFunc <- function(x){
forecast.Arima(x, h=11, xreg=test_list$x$orders)
}
lapply(train_arimas, forecastFunc)
注意:这有点像我问的earlier question,但希望措辞更清楚......
*我知道这似乎有些奇怪,方法论上。但在实际应用中,我在未来的日期有一套保证订单,所以“xreg”变量本身不需要预测
答案 0 :(得分:0)
我会这样做:
# Split the data according to the variable `segment`
data_s <- split(data, data$segment)
# Estimate a model for each list element for the period [01-05.2014; 20-05.2014]
mod <- lapply(data_s, function(x) auto.arima(x$revenue[1:20], xreg = x$orders[1:20]))
# Apply respective models to the data supplying the regressor `orders`:
res <- mapply(function(mod, data_s) forecast.Arima(mod, xreg = data_s$orders[21:31]), mod, data_s)
# In order to get the respective forecasts (`means`):
forecasts <- lapply(apply(res, 2, list), function(x) x[[1]]$mean)
为了在实现之上绘制预测:
plot(data_s$Low$revenue, type="l")
lines(21:31, forecasts$Low, col = "red")
plot(data_s$High$revenue, type="l")
lines(21:31, forecasts$High, col = "red")
plot(data_s$Med$revenue, type="l")
lines(21:31, forecasts$Med, col = "red")