在R中双向越过anova

时间:2015-04-19 03:09:00

标签: r anova

如何在R中进行双向交叉anova:

http://www.itl.nist.gov/div898/handbook/ppc/section2/ppc232.htm

http://www.itl.nist.gov/div898/handbook/ppc/section2/ppc2321.htm

数据如下:

> dput(mydf)
structure(list(coolant = c("A", "A", "A", "A", "A", "B", "B", 
"B", "B", "B"), M1 = c(0.125, 0.127, 0.125, 0.126, 0.128, 0.124, 
0.128, 0.127, 0.126, 0.129), M2 = c(0.118, 0.122, 0.12, 0.124, 
0.119, 0.116, 0.125, 0.119, 0.125, 0.12), M3 = c(0.123, 0.125, 
0.125, 0.124, 0.126, 0.122, 0.121, 0.124, 0.126, 0.125), M4 = c(0.126, 
0.128, 0.126, 0.127, 0.129, 0.126, 0.129, 0.125, 0.13, 0.124), 
    M5 = c(0.118, 0.129, 0.127, 0.12, 0.121, 0.125, 0.123, 0.114, 
    0.124, 0.117)), .Names = c("coolant", "M1", "M2", "M3", "M4", 
"M5"), class = "data.frame", row.names = c(NA, -10L))
> 
> mydf
   coolant    M1    M2    M3    M4    M5
1        A 0.125 0.118 0.123 0.126 0.118
2        A 0.127 0.122 0.125 0.128 0.129
3        A 0.125 0.120 0.125 0.126 0.127
4        A 0.126 0.124 0.124 0.127 0.120
5        A 0.128 0.119 0.126 0.129 0.121
6        B 0.124 0.116 0.122 0.126 0.125
7        B 0.128 0.125 0.121 0.129 0.123
8        B 0.127 0.119 0.124 0.125 0.114
9        B 0.126 0.125 0.126 0.130 0.124
10       B 0.129 0.120 0.125 0.124 0.117

感谢您的帮助。

编辑:我试过了,但我不确定它是否正确:

> mm = melt(mydf, id='coolant')
> aov.out = aov(value~variable + Error(coolant), data=mm)
> aov.out

Call:
aov(formula = value ~ variable + Error(coolant), data = mm)

Grand Mean: 0.12404

Stratum 1: coolant

Terms:
                Residuals
Sum of Squares   3.92e-06
Deg. of Freedom         1

Residual standard error: 0.001979899

Stratum 2: Within

Terms:
                  variable  Residuals
Sum of Squares  0.00030332 0.00036068
Deg. of Freedom          4         44

Residual standard error: 0.002863088
Estimated effects may be unbalanced
> 
> summary(aov.out)

Error: coolant
          Df   Sum Sq  Mean Sq F value Pr(>F)
Residuals  1 3.92e-06 3.92e-06               

Error: Within
          Df    Sum Sq   Mean Sq F value   Pr(>F)    
variable   4 0.0003033 7.583e-05   9.251 1.63e-05 ***
Residuals 44 0.0003607 8.200e-06                     
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> 

1 个答案:

答案 0 :(得分:1)

这是一种基本方法。

mydf$coolant <- as.factor(mydf$coolant) # needs to be a factor
str(mydf)
library("reshape2") # brings the melt function
tmp <- melt(mydf, id.vars = "coolant") # converts each machine to a factor
str(tmp) # compare to str(mydf) to see how things have changed
names(tmp) <- c("coolant", "machine", "value") # just for convenience
fit <- aov(value ~ coolant*machine, data = tmp)
summary(fit)

给出:

                Df    Sum Sq   Mean Sq F value
coolant          1 0.0000039 3.920e-06   0.453
machine          4 0.0003033 7.583e-05   8.766
coolant:machine  4 0.0000147 3.670e-06   0.424
Residuals       40 0.0003460 8.650e-06        
                  Pr(>F)    
coolant            0.505    
machine         3.52e-05 ***
coolant:machine    0.790    
Residuals                   
---
Signif. codes:  
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1