使用 dplyr 将数据传递给 forecast.lm 并执行
Passing data to forecast.lm using dplyr and do
我在 dplyr do
中将数据传递给 forecast.lm
时遇到问题。我想基于一个因素制作多个模型 - hour
- 并使用新数据预测这些模型。
我的数据示例基于以前的优秀示例:
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
这对时间序列模型非常有效,但是使用 lm
我在将数据传递到预测部分时遇到问题。
我对应的 lm
示例如下所示:
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
,则无法找到它 - 但在时间序列示例中可以。
问题是 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)
})
这其实是一个known issue.
我在 dplyr do
中将数据传递给 forecast.lm
时遇到问题。我想基于一个因素制作多个模型 - hour
- 并使用新数据预测这些模型。
我的数据示例基于以前的优秀示例:
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
这对时间序列模型非常有效,但是使用 lm
我在将数据传递到预测部分时遇到问题。
我对应的 lm
示例如下所示:
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
,则无法找到它 - 但在时间序列示例中可以。
问题是 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)
})
这其实是一个known issue.