2015年1月5日星期一

Anova

data<-read.csv("/Users/leilei/Documents/Publication/anova.csv")

1.One-way ANOVA

anova1<-aov(math~instruction,data)
summary(anova1)
##             Df Sum Sq Mean Sq F value Pr(>F)  
## instruction  1    427     427    3.03  0.093 .
## Residuals   28   3940     141                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

2.Two-way ANOVA

anova2<-aov(math~instruction*environment,data)
summary(anova2)
##                         Df Sum Sq Mean Sq F value Pr(>F)  
## instruction              1    427     427    2.97  0.097 .
## environment              1     50      50    0.35  0.560  
## instruction:environment  1    153     153    1.07  0.311  
## Residuals               26   3736     144                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

3.ANCOVA

#simulate data for ANCOVA
ID<-c(1:20)
Y<-rnorm(20,8.2,1.7)
Base<-rnorm(20,68,10)
trt<-c(rep("Drug",10),c(rep("Placebo",10)))
cdata<-data.frame(cbind(ID,Y,Base,trt))
anova3<-aov(Y~Base*trt,data=data)
summary(anova3)
##             Df Sum Sq Mean Sq F value Pr(>F)
## Base         1   0.00   0.003    0.00   0.96
## trt          1   0.96   0.962    0.87   0.36
## Base:trt     1   0.15   0.146    0.13   0.72
## Residuals   16  17.68   1.105

4.Random Effect model assume insturction in random. Random here means that you did not enumerate all posibility. Fixed effect vice versa.

anova4<-aov(math~Error(instruction),data=data)
summary(anova4)
## 
## Error: instruction
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals  1    427     427               
## 
## Error: Within
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 28   3940     141

5.Mixed effect and nested model.

anova5<-aov(math~instruction+Error(environment/instruction),data=data)
summary(anova5)
## 
## Error: environment
##             Df Sum Sq Mean Sq
## instruction  1    9.3     9.3
## 
## Error: environment:instruction
##             Df Sum Sq Mean Sq F value Pr(>F)
## instruction  1    468     468    3.05   0.33
## Residuals    1    153     153               
## 
## Error: Within
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 26   3736     144

Reference: http://www.personality-project.org/r/r.guide.html#anova