On Wed, 20 Feb 2002, Greg Snow wrote:
>
> Trying to answer this question brought up my own question on the workings
> of the C function for defining contrasts. Others in the group, please
> skim to the bottom of my response to look at my question. Thanks.
[...]
> Now my question for the group:
>
> I originally thought that this could also be done using the contrasts or C
> functions, but if I try the following code (S-PLUS 6.0.3, R 1.3.1)
>
> b <- a
> contrasts(b,2) <- cbind( c(1,0,1), c(0,1,0) )
> attributes(b)
>
> temp6 <- lm( y ~ -1 + a + b:x, x=T)
> temp6$x
>
> then there is no evidence that the contrasts were ever used, I expected
> the same x-matrix as temp3$x above. Why are the contrasts not used in
> creating the interaction terms?
>
> I thought that maybe this was just something to do with the code that
> generates the interactions so I tried:
>
> temp7 <- lm( y ~ -1 + b + b:x, x=T, singular.ok=T)
> temp7$x
>
> Now in S-PLUS 6 the intercept has come back despite the -1 (needing the
> singular.ok=T to compute and not giving the desired output, though
> dummy.coef does give the answers), and in R 1.3.1 the contrasts are again
> ignored.
>
> Is this a bug, or am I missing a reason why the contrasts should be
> working the way they are?
You'll do better to look at the model matrix (here with treatment
contrasts in use):
model.matrix(y ~ -1 + a + b:x)
aa ab ac ba:x bb:x bc:x
1 1 0 0 1 0 0
2 1 0 0 2 0 0
3 1 0 0 3 0 0
4 0 1 0 0 1 0
5 0 1 0 0 2 0
6 0 1 0 0 3 0
7 0 0 1 0 0 1
8 0 0 1 0 0 2
9 0 0 1 0 0 3
Note, three columns for b:x, which is correct as there is no x main
effect.
model.matrix.default(y ~ -1 + a + x + b:x)
aa ab ac x x:b1 x:b2
1 1 0 0 1 1 0
2 1 0 0 2 2 0
3 1 0 0 3 3 0
4 0 1 0 1 0 1
5 0 1 0 2 0 2
6 0 1 0 3 0 3
7 0 0 1 1 1 0
8 0 0 1 2 2 0
9 0 0 1 3 3 0
does use the contrasts. So, contrasts are only used when less than the
number of levels is required.
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
|