[Rd] contrasts

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu May 8 09:44:58 MEST 2003

```On Thu, 8 May 2003, Torsten Hothorn wrote:

>
> > On Wed, 7 May 2003, Torsten Hothorn wrote:
> >
> > > I spent some time now trying to understand how functions for the
> > > computation of contrasts
> > >
> > > contr.foo(n, contrasts=TRUE)
> > >
> > > passed to model.matrix are called. My problem is
> > > that for the computation of some contrasts one needs `n' to be the number
> > > of observations at each level of the factor of interest and for others the
> > > number of levels is sufficient. For example, `contr.treatment' has code
> > > for handling vector valued `n' (and ?contrasts states that `n' may be a vector).
> > >
> > > Where can I find the condition under which `n = nlevel(x)' or `n =
> > > table(x)' or how can I define what I would like to see?

BTW, I don't think contr.treatment does this.  It is documented to take
either the set of levels or the number of levels, not a table of counts:
it is coincidental that it works with the latter since it only looks at
the length.

> > >From model.matrix, only the number of levels is passed: src/main/model.c
> > line 1626 (in R-devel and probably also 1.7.0).
>
> Thank you! Therefore one cannot compute contrasts that depend on
> the number of observations at each level, right? Looking at the code in
> `contr.treatment' that handles this case: may I conclude that this is on
> the wishlist?

But you can, just not from inside model.matrix.  Contrast functions have
other uses, including computing contrast matrices to pass to model-fitting
functions via a `contrasts.arg' or 'contrasts' argument.

I don't think altering model.matrix is on anyone's wishlist (look at the C
code if you need dissuading), and it could only assume a minimal spec for
the contrast functions supplied: there are several user-written ones out
there.  E.g. contr.sdif (MASS) will not give sensible labels when supplied
a table of counts.

--
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```