【发布时间】:2013-12-27 12:23:25
【问题描述】:
我有几个想要预测的回归模型,并轻松提取“x-step-ahead”预测。我想用最少且简单的代码来做到这一点,这样我就可以轻松地更改模型并重新运行它们。
所以,一个简单的多元回归模型的例子:
library(plyr)
library(forecast)
# Historical Data
df.h <- data.frame(
hour = factor(rep(1:24, each = 21)),
price = runif(504, min = -10, max = 125),
wind = runif(504, min = 0, max = 2500),
temp = runif(504, min = - 10, max = 25)
)
# Forecasting Data
df.f <- data.frame(
hour = factor(rep(1:24, each = 9)),
wind = runif(216, min = 0, max = 2500),
temp = runif(216, min = - 10, max = 25)
)
models <- dlply(df.h, "hour", function(x) (lm(price ~ wind + temp, data = df.h)))
# Now I have 24 different regression-models, I would like to forecast on each one and
# be able to extract 1 step ahead forecast easily and 2 step ahead, and etc.
# I've done it like this, but it is cumbersome to work with and to extract the data I want
f1 <- forecast(models[[1]], newdata = subset(df.f, df.f$hour == 1))
f2 <- forecast(models[[2]], newdata = subset(df.f, df.f$hour == 2))
....
f24 <- forecast(models[[24]], newdata = subset(df.f, df.f$hour == 24))
# Getting the first-predictive day:
predict.1 <- cbind(f1$mean[1], f2$mean[1], f24$mean[1] )
理想情况下,我希望数据框或列表包含每个小时的每个超前预测,如下所示:
df.prediction
hour step1 step2 .... step9
1
2
3
...
24
但我不确定这是否可能/怎么做?
【问题讨论】:
标签: r plyr apply forecasting