我正在构建一个用于时间序列预测的Shiny App。 其中一个组件是使用ARIMA模型进行预测。 用户指定历史数据的开始和结束时间,以及在ARIMA模型中要使用的p、d和q(如果他们不想使用auto.arima),以及要预测多少个周期。
对于auto.arima以及我使用的其他预测方法(如Holt和Holt-Winters),一切正常。 但是,当使用arima()创建具有自定义p、d和q的模型时,我从forecast()获得以下错误:
创建时间序列:
最后但并非最不重要的是预测 - 这一部分会弹出奇怪的警告消息:
对于auto.arima以及我使用的其他预测方法(如Holt和Holt-Winters),一切正常。 但是,当使用arima()创建具有自定义p、d和q的模型时,我从forecast()获得以下错误:
Warning in .cbind.ts(list(e1, e2), c(deparse(substitute(e1))[1L],
deparse(substitute(e2))[1L]), : non-intersecting series
对我来说奇怪的是,当我在创建时间序列的函数中注释掉开始和结束参数时,一切都正常运行...
以下是我代码中相关的部分:
首先,读取包含要预测值的第二列的指定csv文件,并准备好该列进行预测:
## Reads in the historical data from the uploaded file
readData <- reactive({
inFile <- input$inputFile
if (is.null(inFile))
return(NULL)
data <- read.csv(inFile$datapath)
data
})
## Returns the historical values to be used in forecasting
historical <- reactive({
data <- readData()
## Remove any commas and dollar signs, and convert to a number
data[,2] <- as.numeric(sub("\\,","",
sub("\\$","",data[,2])
)
)
data
})
创建时间序列:
## Converts the historical data to a time series
tsData <- reactive({
data <- historical()
data <- data[,2]
ts <- ts(data,
start=c(input$startYear, input$startTime),
end=c(input$endYear, input$endTime),
frequency = strtoi(input$frequency)
)
ts
})
Create the ARIMA model:
## Create an ARIMA model for forecasting
arimaModel <- reactive({
ts <- tsData()
if(input$arimaAuto){
fit <- auto.arima(ts)
}
else{
fit <- arima(ts,order=c(strtoi(input$arimaP),
strtoi(input$arimaD),
strtoi(input$arimaQ)))
}
fit
})
最后但并非最不重要的是预测 - 这一部分会弹出奇怪的警告消息:
## Creates an ARIMA model and returns a forecast based on that model.
arimaData <- reactive({
fit <- arimaModel()
print("Fit: ")
print(fit)
f <- forecast(fit#,
#h = input$forecast_periods,
#level=c(strtoi(input$confidence1), strtoi(input$confidence2))
)
print("Forecast:")
print(f)
f
})
非常感谢您的帮助!:)