繁体   English   中英

通过ARIMA模型进行时间序列预测

[英]Time series prediction via ARIMA model

嗨,我是时间序列领域的新手。 我想对给定的时间序列进行预测

我使用下面的代码:

library(forecast)
library(TSPred)

dataSet <- 'data'
dataSetPath <- paste0("data/", dataSet, '.csv')

# load data
recDF <- read.csv(dataSetPath, skip=0)
rt = ts(recDF["s2"])

if(dataSet=="data"){
  nTrain <- 3000
  nSkip <- nTrain


nData <- length(rt)
testLength <- nData - nSkip

# testLength 

arima_output90 = vector(mode="numeric", length=testLength)
real = vector(mode="numeric", length=testLength)

pred2 <- arimapred(rt[seq(1, nTrain)], n.ahead=testLength)
forecast::auto.arima(rt[seq(1, nTrain)])


# Brute force ARIMA - recompute model every step
# while making predictions for the next N hours.

for(i in nSkip+1:testLength)
{
  # Compute ARIMA on the full dataset up to this point
  trainSet = window(rt, start=i-nTrain, end=i)
  fit_arima <- forecast::auto.arima(trainSet)

  #   fcast_arima <- predict(fit_arima, n.ahead = 5, se.fit = TRUE)
  #   mean <- fcast_arima$pred
  #   std <- fcast_arima$se

  fcast_arima <- forecast(fit_arima, h=50)
  pred <- fcast_arima$mean


  arima_output50[i] = pred[50]
  real[i] = rt[i]
  cat("step: ",i ,"true : ", rt[i], " prediction: ", pred[50], '\n')

}

我想在同一图中绘制预测值和真实值的图形,以便在相同时间步长下可视化真实值和预测值。 这怎么办?

在上面的模型中,在时间步长t中,预测pred [50]是否参考值rt [i + 50](我想提前50个时间步长预测),还是参考rt [i](根据之前训练过的模型蛮力估计值)?

其中,i是代码中的当前时间步,而rt是时间步i的实际值。

您可以使用:

ts.plot(fit_arima$x, fit_arima$fitted, pred, type='o', col=c('blue', 'green', 'red'), lty=c(1,2,3))
legend('topleft', c('train', 'fitted', 'forecast'), col=c('blue', 'green', 'red'), lty=c(1,2,3))

ts.plot自动从时间序列中获取时间戳并将其绘制在x轴上。 对于标准的AirPassengers数据,您将获得以下输出。 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM