在 R 中预测 Arima 模型返回奇怪的错误
Forecasting an Arima Model in R Returning Strange Error
我正在开发一个用于预测时间序列的 Shiny App。其中一个组成部分是使用 ARIMA 模型进行预测。用户指定历史数据的开始和结束,他们想在 ARIMA 模型中使用什么 p、d 和 q(如果他们不想使用 auto.arima),以及要预测多少个周期为了。
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
})
提前感谢您的帮助! :)
问题是我使用的是 arima(...) 函数而不是 Arima(...)。原来它们是两个不同的函数。我遇到的问题是函数存储数据方式不同的结果。有关此的更多信息,请参见 this post。好消息是,现在一切正常。
我正在开发一个用于预测时间序列的 Shiny App。其中一个组成部分是使用 ARIMA 模型进行预测。用户指定历史数据的开始和结束,他们想在 ARIMA 模型中使用什么 p、d 和 q(如果他们不想使用 auto.arima),以及要预测多少个周期为了。
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
})
提前感谢您的帮助! :)
问题是我使用的是 arima(...) 函数而不是 Arima(...)。原来它们是两个不同的函数。我遇到的问题是函数存储数据方式不同的结果。有关此的更多信息,请参见 this post。好消息是,现在一切正常。