简体   繁体   中英

N - way ANOVA in R

I'm working on conducting an ANOVA test from a latin squares experiment. For some reason, the aov() function only recognizes 2 out of the 3 variables used.

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. Can someone explain why R isn't cooperating and how to fix?

It's just because the combinations of "batch" and "ingredient" are the same (if batch=1, ingredient=A, etc.). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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