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.

install.packages("car")

install.packages("doBy")

library(car)

library(doBy)

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:

names(data)<-c("Y","A","B","Group")

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

data_split<-splitBy(~Group,data=data)

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:

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

Post a Comment