Pascal A. Niklaus
2003-Oct-08 12:19 UTC
[R] Contrast specified with C() - R vs S-Plus problem
Hi, For a n-level factor, I'd like to specify the first contrast and have the remaining n-2 constructed automatically so that the set is orthogonal. I then test the contrasts with summary.lm(anova-object). In S-Plus, the following works: >y.anova <- aov( y ~ C(CO2,c(1,0,-1)) ) >summary.lm(y.anova) In R, it fails with the following error: >levels(CO2) [1] "" "A" "C" "E" >y.anova <- aov(y + C(CO2,c(1,0,-1)) ) Error in "contrasts<-"(*tmp*, value = contr) : wrong number of contrast matrix rows What is the way to do this in R? Thanks Pascal -- Dr. Pascal A. Niklaus Institute of Botany University of Basel Sch?nbeinstrasse 6 CH-4056 Basel / Switzerland
Prof Brian Ripley
2003-Oct-08 12:51 UTC
[R] Contrast specified with C() - R vs S-Plus problem
On Wed, 8 Oct 2003, Pascal A. Niklaus wrote:> Hi, > > For a n-level factor, I'd like to specify the first contrast and have > the remaining n-2 constructed automatically so that the set is > orthogonal. I then test the contrasts with summary.lm(anova-object). > > In S-Plus, the following works: > > >y.anova <- aov( y ~ C(CO2,c(1,0,-1)) ) > >summary.lm(y.anova)I can't reproduce that in S-PLUS 6.1, and it is not as documented: contr what contrasts to use. May be one of four standard names ( helmert, poly, treatment, or sum), a function, or a matrix with as many rows as there are levels to the factor. You haven't given a matrix, and your vector gets converted to a 3x1 matrix when there are four levels. I suspect you have not got the same data in S-PLUS and R, but you haven't given us anything to check that.> In R, it fails with the following error: > > >levels(CO2) > [1] "" "A" "C" "E" > > >y.anova <- aov(y + C(CO2,c(1,0,-1)) ) > Error in "contrasts<-"(*tmp*, value = contr) : > wrong number of contrast matrix rows > > What is the way to do this in R?There are four levels, so> CO2 <- factor(c("", "A", "C", "E")) > attributes(C(CO2, as.matrix(1:4)))$levels [1] "" "A" "C" "E" $class [1] "factor" $contrasts [,1] [,2] [,3] 1 0.0236068 0.5472136 A 2 -0.4393447 -0.7120227 C 3 0.8078689 -0.2175955 E 4 -0.3921311 0.3824045 does as you ask. -- 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