重新安装ARIMA模型时出错

时间:2017-09-20 23:33:33

标签: r xts zoo arima

当我尝试重新调整ARIMA模型时,我收到以下错误。

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

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

编辑:正如约书亚所建议的,这是可重复的例子。

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)

1 个答案:

答案 0 :(得分:1)

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

解决此问题的一种简单方法是明确强制转换为ts

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