regress: OLS with a categorical predictor
Fit a linear model, request robust standard errors, and interpret factor-variable coefficients against a reference group.
Dataset and complete example
The example uses 24 synthetic student records created for teaching. math is the outcome, female is a zero/one indicator, and race contains four synthetic groups. The data do not describe real students.
clear
import delimited "data/student_scores.csv"
regress math female i.race, robust
i.race expands the categorical variable into indicators and uses race group 1 as the reference. robust changes the estimated standard errors and tests, not the OLS coefficients.
Output from stats.camp
Linear regression Number of obs = 24
F(4, 19) = 36.69
Prob > F = 0.0000
R-squared = 0.8545
Root MSE = 3.213
--------------------------------------------------------------------------------
| Robust
math | Coefficient std. err. t P>|t| [95% conf. interval]
-------------+------------------------------------------------------------------
female | 8.916669 1.311638 6.80 0.000 6.171379 11.66196
|
race |
2 | 10.000001 1.892004 5.29 0.000 6.039992 13.96001
3 | 3.8333358 1.937058 1.98 0.063 -.2209728 7.887644
4 | 13.666667 1.871805 7.30 0.000 9.748934 17.5844
|
_cons | 46.041662 1.38414 33.26 0.000 43.14462 48.9387
--------------------------------------------------------------------------------
How to read this result
Within this synthetic dataset and holding race group constant, the fitted coefficient for female is about 8.92 points. The race coefficients compare each group with race group 1. For example, the fitted difference for group 2 is 10 points.
These are associations in invented teaching data, not causal effects or claims about real groups. The confidence intervals and p-values use the requested robust variance estimate.
Common variants and mistakes
- Use
i.race, not barerace, when numeric codes represent categories rather than a linear scale. - Changing the reference group changes coefficient interpretation, not the fitted values.
- Robust standard errors do not repair an incorrectly specified model or turn an association into a causal estimate.
- Check the observation count after adding predictors; observations with missing model variables are excluded.
stats.camp currently documents factor variables plus robust and vce() for regress. It does not claim every Stata option, prefix, stored result, or postestimation workflow. See the current compatibility entry and Stata's introductory regression guide for an external reference.
Related guides
Run this exact example
Clone the example repository in stats.camp and open the exact do-file shown on this page. No stats.camp account is required.
Open this example