From ripley@stats.ox.ac.uk  Mon Aug 14 07:48:39 2000
From: ripley@stats.ox.ac.uk (ripley@stats.ox.ac.uk)
Date: Mon, 14 Aug 2000 08:48:39 +0200 (MET DST)
Subject: [Rd] Possible bug (PR#633)
Message-ID: <200008140648.IAA17196@pubhealth.ku.dk>

On 13 Aug 2000, Peter Dalgaard BSA wrote:

> agusalon@mcr.ucm.es writes:
> 
> >     Rail^0  Rail^1       Rail^2   Rail^3   Rail^4       Rail^5
> >     66.5    54.303    -4.692    -2.658    -0.567    11.192
> > which do not seem correct.
> > 
> > Those reported in the book are:
> > 
> >     Rail2    Rail5    Rail1    Rail6       Rail3    Rail4
> >     31.667    50      54        82.667    84.667    96
> 
> This is a matter of differing conventions in handling
> 
> lm(travel ~ Rail - 1, data = Rail)
> 
> when Rail is an ordered factor. R uses polynomial contrasts in the
> no-intercept case as it does when the intercept is present, whereas
> Splus treats the ordered factor as if it were an unordered factor. If

S-PLUS does it internally, R via contr.*(n, contrast=F). Both 
systems give

> contr.treatment(6, contrasts=F)
  1 2 3 4 5 6
1 1 0 0 0 0 0
2 0 1 0 0 0 0
3 0 0 1 0 0 0
4 0 0 0 1 0 0
5 0 0 0 0 1 0
6 0 0 0 0 0 1

> contr.poly(6, contrasts=F)
     ^0         ^1         ^2         ^3         ^4          ^5
[1,]  1 -0.5976143  0.5455447 -0.3726780  0.1889822 -0.06299408
[2,]  1 -0.3585686 -0.1091089  0.5217492 -0.5669467  0.31497039
[3,]  1 -0.1195229 -0.4364358  0.2981424  0.3779645 -0.62994079
[4,]  1  0.1195229 -0.4364358 -0.2981424  0.3779645  0.62994079
[5,]  1  0.3585686 -0.1091089 -0.5217492 -0.5669467 -0.31497039
[6,]  1  0.5976143  0.5455447  0.3726780  0.1889822  0.06299408

but S-PLUS uses neither.

> you do something like the below, you get your coefficients.
> 
> I suspect that this could indeed be argued to be a bug in R, but
> others may know better.

I suspect it is an undocumented(?) feature.  However, as

lm(travel ~ Rail - 1, data = Rail)

and

lm(travel ~ Rail, data = Rail)

give the same results (up to the coefficient labels) it is not a useful
one, and so I guess we should change it.  Unfortunately we need either
to break the compatibility of contr.poly or to re-structure the internal
code.


Here's a simpler way to get the book's results:

> options(contrasts=rep("contr.treatment", 2))
> lm(travel ~ Rail - 1, data = Rail)

Call:
lm(formula = travel ~ Rail - 1, data = Rail)

Coefficients:
Rail2  Rail5  Rail1  Rail6  Rail3  Rail4  
31.67  50.00  54.00  82.67  84.67  96.00  

or

> contrasts(Rail$Rail) <- "contr.treatment"
> lm(formula = travel ~ Rail - 1, data = Rail)


-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] Possible bug (PR#633)
ripley@stats.ox.ac.uk