Building an automated ARIMA model

The forecast package provides the auto.arima function to fit the best ARIMA models for a univariate time series.

Getting ready

If you have not already downloaded the files for this chapter, do it now and place them in your R working directory. Install and load the forecast package.

How to do it...

To build an automated ARIMA model, follow these steps:

  1. Read the data. The file has monthly stock prices from Yahoo! Finance for Infosys between March 1999 and January 2015:
    > infy <- read.csv("infy-monthly.csv")
  2. Create the time series object:
    > infy.ts <- ts(infy$Adj.Close, start = c(1999,3), frequency = 12)
  3. Run the ARIMA model:
    > infy.arima <- auto.arima(infy.ts)
  4. Generate the forecast using the ARIMA model:
    > infy.forecast <- forecast(infy.arima, ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.