Time Series Analysis using-ARIMA



Time series analysis is a type of analysis of data used to check the behaviour of data over a period of time. The data is collected over time sequentially by the ts() function along with some parameters. It helps in analysing the pattern of the data over a graph. There are many techniques used to forecast the time series object over the plot graph but the ARIMA model is the most widely used approach out of them.

Time Series Forecasting

Time series forecasting is a process of predicting future values with the help of some statistical tools and methods used on a data set with historical data. Some of the applications of time series forecasting are:
  • Predicting stock prices
  • Forecast weather
  • Forecast the sales of a product

ARIMA mode

ARIMA stands for AutoRegressive Integrated Moving Average and is specified by three order parameters: (p, d, q).
AR(p) Autoregression: A regression model that utilizes the dependent relationship between a current observation and observations over a previous period.An auto regressive (AR(p)) component refers to the use of past values in the regression equation for the time series.

I(d) Integration: Uses differencing of observations (subtracting an observation from observation at the previous time step) in order to make the time series stationary. Differencing involves the subtraction of the current values of a series with its previous values d number of times.

MA(q) Moving Average: A model that uses the dependency between an observation and a residual error from a moving average model applied to lagged observations. A moving average component depicts the error of the model as a combination of previous error terms. The order q represents the number of terms to be included in the model.

Types of ARIMA Model

  • ARIMA: Non-seasonal Autoregressive Integrated Moving Averages
  • SARIMA: Seasonal ARIMA
  • SARIMAX: Seasonal ARIMA with exogenous variables

Implementation of ARIMA model in R

In R programming, arima() function is used to perform this technique. ARIMA model is used to fit a univariate data. auto.arima() function returns the best ARIMA model by searching over many models.

Syntax:
auto.arima(x)

Parameters:
x: represents univariate time series object

To know about more optional parameters, use below command in the console: help(“auto.arima”)Example 1:

Example1:

In this example, let’s predict the next 10 sale values by using BJsales dataset present in R packages. This dataset is already a time series object, so there is no need to apply ts() function.
# R program to illustrate
# Time Series Analysis
# Using ARIMA model in R

# Install the library for forecast()
install.packages("forecast")

# library required for forecasting
library(forecast)

# Output to be created as png file
png(file = "TimeSeriesGFG.png")

# Plotting graph without forecasting
plot(BJsales, main = "Graph without forecasting",
col.main = "darkgreen")

# Saving the file
dev.off()

# Output to be created as png file
png(file = "TimeSeriesARIMAGFG.png")

# Fitting model using arima model
fit <- auto.arima(BJsales)

# Next 10 forecasted values
forecastedValues <- forecast(fit, 10)

# Print forecasted values
print(forecastedValues)

plot(forecastedValues, main = "Graph with forecasting",
col.main = "darkgreen")
# saving the file
dev.off()

Outputs:
Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
151       262.8620 261.1427 264.5814 260.2325 265.4915
152       263.0046 260.2677 265.7415 258.8189 267.1903
153       263.1301 259.4297 266.8304 257.4709 268.7893
154       263.2405 258.5953 267.8857 256.1363 270.3447
155       263.3377 257.7600 268.9153 254.8074 271.8680
156       263.4232 256.9253 269.9211 253.4855 273.3608
157       263.4984 256.0941 270.9028 252.1744 274.8224
158       263.5647 255.2691 271.8602 250.8778 276.2516
159       263.6229 254.4529 272.7930 249.5986 277.6473
160       263.6742 253.6474 273.7011 248.3395 279.0089


Explanation:
Following output is produced by executing the above code. 10 next values are predicted by using forecast() function based on ARIMA model of BJsales dataset. First graph shows the visuals of BJsales without forecasting and second graphs shows the visuals of BJsales with forecasted values.

Comments

Popular posts from this blog

Programming in R - Dr Binu V P

Introduction

R Data Types