Seasonal Adjustment and Differencing Implementation

Step 1: Install Necessary Libraries

!pip install pandas
!pip install statsmodels
!pip install matplotlib

Step 2: Import Libraries

Python3

import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

                    

Step 3: Load Dataset

Python3

url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv'
df = pd.read_csv(url)
df['Month'] = pd.to_datetime(df['Month'])
df.set_index('Month', inplace=True)

                    

Step 4: Seasonal Adjustment

Here, 'seasonal_decompose' function is used from the Statsmodels library in to decompose a time series into its trend, seasonal, and residual (irregular or error) components. Multiplicative decomposition is used with a period of 12 (assuming monthly data), meaning that the seasonal component repeats every 12 observations.

Python3

# Seasonal adjustment
result = sm.tsa.seasonal_decompose(df['Passengers'], model='multiplicative', period=12)
df['Seasonal_Adjusted'] = result.resid

                    

Step 5: Differencing

Python3

# Differencing
df['Differenced'] = df['Passengers'].diff()

                    

Step 6: Visualize the Results

Python3

# Visualize the results
plt.figure(figsize=(12, 8))
 
# Original time series
plt.subplot(3, 1, 1)
plt.plot(df['Passengers'])
plt.title('Original Time Series')
plt.xlabel('Month')
plt.ylabel('Passengers')
 
# Seasonal Adjusted
plt.subplot(3, 1, 2)
plt.plot(df['Seasonal_Adjusted'])
plt.title('Seasonal Adjusted Time Series')
plt.xlabel('Month')
plt.ylabel('Seasonal Adjusted Passengers')
 
# Differenced
plt.subplot(3, 1, 3)
plt.plot(df['Differenced'])
plt.title('Differenced Time Series')
plt.xlabel('Month')
plt.ylabel('Differenced Passengers')
 
plt.tight_layout()
plt.show()

                    

Output:

In this part, a subplot containing three panels that each highlight a distinct time series feature is created using Matplotlib

  • The original time series is shown in the first subplot.
  • The adjusted for seasons time series is shown in the second subplot.
  • The distinct time series data are shown in the third subplot.
  • The plot is seen in full by using the plt.show() function.

The output displays the seasonally adjusted time series, the differenced time series, and the original time series of airline passenger counts. The original data’s seasonal oscillations are eliminated from the seasonally adjusted time series, which facilitates the identification of the underlying trend. The data are stationary due to the differenced time series, which is crucial for several statistical studies.

Seasonal Adjustment and Differencing in Time Series

Time series data can be difficult to evaluate successfully because of the patterns and trends it frequently displays. To address these tendencies and improve the data’s suitability for modeling and analysis, two strategies are employed: seasonal adjustment and differencing.

Table of Content

  • Seasonal Adjustment
  • Seasonal Differencing
  • Seasonal Adjustment and Differencing Implementation

Similar Reads

Seasonal Adjustment

Seasonal adjustment is a statistical technique used to remove the effects of regular, repeating patterns (seasonal variations) from a time series dataset. Seasonal variations refer to the predictable fluctuations or patterns that occur at specific intervals, often corresponding to certain times of the year, months, weeks, or days. These patterns can obscure the underlying trends and make it challenging to analyze the true behavior of the data....

Seasonal Differencing

The process of calculating the differences between successive observations in a given time series is known as differencing. Higher-order differences may be obtained by further differentiating the resultant series, which is referred to as the first difference....

Seasonal Adjustment and Differencing Implementation

Step 1: Install Necessary Libraries...

Conclusion

...