TimeSeriesTrends

Step 1: Install Required Libraries

Make sure you have the following libraries installed:

pip install pandas matplotlib statsmodels

Step 2: Import Libraries

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

Step 3: Load Time Series Data

For this tutorial, let’s create a simple time series dataset. You can also load data from a CSV file or other sources.

# Creating a sample time series data
date_rng = pd.date_range(start='2020-01-01', end='2023-01-01', freq='D')
data = pd.DataFrame(date_rng, columns=['date'])
data['data'] = pd.Series(range(1, len(data) + 1)) + (pd.Series(range(len(data))) % 5).cumsum()  # Add some trend
data.set_index('date', inplace=True)

# Display the first few rows
print(data.head())

Step 4: Visualize the Data

Visualizing the data is crucial to identify any trends.

plt.figure(figsize=(12, 6))
plt.plot(data.index, data['data'], label='Time Series Data')
plt.title('Time Series Data')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.show()

Step 5: Decompose the Time Series

You can decompose the time series to analyze its trend, seasonality, and residuals.

decomposition = sm.tsa.seasonal_decompose(data['data'], model='additive')
fig = decomposition.plot()
plt.show()

Step 6: Identify Trends

To identify the trend component, you can simply extract it from the decomposition results.

trend = decomposition.trend
plt.figure(figsize=(12, 6))
plt.plot(data.index, trend, label='Trend', color='orange')
plt.title('Trend Component')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.show()

Step 7: Simple Moving Average

A simple moving average (SMA) can help smooth out short-term fluctuations and highlight longer-term trends.

data['SMA_7'] = data['data'].rolling(window=7).mean()

plt.figure(figsize=(12, 6))
plt.plot(data.index, data['data'], label='Original Data')
plt.plot(data.index, data['SMA_7'], label='7-Day SMA', color='red')
plt.title('Time Series with 7-Day SMA')
plt.xlabel('Date')
plt.ylabel('Value')
plt.legend()
plt.show()

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *