Did causal analysis

From UCSB-NLP-Chang/Skill-Usage · View interactive version

Did causal analysis is an AI agent skill from the GitHub repository UCSB-NLP-Chang/Skill-Usage. Difference-in-Differences causal analysis to identify demographic drivers of behavioral changes with p-value significance testing. Use for event effects, A/B testing, or policy evaluation.

How to use this skill

Skill file

Difference-in-Differences Framework

DiD framework for causal inference with automatic method selection (Multivariate vs Univariate) based on sample size.

Methodology

Intensive Margin

Extensive Margin

Regression Methods

Both methods use: Y = α + βX + γPost + δ(X × Post) + ε where δ is the DiD estimate.

Multivariate Heterogeneous DiD

Univariate DiD

CRITICAL: Both use statsmodels regression to provide p-values for statistical inference.

⚠️ CRITICAL: Data Preparation Requirements

Intensive Margin - Sparse Data (Purchasers/Participants Only)

CORRECT approach - Only aggregate actual transactions:

# Group actual transactions by entity-period
intensive_df = transactions.groupby(['entity_id', 'period'])['value'].sum().reset_index()
# Result: Only rows where events occurred (e.g., 3,404 rows for active participants)

Why this matters: Intensive margin measures "how much MORE did participants engage", not "did non-participants start engaging". Including zeros for non-participants dilutes the effect and changes the research question.

Extensive Margin - Complete Panel (All Users)

CORRECT approach - Create complete entity × group × period grid:

# Create all possible combinations
from itertools import product
all_entities = entities_df['entity_id'].unique()
all_groups = target_groups  # e.g., categories, regions, products
all_periods = ['baseline', 'treatment']

complete_grid = pd.DataFrame(
    list(product(all_entities, all_groups, all_periods)),
    columns=['entity_id', 'period']
)

# Mark who participated (1) vs who didn't (0)
events_lookup = transactions[['entity_id',  'period']].drop_duplicates()
events_lookup['participated'] = 1

extensive_df = complete_grid.merge(events_lookup, how='left')
extensive_df['participated'] = extensive_df['participated'].fillna(0).astype(int)
# Result: All entities × groups × periods with binary outcome

Why this matters: Extensive margin measures "did the participation rate increase", which requires knowing both who participated AND who didn't participate.

API

Constructor Parameters `DIDAnalyzer(min_sample_ratio=10, significance_level=0.05, min_group_size=1)`

`intensive_margin(df, features, value_col, top_n=None, sort_by='estimate', asc=False)`

Analyzes intensive margin: how much behavior changed among participants.

Input Data: Sparse entity-period data (only participants)

Returns: List of {"feature": str, "did_estimate": float, "p_value": float, "method": str}

`extensive_margin(df, features, value_col='purchased', top_n=None, sort_by='estimate', asc=False)`

Analyzes extensive margin: how many people changed behavior.

Input Data: Complete entity-period panel (all entities including non-participants)

Returns: List of {"feature": str, "did_estimate": float, "p_value": float, "method": str}

Internal Method Selection Logic

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.