I noticed that when I run an ANOVA in R (aov) that when I set up the group vector the results will differ whether I use string or numeric encodings for the group vector. So, to differentiate between groups I once set up the group vector as v1 = c(1,1,1,2,2,2,3,3,3) and another time as v1 = c("a","a","a", "b", "b", "b", "c","c","c") and the ANOVA produces different results. Any idea why that is the case?
1 Answer
Some of the ANOVA procedures in R require you to declare the 'group vector' using as.factor
. (Otherwise, if the group vector uses numbers, the 'linear model' may do regression, not the intended ANOVA. You can check the number of DF for the group line in the ANOVA table to be sure you're getting ANOVA.
Examples: Consider the following data with five levels of the factor and ten replications per level. [Toy example for simplicity: All groups have the same population mean.]
x = rnorm(50)
g = rep(1:5, each=10)
The R procedure oneway.test
does not require a factor variable.
[This is a Satherwaite-corrected one-factor ANOVA for use when levels may
have different variances.]
oneway.test(x ~ g)
One-way analysis of means (not assuming equal variances)
data: x and g
F = 2.0076, num df = 4.000, denom df = 22.327, p-value = 0.128
However, the lm
structure does require a factor variable if it is to be an ANOVA.
Incorrect output for ANOVA here:
anova(lm(x~g))
Analysis of Variance Table
Response: x
Df Sum Sq Mean Sq F value Pr(>F)
g 1 0.002 0.00244 0.002 0.9648 # Note DF = 1 (regression)
Residuals 48 59.486 1.23930
We get correct ANOVA output if we use a factor variable:
gp = as.factor(g)
anova(lm(x ~ gp))
Analysis of Variance Table
Response: x
Df Sum Sq Mean Sq F value Pr(>F)
gp 4 9.411 2.3528 2.1143 0.09464 . # Note DF = 4 for five levels
Residuals 45 50.078 1.1128
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
To be safe, it is always a good idea to use factor variables whenever an ANOVA
is intended. (The procedure oneway.test
works with factor variables.)
More:
aov(x ~ g)
Call:
aov(formula = x ~ g)
Terms:
g Residuals
Sum of Squares 0.00244 59.48648
Deg. of Freedom 1 48 # Wrong
Residual standard error: 1.113239
Estimated effects may be unbalanced
aov(x ~ gp)
Call:
aov(formula = x ~ gp)
Terms:
gp Residuals
Sum of Squares 9.41136 50.07756
Deg. of Freedom 4 45 # OK
Residual standard error: 1.05491
Estimated effects may be unbalanced
-
2$\begingroup$ A very comprehensive answer and it explains it well and answers my question. Would upvote multiple times if I could. Thank you very much. $\endgroup$– MattNov 3, 2020 at 5:46
-
1$\begingroup$ Acceptance, upvote, and thanks are much appreciated. $\endgroup$– BruceETNov 3, 2020 at 6:29
as.factor
. $\endgroup$