Forecasting an Arima Model in R Returning Strange Error

4.4k Views Asked by At

I'm working on building a Shiny App for forecasting time series. One component of this is using ARIMA models to forecast. The user specifies the start and end of the historical data, what p, d, and q they would like to use in the ARIMA model (if they don't want to use auto.arima), and how many periods to forecast for.

Everything works fine for auto.arima, as well as other forecasting methods I'm using such as Holt and Holt-Winters. However, when using arima() to create a model with custom p, d, and q, I get the following error from forecast():

    Warning in .cbind.ts(list(e1, e2), c(deparse(substitute(e1))[1L],
deparse(substitute(e2))[1L]),  : non-intersecting series

What's strange to me is that when I comment out the start and end arguments in the function that creates the time series, everything runs fine....

Here's the relevant bits of my code:

First, read in the specified csv file containing the values to be forecasted in the second column, and prepare that column to be forecasted:

## Reads in the historical data from the uploaded file
readData <- reactive({
    inFile <- input$inputFile
    if (is.null(inFile))
      return(NULL)
    data <- read.csv(inFile$datapath)
    data
  })

  ## Returns the historical values to be used in forecasting
  historical <- reactive({
    data <- readData()
    ## Remove any commas and dollar signs, and convert to a number
    data[,2] <- as.numeric(sub("\\,","",
                               sub("\\$","",data[,2])
                               )
                           )
    data
  })

Create a time series:

      ## Converts the historical data to a time series
      tsData <- reactive({
        data <- historical()
        data <- data[,2]
        ts <- ts(data,
                 start=c(input$startYear, input$startTime), 
                 end=c(input$endYear, input$endTime), 
                 frequency = strtoi(input$frequency)
        )
        ts
      })

Create the ARIMA model:
  ## Create an ARIMA model for forecasting
  arimaModel <- reactive({
    ts <- tsData()
    if(input$arimaAuto){
      fit <- auto.arima(ts)
    }
    else{
       fit <- arima(ts,order=c(strtoi(input$arimaP),
                              strtoi(input$arimaD), 
                              strtoi(input$arimaQ)))
    }
    fit
  })

And last but not least, forecasting - the part that throws the strange warning message:

  ## Creates an ARIMA model and returns a forecast based on that model.
  arimaData <- reactive({
    fit <- arimaModel()
    print("Fit: ")
    print(fit)
    f <- forecast(fit#,
                  #h = input$forecast_periods,
                  #level=c(strtoi(input$confidence1), strtoi(input$confidence2))
    )
    print("Forecast:")
    print(f)
    f
  })

Thanks in advance for any help! :)

1

There are 1 best solutions below

0
On BEST ANSWER

The issue wound up being that I was using the arima(...) function instead of Arima(...). It turns out that they are two different functions. The issue that I was experiencing was a result of differences in how the functions store their data. More information about this can be found in this post. The good news is, everything is now working like a charm.