How to Explore Descriptive Statistics with statsmodels

How to Explore Descriptive Statistics with statsmodels
Image by Editor | Midjourney & Canva

Descriptive statistics help summarize and understand data. They show important features of a dataset. Python’s statsmodels library has useful tools for calculating these statistics. They are important for analyzing data and making decisions.

This article will show you how to use statsmodels to explore descriptive statistics.

What Are Descriptive Statistics?

Descriptive statistics help summarize large datasets using key metrics. These include:

  • Measures of Central Tendency: Mean, median, and mode.
  • Measures of Dispersion: Variance, standard deviation, range, and interquartile range.
  • Shape of Distribution: Skewness and kurtosis.

By using these metrics, we can gain valuable insights before applying more complex statistical techniques.

Installing and Importing Dependencies

To begin using statsmodels for descriptive statistics, ensure you have it installed:

pip install statsmodels

Next, import the necessary libraries:

import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels.stats.stattools as stattools
import matplotlib.pyplot as plt

Creating or Loading a Dataset

Let’s create a sample dataset representing sales figures for a store over 50 days.

# Set random seed for reproducibility
np.random.seed(42)

# Generate random sales data
sales = np.random.normal(loc=500, scale=50, size=50)  # Mean = 500, Std Dev = 50

# Convert to DataFrame
df = pd.DataFrame({'Sales': sales})

# Display first few rows
print(df.head())

dataset

Computing Descriptive Statistics

1. Mean and Median

The mean and median are fundamental measures of central tendency that help summarize a dataset:

  • Mean: The arithmetic average, calculated as the sum of all values divided by the total count.
  • Median: The middle value when the data is sorted.
mean_value = desc_stats.mean
median_value = desc_stats.quantile(0.5)  # 50th percentile (median)

print(f"Mean Sales: {mean_value}")
print(f"Median Sales: {median_value}")
  • desc_stats.mean: Returns the mean (average) of the dataset.
  • desc_stats.quantile(0.5): Computes the median (50th percentile).
Mean Sales: 488.726304737193
Median Sales: p
0.5    488.292742
dtype: float64

2. Variance and Standard Deviation

Variance and standard deviation are key measures of dispersion that describe how spread out the data points are from the mean.

  • Variance: Measures the average squared deviation from the mean. A higher variance indicates greater spread in the data.
  • Standard Deviation: The square root of variance. It provides a measure of spread in the same units as the data.
variance = desc_stats.var  # Variance
std_dev = desc_stats.std  # Standard deviation

print(f"Variance: {variance}")
print(f"Standard Deviation: {std_dev}")
  • desc_stats.var: Computes the variance.
  • desc_stats.std: Computes the standard deviation.
Variance: 2135.756615944579
Standard Deviation: 46.21424689362123

3. Skewness and Kurtosis

Skewness and kurtosis are statistical measures that describe the shape of a data distribution.

  • Skewness: Measures the asymmetry of the data distribution.
  • Kurtosis: Measures the “tailedness” of the distribution.
skewness = stattools.robust_skewness(df['Sales'])
kurtosis = stattools.robust_kurtosis(df['Sales'])

print(f"Skewness: {skewness}")
print(f"Kurtosis: {kurtosis}")

  • stattools.robust_skewness(): Computes a robust measure of skewness.
  • stattools.robust_kurtosis(): Computes a robust measure of kurtosis.
Skewness: (0.13593089182491083, -0.04711881997266495, 0.011760323493343494, 0.00938158810645305)
Kurtosis: (-0.45326173151243765, 0.10570348431825849, -0.19134527524705547, -0.027555480273124378)

Visualizing Descriptive Statistics

To better understand our sales data, we can plot visualizations.

1. Histogram

A histogram helps visualize the distribution of sales data.

plt.hist(df['Sales'], bins=10, edgecolor='black', alpha=0.7)
plt.xlabel("Sales")
plt.ylabel("Frequency")
plt.title("Sales Distribution")
plt.show()

histogram

  • The x-axis represents sales values.
  • The y-axis represents frequency.
  • The bins divide the data into intervals.

2. Box Plot

A box plot displays the spread and outliers.

plt.boxplot(df['Sales'])
plt.ylabel("Sales")
plt.title("Box Plot of Sales")
plt.show()

boxplot

  • The box represents the interquartile range (IQR).
  • The line inside the box represents the median.
  • The whiskers show data spread, and dots outside represent outliers.

Conclusion

Exploring descriptive statistics using statsmodels helps in understanding data distribution, spread, and shape. Key metrics like mean, variance, skewness, and kurtosis give useful insights into the data.

By applying these techniques, you can perform a thorough exploratory analysis before moving to advanced modeling.

Leave a Reply

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