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?

  • 1
    $\begingroup$ 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. $\endgroup$ – Randel Nov 22 '15 at 16:55
  • $\begingroup$ 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 $\endgroup$ – 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 best linear unbiased prediction (BLUP).
  • 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".
  • $\begingroup$ 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? $\endgroup$ – Rodrigo Remedio Nov 23 '15 at 18:40
  • 1
    $\begingroup$ @Rodrigo Great. You should estimate $D$ using an iterative algorithm, say, EM algorithm. An explicit procedure is on Page 9 of this excellent paper. $\endgroup$ – Randel Nov 23 '15 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.