# [R] polynomial contrasts in R

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Jan 17 12:25:06 CET 2003

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

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

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

```