使用auto.Arima()和xreg进行ARIMA预测 [英] ARIMA forecasting with auto.Arima() and xreg
问题描述
我正在从事预测商店销售的项目以学习预测.到目前为止,我已经成功地使用简单的auto.Arima()函数进行预测了,但是为了使这些预测更准确,我可以使用协变量.借助假期,促销等协变量会影响使用xreg运算符的商店的销售,而此帖子的帮助是: 如何在R中的auto.arima()中设置xreg参数?
I am working on project to forecast sales of stores to learn forecasting.Till now I have successfully used simple auto.Arima() function for forecasting.But to make these forecast more accurate I can make use of covariates.I have defined covariates like holidays, promotion which affect on sales of store using xreg operator with the help of this post: How to setup xreg argument in auto.arima() in R?
但是我的代码在以下行失败:
But my code fails at line:
ARIMAfit<-auto.arima(saledata,xreg = covariates)
ARIMAfit <- auto.arima(saledata, xreg=covariates)
并给出错误提示:
model.frame.default(formula = x〜xreg,drop.unused.levels = TRUE)中的错误:变量长度不同(为'xreg'找到)另外:警告消息:在!is.na(x)中& !is.na(rowSums(xreg)):较长的对象长度不是较短的对象长度的倍数
Error in model.frame.default(formula = x ~ xreg, drop.unused.levels = TRUE) : variable lengths differ (found for 'xreg') In addition: Warning message: In !is.na(x) & !is.na(rowSums(xreg)) : longer object length is not a multiple of shorter object length
下面是指向我的数据集的链接: https://drive .google.com/file/d/0B-KJYBgmb044blZGSWhHNEoxaHM/view?usp = sharing
Below is link to my Dataset: https://drive.google.com/file/d/0B-KJYBgmb044blZGSWhHNEoxaHM/view?usp=sharing
这是我的代码:
data = read.csv("xdata.csv")[1:96,]
View(data)
saledata <- ts(data[1:96,4],start=1)
View(saledata)
saledata[saledata == 0] <- 1
View(saledata)
covariates = cbind(DayOfWeek=model.matrix(~as.factor(data$DayOfWeek)),
Customers=data$Customers,
Open=data$Open,
Promo=data$Promo,
SchoolHoliday=data$SchoolHoliday)
View(head(covariates))
# Remove intercept
covariates <- covariates[,-1]
View(covariates)
require(forecast)
ARIMAfit <- auto.arima(saledata, xreg=covariates)//HERE IS ERROR LINE
summary(ARIMAfit)
还告诉我未来48天的预测方法.我知道如何使用简单的auto.Arima()和n.ahead进行预测,但不知道使用xreg时如何进行预测.
Also tell me how I can forecast for next 48 days.I know how to forecast using simple auto.Arima() and n.ahead but dont know how to do it when xreg is used.
推荐答案
几点.一种,您可以将整个矩阵转换为ts对象,然后稍后隔离变量.其次,如果您在Arima模型中使用协变量,那么在预测样本外时将需要提供协变量.这可能意味着在生成您感兴趣的变量的预测之前,先预测每个协变量.在下面的示例中,为简单起见,我将数据分为两个样本.
A few points. One, you can just convert the entire matrix to a ts object and then isolate the variables later. Second, if you are using covariates in your arima model then you will need to provide them when you forecast out-of-sample. This may mean forecasting each of the covariates before generating forecasts for your variable of interest. In the example below I split the data into two samples for simplicity.
dta = read.csv("xdata.csv")[1:96,]
dta <- ts(dta, start = 1)
# to illustrate out of sample forecasting with covariates lets split the data
train <- window(dta, end = 90)
test <- window(dta, start = 91)
# fit model
covariates <- c("DayOfWeek", "Customers", "Open", "Promo", "SchoolHoliday")
fit <- auto.arima(train[,"Sales"], xreg = train[, covariates])
# forecast
fcast <- forecast(fit, xreg = test[, covariates])
这篇关于使用auto.Arima()和xreg进行ARIMA预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!