Two-factor ANOVA Help
On 10/8/05, Jim Brindle <j_brindle at hotmail.com> wrote:
Hello,
I am trying to perform a two-factor ANOVA analysis using a blocking design with "Vol" as the response variable. My intent is to have "Rater" treated as the treatment variable and the "Pipe" treated as the blocking variable. I am reading and preparing my dataset using the following three lines of code:
values <- read.table("filename", header=TRUE)
attach(values)
values = data.frame(values)
The dataset is the following:
Pipe Rater Volume
1 A 5.129
1 B 5.296
1 C 4.679
1 D 4.776
2 A 8.519
2 B 8.482
2 C 7.659
2 D 7.798
3 A 13.769
3 B 14.621
3 C 12.418
3 D 13.189
Below there are 2 versions which I've used to run my analysis.
Option #1:
g <- lm(Volume ~ factor(Rater) + factor(Pipe), values)
print(anova(g))
Option #2:
Rater <- as.factor(Rater)
Pipe <- as.factor(Pipe)
g <- lm(Volume ~ Rater + Pipe, values)
print(anova(g))
A couple of questions I have are:
1. I thought that option #1 and option #2 would have given me the same results and they don't appear to. The only difference (to me) is how I have specified the factors used in the model. However, there appears to be something else I am missing and I was just wondering if anyone has any insight as to which is the correct way to code this analysis?
Note that values, as returned from read.table, is already a data frame
and Rater is already a factor so you only need to convert Pipe to a
factor:
values <- read.table("filename.dat", header = TRUE)
# shows classes of columns among other things
# note that Rater is already a factor and values is already a data frame
str(g)
# convert Pipe to a factor
values$Pipe <- factor(values$Pipe)
g <- lm(Volume ~., values)
g
2. Unless otherwise specified is there a particular reference level that R uses by default - for example in this case, the second treatment level (Rater B)?
By default R uses treatment contrasts and uses the first level as the baseline. You can change this using contrasts and contr.treatment. e.g. To use treatment effects on Pipe with level 2 as the baseline: contrasts(values$Pipe) <- contr.treatment(3, base = 2) g2 <- lm(Volume ~., values) g2
3. Is there a good reference someone can point me to for more insight on the two-factor ANOVA analysis with R?
See ?read.table, ?contrasts, ?contr.treatment