简体   繁体   English

R中的N向ANOVA

[英]N - way ANOVA in R

I'm working on conducting an ANOVA test from a latin squares experiment. 我正在根据一个拉丁方实验进行ANOVA测试。 For some reason, the aov() function only recognizes 2 out of the 3 variables used. 由于某些原因,aov()函数只能识别使用的3个变量中的2个。

batch = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
day = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
ingredient = c('A','B','C','D','E','A','B','C','D','E','A','B','C','D','E','A','B','C','D','E','A','B','C','D','E')
rxn.time = c(8,11,4,6,4,7,2,9,8,2,1,7,10,6,3,7,3,1,6,8,3,8,5,10,8)
rxn.exp = data.frame(batch, day, ingredient, rxn.time)
test.10 = aov(rxn.exp$rxn.time~as.factor(rxn.exp$batch)+as.factor(rxn.exp$day)+as.factor(ingredient))
summary(test.10)

The summary produces an output for only the first 2 factors. 摘要仅针对前两个因素产生输出。 I've tried using just the factor() function instead of as.factor as well as several other attempts to get R to run the test as desired with 3 different sources of variation plus any noise. 我试过只使用factor()函数而不是as.factor以及其他几种尝试让R根据需要使用3种不同的变异源以及任何噪声来运行测试。 Can someone explain why R isn't cooperating and how to fix? 有人可以解释为什么R不合作以及如何解决吗?

It's just because the combinations of "batch" and "ingredient" are the same (if batch=1, ingredient=A, etc.). 仅仅是因为“批次”和“成分”的组合相同(如果批次= 1,成分= A,等等)。 If you use random ingredients, it works. 如果您使用随机成分,它会起作用。 Eg: 例如:

ingredient = LETTERS[sample(1:5, size= length(batch), replace=T)]
rxn.exp = data.frame(batch=as.factor(batch), day=as.factor(day), ingredient=as.factor(ingredient), rxn.time=rxn.time)
test.10 = lm(rxn.time~batch+day+ingredient, data=rxn.exp)
summary(test.10)

Also note that I refer to the dataframe in the lm function. 另请注意,我在lm函数中引用了数据框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM