# How does R decide which parameters are inside the intercept for dummy coding?

Let's construct a simple example. Below is the code.

A<-gl(2,4) #factor of 2 levels
B<-gl(4,2) #factor of 4 levels
df<-data.frame(y,A,B)


As you can see, B is nested within A. The peculiar result I am interested in the output of the model matrix when I fit for a nested model . How does R decide what is included inside the intercept? Since we are using dummy coding, the coefficients of the model is interpreted as the difference between a particular level and the reference level/the intercept for an single factor model. I understand for model ~A, A1 becomes the intercept and that for model ~A+B, A1 and B1 (both) become the intercept.

I do not get why when we use a nested model, A1:B2 appears as a column inside the model matrix. Why isn't the first parameter of the interaction subspace A1:B1 or A2:B1? I think I am missing the concept. I think the intercept is A1. Hence, Why do we not compare the levels of A1:B1 and A1(intercept) or A2:B1 and A1(intercept)?

#nested model
> mod<-aov(y~A+A:B)
> model.matrix(mod)
(Intercept) A2 A1:B2 A2:B2 A1:B3 A2:B3 A1:B4 A2:B4
1           1  0     0     0     0     0     0     0
2           1  0     0     0     0     0     0     0
3           1  0     1     0     0     0     0     0
4           1  0     1     0     0     0     0     0
5           1  1     0     0     0     1     0     0
6           1  1     0     0     0     1     0     0
7           1  1     0     0     0     0     0     1
8           1  1     0     0     0     0     0     1

• The intercept represents the first group (depending on the factor levels). In your example the first group would be A == A1 and B == B1. Thus, an interaction A1:B1 is not needed in the model. Aug 22, 2016 at 16:17
• @Roland what about A2:B1? Why is A1:B2 included but not A2:B1? Aug 22, 2016 at 16:39
• Because that group can be determined by the intercept plus the A2 effect. Aug 22, 2016 at 18:23
• The mean of the group A1/B1 is predicted by the intercept; the mean of the group A2/B1 is predicted by the intercept plus the A2 effect; the mean of the group A2/B4 is predicted by the intercept plus the A2 effect + the A2:B4 effect. You can check this by using predict and comparing the result with manual calculations. Aug 23, 2016 at 13:07
• The model predicts the group means. Aug 24, 2016 at 17:15