Reputation: 1610
I would like to use dplyr to forecast several models. The models are fitted on time series data, so each hour is it own model. Ie, hour = 1 is a model, and hour = 18 is a model.
Example:
# Historical data - Basis for the models:
df.h <- data.frame(
hour = factor(rep(1:24, each = 100)),
price = runif(2400, min = -10, max = 125),
wind = runif(2400, min = 0, max = 2500),
temp = runif(2400, min = - 10, max = 25)
)
# Forecasted data for wind and temp:
df.f <- data.frame(
hour = factor(rep(1:24, each = 10)),
wind = runif(240, min = 0, max = 2500),
temp = runif(240, min = - 10, max = 25)
)
I can fit each model, hour by hour as so:
df.h.1 <- filter(df.h, hour == 1)
fit = Arima(df.h.1$price, xreg = df.h.1[, 3:4], order = c(1,1,0))
df.f.1 <- filter(df.f, hour == 1)
forecast.Arima(fit, xreg = df.f.1[ ,2:3])$mean
But it would be awesome to do something like this:
fits <- group_by(df.h, hour) %>%
do(fit = Arima(df.h$price, order= c(1, 1, 0), xreg = df.h[, 3:4]))
df.f %>% group_by(hour)%>% do(forecast.Arima(fits, xreg = .[, 2:3])$mean)
Upvotes: 3
Views: 1000
Reputation: 22293
If you want to pack it into one call, you can bind the data into a single data.frame
and then split it up again in the do
call.
df <- rbind(df.h, data.frame(df.f, price=NA))
res <- group_by(df, hour) %>% do({
hist <- .[!is.na(.$price), ]
fore <- .[is.na(.$price), c('hour', 'wind', 'temp')]
fit <- Arima(hist$price, xreg = hist[,3:4], order = c(1,1,0))
data.frame(fore[], price=forecast.Arima(fit, xreg = fore[ ,2:3])$mean)
})
res
Upvotes: 5