Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

RE: st: how to replicate Stata's random effects estimation results?

 From "Schaffer, Mark E" To Subject RE: st: how to replicate Stata's random effects estimation results? Date Fri, 3 Sep 2010 01:10:57 +0100

```An alternative is look directly at the code Stata uses for random effects.  This can be found in the ado file xtreg_re.ado, which is sitting in your ..ado\base\x folder.  Reading Stata code isn't easy, but I think in this case it's a practical suggestion.

Looking at the code, I can see that the first 200 lines or so are mainly taken up with data preparation and obtaining the estimate of theta used for quasi-demeaning.  Once this is obtained, the code is very simple and there is in fact an internal call to -regress- to get the RE results (just after the comment in the file that says "obtain random-effects estimates").

Replicating the RE results thus amounts to replicating the derivation of theta, which is either a constant (in the case of balanced data) or a column vector.  There is an option to report theta, so you can start by replicating the RE estimator for balanced data by quasi-demeaning by hand using the reported theta.

The code for deriving theta looks pretty messy, though, I must confess.

--Mark

> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of
> Stas Kolenikov
> Sent: 02 September 2010 23:21
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: how to replicate Stata's random effects
> estimation results?
>
> You can probably replicate -xtreg-, re- using Mata, but to
> get as far down as -regress-, you would need to perform a
> special transformation of the data by the inverse square root
> of the covariance matrix of all observations (the Kronecker
> product of the identity matrix and the covariance matrix of
> residuals if the data are homoskedastic and balanced). This
> will give you some insight into numerics such as Cholesky
> decomposition of a matrix, but I doubt one can benefit
> greatly from trying to get this by hand.
>
> On Thu, Sep 2, 2010 at 9:02 AM, Ada Ma <heu034@googlemail.com> wrote:
> > Statalisters,
> >
> > I want to replicate Stata's Random Effects estimates using only the
> > -regress- command, to gain better understanding as well as to do
> > something that the current -xtreg- command wouldn't allow me to do.
> >
> > Do you know of any demonstration where people have been able to
> > replicate Stata's RE estimates by hand?  Or journal articles that
> > describe exactly how the RE estimates are calculated?  I
> have looked
> > through several econometrics textbooks and it is not entirely clear.
> >
> > Many thanks in advance.
> >
> > Regards,
> >
> >
> > --
> > Research Fellow
> > Health Economics Research Unit
> > University of Aberdeen, UK.
> > http://www.abdn.ac.uk/heru/
> > Tel: +44 (0) 1224 555189
> > Fax: +44 (0) 1224 550926
> > *
> > *   For searches and help try:
> > *   http://www.stata.com/help.cgi?search
> > *   http://www.stata.com/support/statalist/faq
> > *   http://www.ats.ucla.edu/stat/stata/
> >
>
>
>
> --
> Stas Kolenikov, also found at http://stas.kolenikov.name
> Small print: I use this email account for mailing lists only.
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

--
Heriot-Watt University is a Scottish charity
registered under charity number SC000278.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```