重新拟合 ARIMA 模型时出错

Error while refitting ARIMA model

当我尝试重新拟合 ARIMA 模型时出现以下错误。

new_model <- Arima(data,model=old_model)
Error in Ops.Date(driftmod$coeff[2], time(x)) : 
  * not defined for "Date" objects

注:数据class为zoo。我也尝试使用 xts,但我得到了同样的错误。

编辑:正如 Joshua 所建议的,这里是可重现的例子。

library('zoo')
library('forecast')

#Creating sample data
sample_range <- seq(from=1, to=10, by=1)
x<-sample(sample_range, size=61, replace=TRUE)
ts<-seq.Date(as.Date('2017-03-01'),as.Date('2017-04-30'), by='day')
dt<-data.frame(ts=ts,data=x)

#Split the data to training set and testing set
noOfRows<-NROW(dt)
trainDataLength=floor(noOfRows*0.70)
trainData<-dt[1:trainDataLength,]
testData<-dt[(trainDataLength+1):noOfRows,]

# Use zoo, so that we get dates as index of dataframe
trainData.zoo<-zoo(trainData[,2:ncol(trainData)], order.by=as.Date((trainData$ts), format='%Y-%m-%d'))
testData.zoo<-zoo(testData[,2:ncol(testData)], order.by=as.Date((testData$ts), format='%Y-%m-%d'))

#Create Arima Model Using Forecast package 
old_model<-Arima(trainData.zoo,order=c(2,1,2),include.drift=TRUE)

# Refit the old model with testData
new_model<-Arima(testData.zoo,model=old_model)

?Arima 页面说 y(第一个参数)应该是一个 ts 对象。我的猜测是第一次调用 Arima 将您的 zoo 对象强制为 ts,但第二次调用不会。

解决此问题的一个简单方法是显式强制 ts:

# Refit the old model with testData
new_model <- Arima(as.ts(testData.zoo), model = old_model)