Javatpoint Logo
Javatpoint Logo

R-Time Series Analysis

Any metric which is measured over regular time intervals creates a time series. Analysis of time series is commercially important due to industrial necessity and relevance, especially with respect to the forecasting (demand, supply, and sale, etc.). A series of data points in which each data point is associated with a timestamp is known as time series.

The stock price at different points in a day in the stock market is the simplest example of the time series. The amount of rainfall in an area in different months of the year is another example of it. R provides several functions for creating, manipulating, and plotting time series data. In the R-object, the time series data is known as the time-series object. It is just like a vector or data frame.

Creating a Time Series

R provides ts() function for creating a Time Series. There is the following syntax of the ts() function:

Here,

S.No Parameter Description
1. data It is a vector or matrix which contains the value used in time series.
2. start It is the start time for the first observation
3. end It is the end time for the last observation
4. frequency It specifies the number of observations per unit time.

Let's see an example to understand how ts() function is used for creating Time Series.

Example:

In the below example, we will consider the annual snowfall details at a place starting from January 2013. We will create an R time series object for a period of 12 month and plot it.

Output:

R Time Series Analysis
R Time Series Analysis

What is Stationary Time Series?

A Stationary Time Series is a time series, if:

  1. The mean value of the time-series is constant over time. This implies that the trend component is declared null.
  2. The variance should not increase over time.
  3. The seasonality effect should be minimal.

This means that it is devoid of or trendseasonal patterns, which resemble a random white noise regardless of the time interval observed.

In simple words, a stationary time series is the one whose statistical properties like mean, variance, and autocorrelation, etc. are all constant over time.

Extracting the trend, seasonality, and error

We can decompose the time series by splitting it into three components, such as seasonality, trends, and random fluctuations.

Time series decomposition is a mathematical process that transforms a time series into multiple time series.

Seasonal:

Patterns that repeat over a certain period of time

Trend:

An underlying trend of the matrices.

Random:

It is the residuals of the original time series after the seasonal and trend series are removed.

Additive and Multiplicative Decomposition

Additive and Multiplicative decompositions are the models that are used for analyzing the series. When the seasonal variation seems to be constant means when the seasonal variation does not change at the time when the value of the time series increases, then we use the Additive model else we use the multiplicative model.

R Time Series Analysis

Let's see a step by step procedure to understand how we can decompose the time series using both Additive and Multiplicative model. For the additive model, we use ausbeer dataset, and for multiplicative, we use the AirPassengersdataset.

Step 1: Load data and create time series

For Additive Model

Output:

R Time Series Analysis

For Multiplicative Model

Output:

R Time Series Analysis

Step 2: Detecting the Trend

For Additive Model

Output1:

R Time Series Analysis

Output2:

R Time Series Analysis

For Multiplicative Model:

Output1:

R Time Series Analysis

Output2:

R Time Series Analysis

Step 3: Detrend of Time Series

For Additive Model

Output:

R Time Series Analysis

For Multiplicative Model

Output:

R Time Series Analysis

Step 4: Average the Seasonality

For Additive Model

Output:

R Time Series Analysis

For Multiplicative Model

Output:

R Time Series Analysis

Step 5: Examining the Remaining Random Noise

For Additive Model

Output:

R Time Series Analysis

For Multiplicative Model

Output:

R Time Series Analysis

Step 5: Reconstruction of Original Signal

For Additive Model

Output1:

R Time Series Analysis

For Multiplicative Model

Output:

R Time Series Analysis

Time Series Decomposition using decompose()

For Additive Model

Output:

R Time Series Analysis

For Multiplicative Model

Output:

R Time Series Analysis
Next TopicR-Random Forest





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA