From ggrothendieck at gmail.com  Thu Apr 22 06:26:07 2010
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Thu, 22 Apr 2010 00:26:07 -0400
Subject: [Rd] Bugs? when dealing with contrasts
In-Reply-To: <20100422113848.1187240f@bossiaea>
References: <l2u971536df1004211100q9beecbep1bceb5bbbd3b8175@mail.gmail.com> 
	<20100422113848.1187240f@bossiaea>
Message-ID: <s2u971536df1004212126xb4b133a5l6ad338f30472b88e@mail.gmail.com>

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.



[Rd] Bugs? when dealing with contrasts
Gabor Grothendieck