In this guide, we will discuss 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:
Timeseries_object_name<- ts(data, start, end, frequency)
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 months and plot it.
# Getting the data points in form of a R vector. snowfall <- c(790,1170.8,860.1,1330.6,630.4,911.5,683.5,996.6,783.2,982,881.8,1021) # Convertting it into a time series object. snowfall_timeseries<- ts(snowfall,start = c(2013,1),frequency = 12) # Printing the timeseries data. print(snowfall_timeseries) # Giving a name to the chart file. png(file = "snowfall.png") # Plotting a graph of the time series. plot(snowfall_timeseries) # Saving the file. dev.off()
Output:
What is Stationary Time Series?
A Stationary Time Series is a time series, if:
- The mean value of the time-series is constant over time. This implies that the trend component is declared null.
- The variance should not increase over time.
- 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:
t 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.
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
#Importing library fpp library(fpp) #Using ausbeer data data(ausbeer) #Creating time series for ausbeer dataset timeserie.beer = tail(head(ausbeer, 17*4+2),17*4-4) # Giving a name to the chart file. png(file = "time.png") plot(as.ts(timeserie_beer), col="magenta") # Saving the file. dev.off()
Output:
For Multiplicative Model
#Importing library Ecdat library(Ecdat) #Using AirPassengers data data(AirPassengers) #Creating time series for AirPassengers dataset timeserie_air = AirPassengers # Giving a name to the file. png(file = "time.png") plot(as.ts(timeserie_air)) # Saving the file. dev.off()
Output:
Step 2: Detecting the Trend
For Additive Model
#Detecting trend trend.beer = ma(timeserie.beer, order = 4, centre = T) # Giving a name to the file. png(file = "time.png") plot(as.ts(timeserie.beer),col="red") lines(trend.beer,col="red") plot(as.ts(trend.beer),col="red") # Saving the file. dev.off()
Output1:
Output2:
For Multiplicative Model:
#Detecting trend trend.air = ma(timeserie.air, order = 12, centre = T) # Giving a name to the file. png(file = "time.png") plot(as.ts(timeserie.air),col="blue") lines(trend.air,col="blue") plot(as.ts(trend.air),col="blue") # Saving the file. dev.off()
Output1:
Output2:
Step 3: Detrend of Time Series
For Additive Model
#Detrend the time series. detrend.beer=timeserie.beer-trend.beer # Giving a name to the file. png(file = "time.png") plot(as.ts(detrend.beer),col="magenta") # Saving the file. dev.off()
Output:
For Multiplicative Model
#Detrend of time series detrend.air=timeserie.air / trend.air # Giving a name to the file. png(file = "time.png") plot(as.ts(detrend.air),col="blue") # Saving the file. dev.off()
Output:
Step 4: Average the Seasonality
For Additive Model
#Average the seasonality m.beer = t(matrix(data = detrend.beer, nrow = 4)) seasonal.beer = colMeans(m.beer, na.rm = T) # Giving a name to the file. png(file = "time.png") plot(as.ts(rep(seasonal.beer,16)),col="magenta") # Saving the file. dev.off()
Output:
For Multiplicative Model
#Average the seasonality m.air = t(matrix(data = detrend.air, nrow = 12)) seasonal.air = colMeans(m.air, na.rm = T) # Giving a name to the file. png(file = "time.png") plot(as.ts(rep(seasonal.air,12)),col="blue") # Saving the file. dev.off()
Output:
Step 5: Examining the Remaining Random Noise
For Additive Model
# Examining the Remaining Random Noise random.beer = timeserie.beer - trend.beer - seasonal.beer # Giving a name to the file. png(file = "time.png") plot(as.ts(rep(random.beer)),col="magenta") # Saving the file. dev.off()
Output:
For Multiplicative Model
# Examining the Remaining Random Noise random.air = timeserie.air / (trend.air * seasonal.air) # Giving a name to the file. png(file = "time.png") plot(as.ts(random.air),col="blue") # Saving the file. dev.off()
Output:
Step 5: Reconstruction of Original Signal
For Additive Model
#Reconstruction of original signal recomposed.beer=trend.beer+seasonal.beer+random.beer # Giving a name to the file. png(file = "time.png") plot(as.ts(recomposed.beer),col="magenta") # Saving the file. dev.off()
Output1:
For Multiplicative Model
#Reconstruction of original signal recomposed.air = trend.air*seasonal.air*random.air # Giving a name to the file. png(file = "time.png") plot(as.ts(recomposed.air),col="blue") # Saving the file. dev.off()
Output:
Time Series Decomposition using decompose()
For Additive Model
#Importing libraries library(forecast) library(timeSeries) library(fpp) #Using ausbeer data data(ausbeer) #Creating time series timeserie.beer = tail(head(ausbeer, 17*4+2),17*4-4) #Detect trend trend.beer = ma(timeserie.beer, order = 4, centre = T) #Detrend of time series detrend.beer=timeserie.beer-trend.beer #Average the seasonality m.beer = t(matrix(data = detrend.beer, nrow = 4)) seasonal.beer = colMeans(m.beer, na.rm = T) #Examine the remaining random noise random.beer = timeserie.beer - trend.beer - seasonal.beer #Reconstruct the original signal recomposed.beer = trend.beer+seasonal.beer+random.beer #Decomposed the time series tsts.beer = ts(timeserie.beer, frequency = 4) decomposedecompose.beer = decompose(ts.beer, "additive") # Giving a name to the file. png(file = "time.png") par(mfrow=c(2,2)) plot(as.ts(decompose.beer$seasonal),col="magenta") plot(as.ts(decompose.beer$trend),col="magenta") plot(as.ts(decompose.beer$random),col="magenta") plot(decompose.beer,col="magenta") # Saving the file. dev.off()
Output:
For Multiplicative Model
#Importing libraries library(forecast) library(timeSeries) library(fpp) library(Ecdat) #Using Airpassengers data data(AirPassengers) #Creating time series timeseries.air = AirPassengers #Detect trend trend.air = ma(timeseries.air, order = 12, centre = T) #Detrend of time series detrend.air=timeseries.air / trend.air #Average the seasonality m.air = t(matrix(data = detrend.air, nrow = 12)) seasonal.air = colMeans(m.air, na.rm = T) #Examine the remaining random noise random.air = timeseries.air / (trend.air * seasonal.air) #Reconstruct the original signal recomposed.air = trend.air*seasonal.air*random.air #Decomposed the time series tsts.air = ts(timeseries.air, frequency = 12) decomposedecompose.air = decompose(ts.air, "multiplicative") # Giving a name to the file. png(file = "time.png") par(mfrow=c(2,2)) plot(as.ts(decompose.air$seasonal),col="blue") plot(as.ts(decompose.air$trend),col="blue") plot(as.ts(decompose.air$random),col="blue") plot(decompose.air,col="blue") # Saving the file. dev.off()
Output:
Next Topic: Click Here
Thank you ever so for you blog post.Really looking forward to read more. Really Great.