Forecasting Multiple Time Series Models, dplyr

I would like to use dplyr to predict multiple models. The models are set on time series data, so each hour is its own model. That is, hour = 1 is the model and hour = 18 is the 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 every model every hour:

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

      

+3


source to share


1 answer


If you want to package it into a single call, you can bind the data to one data.frame

and then break it down into a call again do

.



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

      

+5


source







All Articles