# Re: [R] Name for factor's levels with contr.sum

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu 14 Jul 2005 - 18:19:26 EST

I think contr.treatment is misleading (it labels contrasts of two levels by just one of them), and Christoph's labels are informative but impractically long. But if you want to label each contrast by the level it contrasts with the rest,

``` 	cont <- array(0, c(lenglev, lenglev - 1),
list(levels, levels[-lenglev]))

```

in a modified contr.sum will do it. E.g.

> z <- factor(letters[1:3])
> contr.treatment(z)

b c
a 0 0
b 1 0
c 0 1
> contr.sum(z)

[,1] [,2]
a 1 0
b 0 1
c -1 -1
> mycontr.sum(z)

a b
a 1 0
b 0 1
c -1 -1

On Wed, 13 Jul 2005, Christoph Buser wrote:

> Dear Ghislain
>
> I do not know a general elegant solution, but for some
> applications the following example may be helpful:
>
> ## Artificial data for demonstration: group is fixed, species is random
> dat <- data.frame(group = c(rep("A",20),rep("B",17),rep("C",24)),
> species = c(rep("sp1", 4), rep("sp2",5), rep("sp3",5),
> rep("sp4",6), rep("sp5",2), rep("sp6",5), rep("sp7",3),
> rep("sp8",3), rep("sp9",4), rep("sp10",6), rep("sp11",6),
> rep("sp12",6), rep("sp13",6)),
> area = rnorm(61))
>
> ## You can attach a contrast at your fixed factor of interest "group"
> ## Create the contrast you like to test (in our case contr.sum for 3
> ## levels)
> mat <- contr.sum(3)
> ## You can add the names you want to see in the output
> ## Be carefull that you give the correct names to the concerned
> ## column. Otherwise there is the big danger of misinterpretation.
> colnames(mat) <- c(": A against rest", ": B against rest")
> ## Attatch the contrast at your factor "group"
> dat[,"group"] <- C(dat[,"group"],mat)
> ## Now calculate the lme
> library(nlme)
> reg.lme <- lme(area ~ group, data = dat, random = ~ 1|species)
> summary(reg.lme)
>
> Maybe someone has a better idea how to do it generally.
>
> Hope this helps
>
> Christoph Buser
>
> --------------------------------------------------------------
> Christoph Buser <buser@stat.math.ethz.ch>
> Seminar fuer Statistik, LEO C13
> ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND
> phone: x-41-44-632-4673 fax: 632-1228
> http://stat.ethz.ch/~buser/
> --------------------------------------------------------------
>
>
> Ghislain Vieilledent writes:
> > Good morning,
> >
> > I used in R contr.sum for the contrast in a lme model:
> >
> > > options(contrasts=c("contr.sum","contr.poly"))
> > > Septo5.lme<-lme(Septo~Variete+DateSemi,Data4.Iso,random=~1|LieuDit)
> > > intervals(Septo5.lme)\$fixed
> > lower est. upper
> > (Intercept) 17.0644033 23.106110 29.147816
> > Variete1 9.5819873 17.335324 25.088661
> > Variete2 -3.3794907 6.816101 17.011692
> > Variete3 -0.5636915 8.452890 17.469472
> > Variete4 -22.8923812 -10.914912 1.062558
> > Variete5 -10.7152821 -1.865884 6.983515
> > Variete6 0.2743390 9.492175 18.710012
> > Variete7 -23.7943250 -15.070737 -6.347148
> > Variete8 -21.7310554 -12.380475 -3.029895
> > Variete9 -27.9782575 -17.480555 -6.982852
> > DateSemi1 -5.7903419 -1.547875 2.694592
> > DateSemi2 3.6571596 8.428417 13.199675
> > attr(,"label")
> > [1] "Fixed effects:"
> >
> > How is it possible to obtain a return with the name of my factor's levels as
> > with contr.treatment ?
> >
> > Thanks for you help.
> >
> > --
> > Ghislain Vieilledent
> > 30, rue Bernard Ortet 31 500 TOULOUSE
> > 06 24 62 65 07
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

```--
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
```
Received on Thu Jul 14 18:22:38 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:39 EST