在R中,rep(1, n.ahead)的错误:'times'参数无效

3

我正在处理数据集,使用ARIMA进行预测。我接近最后一步,但是出现了错误,无法找到参考资料来确定我缺少什么。

每次执行以下命令时,我都会收到错误消息:

ForcastData<-forecast(fitModel,testData)

Error in rep(1, n.ahead) : invalid 'times' argument

我将简要介绍我的工作,其中我将数据集从数据框架更改为时间序列,并进行了所有测试以检查波动性,并检测数据是否静止。
然后,我得到了“DataAsStationary”这样干净好用的数据来应用ARIMA模型。但是,由于我想在训练数据上训练模型并在其他部分数据上进行测试,所以我将数据集分成70%的训练和30%的测试:

ind <-sample(2, nrow(DataAsStationary), replace = TRUE, prob = c(0.7,0.3))
traingData<- DataStationary1[ind==1,]
testData<- DataStationary1[ind==2,]

我使用了自动选择算法,发现Arima(2,0,3)是最佳模型。

autoARIMAFastTrain1<- auto.arima(traingData, trace= TRUE, ic ="aicc", approximation = FALSE, stepwise = FALSE)

我必须提到,我确实检查了残差是否不相关(白噪声),并对其进行了处理。

library(tseries)
library(astsa)
library(forecast)

然后我使用训练数据集来拟合模型:

fitModel <- Arima(traingData, order=c(2,0,3))
fitted(fitModel)

ForcastData<-forecast(fitModel,testData)
output <- cbind(testData, ForcastData)
accuracy(testData, ForcastData) 
plot(outp)

找不到关于以下错误的任何资源:

Error in rep(1, n.ahead) : invalid 'times' argument

有什么建议吗!!真的很需要。

我已经尝试了。

ForcastData<-forecast.Arima(fitModel,testData)

但是我遇到以下错误:

无法找到forecast.Arima!

你有任何想法为什么会出现这个错误吗?


关于您遇到的 forecast.Arima 未找到错误,请参考 此答案 ,该答案是针对 forecast 包的不同问题。 - duckmayr
2个回答

2

您需要用不同的方式指定forecast()的参数;由于您没有发布示例数据,我将使用forecast包中的gold数据集进行演示:

library(forecast)
data(gold)
trainingData <- gold[1:554]
testData <- gold[555:1108]
fitModel <- Arima(trainingData, order=c(2, 0, 3))
ForcastData <- forecast(fitModel, testData)
# Error in rep(1, n.ahead) : invalid 'times' argument
ForcastData <- forecast(object=testData, model=fitModel) # no error
accuracy(f=ForcastData) # you only need to give ForcastData; see help(accuracy)
                    ME     RMSE      MAE        MPE      MAPE     MASE
Training set 0.4751156 6.951257 3.286692 0.09488746 0.7316996 1.000819
                   ACF1
Training set -0.2386402

您可能需要花些时间阅读 预测包文档,了解各种函数的参数名称和顺序。

关于您遇到的“未找到 forecast.Arima”错误,您可以参考这个回答来处理。基本上,该函数不是供用户调用的,而是由forecast函数调用。

编辑:

根据您的评论,以下内容可能有所帮助:

library(forecast)
# Read in the data
full_data <- read.csv('~/Downloads/onevalue1.csv')
full_data$UnixHour <- as.Date(full_data$UnixHour)
# Split the sample
training_indices <- 1:floor(0.7 * nrow(full_data))
training_data <- full_data$Lane1Flow[training_indices]
test_data <- full_data$Lane1Flow[-training_indices]
# Use automatic model selection:
autoARIMAFastTrain1 <- auto.arima(training_data, trace=TRUE, ic ="aicc",
                                  approximation=FALSE, stepwise=FALSE)
# Fit the model on test data:
fit_model <- Arima(training_data, order=c(2, 0, 3))
# Do forecasting
forecast_data <- forecast(object=test_data, model=fit_model)
# And plot the forecasted values vs. the actual test data:
plot(x=test_data, y=forecast_data$fitted, xlab='Actual', ylab='Predicted')

enter image description here

# It could help more to look at the following plot:
plot(test_data, type='l', col=rgb(0, 0, 1, alpha=0.7),
     xlab='Time', ylab='Value', xaxt='n', ylim=c(0, max(forecast_data$fitted)))
ticks <- seq(from=1, to=length(test_data), by=floor(length(test_data)/4))
times <- full_data$UnixHour[-training_indices]
axis(1, lwd=0, lwd.ticks=1, at=ticks, labels=times[ticks])
lines(forecast_data$fitted, col=rgb(1, 0, 0, alpha=0.7))
legend('topright', legend=c('Actual', 'Predicted'), col=c('blue', 'red'),
       lty=1, bty='n')

enter image description here


我正在使用这个数据集 https://ufile.io/i526y 并且已经完成了以下操作:ForcastData <- forecast(object=testData, model=fitModel) # 没有错误。我已经查阅了许多关于时间序列和预测包的文档,并需要获取最终预测结果以将其与实际结果进行比较,但现在出现了以下错误:Error in error(x,..) : improper length of one or more arguments to merge.xts。我想要绘制ForcastData和测试数据的图表以比较Aroma模式的强度。请帮忙解决。 - Reta
数据在上传时已经发生了更改,其中索引列看起来像是2017-07-17 01:00:00而不是1,2,3... 请查看数据外观图片:https://ufile.io/6m5mg 因为我想在图表上显示日期和时间,并且当我将数据设置为时间序列时,我已经将UnixHour列设置为最后一个索引:DataTS <- as.xts(x = DataNotTS[, -1], order.by = DataNotTS$UnixHour, frequency = 4) 这是包含两个列UnixHour和Lane1Flow的数据链接:https://ufile.io/kui6p 这是我使用的整个代码链接:https://ufile.io/aujex - Reta
我想将数据转换为时间序列,然后建立Arima模型进行训练,接着对数据进行训练和测试,并展示预测数据并将其与实际数据在图表上进行比较,同时显示日期和时间。我已经花费了一个月的时间研究这个概念,但现在卡住了,需要最终确定结果。 - Reta

0

我成功地运行了

ForcastData <- forecast(object=testData, model=fitModel)

没有错误,现在想要绘制测试数据和预测数据,并检查我的模型是否准确:

所以我做了以下操作:

output <- cbind(testData, ForcastData) plot(output) 但是出现了错误:

Error in error(x, ...) : 
  improper length of one or more arguments to merge.xts

所以当我检查ForcastData时,它给出了输出:

> ForcastData
        Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
2293201    -20.2831770 -308.7474 268.1810 -461.4511 420.8847
2296801    -20.1765782 -346.6400 306.2868 -519.4593 479.1061
2300401    -18.3975657 -348.8556 312.0605 -523.7896 486.9945
2304001     -2.2829565 -332.7483 328.1824 -507.6860 503.1201
2307601      2.7023277 -327.8611 333.2658 -502.8509 508.2555
2311201      4.5777316 -328.6756 337.8311 -505.0893 514.2447
2314801      4.3198927 -331.4470 340.0868 -509.1913 517.8310
2318401      3.8277285 -332.7898 340.4453 -510.9844 518.6398
2322001      1.4364973 -335.2403 338.1133 -513.4662 516.3392
2325601     -0.4013561 -337.0807 336.2780 -515.3080 514.5053

我以为我会得到与我的测试数据相同的结果列表。我需要获取显示两条线实际数据(testData)和预期数据(ForcastData)的图表。 我已经阅读了许多关于预测的文档,但是我找不到解释我想要做什么的东西。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接