Overview
This panel data analysis tutorial shows how to run panel regression in Stata, step by step: put your data in long format (one row per firm per year), declare the panel with xtset, estimate pooled OLS, fixed effects and random effects with firm-clustered standard errors, choose between fixed and random effects with a cluster-robust test, then report all three side by side.
At a glance
- Panel data follows the same firms or banks over several years, so each firm’s fixed traits must be handled.
- Fixed effects is the usual default in corporate finance. Random effects needs a stronger assumption.
- With clustered standard errors, the classic Hausman test is not valid. Use the Mundlak test instead.
- Small markets such as Qatar give short, unbalanced panels, so report the number of firms and years.
- Save every command in a do-file.
If your finance thesis follows the same firms or banks over several years, a simple regression is not enough. Think of companies listed on the Qatar Stock Exchange, or banks across the GCC, observed from 2010 to 2023. Each firm has characteristics, such as management culture or founding history, that stay with it over time, and your model has to account for them [1][2]. The example below regresses return on assets (roa) on leverage (lev), firm size (size) and sales growth (growth). It is a Stata regression step by step guide for doctoral scholars in Qatar who are running their first panel model and want to defend it with confidence.
How to Run Panel Regression in Eight Steps
Step 1: Prepare your data in long format
Panel data needs one row for each firm in each year, with columns for the firm ID, the year and each variable. If your Bloomberg or Refinitiv download lists years across columns, reshape it first:
reshape long roa lev size growth, i(firm_id) j(year)
duplicates report firm_id year
Check for duplicate rows, missing years and obvious entry errors. A small mistake here quietly affects every result that follows.
Step 2: Declare the panel
Tell Stata which variable identifies the firm and which identifies time:
xtset firm_id year
Without this, Stata treats the data as unrelated observations and cannot apply panel estimators. Once the panel is declared, later xt commands use the structure automatically.
Step 3: Check the panel structure and clean your variables
xtdescribe
xtsum roa lev size growth
- xtdescribe shows whether every firm appears in every year. Unbalanced panels are common in small markets because firms list, delist or stop reporting. They are acceptable if you report the number of firms and observations.
- xtsum splits each variable’s variation into between firms and within firms. A variable that never changes over time, such as sector, cannot be estimated in a fixed effects model.
- Winsorise extreme financial ratios (often at the 1st and 99th percentiles) and state this in your methodology.
Step 4: Run a pooled OLS baseline
regress roa lev size growth, vce(cluster firm_id)
estimates store pooled
Cluster standard errors by firm, because observations from the same firm are correlated over time [3][4]. Pooled OLS assumes all firms are alike, which is rarely true, so treat it as a baseline only.
Step 5: Run the fixed effects model
xtreg roa lev size growth i.year, fe vce(cluster firm_id)
estimates store fe
Fixed effects (the within estimator) uses only changes inside each firm over time, so anything about a firm that does not change, whether you measured it or not, is removed [1]. Adding i.year controls for shocks that hit every firm in the same year, such as an oil-price fall. Check the within R², which tells you how much of the variation inside firms the model explains, and check the sign and size of each coefficient against your hypotheses.
Step 6: Run the random effects model
xtreg roa lev size growth i.year, re vce(cluster firm_id)
estimates store re
Random effects assumes that each firm’s unobserved characteristics are unrelated to the regressors. That is a stronger assumption than fixed effects needs, but if it holds, the estimates are more precise and time-invariant variables can stay in the model [2].
Step 7: Choose between fixed and random effects
The classic Hausman test compares the two models. A significant result favours fixed effects [5]:
quietly xtreg roa lev size growth i.year, fe
estimates store fe_h
quietly xtreg roa lev size growth i.year, re
estimates store re_h
hausman fe_h re_h
The standard Hausman test is only valid without clustered errors. If you cluster, as you should here, use the Mundlak (correlated random effects) test. Add the firm means of the time-varying regressors to the random effects model and test whether they are jointly zero [6]:
foreach v in lev size growth {
bysort firm_id: egen m_`v' = mean(`v')
}
xtreg roa lev size growth m_lev m_size m_growth i.year, re vce(cluster firm_id)
test m_lev m_size m_growth
A significant result rejects random effects in favour of fixed effects. State which test you used and why. Examiners care about your reasoning as much as the result.
Step 8: Interpret and report your results
- Put pooled OLS, fixed effects and random effects side by side in one table, with the number of firms, observations and the clustering level beneath it.
- Explain each coefficient in plain terms, for example what a one-unit rise in leverage means for return on assets, and say whether the effect is economically meaningful and not only statistically significant.
- Save every command in a do-file so the analysis can be rerun after supervisor feedback.
For reporting conventions, see reporting econometric results in finance.
A note for small markets like Qatar
Qatar panels are often short (few years) and narrow (few firms), and firms in a small market tend to move together. Keep the model simple, cluster by firm, and test for cross-sectional dependence before trusting the standard errors. The Stata and R walkthrough covers those checks.
FAQ
- Should I use fixed or random effects for a finance thesis?
Fixed effects is the usual default for firm-level data, because unobserved firm traits are likely to be related to leverage or size. Use a cluster-robust Mundlak test to justify the choice.
- Can I run the Hausman test with clustered standard errors?
Not the standard version. Use the Mundlak test shown in Step 7, or a cluster-robust overidentification test.
- Why did my sector dummy drop out of the fixed effects model?
Fixed effects removes anything that does not change within a firm over time, so time-invariant variables cannot be estimated.
- Is an unbalanced panel a problem?
Not by itself. Report the number of firms and observations, and check whether firms leave the sample for reasons linked to your outcome.Next step
Once you are comfortable with these eight steps, take them further with Panel Regression and Time-Series Analysis in Stata and R: A Downloadable Walkthrough for Qatar Scholars.
For finance PhD scholars in Qatar
If you want a second pair of eyes on your model choice, Stata code or results chapter, PhD Assistance’s econometrics team can review your panel analysis and help you defend it. Our finance data analysis and econometrics support guides Qatar doctoral scholars through model selection, Stata and R coding and results interpretation.
