Drawing A Lag Plot Of A Series Using Pandas

Overview:

  • A lag in a time-series data is how much one point is falling behind in time from another data point.
  • In a time-series data, data points are marked over time at varying degrees of intervals.
  • To analyse and find out if a time-series data follows any pattern, a lag plot can be employed.
  • A lag plot is drawn by representing the time series data in x-axis and the lag of the time series data point in y axis. For a data point, if the order of the lag is one, the lag is the previous data point. If the lag is two, the lag is data point before two data points in time.
  • By drawing a lag plot, patterns like randomness, trends and seasonality can be searched for.

Lag plot through the plotting module of pandas:

  • The pandas library provides a plotting module that has interafce for drawing several statistical plots.
  • The function lag_plot() draws a lag plot for a given time series-data as a pandas series and for the given lag.

Example 1 - Lag plot showing strong auto correlation in the time-series data:

# Example Python program that draws a lag plot
import pandas as pds
import matplotlib.pyplot as plt

# Create a pandas Series
s1 = pds.Series([11669.15, 11813.50, 11908.50,
                 12120.30, 12263.55, 12461.05,    
                 12631.10, 12749.15, 12690.80,
                 12719.95, 12780.25, 12874.20,
                 12938.25, 12771.70]);    

# Draw a lag plot
pds.plotting.lag_plot(s1, lag=1);
plt.title("Lag plot with lag=1");
plt.show(block=True);

Output:

The example shows that the data exhibits high auto correlation behaviour as the points in the lag plot lie around the diagonal line.

Lag plot high auto correlation

Example 2 - Lag plot showing weak auto correlation in the time-series data:

# Example Python program
# Data Courtesy:https://archive.ics.uci.edu/ml/datasets/Air+Quality
import pandas as pds
import matplotlib.pyplot as plt

# Hourly averaged sensor response
hourlyAverage = pds.Series([62, 609, 561, 21, 512, 553,
                            667, 174, 960, 827, 762, 774,
                            869, 1034, 933, 912, 1020, 1319,
                            1488, 1404, 1076, 749]);

pds.plotting.lag_plot(hourlyAverage, lag=1);
plt.title("Lag plot with lag=1, showing weak auto correlation in the data");
plt.show(block=True);

Output:

Lag plot showing weak auto correlation in the data

 


Copyright 2023 © pythontic.com