[Rd] Bugs? when dealing with contrasts

Gabor Grothendieck ggrothendieck at gmail.com
Thu Apr 22 06:26:07 CEST 2010


On Wed, Apr 21, 2010 at 11:38 PM, Berwin A Turlach
<berwin at maths.uwa.edu.au> wrote:
>> > # But I was expecting this since I am using contr.sum
>> > cbind(1, model.matrix(~ fac)[,2:3] * scores)
>>      fac1 fac2
>> 1  1   -2    0
>> 2  1   -1    0
>> 3  1    0    0
>> 4  1    1    0
>> 5  1    2    0
>> 6  1    0   -2
>> 7  1    0   -1
>> 8  1    0    0
>> 9  1    0    1
>> 10 1    0    2
>> 11 1    2    2
>> 12 1    1    1
>> 13 1    0    0
>> 14 1   -1   -1
>> 15 1   -2   -2
>
> If you wish to have the design matrix that contains the interaction
> terms as if the model had the main effects too but without the columns
> corresponding to the main effects, then just instruct R that you want
> to have such a matrix:
>
> R> model.matrix(~ scores*fac)[,-(2:4)]
>   (Intercept) scores:fac1 scores:fac2
> 1            1          -2           0
> 2            1          -1           0
> 3            1           0           0
> 4            1           1           0
> 5            1           2           0
> 6            1           0          -2
> 7            1           0          -1
> 8            1           0           0
> 9            1           0           1
> 10           1           0           2
> 11           1           2           2
> 12           1           1           1
> 13           1           0           0
> 14           1          -1          -1
> 15           1          -2          -2
>
> Though, I am not sure why one wants to fit such a model.

To save on degrees of freedom.

I had reduced it to the minimum needed to illustrate but in fact its
closer to this (except the coding is not the one needed):

options(contrasts = c("contr.sum", "contr.poly"))
tab <- as.table(matrix(1:21, 7))
dimnames(tab) = list(X = letters[1:7], Y = LETTERS[1:3])
rr <- factor(row(tab))
cc <- factor(col(tab))
scores <- rep(seq(-3,3), 3)
model.matrix( ~ rr + cc + scores:cc)

so the main effects are rr and cc but scores takes the place of rr in
the interaction.

Your description of the process seems right since it would predict
that the following gives the required coding and it does:

model.matrix(~ scores*cc + rr)[,-2]

Thanks.



More information about the R-devel mailing list