# How to include in a regression two factors where one level of one factor is necessarily nested with one level of another factor?

I've got a situation where I have a series of individuals who have two factors describing their health. Factor A has 3 levels while factor B has 4 levels. Biologically, level 1 of factor A appears if and only if factor B is level 1. Therefore, I have data structured like this:

ID   Factor A  Factor B   Score
1      a        alpha       0.1
2      a        alpha       0.2
3      b        beta        0.3
4      b        gamma       0.4
5      b        delta       0.5
6      c        beta        0.6
7      c        gamma       0.7
8      c        delta       0.8


I'm interested in studying the effects of both factors, but I'm struggling with how to properly code this. Giving each factor their own full set of dummy codes is rank deficient and leaving out a level of either factor creates a model with impossible combinations (ie level a ONLY occurs with level alpha, and vice versa). How could I code these fixed effects so that I can investigate the impact of both factors on our score? Additionally, how would I interpret this coding scheme?

EDIT: To give some further context on how this happens, Factor A is series of related diseases (each phenotypically similar, but genetically different) while Factor B is severity of that diseased state. So a is the control group while alpha is healthy severity. These two occur contemporaneously by necessity (ie a healthy severity must be a control while a control can be nothing but healthy).

## 1 Answer

After some pondering, I have an answer to my own question (to which I welcome feedback). I think the most straight-forward way to handle this is to create a new factor that is a cross-combination of the two factors of interest. And so, the data would look like this:

ID   Factor A  Factor B   Score    Cross-Factor
1      a        alpha       0.1       a-alpha
2      a        alpha       0.2       a-alpha
3      b        beta        0.3       b-beta
4      b        gamma       0.4       b-gamma
5      b        delta       0.5       b-delta
6      c        beta        0.6       c-beta
7      c        gamma       0.7       c-gamma
8      c        delta       0.8       c-delta


The model would then include a single factor with 7 levels. I can interpret the factor relative to the a-alpha level using contrasts to extract the change from baseline to whatever combination of the cross-factor I'm interested in, or between means of other level comparisons. In fact, I could set up the following contrast matrix using the method outlined on the UCLA page, as one example of a contrast matrix.

Original Matrix

     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    1    3    3    1    0    0    0
[2,]    0   -1    0    0    0    0    1
[3,]    0   -1    0    0    1    0   -1
[4,]    0   -1    0    0   -1    0    0
[5,]    0    0   -1    0    0    0    1
[6,]    0    0   -1    0    0    1   -1
[7,]    0    0   -1    1    0   -1    0


If this matrix is transposed, solved, and the first column removed to create a contrast matrix, the resulting coefficients would be the following comparisons:

1. Intercept is the baseline mean value (a-alpha)
2. Coefficient 1: Difference between baseline and average of all severity levels of b
3. Coefficient 2: Difference between baseline and average of all severity levels of c
4. Coefficient 3: Difference between baseline and the most severe form of c (c-delta)
5. Coefficient 4: Difference between the moderate and most severe forms of b
6. Coefficient 5: Difference between the moderate and most severe forms of c
7. Coefficient 6: Difference between mean of both mild forms and the mean of the moderate forms.
• Shamelessly accepting my own answer, but will consider both feedback and other answers if they appear. – Ashe Feb 9 '17 at 15:19