Skip to content

brycewang-stanford/Auto-Empirical-Research-Skills / skills/14-luischanci-claude-code-research-starter/dot-claude/skills/stata

Comprehensive Stata reference for writing correct .do files, data management, econometrics, causal inference, graphics, Mata programming, and 17+ community packages (reghdfe, estout, did, rdrobust, etc.). Covers syntax, options, gotchas, and idiomatic patterns. Use this skill whenever the user asks you to write, debug, or explain Stata code.

FileKindSize
SKILL.mdskill md13.1 KB
packages/asdoc.mdmarkdown8.5 KB
packages/binsreg.mdmarkdown10.0 KB
packages/coefplot.mdmarkdown9.3 KB
packages/data-manipulation.mdmarkdown10.1 KB
packages/diagnostics.mdmarkdown15.3 KB
packages/did.mdmarkdown15.4 KB
packages/estout.mdmarkdown16.9 KB
packages/event-study.mdmarkdown29.1 KB
packages/graph-schemes.mdmarkdown14.1 KB
packages/ivreg2.mdmarkdown11.1 KB
packages/nprobust.mdmarkdown12.4 KB
packages/outreg2.mdmarkdown11.2 KB
packages/package-management.mdmarkdown10.2 KB
packages/psmatch2.mdmarkdown17.8 KB
packages/rdrobust.mdmarkdown13.4 KB
packages/reghdfe.mdmarkdown10.4 KB
packages/synth.mdmarkdown22.6 KB
packages/tabout.mdmarkdown12.1 KB
packages/winsor.mdmarkdown6.7 KB
packages/xtabond2.mdmarkdown14.9 KB
references/advanced-programming.mdmarkdown10.1 KB
references/basics-getting-started.mdmarkdown5.8 KB
references/bootstrap-simulation.mdmarkdown6.7 KB
references/data-import-export.mdmarkdown7.4 KB
references/data-management.mdmarkdown9.2 KB
references/date-time-functions.mdmarkdown6.8 KB
references/descriptive-statistics.mdmarkdown6.4 KB
references/difference-in-differences.mdmarkdown18.6 KB
references/external-tools-integration.mdmarkdown19.6 KB
references/gmm-estimation.mdmarkdown8.8 KB
references/graphics.mdmarkdown8.2 KB
references/limited-dependent-variables.mdmarkdown7.8 KB
references/linear-regression.mdmarkdown11.0 KB
references/machine-learning.mdmarkdown12.4 KB
references/mata-data-access.mdmarkdown6.8 KB
references/mata-introduction.mdmarkdown6.4 KB
references/mata-matrix-operations.mdmarkdown5.8 KB
references/mata-programming.mdmarkdown8.4 KB
references/matching-methods.mdmarkdown19.2 KB
references/mathematical-functions.mdmarkdown7.0 KB
references/maximum-likelihood.mdmarkdown16.6 KB
references/missing-data-handling.mdmarkdown18.2 KB
references/nonparametric-methods.mdmarkdown11.7 KB
references/panel-data.mdmarkdown7.7 KB
references/programming-basics.mdmarkdown7.9 KB
references/regression-discontinuity.mdmarkdown13.4 KB
references/sample-selection.mdmarkdown17.7 KB
references/sem-factor-analysis.mdmarkdown14.5 KB
references/spatial-analysis.mdmarkdown19.5 KB
references/string-functions.mdmarkdown8.7 KB
references/survey-data-analysis.mdmarkdown12.8 KB
references/survival-analysis.mdmarkdown12.3 KB
references/tables-reporting.mdmarkdown24.1 KB
references/time-series.mdmarkdown7.1 KB
references/treatment-effects.mdmarkdown23.1 KB
references/variables-operators.mdmarkdown4.9 KB
references/workflow-best-practices.mdmarkdown25.2 KB

references/panel-data.md

View on GitHub
# Panel Data Analysis in Stata

## Setting Up Panel Data: xtset

```stata
xtset panelvar [timevar] [, options]
```

```stata
xtset country year
xtset firm_id year, yearly
xtset person_id              // No time variable
xtset                        // Check current settings
```

- Panel variable must uniquely identify each unit
- Settings persist after save/reload
- Use `xtdescribe` to check if panel is balanced/unbalanced

---

## Time-Series Operators in Panel Data

After `xtset`, these operators work within panels:

```stata
L.variable      // First lag (t-1)
L2.variable     // Second lag
L(1/3).variable // Lags 1 through 3
F.variable      // First lead (t+1)
D.variable      // First difference (y[t] - y[t-1])
D2.variable     // Second difference
LD.variable     // Lag of first difference

// Use directly in regressions
xtreg consumption L.gdp L.income, fe
```

---

## Descriptive Commands

### xtsum - Between/Within Decomposition

```stata
xtsum ln_wage age tenure
```

Output decomposes variation into:
- **Overall**: All observations (ignoring panel structure)
- **Between**: Across panel units (comparing averages across units)
- **Within**: Over time within units

Relationship: sigma_overall^2 approximately equals sigma_between^2 + sigma_within^2

This decomposition is critical for understanding whether your variation is primarily cross-sectional or longitudinal, which informs model choice.

### xttab - Panel Cross-Tabulation

```stata
xttab union
```

Shows overall frequency, between frequency (% of units ever in each state), and within variation.

### xttrans - Transition Probabilities

```stata
xttrans union    // Transition matrix between states over time
```

---

## Fixed Effects (FE)

Eliminates all time-invariant heterogeneity by demeaning (within transformation). Controls for unobserved time-invariant confounders but **cannot estimate effects of time-invariant variables** (gender, race, etc.).

```stata
xtreg ln_wage age tenure, fe
xtreg ln_wage age tenure, fe vce(robust)
xtreg ln_wage age tenure, fe vce(cluster idcode)

// Entity AND time fixed effects
xtreg ln_wage age tenure i.year, fe
testparm i.year                         // Test if time FEs are needed
```

### reghdfe - High-Dimensional Fixed Effects

For multiple sets of fixed effects (faster, more flexible than `xtreg`):
```stata
// ssc install reghdfe
reghdfe ln_wage age tenure, absorb(idcode)
reghdfe ln_wage age tenure, absorb(idcode year)
reghdfe ln_wage age tenure, absorb(idcode year) vce(cluster idcode year)  // Two-way clustering
```

### Key Output Statistics
- **Within R-squared**: Variation explained within entities over time
- **Between R-squared**: Variation explained across entities
- **sigma_u**: SD of entity-specific effects
- **sigma_e**: SD of idiosyncratic error
- **rho**: Fraction of variance due to entity effects = sigma_u^2 / (sigma_u^2 + sigma_e^2)
- **corr(u_i, Xb)**: FE allows this to be nonzero (unlike RE)

---

## Random Effects (RE)

Uses GLS, exploiting both within and between variation. Assumes individual effects u_i are uncorrelated with regressors. More efficient than FE when assumption holds; can include time-invariant variables.

```stata
xtreg ln_wage age tenure, re
xtreg ln_wage age tenure, re vce(robust)
xtreg ln_wage age tenure female, re      // Can include time-invariant vars
```

Key output includes **theta**: the GLS transformation parameter (theta=1 equals FE; theta=0 equals pooled OLS).

### Between Effects and Pooled OLS
```stata
xtreg ln_wage age tenure, be     // Uses group means only
reg ln_wage age tenure           // Pooled OLS (ignores panel structure)
```

---

## First Differences (FD)

Eliminates individual effects by differencing consecutive observations. Preferred over FE when T is small and errors follow a random walk.

```stata
xtreg gdp investment population, fd
xtreg gdp investment population, fd vce(robust)

// Or manually
reg D.gdp D.investment D.population, noconstant vce(cluster country)
```

---

## Hausman Test: FE vs RE

Tests whether individual effects are correlated with regressors.

- **H0**: RE is consistent and efficient (u_i uncorrelated with X)
- **H1**: Only FE is consistent

```stata
quietly xtreg ln_wage age tenure, fe
estimates store fixed

quietly xtreg ln_wage age tenure, re
estimates store random

hausman fixed random
// p < 0.05: Reject H0 -> Use FE
// p >= 0.05: Fail to reject -> RE acceptable
```

### Mundlak Test (Alternative)
Include group means of time-varying regressors in RE model. If jointly significant, FE is preferred.

```stata
bysort idcode: egen mean_age = mean(age)
bysort idcode: egen mean_tenure = mean(tenure)
xtreg ln_wage age tenure mean_age mean_tenure, re
test mean_age mean_tenure
```

---

## Testing for Panel Effects

```stata
// Breusch-Pagan LM test (after RE estimation)
xtreg ln_wage age tenure, re
xttest0
// H0: sigma_u^2 = 0 (pooled OLS appropriate)
// p < 0.05: Use panel methods
```

---

## Dynamic Panel Data Models

Including lagged dependent variable creates "Nickell bias" in standard FE when T is small. GMM estimators address this.

### xtabond - Arellano-Bond (Difference GMM)

Differences the equation to remove individual effects, uses lagged levels as instruments.

```stata
webuse abdata
xtset id year

xtabond n L(0/2).(w k), vce(robust)
xtabond n w k, lags(2) maxldep(3) vce(robust)
```

### xtdpdsys - Blundell-Bond (System GMM)

Combines difference and level moment conditions. More efficient, especially with persistent series.

```stata
xtdpdsys n L(0/1).(w k), vce(robust)
xtdpdsys n w k, lags(2) maxldep(2) vce(robust)
```

### Dynamic Panel Diagnostics

```stata
// After xtabond or xtdpdsys:
estat abond     // AR test: Expect AR(1) significant, AR(2) NOT significant
estat sargan    // Overidentification: H0 = instruments valid (want p > 0.05)
```

### xtabond2 (User-Written, More Flexible)
```stata
ssc install xtabond2
xtabond2 n L.n w k, gmm(L.(n w k)) iv(yr1980-yr1984) robust
```

---

## When to Use Each Model

| Model | When |
|-------|------|
| **Fixed Effects** | Control for time-invariant unobserved heterogeneity; T >= 3; Hausman rejects RE |
| **Random Effects** | Need time-invariant variables; units are random sample; Hausman doesn't reject |
| **First Differences** | T = 2; errors follow random walk |
| **Dynamic GMM** | Lagged dependent variable as regressor; small T, large N; potential endogeneity |

---

## Summary Table

| Estimator | Command | Key Feature |
|-----------|---------|-------------|
| Pooled OLS | `reg y x` | Ignores panel structure |
| Fixed Effects | `xtreg y x, fe` | Eliminates time-invariant confounders |
| Random Effects | `xtreg y x, re` | More efficient; can estimate time-invariant effects |
| First Difference | `xtreg y x, fd` | Alternative to FE |
| Between Effects | `xtreg y x, be` | Cross-sectional variation only |
| Arellano-Bond | `xtabond y L.y x` | Dynamic panel, difference GMM |
| System GMM | `xtdpdsys y L.y x` | Dynamic panel, more efficient |
| Multi-way FE | `reghdfe y x, absorb(id1 id2)` | High-dimensional fixed effects |

---

## Common Pitfalls

- Including lagged dependent variable with standard FE (use xtabond/xtdpdsys)
- Using RE when individual effects likely correlate with regressors
- Ignoring serial correlation in errors
- Too many instruments in dynamic panel models (overfitting; Sargan test loses power)
- Forgetting to cluster standard errors at the panel level
- Not checking panel balance with `xtdescribe`

## Best Practices

1. Start with `xtdescribe` and `xtsum` to understand your data structure
2. Run Hausman test to choose between FE and RE
3. Use `xttest0` to test whether panel methods are needed at all
4. Always use robust or clustered standard errors
5. For dynamic models, check AR(2) and Sargan/Hansen tests
6. Report: model type, N obs, N groups, R-squared (within/between/overall), SE type, diagnostic tests
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall