2015-04-19 131 views
1

我怎樣才能做到雙向交叉ANOVA在R,正如在這些網頁上被執行:雙向交叉ANOVA R中

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 
> 
+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 
+0

因此,這是評估機器和冷卻液的影響以及它們的相互作用?如果我只對冷卻液的效果感興趣會怎麼樣?我是否需要添加錯誤(機器)或類似的錯誤條款?這也可能與lm(value〜coollant * machine,mydf)相同。他們是一樣的嗎? – rnso

+0

查看'?公式'瞭解詳情。 「價值〜冷卻劑+機器」會分開看待它們,但不能交叉。如果你只需要冷卻液,則需要「冷卻液的價值」。你通常不需要一個錯誤術語AFAIK。 –