[R] polynomial contrasts in R

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Fri Jan 17 12:43:03 CET 2003

On 17 Jan 2003, Peter Dalgaard BSA wrote:

> ripley at stats.ox.ac.uk writes:
> > I don't think you are describing contrasts for an ordered factor, but 
> > orthogonal polynomials in a numeric variable.  The latter are computed by 
> > the function poly() in both R and S-PLUS.  You could set them up to give a 
> > contrasts matrix if you want, but not a contrasts function (as that is 
> > only passed the number of levels, AFAIR).
> [Pet peeve] However, contr.poly *also* treats the factor as a numeric
> variable, it just assumes that the levels are equidistant. What other
> sense would a (say) linear term make? 

Not quite, it allocates scores to the factor levels, equally spaced 
scores.  The assumption is that is the best scoring you have for ordered 
factors (and nothing else known about them).

> Helmert contrasts (if they are
> useful anywhere...) or successive differences would have been more
> relevant for ordered factors, but for some reason S-PLUS chose
> differently and R has kept the same convention for ordered factors
> although we did depart from S-PLUS's use of Helmert contrasts for
> unordered factors.

Several `by default's are needed in there.

> Contrast functions are called with "..." it seems, but not the object
> itself, so if a factor has numeric levels, you could potentially have
> something like
> f.p <- C(f, contr.xpoly, x=as.numeric(levels(f)))
> but you do need the extra argument and it is hardly better than
> x <- as.numeric(levels(f))
> f.p <- C(f, poly(x, degree=length(x)-1))

That's only relevant to C(), I believe. But contrast functions (as set by
options(contrasts=) or via the constrasts(.arg) argument of model-fitting
functions) are called by model.matrix.default() without such an argument.  
(And in the functions contrasts().)

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

More information about the R-help mailing list