Time Series Analysis using ARIMA model in R Programming
Last Updated :
07 Jul, 2025
Time series analysis using the ARIMA (AutoRegressive Integrated Moving Average) model in R is a method to analyze and forecast data that changes over time. R provides functions like arima() and auto.arima() from the forecast package to model time series data. These functions help fit the model to historical data and predict future values based on patterns in the series.
Time Series Analysis
Time series analysis is the process of examining data points collected in time order to understand trends, seasonal patterns or behaviors. It helps predict future values based on past observations. Common examples include analyzing stock prices, weather data and monthly sales.
ARIMA Model
The ARIMA model is a forecasting technique that combines three elements: autoregression (AR), differencing (I) and moving average (MA). It is written as ARIMA(p, d, q), where p is the order of autoregression, d is the number of times the data is differenced to become stationary and q is the order of the moving average.
Components of ARIMA
- AR (AutoRegressive): Uses past values to predict the current value.
- I (Integrated): Applies differencing to remove trends and make the data stationary.
- MA (Moving Average): Uses past forecast errors to improve predictions.
Types of ARIMA Models
- ARIMA: For non-seasonal time series data.
- SARIMA: Seasonal ARIMA, used when the data shows seasonality.
- SARIMAX: Seasonal ARIMA with exogenous variables, includes external factors to improve accuracy.
Implementation of Forecasting with BJsales Dataset Using ARIMA Model
We forecast the next 10 values from the BJsales dataset using the ARIMA model in R.
1. Installing the required package
We install the forecast
package to access functions for time series forecasting.
- install.packages(): used to download and install R packages.
- forecast: used to create ARIMA models and generate forecasts.
R
install.packages("forecast")
2. Loading the required library
We load the forecast library to apply ARIMA and forecast-related functions.
- library(): used to load an installed package into the session.
- forecast: contains auto.arima() and forecast() functions.
R
3. Plotting the original time series
We plot the original BJsales time series data without any forecasting.
- plot(): used to create a line graph of the dataset.
- BJsales: a built-in time series dataset of monthly sales.
R
plot(BJsales, main = "Graph without forecasting", col.main = "darkgreen")
Output:
Output4. Fitting the ARIMA model and forecasting
We apply auto.arima() to the BJsales data and forecast the next 10 values.
- auto.arima(): used to automatically select the best ARIMA model.
- forecast(): used to generate future values from the fitted model.
- print(): used to display forecast results in the console.
- plot(): used to display the forecasted time series with prediction intervals.
R
fit <- auto.arima(BJsales)
forecastedValues <- forecast(fit, 10)
print(forecastedValues)
plot(forecastedValues, main = "Graph with forecasting", col.main = "darkgreen")
Output:
OutputThe output displays the next 10 forecasted sales values from the ARIMA model along with 80% and 95% confidence intervals. The plot visualizes these forecasts, showing the projected trend and uncertainty bands over time.