Difference-in-differences
Two-way fixed effects
Two-way fixed effects compare changes within units after removing common time shocks.
The task
Task statement
Estimate y_twocluster on x with firm and year fixed effects. Report the x coefficient with a standard error clustered on firm as row x.
Data
Panel of 200 firms by 10 years. Columns include firm, year, x, y_twocluster, cohort, treat_post, y_did, z_sel, w, selected, and y_sel.
- Files
- input.csv
Required output
Scored estimates: x.
The standard errors of x are also scored, because the main decision in this task is how to compute them.
Reference output
| Term | Reference estimate | Reference standard error | Sample size |
|---|---|---|---|
x | 0.53529929 | 0.024753181 | 2,000 |
A run succeeds when every scored estimate lies within 1 percent or 0.005 of its reference value, whichever is larger, and every scored standard error lies within 1 percent of the reference or of an accepted alternative convention. Sample sizes are not scored.
Task success
| Agency condition | Claude Sonnet 4.6 | GPT-5.6 Luna |
|---|---|---|
| Chatbot | 73.3% | 86.7% |
| Self-repair | 78.3% | 93.3% |
| Constrained agent | 100.0% | 100.0% |
- All runs
- 88.6% of 360 runs
- By prompt condition
- Zero-shot 89.4%, One-shot 87.8%
- By software
- Python 97.5%, R 94.2%, Stata 74.2%
Removing unit and time averages
The two-way fixed-effects model gives each unit and each date its own intercept:
The coefficient uses variation left after removing unit means and time means (Wooldridge, 2010). For a balanced panel, the transformed regressor is
A demeaning table
| Unit and time | $x_{it}$ | Unit mean | Time mean | Grand mean | $\widetilde x_{it}$ |
|---|---|---|---|---|---|
| A, 1 | 2 | 3 | 3 | 4 | 0 |
| A, 2 | 4 | 3 | 5 | 4 | 0 |
| B, 1 | 4 | 5 | 3 | 4 | 0 |
| B, 2 | 6 | 5 | 5 | 4 | 0 |
This example has no usable two-way within variation. A regressor that only adds unit and time components cannot identify $\beta$.
Assumptions and interpretation
Strict exogeneity requires current errors to have zero conditional mean given the full regressor history and fixed effects. Identification also requires variation after both effects are removed.
The coefficient describes a weighted within-unit association after common time shocks. Causal interpretation needs a design that supports the exogeneity condition.
Implementation issues
Dummy-variable and absorption algorithms should give the same coefficient. They can count parameters differently for finite-sample covariance corrections. Standard errors should follow the error dependence, often through unit clustering.
The within transformation decomposes the panel matrix into unit means, time means, and a residual matrix.
Interpretation and reporting
The coefficient uses regressor variation that remains after both sets of effects are removed. In the table, every transformed value is zero. That sample cannot estimate $\beta$, even though raw $x$ varies across cells.
Report the amount of two-way residualized variation, the unit count, the time count, and the covariance estimator. Describe the comparison that supplies identification. A coefficient based on a small set of changing units can have limited population scope.
Unit effects absorb stable differences. Time effects absorb shocks common to all units. Neither set removes unit-specific time-varying confounding. In staggered-treatment settings, one pooled coefficient can also combine heterogeneous cohort-time effects with hard-to-read weights.
Reproducible implementation
Validate unique unit-time observations and define the estimation sample. Residualize both $y$ and $x$ against unit and time indicators. For an unbalanced panel, use an absorption algorithm or explicit indicators instead of the balanced-panel shortcut.
Regress the residualized outcome on the residualized regressor. Compare this coefficient with the full dummy-variable specification. Inspect the residualized regressor for near-zero variation.
Choose covariance from the error dependence, commonly clustering by unit. Match absorbed-parameter counts before comparing software standard errors. Save the sample indicators and fixed-effect dimensions. If treatment timing varies, supplement the pooled model with cohort-time estimates that identify their comparison groups (Wooldridge, 2010).
Further reading
Wooldridge develops fixed-effects estimation and inference for panels (Wooldridge, 2010).
Sources and further reading
- Jeffrey M. Wooldridge. 2010. Econometric Analysis of Cross Section and Panel Data. MIT Press. Source.
About this benchmark task
- Method group
- Difference-in-differences
- Software
- Stata, R, and Python
- Runs
- 360 runs, ten in each combination of model, software, prompt, and agency condition