Hey, you will have to manipulate your data such that it is in this format:
position <- c('500','501','502','503','504','505')
coverage <- data.frame(
position = position,
A1=c(30,20,29,22,30,30),
A2=c(35,33,22,43,32,29),
B1=c(21,32,1,33,43,44),
B2=c(25,25,33,31,32,33),
C1=c(50,45,39,40,50,55),
C2=c(60,59,23,56,55,44))
rownames(coverage) = coverage$position
coverage <- coverage[,-1]
group <- c('A','A','B','B','C','C')
coverage <- data.frame(group, t(coverage))
coverage
group X500 X501 X502 X503 X504 X505
A1 A 30 20 29 22 30 30
A2 A 35 33 22 43 32 29
B1 B 21 32 1 33 43 44
B2 B 25 25 33 31 32 33
C1 C 50 45 39 40 50 55
C2 C 60 59 23 56 55 44
Now, we can set up a loop that will test each position in an ANOVA. You should study the functionality of the foreach() function. The actual ANOVA call is made with aov()
require(foreach)
res <- foreach(i = 2:ncol(coverage), .inorder = TRUE) %do% {
pos <- colnames(coverage)[i]
f <- as.formula(paste0(pos, ' ~ group'))
summary(aov(f, data = coverage))[[1]]["Pr(>F)"][1,1]
}
data.frame(
position = position,
pvalue = do.call(rbind, res))
position pvalue
1 500 0.01516230
2 501 0.09260065
3 502 0.67507024
4 503 0.36883595
5 504 0.04883831
6 505 0.11202618
The result is a 2-column data-frame with position and p-value from the ANOVA.
I expect you to adapt this code to your own situation.
Kevin
Since ANOVA is run in each row for each position of genome between breeds, and the number of positions are around 900,000, I can't plot them, so I want to know is it necessary checking the normality? How can I do that? If Shapiro Test is over-sensitive what procedure is recommended? Thank you for your time
I moved your message to a comment (you had posted it as an answer).
Ah - it is in these situations with large variable numbers whereby the Shapiro test will virtually always say 'not normally distributed', but don't quote me on this. I am not a Professor of Statistics. There is a good discussion here: https://stats.stackexchange.com/questions/12053/what-should-i-check-for-normality-raw-data-or-residuals
Also, given your large number of variables, you will want to 'parallelise' my code (below). You can do this by replacing
%do%with%dopar%in theforeachloop. You will also require doParallel package. Please see here for information on how to choose number of threads / CPU cores (system dependent): R functions for parallel processing