首页 文章

有没有一种简单的方法可以将预测恢复为绘制时间序列?

提问于
浏览
2

我是R的新手并且发现这个网站非常有用,所以这涵盖了我的问题的后半部分(每个帖子一个问题) . 感谢您提前给予的帮助 .

Background: 我正在绘制历史数据,其中多个预测覆盖了视觉准确性检查 . 当在'observations'的x轴上显示时,这很有效 . 但是,当在x轴上绘制日期时,数据更容易理解,所以我使用ts()创建了一个时间序列,并按预期绘制了时间序列数据 . 但是,(A)它没有在时间尺度上绘制预测数据,因为它们不是时间序列; (B)我不确定如何强制x轴加1年以允许显示预测 .

Question: (A)如何将原始时间戳恢复为预测数据?我知道我可以手动重新创建时间序列,但这在预测的每次迭代中都是必需的 . 我考虑使用forecast()而不是predict(),但是额外的预测迭代仍然存在不是时间序列的相同问题 . Is there a simple way to restore the original time stamp to the forecast data?

require(forecast)  [EDITED for clarity]

  data <- rep(cos(1:52*(3.1416/26)),5)*100+1000

  arima.ts <- ts(data,start=c(2009,1),frequency=52) #not plotted as time series

  # Create the current fit on data and predict one year out
  plot(arima.ts, type="l", xlab="weeks", ylab="counts",
  main="Overlay forecasts & actuals",
       sub="green=FIT(1-105,by 16) wks back & PREDICT(26) wks, blue=52 wks")
  ############## This plotted correctly as "Arima(data),..."
  arima.fit <- auto.arima(tail(arima.ts,156)) 
  arima.pred <- predict(arima.fit, n.ahead=52)
  lines(arima.pred$pred, col="blue")
  lines(arima.pred$pred+2*arima.pred$se, col="red")
  lines(arima.pred$pred-2*arima.pred$se, col="red")

  # Loop back and perform comparison plotting of forecast to actuals
  for (j in seq(1,105,by=16)) { 
    result <- tryCatch({
      ############## This plotted correctly as "Arima(head(data,-j),..."
      arima1.fit <- auto.arima(head(tail(arima.ts,-j),156))
      arima1.pred <- predict(arima1.fit, n.ahead=52)
      lines(arima1.pred$pred, col="green", lty=(numtests %% 6) + 1 )
    }, error = function(e) {return(e$message)}) ## Trap errors
  }

2 回答

  • 1

    正在解决的核心问题是"how to restore the original time stamps to the forecast data" . 我通过试验和错误学到的是"configure, then never loose the time series attribute",通过应用这些步骤:

    1: Make a time series 使用ts()命令创建时间系列 .
    2: Subset a time series 使用'window()'在'for()'循环中创建时间系列的子集 . 在数据上使用'start()'和'end()'来显示时间轴位置 .
    3: Forecast a time series 使用按时间序列操作的'forecast()'或'predict()' .
    4: Plot a time series 绘制时间序列时,时间轴将使用lines()命令正确对齐其他数据 . {绘图选项是用户偏好 . }

    这会使预测在正确的时间轴位置上绘制在历史数据上 .

    require(forecast)     ### [EDITED for clarity]
    
      data <- rep(cos(1:52*(3.1416/26)),5)*100+1000
      a.ts <- ts(data,start=c(2009,1),frequency=52)
    
      ## Predict from previous '3' years then one year out & generate the plot
      a.win  <- window(a.ts,start=c(end(a.ts)[1]-3,end(a.ts)[2]),frequency=52)
      a.fit  <- auto.arima(a.win)  
      a.pred <- forecast(a.fit, h=52)
      plot(a.pred, type="l", xlab="weeks", ylab="counts",
           main="Overlay forecasts & actuals",
           sub="green=FIT(1-105,by 16) wks back & PREDICT(26) wks, blue=52 wks")
    
      for (j in seq(1, 90, by=8)) {   ## Loop to overlay early forecasts 
        result1 <- tryCatch({
          b.end   <- c(end(a.ts)[1],end(a.ts)[2]-j) ## Window the time series  
          b.start <- c(b.end[1]-3,b.end[2])
          b.window <- window(a.ts, start=b.start, end=b.end, frequency=52)
    
          b.fit  <-auto.arima(b.window) 
          b.pred <- forecast(b.fit, h=26)
          lines(b.pred$mean, col="green", lty="dashed" )
        }, error = function(e) {return(e$message)} ) ## Skip Errors
      }
    
  • 2
    install.packages(c("forecast"))
    
    library(forecast)
    
    # Load your data
    data <- c(11,53,50,53,57,69,70,65,64,66,66,64,61,65,69,61,67,71,74,71,77,75,85,88,95,
              93,96,89,95,98,110,134,127,132,107,94,79,72,68,72,70,66,62,62,60,59,61,67,
              74,87,112,134,51,50,38,40,44,54,52,51,48,50,49,49,48,57,52,53,50,50,55,50,
              55,60,65,67,75,66,65,65,69,72,93,137,125,110,93,72,61,55,51,52,50,46,46,45,
              48,44,45,53,55,65,89,112,38,7,39,35,37,41,51,53,57,52,57,51,52,49,48,48,51,
              54,48,50,50,53,56,64,71,74,66,69,71,75,84,93,107,111,112,90,75,62,53,51,52,
              51,49,48,49,52,50,50,59,58,69,95,148,49,83,40,40,40,53,57,54,52,56,53,55,
              55,51,54,45,49,46,52,49,50,57,58,63,73,66,63,72,72,71,77,105,97,104,85,73,
              66,55,52,50,52,48,48,46,48,53,49,58,56,72,84,124,76,4,40,39,36,38,48,55,49,
              51,48,46,46,47,44,44,45,43,48,46,45,50,50,56,62,53,62,63)
    
    data2 <- c(rnorm(237))
    
    # Make data a time series, starting Jan 2009
    data.ts<-ts(data, start=c(2009,1),frequency=52)
    data2.ts<-ts(data2, start=c(2009,1),frequency=52)
    
    # Plot just the time series
    plot(data.ts)
    
    # Do the arima (and other functions you wish)
    fit <- arima(data.ts)
    fit2 <- arima(data2.ts)
    # This part should solve your timeseries problem
    # h=1 specifies 1 frequency (or in this case, a week) ahead
    data.forecast <- forecast(fit, h=1)
    data2.forecast <- forecast(fit2,h=1)
    
    #plot the forecast data
    plot(data.forecast)
    
    # suppose you have another data set, surpress the first graph
    par(new=T)
    
    # plot the next graph
    plot(data2.forecast)
    

    如果您需要进一步详细说明,请与我们联系 .

相关问题