Thursday, May 21, 2009

Multiple ANOVA tests in R by a grouping factor

If you have ever needed to run multiple ANOVA's according to or by a certain grouping factor here is your guide:

To do Type III sums of squares you'll need the car package. To split up your data by a grouping factor you'll need the doBy package.


Begin with a dataframe that has your factors, response, and grouping factor as columns (Y = response, A = factor A, B = factor B, Group = grouping factor)

If you have never read in data before into R, try something like this:

data<- read.table("/home/user/Desktop/data.txt")

I prefer to simply use text files that are delimited by space or tab.

Next name your columns:


Now you need to split your data up according to your grouping factor.


Lastly, run this loop:

i <-1 while(i <=length(data_split)){    
print (Anova(lm(Y~A+B+A*B, data_split[[i]]),type="III")[4,4])
i<- i +1

R output can be accessed in row and column fashion [r,c]. I have this particular code give me only the p-value from the interaction of factors A and B. By changing [4,4] to other numbers you can access whatever part of the ANOVA table you want. Hope this helps.

1 comment:

Anonymous said...

Very nice. However, you have several syntax errors in your script.