r - Forecasting several time series models, dplyr -
i use dplyr forecast several models. models fitted on time series data, each hour own model. ie, hour = 1 model, , hour = 18 model.
example:
# historical data - basis 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 wind , 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 hour 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 awesome 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)
if want pack 1 call, can bind data single data.frame
, split again in 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
Comments
Post a Comment