# How does Stata calculate the “predict varname, u” after xtreg random-effect?

What "predict varname, u" after xtreg with random-effects really do in Stata? How it works? I mean, how the ("individual") random-error component u_i is extracted from the overall e_it error component?

Is there a way doing this in R?

• Welcome to the site! For R, you can use ranef() in the lme4 package. Stata and R seem to both calculate empirical Bayes estimates of random effects. – Randel Nov 22 '15 at 16:55
• For R's plm package, there is the function ranef() in it's development version. This should be the equivalent to Stata's predict ..., u after xtreg – Helix123 Jun 9 '17 at 13:32

Let me expand my comment above. Assume your mixed-effects model is $$\boldsymbol y_i = \boldsymbol X_i \boldsymbol \beta + \boldsymbol Z_i \boldsymbol b_i + \boldsymbol \epsilon_i,$$ where $i$ denotes cluster $i$, random effects $\boldsymbol b_i \sim N(0,\boldsymbol D)$. Without loss of generality, assume $\boldsymbol \epsilon_i \sim N(0,\sigma^2 \boldsymbol I)$. Then the formula to calculate random effects is $$\hat{\boldsymbol b}_i=(\boldsymbol Z_i'\boldsymbol Z_i+\sigma^2 \boldsymbol D^{-1})^{-1}\boldsymbol Z_i'(\boldsymbol y_i - \boldsymbol X_i \boldsymbol \beta),$$ which can be generalized if you have a more general structure for $\mathrm{var}(\boldsymbol \epsilon_i).$
As far as I know, we can think the estimate $\hat{\boldsymbol b}_i$ in at least three ways:
• the conditional mean/mode of $\boldsymbol b_i | \boldsymbol y_i$. If we use EM algorithm for the estimation, we will see the above formula in the E step.
• the empirical Bayes estimator, since we do not specify a prior for $\boldsymbol b_i$ as in full Bayesian analysis. In some areas, it is also called "expected a posteriori".
• Thank you Randel! That was what I was asking for. The general references in microeconometrics literature (Wooldridge and Cameron/Triverdi) don't get so far. Trying use yor notation, I could reproduce the Stata's output using the plm function in R in this way: ${b}_i=c_i(\overline{y}_i-\overline{x}'_i\widehat{\beta}_{RE})$, where $c_i=\frac{\widehat{\sigma_b}}{\widehat{\sigma_b^2}+(\widehat{\sigma}_{\epsilon}/T_i)}$, and where $T_i$ is the number of times the individual appears (times, waves...). So, I ask you, what/how is matrix $D$ estimated? – Rodrigo Remedio Nov 23 '15 at 18:40
• @Rodrigo Great. You should estimate $D$ using an iterative algorithm, say, EM algorithm. An explicit procedure is on Page 9 of this excellent paper. – Randel Nov 23 '15 at 21:11