dd-ie: Double Demeaning for Fixed Effects Interactions
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:
- Demean each variable within units:
X* = X - mean_i(X),Z* = Z - mean_i(Z) - Create the interaction from demeaned variables:
X* x Z* - 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_dataComparisonResult-- coefficient comparison.table(DataFrame) and.interaction_differenceHausmanResult-- 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
Metadata
Release files for ddinteract 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ddinteract-0.2.0.tar.gz | 170.8 kB | Without provenance | Details for ddinteract-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ddinteract-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for ddinteract-0.2.0-py3-none-any.whl |
Total release size: 187.2 kB
Release files / ddinteract-0.2.0.tar.gz
| Download URL | ddinteract-0.2.0.tar.gz |
|---|---|
| Size | 170.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2eddab3956c346744b19e21e021795c6b2e6c111d8201244f6176ec25fd26a61
|
|
BLAKE2b-256 checksum How to use checksums |
da61baedbf0803ac986c47aca646cc2c5209cc68652b9ef4eff7e8f5cc68d520
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.11
|
Release files / ddinteract-0.2.0-py3-none-any.whl
| Download URL | ddinteract-0.2.0-py3-none-any.whl |
|---|---|
| Size | 16.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
29c78d2b9be271535c8e30de28d27157d7d32eab30977a7c2d65b7abcf91e2a9
|
|
BLAKE2b-256 checksum How to use checksums |
75ec99ead31dcd598f5b42ccb8af8ab72c0b5fd706601d16d03d29c4fca7764f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.11
|
Security for ddinteract 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.