[R] creating 'all' sum contrasts

Berwin A Turlach berwin at maths.uwa.edu.au
Fri Oct 15 14:55:53 CEST 2010


G'day Michael,

On Fri, 15 Oct 2010 12:09:07 +0100
Michael Hopkins <hopkins at upstreamsystems.com> wrote:

> OK, my last question didn't get any replies so I am going to try and
> ask a different way.
> 
> When I generate contrasts with contr.sum() for a 3 level categorical
> variable I get the 2 orthogonal contrasts:
> 
> > contr.sum( c(1,2,3) )
>   [,1] [,2]
> 1    1    0
> 2    0    1
> 3   -1   -1

These two contrasts are *not* orthogonal.

> This provides the contrasts <1-3> and <2-3> as expected.  But I also
> want it to create <1-2> (i.e. <1-3> - <2-3>).  So in general I want
> all possible orthogonal contrasts - think of it as the contrasts for
> all pairwise comparisons between the levels.

You have to decide what you want.  The contrasts for all pairwise
comparaisons between the levels or all possible orthogonal contrasts?

The latter is actually not well defined.  For a factor with p levels,
there would be p orthogonal contrasts, which are only identifiable up to
rotation, hence infinitely many such sets. But there are p(p-1)
pairwise comparisons. So unless p=2, yo have to decide what you want....

> Are there are any options for contrast() or other functions/libraries
> that will allow me to do this automatically?

Look at package multcomp, in particular functions glht and mcp, these
might help.

Cheers,

	Berwin

========================== Full address ============================
Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)
School of Maths and Stats (M019)            +61 (8) 6488 3383 (self)
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway                   
Crawley WA 6009                e-mail: berwin at maths.uwa.edu.au
Australia                        http://www.maths.uwa.edu.au/~berwin



More information about the R-help mailing list