<h2>xtreg</h2>
<p><code>xtreg</code> is a <em>general</em> command for panel regression. The panel regressions will have the following general form (see <a href="https://www.stata.com/manuals/xtxtreg.pdf" rel="noreferrer">stata manual</a>):</p>
<p><span class="math-container">$$y_{it} = α + \mathbf{x_{it}β} + ν_i + \epsilon_{it}$$</span></p>
<p>where <span class="math-container">$y_{it}$</span> is  dependent variables <span class="math-container">$x_{it}$</span> independent variables,  <span class="math-container">$\alpha$</span> is constant, <span class="math-container">$\beta$</span> parameters, <span class="math-container">$v_i$</span> are fixed effects and <span class="math-container">$\epsilon$</span> error term.</p>
<p>Depending on what option you choose <code>xtreg</code> can be:</p>
<ol>
<li><strong>fixed effect model</strong> <code>xtreg dep var ind var, fe</code>  - this estimation uses within estimator:</li>
</ol>
<p><span class="math-container">$$ (y_{it} − \bar{y}_i) = (x_{it} − \bar{x}_i)β + (\epsilon_{it} − \bar{\epsilon}_i)$$</span></p>
<ol start="2">
<li><strong>random effect model</strong> <code>xtreg dep var ind var, re</code> or <code>,mle</code> or <code>,pa</code> - this estimation uses random effects estimator given by:</li>
</ol>
<p><span class="math-container">$$(y_{it} − θ\bar{y}_i) = (1 − θ)α + (x_{it} − θ\bar{x}_i)β + (1 − θ)ν_i + (\epsilon_{it} − θ\bar{\epsilon}_i)$$</span></p>
<p>where <span class="math-container">$θ$</span> is a function of <span class="math-container">$\sigma_ν^2$</span> and <span class="math-container">$\sigma_\epsilon^2$</span>. You can view it as a 'weighted' version of FE estimator. In fact if <span class="math-container">$\sigma_ν^2= 0 \implies \theta=0$</span> so this estimator becomes regular multivariate regression, and if <span class="math-container">$\sigma_\epsilon=0 \implies \theta=1$</span> and this becomes within estimator described above.</p>
<p>The <code>mle</code> and <code>pa</code> 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.</p>
<ol start="3">
<li><strong>Between effects model</strong> <code>xtreg dep var ind var, be</code> uses the between estimator which uses:</li>
</ol>
<p><span class="math-container">$$ \bar{y}_i = α + \bar{x}_iβ_1 + ν_i + \bar{\epsilon}_i$$</span></p>
<p>where means here are calculated across time e.g. <span class="math-container">$\bar{y_i} = \frac{\sum_t y_{ti}}{(n-1)}$</span></p>
<h2>areg</h2>
<p><code>areg</code> is command that estimates  a linear regression absorbing one categorical factor (see <a href="https://www.stata.com/manuals14/rareg.pdf" rel="noreferrer">stata manual</a>). This is used when you face model such as:</p>
<p><span class="math-container">$$ y = Xβ + d_1γ_1 + d_2γ_2 + · · · + d_kγ_k + \epsilon$$</span></p>
<p>where <span class="math-container">$d_1,...,d_k$</span> are some dummies.</p>
<p>The model above can be in principle fitted by regular regression and in stata with <code>reg</code> command, but if <span class="math-container">$k$</span> 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 &lt;- this is unique problem related to stata and how they 'nudge' people to buy larger versions).</p>
<p><code>areg</code> circumvents the matsize problem by absorbing the dummies, there is no other significant difference between <code>areg</code> and <code>reg</code></p>
<h2>reghdfe</h2>
<p><code>reghdfe</code> 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 <a href="http://scorreia.com/software/reghdfe/" rel="noreferrer">here</a>.</p>
<p>According to the authors <code>reghde</code> is generalization of the fixed effects model and thus the <code>xtreg ..., fe</code>. Here the command is generalized to allow for multiple fixed effects so you could run something like:</p>
<p><span class="math-container">$$ Y = Zβ + D_1α + D_2γ + \epsilon $$</span></p>
<p>where both <span class="math-container">$D_1$</span> and <span class="math-container">$D_2$</span> are fixed panel effects but with different dimensionality.</p>
<p>In addition, the command allows for multi-way clustering of errors. Clustering of errors is technique to control for heteroskedasticity and autocorrelation. <code>xtreg</code> 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.</p>
<h2>Differences</h2>
<p>There are obviously several differences between all of the estimators above and it is impossible to summarize them all in one single SE post.</p>
<p>(Very) broadly speaking:</p>
<ul>
<li>Within estimator - in within estimator all panel members are assigned <code>fixed</code> 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).</li>
<li>random effects estimator - random effects estimator allows for <code>partial pooling</code> 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 <code>hide</code> 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 <a href="https://stats.stackexchange.com/a/188559/89502">here</a>).</li>
<li>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 <span class="math-container">$x$</span> on <span class="math-container">$y$</span> 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 <span class="math-container">$x$</span> between effects would be more appropriate.</li>
<li><code>areg</code> is just <code>reg</code> 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.</li>
<li><code>reghdfe</code> is an extension of <code>xtreg ..., fe</code> that allows you to add more fixed effects and more clustering which is important in situations where that is appropriate.</li>
</ul>
<p>The reason why you are getting similar result is that depending on how you estimate these models they might give you very similar estimators.</p>
<p>For example, the within estimator <code>xtreg ..., fe</code> is in essence equivalent to running a pooled OLS with dummies for each panel member and this same result can be achieved by <code>reg</code> or <code>areg</code> depending on how you specify your dummies. In addition, depending on how you set up <code>reghdfe</code> 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.</p>
<p>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.</p>

CC BY-SA 4.0
2021-06-12T08:24:16.027Z
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)."
Phil Nguyen
CC BY-SA 4.0
2021-06-12T09:58:06.633Z
@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
CC BY-SA 4.0
2021-06-11T18:19:09.337Z
2021-06-11T18:19:09.337Z
1muflon1
<p>Normally, when I run regressions for panel data in Stata using these three commands (<code>xtreg</code>,<code>areg</code>, <code>reghdfe</code>), the results regarding the coefficients of variables are quite similar; the only differences are about the <strong>R-square</strong> and <strong>intercept</strong>. I am wondering what are the main differences in these three codes?</p>
<p>Apart from that the result from running by <code>areg</code> or <code>reghdfe</code> are much higher than that in <code>xtreg</code>, so is there any restriction in reporting the results by using <code>areg</code> or <code>reghdfe</code> rather than <code>xtreg</code>?</p>
<p><strong>xtreg regression:</strong></p>
<pre><code>xtset TYPE2 yr

xtreg y x i.yr, fe
</code></pre>
<p><strong>areg regression:</strong></p>
<pre><code>areg y x i.yr, a(TYPE2)
</code></pre>
<p><strong><a href="http://scorreia.com/software/reghdfe/quickstart.html" rel="nofollow noreferrer">reghdfe regression</a>:</strong></p>
<pre><code>reghdfe y x, a(TYPE2 yr)
</code></pre>
<p>I saw a small explanation <a href="https://economics.stackexchange.com/questions/45378/why-excluding-intercept-is-dangerous-if-there-is-no-literature-back-up-in-did-se">here</a> about the difference in intercepts between areg and xtreg</p>

<econometrics><applied-econometrics><stata>
What are the main differences among xtreg, areg, reghdfe?
Phil Nguyen
# What are the main differences among xtreg, areg, reghdfe?

(Asked by: username_0 on 2021-06-11)

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

**Tags:** econometrics, applied-econometrics, stata


---

## 1 answers

### Answer by username_1 on 2021-06-11. Score: 8 | Accepted answer

## xtreg

`xtreg` is a *general* command for panel regression. The panel regressions will have the following general form (see [stata manual](https://www.stata.com/manuals/xtxtreg.pdf)):

$$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)$$

2. **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.

3. **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](https://www.stata.com/manuals14/rareg.pdf)). 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](http://scorreia.com/software/reghdfe/).

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.

(Very) broadly speaking:

* 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](https://stats.stackexchange.com/a/188559/89502)).
* 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.

> **Comments:**
> **username_0**: Thank you username_1, 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)."  
> **username_1**: @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  

---

