# What are the main differences among xtreg, areg, reghdfe?

Normally, when I run regressions for panel data in Stata using these three commands (xtreg,areg, reghdfe), the results regarding the coefficients of variables are quite similar; the only differences are about the R-square and intercept. I am wondering what are the main differences in these three codes?

Apart from that the result from running by areg or reghdfe are much higher than that in xtreg, so is there any restriction in reporting the results by using areg or reghdfe rather than xtreg?

xtreg regression:

xtset TYPE2 yr

xtreg y x i.yr, fe


areg regression:

areg y x i.yr, a(TYPE2)


reghdfe regression:

reghdfe y x, a(TYPE2 yr)


I saw a small explanation here about the difference in intercepts between areg and xtreg

## xtreg

xtreg is a general command for panel regression. The panel regressions will have the following general form (see stata manual):

$$y_{it} = α + \mathbf{x_{it}β} + ν_i + \epsilon_{it}$$

where $$y_{it}$$ is dependent variables $$x_{it}$$ independent variables, $$\alpha$$ is constant, $$\beta$$ parameters, $$v_i$$ are fixed effects and $$\epsilon$$ error term.

Depending on what option you choose xtreg can be:

1. fixed effect model xtreg dep var ind var, fe - this estimation uses within estimator:

$$(y_{it} − \bar{y}_i) = (x_{it} − \bar{x}_i)β + (\epsilon_{it} − \bar{\epsilon}_i)$$

1. random effect model xtreg dep var ind var, re or ,mle or ,pa - this estimation uses random effects estimator given by:

$$(y_{it} − θ\bar{y}_i) = (1 − θ)α + (x_{it} − θ\bar{x}_i)β + (1 − θ)ν_i + (\epsilon_{it} − θ\bar{\epsilon}_i)$$

where $$θ$$ is a function of $$\sigma_ν^2$$ and $$\sigma_\epsilon^2$$. You can view it as a 'weighted' version of FE estimator. In fact if $$\sigma_ν^2= 0 \implies \theta=0$$ so this estimator becomes regular multivariate regression, and if $$\sigma_\epsilon=0 \implies \theta=1$$ and this becomes within estimator described above.

The mle and pa options also estimate random effects model with the former estimating maximum likelihood random estimator, and the latter population average model. I won't go into these variants as it would be beyond scope of this answer but they are just variation on the above.

1. Between effects model xtreg dep var ind var, be uses the between estimator which uses:

$$\bar{y}_i = α + \bar{x}_iβ_1 + ν_i + \bar{\epsilon}_i$$

where means here are calculated across time e.g. $$\bar{y_i} = \frac{\sum_t y_{ti}}{(n-1)}$$

## areg

areg is command that estimates a linear regression absorbing one categorical factor (see stata manual). This is used when you face model such as:

$$y = Xβ + d_1γ_1 + d_2γ_2 + · · · + d_kγ_k + \epsilon$$

where $$d_1,...,d_k$$ are some dummies.

The model above can be in principle fitted by regular regression and in stata with reg command, but if $$k$$ is too large you would exceed the set mat size of stata (that is limitation on a size of matrixes, student version of stata has especially small mat size where this can easily be an issue, small stata can only have matsize of 100 and stata IC only 800 <- this is unique problem related to stata and how they 'nudge' people to buy larger versions).

areg circumvents the matsize problem by absorbing the dummies, there is no other significant difference between areg and reg

## reghdfe

reghdfe runs linear and instrumental-variable regressions with many levels of fixed effects, by implementing the estimator of Correia (2015) according to the authors of this user written command see here.

According to the authors reghde is generalization of the fixed effects model and thus the xtreg ..., fe. Here the command is generalized to allow for multiple fixed effects so you could run something like:

$$Y = Zβ + D_1α + D_2γ + \epsilon$$

where both $$D_1$$ and $$D_2$$ are fixed panel effects but with different dimensionality.

In addition, the command allows for multi-way clustering of errors. Clustering of errors is technique to control for heteroskedasticity and autocorrelation. xtreg only allows for one way clustering, so for example in regression of academic outcomes of pupils on some education policy you could cluster on school level which would allow for heteroskedasticity of errors within cluster. Multi-way clustering allows you to add additional layers to those cluster, so you could maybe additionally cluster on county level or by year etc.

## Differences

There are obviously several differences between all of the estimators above and it is impossible to summarize them all in one single SE post.

• Within estimator - in within estimator all panel members are assigned fixed effect which captures the time invariant unobservables. One problem with this estimator is that it cannot handle time invariant regressors (ass opposed to let's say random effects estimator).
• random effects estimator - random effects estimator allows for partial pooling where a group effect estimates are partially derived from information from more abundant groups. This can have an advantage over completely pooling all groups (i.e. fixed effects), as that can hide group-level variation, and estimating an effect for all panel/group members separately, can give poor estimates for low-sample groups. In addition, this estimator can handle also more rich model specifications (e.g. even time invariant regressors etc.), but these advantages also come at some cost of more easily falling prey to some subtle biases (see discussion of that here).
• between effects estimator is sort of an 'opposite' of fixed effect estimator. Whereas fixed effect estimator in essence utilizes time series information from the panel whereas between effects estimator utilizes the cross-sectional information from the panel. Most of the times we are interested in effect of $$x$$ on $$y$$ on particular individual which is when fixed effect is more appropriate, but in those cases where you are primarily interested in differences between two individuals caused by $$x$$ between effects would be more appropriate.
• areg is just reg with absorbed dummies and you can use it when you want to estimate any regression with too many dummy variables for stata to handle with its matsize restrictions (e.g. various pooled cross-sections models). This is mainly useful if you have the small/IC version of stata or work with very large panels.
• reghdfe is an extension of xtreg ..., fe that allows you to add more fixed effects and more clustering which is important in situations where that is appropriate.

The reason why you are getting similar result is that depending on how you estimate these models they might give you very similar estimators.

For example, the within estimator xtreg ..., fe is in essence equivalent to running a pooled OLS with dummies for each panel member and this same result can be achieved by reg or areg depending on how you specify your dummies. In addition, depending on how you set up reghdfe you again might end up with just fixed effects within estimator. If you let all variables be just instruments for themselves, if you do not use any fancy two way effects or clustering then you should not see much difference in those cases, but otherwise they are distinct estimators.

Furthermore, an important caveat to keep in mind is that these are only surface level differences between the models. There is more when you look 'under the hood' of each estimator (see the linked sources). You should generally not use them as a substitute for each other, and use each based on the details of particular problem you face and based on what you are interested in uncovering.

• Thank you 1muflon1, I am a little bit confuse here ? "Within estimator - in within estimator all panel members are assigned fixed effect which captures the time invariant unobservables. One problem with this estimator is that it cannot handle time invariant regressors (ass opposed to let's say random effects estimator)." Jun 12 at 8:24
• @Knowledge-chaser what exactly confused you about that? If you have some $x_i$ it is impossible to estimate beta since within estimator is based on $(x - \bar{x})\beta$ and with $x_i$ without any $t$ dimension the bracket is always $0$ meaning it’s equivalent to have $0\cdot \beta$ which is equivalent to never including that beta in reg in the first place
– 1muflon1
Jun 12 at 9:58
• I got it now, thanks 1muflon1 Jun 12 at 21:24