1
$\begingroup$

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
$\endgroup$
9
  • $\begingroup$ 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. $\endgroup$
    – Roland
    Aug 22, 2016 at 16:17
  • $\begingroup$ @Roland what about A2:B1? Why is A1:B2 included but not A2:B1? $\endgroup$ Aug 22, 2016 at 16:39
  • 1
    $\begingroup$ Because that group can be determined by the intercept plus the A2 effect. $\endgroup$
    – Roland
    Aug 22, 2016 at 18:23
  • 1
    $\begingroup$ 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. $\endgroup$
    – Roland
    Aug 23, 2016 at 13:07
  • 1
    $\begingroup$ The model predicts the group means. $\endgroup$
    – Roland
    Aug 24, 2016 at 17:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.