# [R] polynomial contrasts in R

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Jan 17 13:44:02 CET 2003

```ripley at stats.ox.ac.uk writes:

> > [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).

...which is a metaphysical argument at best. The alternative is to say
that you should use contrasts that do not depend on *any* scoring.

> > 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.

Agreed.

> > 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().)

Yes, that leaves a couple of spots where a contr.xpoly wouldn't be
usable. I wasn't arguing for it; the matrix route is much clearer.

--
O__  ---- Peter Dalgaard             Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
(*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

```