[R] contr.treatments query

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue May 20 11:14:30 CEST 2008


On 20-May-08 06:10:48, Prof Brian Ripley wrote:
>>From ?contrasts
> Usage:
>       contrasts(x, how.many) <- value
> ...
> how.many: How many contrasts should be made. Defaults to one less than
>            the number of levels of 'x'.  This need not be the same as
>            the number of columns of 'ctr'.
> 
> so that is 2 in your example, and it takes the first 2 of the 3 you 
> supplied.
> (The posting guide does ask you to read the help before posting.)

I had read it (and other), and it did not help me. The point of my
query was not the fact of getting 2 columns; I was expecting that.
The point was the difference, after

  N<-factor(sample(c(1,2,3),1000,replace=TRUE))

between

[A]
  contr.treatment(3, base=1, contrasts=TRUE)
  #   2 3
  # 1 0 0
  # 2 1 0
  # 3 0 1

i.e. omitting "level 1" as implied by

  "contr <- contr[, -base, drop = FALSE]"

in the code for contr.treatment(), and the result of

[B]
  contrasts(N)<-contr.treatment(3, base=1, contrasts=FALSE)
  contrasts(N)
  #   1 2
  # 1 1 0
  # 2 0 1
  # 3 0 0

i.e. omitting "level 3", despite having had the contrasts
assigned from exactly the same expression as in [A].

Possibly Bill Venables' comments may contain the clue;
but I would need to experiment to see whether that it is
fact the root cause.

With thanks,
Ted.

> On Tue, 20 May 2008, Ted.Harding at manchester.ac.uk wrote:
> 
>> Hi Folks,
>> I'm a bit puzzled by the following (example):
>>
>> N<-factor(sample(c(1,2,3),1000,replace=TRUE))
>> unique(N)
>> # [1] 3 2 1
>> # Levels: 1 2 3
>>
>> So far so good. Now:
>>
>> contrasts(N)<-contr.treatment(3, base=1, contrasts=FALSE)
>> contrasts(N)
>> #   1 2
>> # 1 1 0
>> # 2 0 1
>> # 3 0 0
>>
>> whereas:
>>
>> contr.treatment(3, base=1, contrasts=FALSE)
>> #   1 2 3
>> # 1 1 0 0
>> # 2 0 1 0
>> # 3 0 0 1
>>
>> contr.treatment(3, base=1, contrasts=TRUE)
>> #   2 3
>> # 1 0 0
>> # 2 1 0
>> # 3 0 1
>>
>> I can follow the last two fine -- they are what is implied
>> by the code for contr.treatment().
>>
>> Likewise:
>>
>> contrasts(factor(Nlevs <-c(1,2,3)))
>> #   2 3
>> # 1 0 0
>> # 2 1 0
>> # 3 0 1
>>
>> But why the different result when applied to N?
>>
>> With thanks,
>> Ted.
>>
>> --------------------------------------------------------------------
>> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
>> Fax-to-email: +44 (0)870 094 0861
>> Date: 20-May-08                                       Time: 01:12:30
>> ------------------------------ XFMail ------------------------------
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> -- 
> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 20-May-08                                       Time: 10:14:26
------------------------------ XFMail ------------------------------



More information about the R-help mailing list