[R] anova planned comparisons/contrasts

Peter Alspach PAlspach at hortresearch.co.nz
Thu Nov 22 21:44:10 CET 2007


Tyler 

For balanced data like this you might find aov() gives an output which
is more comparable to Sokal and Rohlf (which I don't have):

> trtCont <- C(sugars$treatment, matrix(c(-4,1,1,1,1, 0,-1,3,-1,-1), 5,
2))
> sugarsAov <- aov(length ~ trtCont, sugars)
> summary(sugarsAov, split=list(trtCont=list('control vs rest'=1, 'gf vs
others'=2)))
                           Df  Sum Sq Mean Sq  F value    Pr(>F)    
trtCont                     4 1077.32  269.33  49.3680 6.737e-16 ***
  trtCont: control vs rest  1  832.32  832.32 152.5637 4.680e-16 ***
  trtCont: gf vs others     1   48.13   48.13   8.8228  0.004759 ** 
Residuals                  45  245.50    5.46                       
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
> model.tables(sugarsAov, type='mean', se=T)
Tables of means
Grand mean
      
61.94 

 trtCont 
trtCont
   control   fructose gluc+fruct    glucose    sucrose 
      70.1       58.2       58.0       59.3       64.1 

Standard errors for differences of means
        trtCont
          1.045
replic.      10

Since the two specified contrasts are orthogonal, the difference among
the remaining three sugars can be obtained by substraction:

> sugarsSum <- summary(sugarsAov,
                       split=list(trtCont=list('control vs rest'=1, 'gf
vs others'=2)))
# Sum Sq
> sugarsSum[[1]][1,2]-sum(sugarsSum[[1]][2:3,2])
         
196.8667 
# Mean Sq
> (sugarsSum[[1]][1,2]-sum(sugarsSum[[1]][2:3,2]))/2
         
98.43333 
# F value
> ((sugarsSum[[1]][1,2]-sum(sugarsSum[[1]][2:3,2]))/2) /
sugarsSum[[1]][4,3]
         
18.04277 
# Pr(>F)
> 1-pf(((sugarsSum[[1]][1,2]-sum(sugarsSum[[1]][2:3,2]))/2) /
sugarsSum[[1]][4,3], 2, 45)
             
1.762198e-06 

HTH ........

Peter Alspach

> [mailto:r-help-bounces at r-project.org] On Behalf Of Tyler Smith
> Subject: [R] anova planned comparisons/contrasts
> 
> Hi,
> 
> I'm trying to figure out how anova works in R by translating 
> the examples in Sokal And Rohlf's (1995 3rd edition) 
> Biometry. I've hit a snag with planned comparisons, their box 
> 9.4 and section 9.6. It's a basic anova design:
> 
> treatment <- factor(rep(c("control", "glucose", "fructose", 
> 	                  "gluc+fruct", "sucrose"), each = 10))
> 
> length <- c(75, 67, 70, 75, 65, 71, 67, 67, 76, 68,
>             57, 58, 60, 59, 62, 60, 60, 57, 59, 61,
>             58, 61, 56, 58, 57, 56, 61, 60, 57, 58,
>             58, 59, 58, 61, 57, 56, 58, 57, 57, 59,
>             62, 66, 65, 63, 64, 62, 65, 65, 62, 67)
> 
> sugars <- data.frame(treatment, length)
> 
> The basic analysis is easy enough:
> 
> anova(lm(length ~ treatment, sugars))
> 
> S&R proceed to calculate planned comparisons between control 
> and all other groups, between gluc+fruct and the remaining 
> sugars, and among the three pure sugars. I can get the first 
> two comparisons using the
> contrasts() function:
> 
> contrasts(sugars$treatment) <- matrix(c(-4, 1, 1,  1,  1,
>                                         0, -1, 3, -1, -1), 5, 2) 
> 
> summary(lm(length ~ treatment, sugars))
> 
> The results appear to be the same, excepting that the book 
> calculates an F value, whereas R produces an equivalent t 
> value. However, I can't figure out how to calculate a 
> contrast among three groups. For those without access to 
> Sokal and Rohlf, I've written an R function that performs the 
> analysis they use, copied below. Is there a better way to do 
> this in R?
> 
> Also, I don't know how to interpret the Estimate and Std. 
> Error columns from the summary of the lm with contrasts. It's 
> clear the intercept in this case is the grand mean, but what 
> are the other values? They appear to be the difference 
> between one of the contrast groups and the grand mean -- 
> wouldn't an estimate of the differences between the 
> contrasted groups be more appropriate, or am I (likely) 
> misinterpreting this?
> 
> Thanks!
> 
> Tyler
[Code deleted]



More information about the R-help mailing list