[R] linear contrasts with anova

Steffen Katzner steffen.katzner at mail.gwdg.de
Wed Jan 18 19:14:06 CET 2006


group=factor(rep(c(0:2), each = 8))
ar = data.frame(group, dip)

con = matrix(c(1, -1, 0, 1, 0, -1), nrow=3, ncol=2, byrow=F)
contrasts(ar$group)=con

aovRes = aov(dip~group, ar)

 > summary.aov(aovRes, split=list(group = list("0 vs 1" = 1, "0 vs 3" = 2)))

                 Df Sum Sq Mean Sq  F value    Pr(>F)
group            2 919.10  459.55  57.3041 3.121e-09 ***
   group: 0 vs 1  1   2.10    2.10   0.2622     0.614
   group: 0 vs 3  1 917.00  917.00 114.3460 5.915e-10 ***
Residuals       21 168.41    8.02
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


I only don't know why it does not work with within-subject designs.
I have posted this question before, does anybody know?

-steffen






> I have some doubts about the validity of my procedure to estimeate linear contrasts ina a factorial design.
> For sake of semplicity, let's imagine a one way ANOVA with three levels. I am interested to test the significance of the difference between the first and third level (called here contrast C1) and between the first and the seconda level (called here contrast C2). I used the following procedure:
> 
> 
> ------------------- reading data from a text file -----------------------
> 
>> ar <-read.table("C:/Programmi/R/myworks/contrasti/cont1.txt",header=TRUE)
> 
>> ar
> 
>      CC GROUP
> 
> 1   3.0     0
> 
> 2   3.0     0
> 
> 3   4.0     0
> 
> 4   5.0     0
> 
> 5   6.0     0
> 
> 6   7.0     0
> 
> 7   3.0     0
> 
> 8   2.0     0
> 
> 9   1.0     1
> 
> 10  6.0     1
> 
> 11  5.0     1
> 
> 12  7.0     1
> 
> 13  2.0     1
> 
> 14  3.0     1
> 
> 15  1.5     1
> 
> 16  1.7     1
> 
> 17 17.0     2
> 
> 18 12.0     2
> 
> 19 15.0     2
> 
> 20 16.0     2
> 
> 21 12.0     2
> 
> 22 23.0     2
> 
> 23 19.0     2
> 
> 24 21.0     2
> 
>  
> 
> ------------------- creating a new array of data-----------------------
> 
>> ar<-data.frame(GROUP=factor(ar$GROUP),DIP=ar$CC)
> 
>> ar
> 
>    GROUP  DIP
> 
> 1      0  3.0
> 
> 2      0  3.0
> 
> 3      0  4.0
> 
> 4      0  5.0
> 
> 5      0  6.0
> 
> 6      0  7.0
> 
> 7      0  3.0
> 
> 8      0  2.0
> 
> 9      1  1.0
> 
> 10     1  6.0
> 
> 11     1  5.0
> 
> 12     1  7.0
> 
> 13     1  2.0
> 
> 14     1  3.0
> 
> 15     1  1.5
> 
> 16     1  1.7
> 
> 17     2 17.0
> 
> 18     2 12.0
> 
> 19     2 15.0
> 
> 20     2 16.0
> 
> 21     2 12.0
> 
> 22     2 23.0
> 
> 23     2 19.0
> 
> 24     2 21.0
> 
>  
> 
> ------------------- creating two dummy variables (C1 and C2) for linear contrasts-----------------------
> 
>> ar<-data.frame(GROUP=factor(ar$GROUP),C1=factor(ar$GROUP),C2=factor(ar$GROUP),DIP=ar$DIP)
> 
>> ar
> 
>    GROUP C1 C2  DIP
> 
> 1      0  0  0  3.0
> 
> 2      0  0  0  3.0
> 
> 3      0  0  0  4.0
> 
> 4      0  0  0  5.0
> 
> 5      0  0  0  6.0
> 
> 6      0  0  0  7.0
> 
> 7      0  0  0  3.0
> 
> 8      0  0  0  2.0
> 
> 9      1  1  1  1.0
> 
> 10     1  1  1  6.0
> 
> 11     1  1  1  5.0
> 
> 12     1  1  1  7.0
> 
> 13     1  1  1  2.0
> 
> 14     1  1  1  3.0
> 
> 15     1  1  1  1.5
> 
> 16     1  1  1  1.7
> 
> 17     2  2  2 17.0
> 
> 18     2  2  2 12.0
> 
> 19     2  2  2 15.0
> 
> 20     2  2  2 16.0
> 
> 21     2  2  2 12.0
> 
> 22     2  2  2 23.0
> 
> 23     2  2  2 19.0
> 
> 24     2  2  2 21.0
> 
>  
> 
> ------------------- selecting the contrast levels-----------------------
> 
>> ar$C1 <- C(ar$C1, c(1,0,-1), how.many = 1)
> 
>> ar$C2 <- C(ar$C2, c(1,-1,0), how.many = 1)
> 
>  
> 
>  
> 
> ------------------- contrast analysis of C2 -----------------------
> 
>> r.aov8 <-aov(DIP ~  C2 + GROUP , data = ar)
> 
>> anova(r.aov8)
> 
> Analysis of Variance Table
> 
>  
> 
> Response: DIP
> 
>           Df Sum Sq Mean Sq  F value    Pr(>F)    
> 
> C2         1   2.10    2.10   0.2622     0.614    
> 
> GROUP      1 917.00  917.00 114.3460 5.915e-10 ***
> 
> Residuals 21 168.41    8.02                       
> 
> ---
> 
> Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
> 
>  
> 
> ------------------- contrast analysis of C1 -----------------------
> 
>> r.aov9 <-aov(DIP ~  C1 + GROUP , data = ar)
> 
>> anova(r.aov9)
> 
> Analysis of Variance Table
> 
>  
> 
> Response: DIP
> 
>           Df Sum Sq Mean Sq F value    Pr(>F)    
> 
> C1         1 650.25  650.25  81.083 1.175e-08 ***
> 
> GROUP      1 268.85  268.85  33.525 9.532e-06 ***
> 
> Residuals 21 168.41    8.02                      
> 
> ---
> 
> Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
> 
>  
> 
> ------------------- anova of the global design -----------------------
> 
>> r.aov10 <-aov(DIP ~  GROUP , data = ar)
> 
>> anova(r.aov10)
> 
> Analysis of Variance Table
> 
>  
> 
> Response: DIP
> 
>           Df Sum Sq Mean Sq F value    Pr(>F)    
> 
> GROUP      2 919.10  459.55  57.304 3.121e-09 ***
> 
> Residuals 21 168.41    8.02                      
> 
> ---
> 
> Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
> 
> 
> 
> 
> 
> 
> 
> 
> 
> I would like to know if there is a more economic procedure with R to do linear contrasts.
> 
> Every comments will be well accepted.
> 
> 
> 
> Thank you very much and best regards
> 
> 
> 
> Marco Tommasi
> 
> 	[[alternative HTML version deleted]]
>




More information about the R-help mailing list