# Three-level mixed effects model with crossed effects in Stata [closed]

I have a dataset of individuals that includes their wage and occupations for several years. The data is in panel (long) format (xtset id year, yearly).

I want to fit a three-level structure: occupation (top level), individual (middle), and year (lower). Importantly, some individuals change occupations over the sample, which means nesting is not complete (if this is a big problem, I appreciate if this problem is pointed out. However, I could get rid of these observations, which are not many, so an answer to my problem would be still useful).

Consider the following error-component model for the wage $w$ of worker $i$ in occupation $j$ in period $t$:

$$w_{ijt}= \theta_{jt} + \eta_{ij} + \mu_{ijt}$$

The components of this model are three "error terms" (or random effects). Note that this model has crossed effects (opposed to nested effects, which would be the case if $\theta_{j}$, for example).

I am struggling to estimate this model in Stata 12. Based on the reference manual and online help, models with crossed effects should be estimated using the _all:R.levelvar notation. So in this case, this should be xtmixed wage || _all: R.occupation || _all: R.id: (or, more succinctly and computationally efficient xtmixed wage || _all: R.occupation || id:). Yet, the estimation results are not properly crossed. By this I mean that the estimated random effects $\theta$ vary between occupation but not between years, and the random effects $\eta$ vary between individuals but not between occupations. In other words, Stata is estimating the following equation:

$$w_{ijt}= \theta_{j} + \eta_{i} + \mu_{ijt}$$

How can I tell Stata to compute the crossed random effects as I need to? Is it an issue of my data structure?

## closed as off-topic by Nick Cox, Peter Flom♦Jun 28 '16 at 11:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.

After hours of trial and error, I managed to make it work. The key is to use the factor variables for an specific level of the model and not for _all, and in the correct order. The code in my example is:

xtmixed wage || occupation: R.year || id: R.occupation


This is telling Stata to find random effects that vary over period at the top level (occupations) and random effects that vary over occupations at the middle level (individuals). Stata will automatically add the other error at the bottom level.

Note that the order of the levels in the command is very important (top, middle, bottom). I tried many alternatives. Only the one above worked. For the sake of a more complete solution, I list them below, together with the actual equation being estimated by Stata:

xtmixed wage || _all: R.occupation || id: R.occupation // y_jit = u + u_j + u_ij + e_jit
xtmixed wage || year: R.occupation || _all: R.occupation // y_jit = u + u_jt + v_jt + e_jit
xtmixed wage || _all: R.occupation || year: R.occupation // y_jit = u + u_jt + u_j + e_jit
xtmixed wage || id: R.occupation || year: R.occupation // y_jit = u + u_jit + u_ij + e_jit
xtmixed wage || year: R.occupation || id: R.occupation // y_jit = u + u_jt + u_jit + e_jit
xtmixed wage || id: R.occupation || occupation: R.year // y_jit = u + u_jit + u_ij + e_jit