Passing data to .ll forecast using dplyr and do

I'm having problems with data transmission on forecast.lmin dplyr do. I want to make several models based on the coefficient - hour- and predict these models using new data.

Based on previous excellent examples, an example of my data is given:

require(dplyr)
require(forecast)

# Training set
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)  
)

# Forecasting set
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)  
)

# Bind training & forecasting
df <- rbind(df.h, data.frame(df.f, price=NA))

# Do a training model and then forecast using the new data
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

This works great with a time series model, but with the help lmof me I have a problem with transferring data to the forecasting part.

My relevant example is lmas follows:

res <- group_by(df, hour) %>% do({
  hist <- .[!is.na(.$price), ]
  fore <- .[is.na(.$price), c('hour', 'wind', 'temp')]
  fit <- lm(hist$price ~ wind + temp, data = hist)
  data.frame(fore[], price = forecast.lm(fit, newdata = fore[, 2:3])$mean)
})

, newdata =. hist$ , , - , data = fore, , .

+4
1

, forecast.lm , fit data. glm tslm, . lm data. fit$data <- hist forecast.lm .

res <- group_by(df, hour) %>% do({
  hist <- .[!is.na(.$price), ]
  fore <- .[is.na(.$price), c('hour', 'wind', 'temp')]
  fit <- lm(price ~ wind + temp, data = hist)
  fit$data <- hist # have to add data manually
  data.frame(fore[], price = forecast.lm(fit, newdata = fore[, 2:3])$mean) 
})

.

+3

All Articles