[R] contr.treatments query

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 20 08:10:48 CEST 2008


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


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



More information about the R-help mailing list