[R] contrasts in lm

Prof Brian D Ripley ripley at stats.ox.ac.uk
Fri Aug 31 08:06:31 CEST 2001


On Thu, 30 Aug 2001, Warnes, Gregory R wrote:

> I've been playing around with contrasts in lm by specifying the contrasts
> argument.  So, I want to specify a specific contrast to be tested
>
> Say:
>
> 	> y _ rnorm(100)
> 	> x _  cut(rnorm(100, mean=y, sd=0.25),c(-3,-1.5,0,1.5,3))
> 	> reg _ lm(y ~ x, contrasts=list(x=c(1,0,0,-1)))
> 	> coef(reg)[2]
> 	      x1
> 	-1.814101
>
> I was surprised to see that I get a different estimate for the contrast
> specified this way than if I do

That's not an estimate of the contrast, just of one element of the
contrast.  The full answer is something like

> coef(reg)
(Intercept)          x1          x2          x3
 0.09832456 -1.84557466  0.74762184  0.34896487

>
> 	> reg2 _ lm(y ~ x, contrasts=list(x=contr.sum))
> 	> coef(reg2)[2]
> 	       x1
> 	-1.816682
>
> even though the first contrast in the same in both cases:
>
> 	> reg$contrasts
> 	$x
> 	          [,1]       [,2]        [,3]
> 	(-3,-1.5]    1 -0.2697521  0.42099143
> 	(-1.5,0]     0 -0.3256196 -0.80247857
> 	(0,1.5]      0  0.8651239 -0.03950429
> 	(1.5,3]     -1 -0.2697521  0.42099143
> 	> reg2$contrasts
> 	$x
> 	          [,1] [,2] [,3]
> 	(-3,-1.5]    1    0    0
> 	(-1.5,0]     0    1    0
> 	(0,1.5]      0    0    1
> 	(1.5,3]     -1   -1   -1

Why were you surprised?  If I do

lm(y ~ x + z)
lm(y ~ x + w)

for continuous x and different continuous z and w I should not expect the
same regression coefficient for x.  That's what you've done.

The exception is where z and w are orthogonal to x, but columns of
the model matrix are not in general orthogonal, especially with
unbalanced designs (as here) and several of the standard contrasts
functions (as in your second example).

> Questions:
>
>   1) How are columns 2 and 3 of the first contrast matrix being created and
> why?

by contrasts<-(), to complete a span of the space.  It uses orthogonal
terms,

>   2) What is the proper way of doing custom contrasts?  I'm interested in a
> 'general' solution.

function C().  I suspect you actually intended

lm(y ~ C(x, contr.sum, 1))

and confused yourself by not looking at the whole answer.

I do recommend Bill Venables' account of coding in chapter 6 of MASS:
there seem to be several misconceptions to clear up here.
One thing to watch is that R's default, contr.treatment, are *not
contrasts* in the conventional meaning.


-- 
Brian D. Ripley,                  ripley at 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

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list