Panel data — repeated observations on the same units (firms, countries, individuals) over time — is where Stata is at its best. xtset declares the panel structure; xtreg fits panel models; the within-transformation that powers fixed effects is one command and one option.
xtset — declare the panel
xtset bank_id year // panel ID + time variablextdescribe // panel structure: balanced or notxtsum lending_rate // within / between / overall variance
xtreg with fixed effects
xtreg lending_rate deposit_rate, fe // bank fixed effectsxtreg lending_rate deposit_rate i.year, fe // bank FE + year FExtreg lending_rate deposit_rate, fe vce(cluster bank_id) // clustered SEs
What the within transformation actually does
Fixed effects are equivalent to subtracting the bank-specific mean from each variable, then running OLS on the de-meaned data. This 'within' estimator removes any time-invariant bank characteristics — they get differenced out. The cost: any time-invariant predictor is unidentified (you can't include 'bank size at IPO' as a regressor when it never varies within bank).
Random effects
xtreg lending_rate deposit_rate, re // random effectshausman fe re // Hausman test: FE vs RE
Random effects assumes the bank effect is uncorrelated with the regressors. The Hausman test compares FE and RE estimates. If they differ significantly, the random-effects assumption is rejected — use FE.
reghdfe — high-dimensional fixed effects
* User-written, fast, recommended for multi-way FEssc install reghdfereghdfe lending_rate deposit_rate, absorb(bank_id year) cluster(bank_id)
Time-series operators
xtset bank_id yeargenerate lag_rate = L.lending_rate // L. laggenerate lead_rate = F.lending_rate // F. leadgenerate diff_rate = D.lending_rate // D. first difference
Two-way clustering needs a special command
xtreg only does one-way clustering. For two-way (e.g., cluster by both bank and year), use reghdfe or ivreg2 with cluster(bank year). The choice can change the SEs by 30-50%.
Exercise
Declare a panel with bank_id and year, then fit a fixed-effects model of lending_rate on deposit_rate.