Skip to main content

Double demeaning for unbiased estimation of interactions in fixed effects models

Project description

dd-ie: Double Demeaning for Fixed Effects Interactions

CI PyPI Python 3.9+ License: MIT Typed

A Python implementation of the double demeaning technique for unbiased estimation of interactions in fixed effects regression models.

The Problem

Standard fixed effects interactions (X x Z) may be biased when both variables vary within units and correlate with unobserved unit-specific moderators. The standard FE interaction estimator (FE-IE) uses between-unit differences in the effects of the interacted variables, making it susceptible to omitted variable bias from time-constant unobserved moderators.

The Solution

Double demeaning (dd-IE) provides an unbiased within-unit interaction estimator:

  1. Demean each variable within units: X* = X - mean_i(X), Z* = Z - mean_i(Z)
  2. Create the interaction from demeaned variables: X* x Z*
  3. Use this in a standard FE regression (which demeans it again)

This eliminates all between-unit heterogeneity from the interaction term.

Installation

pip install dd-ie

For development:

git clone https://github.com/nkoutoun/dd-ie.git
cd dd-ie
pip install -e ".[dev]"

Quick Start

import pandas as pd
from dd_ie import DoubleDemeanAnalysis

# Load panel data
df = pd.read_csv("your_data.csv")

# Run analysis
analysis = DoubleDemeanAnalysis(
    data=df,
    unit_var="unit_id",      # Unit identifier
    time_var="time_id",      # Time identifier
    y_var="outcome",         # Dependent variable
    x_var="treatment",       # First interacting variable
    z_var="moderator",       # Second interacting variable
    w_vars=["control1"],     # Control variables (optional)
)

results = analysis.run_analysis()

# Access structured results
print(results.comparison.table)          # Coefficient comparison
print(results.hausman.statistic)         # Hausman test statistic
print(results.hausman.p_value)           # Hausman p-value
print(results.hausman.conclusion)        # "SYSTEMATIC_BIAS" or "NO_SYSTEMATIC_BIAS"

Model Comparison

Standard FE (potentially biased)

Y = b_x * X + b_z * Z + b_xz * (X * Z) + g * W + a_i + e

Double Demeaned FE (unbiased)

Y = b_x * X + b_z * Z + b_xz * (X* x Z*) + g * W + a_i + e
where X* = X - mean_i(X), Z* = Z - mean_i(Z)

Result Types

The package returns typed dataclasses for all results:

  • AnalysisResult -- complete pipeline output with .standard_results, .dd_results, .comparison, .hausman, .processed_data
  • ComparisonResult -- coefficient comparison .table (DataFrame) and .interaction_difference
  • HausmanResult -- test .statistic, .p_value, .degrees_of_freedom, .conclusion, .positive_definite

All result types have a .to_dict() method for backward compatibility.

Advanced Usage

from dd_ie import (
    create_double_demeaned_interaction,
    estimate_fe_models,
    perform_hausman_test,
)

# Step-by-step analysis
df_processed = create_double_demeaned_interaction(df, "x", "z", "unit_id")
standard_results, dd_results, comparison = estimate_fe_models(
    df_processed, "y", "x", "z", ["control1"]
)
hausman = perform_hausman_test(standard_results, dd_results, "x", "z")

Data Requirements

  • Panel structure: unit and time identifiers
  • Minimum periods: T > 2 per unit for identification
  • Within-unit variation: both X and Z must vary within units
  • Format: long format with one row per unit-time observation

Interpreting Results

  • Hausman test p >= 0.05: no systematic bias detected; standard FE is more efficient
  • Hausman test p < 0.05: systematic bias detected; prefer the double-demeaned estimator

Citation

If you use this package, please cite the original methodology:

@article{giesselmann2022interactions,
  title={Interactions in Fixed Effects Regression Models},
  author={Giesselmann, Marco and Schmidt-Catran, Alexander W},
  journal={Sociological Methods \& Research},
  volume={51},
  number={3},
  pages={1100--1127},
  year={2022},
  publisher={SAGE Publications},
  doi={10.1177/0049124120914934}
}

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ddinteract-0.2.0.tar.gz (170.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ddinteract-0.2.0-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file ddinteract-0.2.0.tar.gz.

File metadata

  • Download URL: ddinteract-0.2.0.tar.gz
  • Upload date:
  • Size: 170.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for ddinteract-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2eddab3956c346744b19e21e021795c6b2e6c111d8201244f6176ec25fd26a61
MD5 610c0b7ab7262e3d6cf230bd5b2e6b38
BLAKE2b-256 da61baedbf0803ac986c47aca646cc2c5209cc68652b9ef4eff7e8f5cc68d520

See more details on using hashes here.

File details

Details for the file ddinteract-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ddinteract-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for ddinteract-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29c78d2b9be271535c8e30de28d27157d7d32eab30977a7c2d65b7abcf91e2a9
MD5 46d8acc29317d8e1841fbd827df156b6
BLAKE2b-256 75ec99ead31dcd598f5b42ccb8af8ab72c0b5fd706601d16d03d29c4fca7764f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page