简体   繁体   中英

Time series prediction via ARIMA model

Hi i am new in the field of time series. I want to make predictions for a given time series

I use the code below :

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')

}

I want to plot in a graph predicted and true values, in same graph to have a visualization of true values and predicted values for same time step. How can this done?

In the model above in timestep t,does prediction pred[50] refer to value rt[i+50] (i want 50 time steps ahead prediction) , or refer to rt[i](estimated from model brute force trained, from previous values)?

Where i is current timestep as in code, and rt is the real value for timestep i.

You can use:

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 automatically fetches the timestamps from the time series and plots them on the x-axis. For the standard AirPassengers data you will get the following output. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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