Why Incidence Matrices Matter
A mixed model describes how observations relate to group-level effects. In a simple random-intercept model, each observation receives the random intercept for the group to which it belongs. With several nested or crossed factors, the same idea still applies, but the bookkeeping becomes more important: every observation must be connected to the correct level of every random-effect factor. An incidence matrix makes those connections explicit.
The matrix is a design map, not an estimate of the random effects and not a statement about their covariance. Its rows correspond to observations; its columns correspond to random-effect levels. An entry records whether an observation belongs to the level represented by that column. Once the mapping is correct, multiplying the matrix by a vector of random effects selects the appropriate effect for every row.
For example, if \(b\) is the \(q\)-vector of random intercepts, then the product \(Zb\) is an \(n\)-vector. Its \(i\)th element is the random intercept assigned to observation \(i\). In a GLMM with several random-effect terms, the linear predictor can be written as
Here \(X\beta\) is the fixed-effects contribution, and each \(Z_kb_k\) maps the random effects for one grouping factor into the observation rows. Each \(Z_k\) is constructed from the membership pattern for its own factor. This representation concerns the design and linear predictor; distributional and covariance assumptions for the random effects are specified separately in the model.
Constructing a Matrix for a Nested Factor
A factor \(A\) is nested within factor \(B\) when each level of \(A\) belongs to exactly one level of \(B\). For incidence-matrix construction, the crucial issue is how the levels are named. If class labels such as “Class 1” are reused in different schools, the class columns must still distinguish the different school-specific classes. One safe convention is to label a nested level by its full path through the hierarchy, such as “School 1: Class 1.”
Worked Example: Schools and Classes Nested Within Schools
Suppose six hypothetical student records come from two schools. School 1 has two classes, and School 2 has one class. The records appear in this order:
| Observation | School | Class within school |
|---|---|---|
| 1 | S1 | S1:C1 |
| 2 | S1 | S1:C1 |
| 3 | S1 | S1:C2 |
| 4 | S2 | S2:C1 |
| 5 | S2 | S2:C1 |
| 6 | S2 | S2:C1 |
For the school random intercept, use columns S1 and S2. The matrix is \(6 \times 2\), because there are six observations and two school levels:
For class-within-school random intercepts, use the three distinct levels S1:C1, S1:C2, and S2:C1. The corresponding \(6 \times 3\) matrix is
The first two rows share a 1 in the S1:C1 column because both observations are from that class; rows 4–6 share a 1 in the S2:C1 column. Although both classes have the local label “C1,” they are different levels, so they require separate columns. If the random effects are \(b_{\text{school}}\) and \(b_{\text{class}}\), the fourth row of \(Z_{\text{school}}b_{\text{school}}\) selects the S2 effect, while the fourth row of \(Z_{\text{class}}b_{\text{class}}\) selects the S2:C1 effect.
The hierarchy does not require the class matrix to be derived by multiplying the school matrix by another matrix. It is enough to encode the actual class-within-school level for every observation. The nesting relationship is visible in the level labels and the membership pattern: each class column has nonzero entries only for observations in its parent school.
Crossed Factors and Their Combined Design
Two factors are crossed when levels of one factor can occur with levels of the other, rather than each level of one factor belonging to only one level of the other. In an incomplete crossed design, not every possible pair must be observed. Each factor still gets its own incidence matrix, with one column per level of that factor.
Worked Example: Subjects Crossed With Test Items
Consider five hypothetical responses from three subjects and three items. The observed subject-item pairs, in row order, are (A, 1), (A, 2), (B, 1), (B, 3), and (C, 2). There are \(n=5\) response rows. The subject matrix has three columns, and the item matrix has three columns:
The columns of the first matrix are subjects A, B, and C; the columns of the second are items 1, 2, and 3. For instance, row 4 has a 1 in the subject-B column and a 1 in the item-3 column. It identifies one subject level and one item level for that response. The pair (C, 2) is observed, but other possible pairs need not be present for these matrices to be defined.
For an additive random-intercept model, place the matrices side by side:
Each row of this combined matrix has two 1s: one for its subject and one for its item. This does not violate the one-1-per-row rule for a single factor. That rule applies separately to each incidence matrix when every observation has exactly one level of that factor.
A Hierarchy With a Crossed Factor
A design can contain both nested and crossed factors. Construct one incidence matrix for each random-effect factor, always keeping the rows in the same observation order. Then concatenate the matrices when a single design matrix for all random effects is useful. The concatenated columns remain identifiable by their factor and level labels.
Worked Example: Classes Nested in Schools and Test Forms Crossed With Classes
Suppose six hypothetical student records come from the school-class assignments in the first example. Each record also has a test form, A or B. In row order, the forms are A, B, A, B, A, B. The school and class matrices are as above. The test-form incidence matrix is
with columns A and B. The overall random-effects design for school, class-within-school, and form is
Row 5 has a 1 in the S2 school column, a 1 in the S2:C1 class column, and a 1 in the form-A column. Thus its random-effects contribution is the sum of the corresponding three random intercepts. The form factor is crossed with the hierarchy in the sense that each form occurs in more than one class; it is not a level nested in just one class.
Why the Matrix Product Works
The row-selection property follows directly from matrix multiplication. Let \(b=(b_1,\ldots,b_q)^\mathsf{T}\) contain the random intercepts for a factor. If observation \(i\) belongs to level \(j(i)\), its incidence row has a 1 in position \(j(i)\) and zeros elsewhere. Therefore
For several factors, matrix multiplication is applied to each factor separately and the resulting contributions are added. This is why observation ordering must be identical across \(X\), each \(Z_k\), the response, and any other row-level quantities: a row mismatch assigns an effect to the wrong observation.
Specify exactly what one row represents, such as one student record or one response to one item.
For nested factors, make nested level labels unique, for example by including the parent level in the label.
For each row, put a 1 in the column for its level and 0 in all other columns for that factor.
The row count must equal the number of observations. For one-level-per-row membership, each separate factor matrix must have exactly one 1 in each row.
Concatenating matrices gives a convenient overall design, but it does not change which factor each column represents.
Repeated Indicators, Diagnostics, and Alternatives
A column often contains many 1s: these are observations that share a level. Repetition down a column is expected and is the main way the matrix records grouping. Repetition in the same row across separate factor matrices is also expected when an observation belongs to one level of each factor. By contrast, if a row has two 1s within one factor matrix, the data or model structure needs review. It may reflect a coding error, or it may represent multiple membership, which requires a different construction than the one-level-per-row incidence matrix defined here.
- Check the row count. A matrix with one row per unique subject is wrong if the response data have one row per subject-item response.
- Check level labels. Reused local labels for nested groups can accidentally combine distinct levels into one column.
- Check row sums by factor. For ordinary single membership, every row sum is 1 in that factor’s matrix. A row sum of 0 indicates missing or unmatched membership; a sum above 1 signals duplicate or multiple membership.
- Check column use. An all-zero column represents a level with no observation in the analyzed data and usually should not be included in the analysis matrix.
- Check row alignment. The order of observations must match across all design matrices and the response; matching dimensions alone does not guarantee correct alignment.
Incidence matrices are the clearest way to inspect the mapping from observations to levels and are especially useful for explaining complex designs. Software formulas provide a more compact alternative: they can express nested terms and crossed terms without displaying a full matrix. But compact syntax can conceal reused labels, unexpected missing levels, or row-order mistakes. The explicit matrix is a valuable verification tool even when software constructs it automatically.
The matrix itself does not establish that random effects are independent, determine their variances, or guarantee that the model is identifiable. Those are separate modeling questions. Nor does an ordinary one-hot incidence matrix represent an observation that belongs partly to several levels of the same factor. In that setting, the row needs weights or another explicit membership structure rather than a single 1; the ordinary construction should not be forced onto such data.
Common Mistake
A frequent error is to make one “Class 1” column for all classes called Class 1, even when that label occurs in several schools. The mistaken reasoning is that identical text labels must identify identical groups. In a nested design, a class is identified by its position within its parent school, so S1:C1 and S2:C1 are distinct columns. Combining them changes the model: observations in those separate classes would be assigned the same class random effect. Use globally unique nested labels, and verify that each column’s observations belong to one parent level.
Check Your Understanding
Answer each question, then compare your reasoning with the solutions below.
- There are four observations from two clinics: clinic assignments are A, A, B, B. What is the dimension of the clinic incidence matrix, and what are its rows?
- Two schools each have a class labeled “Room 1.” Why should the class-within-school matrix have two columns for these classes?
- For an observation with subject B and item 3, how many 1s appear in its row of the subject matrix and item matrix separately?
- A row of a factor’s incidence matrix contains two 1s. What should be checked before treating this as a valid ordinary incidence row?
Solutions. (1) The matrix is \(4 \times 2\): four observation rows and one column for each clinic, A and B. Its rows indicate A, A, B, and B in that order. (2) The classes belong to different parent schools, so they are distinct nested levels; separate columns prevent their observations from being assigned one shared class effect. (3) Each separate matrix has one 1 in that row: the subject matrix selects B, and the item matrix selects 3. (4) Check for duplicate coding, incorrect labels, or a genuinely multiple-membership observation. Under the ordinary one-level-per-row definition, two 1s in one factor’s row are not valid.