# Why does ANOVA give different results when a binary factor is treated as numeric?

I've encountered several times the situation where ANOVA gives drastically different results depending on whether the variable is entered as a factor or as a numeric variable. Why is this the case?

I understand somewhat that this classification evokes different analyses in the case where the variable has more than two levels - for example, say the variable is "grade level" and has levels 5, 6, 7. But, I sometimes get quite different results even when the variable has only two levels, say "grade level" with levels 5 and 6. Should I ever get different results in this case? Why? And how should I decide which result to use?

If it matters, I'm using ezANOVA in R with type 3 SS and the variable in question is between-subject, but the analysis also includes other between- and within-subjects variables. I'm not sure if this phenomenon reflects normal behavior of ANOVA or potentially a problem with ezANOVA.

In this reproducible example, ANOVA finds a significant effect of the within-subjects factor when the between-subjects variable is treated as a factor, but not when it is treated as numeric.

library(ez)
set.seed(1)
N       = 60
subjid  = factor( rep(1:N,each=2) )
between = c( rep(7,N), rep(8,N) )
within  = factor( rep(c("A","B"),N) )
x       = as.numeric( between )
y       = as.numeric( within=="A" )
dv      = x + 0.5*y + runif(length(between))
D       = data.frame( subjid=subjid, between=between, within=within, dv=dv )
D$betweenF = factor( D$between )

ezANOVA( data=D, wid=subjid, dv=dv, between=between, within=within, type=3 )
## Warning: "between" will be treated as numeric.
## $ANOVA ## Effect DFn DFd F p p<.05 ges ## 2 between 1 58 380.47083320 3.725618e-27 * 0.7765827418 ## 3 within 1 58 0.85506909 3.589537e-01 0.0068830702 ## 4 between:within 1 58 0.02700153 8.700494e-01 0.0002188134 ezANOVA( data=D, wid=subjid, dv=dv, between=betweenF, within=within, type=3 ) ##$ANOVA
##            Effect DFn DFd            F            p p<.05          ges
## 2        betweenF   1  58 380.47083320 3.725618e-27     * 0.7765827418
## 3          within   1  58 130.79260911 1.701170e-16     * 0.5145964337
## 4 betweenF:within   1  58   0.02700153 8.700494e-01       0.0002188134


It's because of how Type III SS are defined; it's the effect of the variable after accounting for all other variables including interactions, and the interaction is different when it's categorical than when it's continuous. In particular, when continuous and away from zero, the interaction is correlated with the main effect, resulting in the non-significant main effect.

Although SAS uses Type III by default, many statisticians believe Type II are more reasonable; this method accounts for all other variables and interactions except interactions including the variable of interest. So Type II SS are the same whether it's continuous or categorical.

ezANOVA( data=D, wid=subjid, dv=dv, between=between, within=within, type=2 )
## Warning: "between" will be treated as numeric.
## $ANOVA ## Effect DFn DFd F p p<.05 ges ## 2 between 1 58 380.47083320 3.725618e-27 * 0.7765827418 ## 3 within 1 58 130.79260911 1.701170e-16 * 0.5145964337 ## 4 between:within 1 58 0.02700153 8.700494e-01 0.0002188134 ezANOVA( data=D, wid=subjid, dv=dv, between=betweenF, within=within, type=2 ) ##$ANOVA
##            Effect DFn DFd            F            p p<.05          ges
## 2        betweenF   1  58 380.47083320 3.725618e-27     * 0.7765827418
## 3          within   1  58 130.79260911 1.701170e-16     * 0.5145964337
## 4 betweenF:within   1  58   0.02700153 8.700494e-01       0.0002188134

• Thank you for the useful reply. If I DO want to use type 3 SS, it sounds like using the non-numeric (i.e. factor) version would be more appropriate - would you agree? Apparently, zero-centering the numeric variable produces the same result. Jul 15 '16 at 20:18