# Understanding ANOVA as regression / type III SS in R

I'm confused about how type III SS are calculated for a "main effect". According to what I have read, Type III SS is calculated by evaluating the change in the SSE by removing only the variable in question, not interactions involving that variable. So, I took a dataset (for a 2x2 ANOVA) and evaluated it with Type III SS (using ezANOVA in R; car's Anova gives same results). I then recoded the IVs as simple indicator variables, and calculated a third indicator for the interaction. I then used lm() to fit a series of linear models in which one term was dropped, and calculated SSE vs. full model. SSE difference was equivalent to the SS term for the interaction in the ezANOVA model, but it was not correct for the main effects terms! What am I not understanding about how Type III SS is calculated for main effects? Example code below.

library(dplyr)
library(ez)

# from above, Type III SS for...
# Gender:Doseage is 0.0625
# Gender is 76.56
# Doseage is 5.06

# recoding variables as indicators for regression
df %>% mutate(male=ifelse(Gender=="m",1,0)) %>% mutate(dosea=ifelse(Dosage=="a",1,0)) %>% mutate(intx = ifelse(male&dosea, 1, 0)) -> df

# calculate SS for interaction by dropping interaction term on the left-hand side
# result is 0.0625, like ezANOVA

# calculate SS for doseage by dropping dosea term on left side
# result is 2, doesn't match ezANOVA output (5.06)

# calculate SS for gender by dropping male term on left side
# result is 36.125, doesn't match ezANOVA output (76.56)


EDIT: I think I figured the issue out. If I code the main effects as 1's and 0's, and take the product of the two as the interaction, then I get a vector that is correlated with the main effects. If I recode factors as 1 and -1 (i.e, mean-center), instead, then the interaction term is not correlated. In this case, I get the correct values.

• This yields the correct values, but isn't that the Type II SS method? – TeeVee Oct 13 '18 at 21:25
• The model with interaction A*B and without A and/or B like this, Alertness~male+intx, is meaningless in most situations, and it is suitable when researchers have very strong believes that this model is correct form. So I think ezANOVA drops interaction when comparing the main effect. I know very little about R, so I am not so confident about my answer, otherwise I would put it into Answer window. – user158565 Oct 13 '18 at 21:58

This has to do with contrasts, which can be thought of as the interpretation of the statistic of interest. In ANOVA, the main effect of variable A is the difference in means between group A0 and A1, marginalizing over variable B. In regression, when an interaction is present, the "main effect" of variable A is the difference between A0 and A1 when B = 0. These are not the same statistics, so you would not expect them to have the same SS.

To get them to have the same SS, you need to change the way the contrasts are computed, which changes the interpretation of the statistics in the regression model. One way to do this is to set the default unordered contrast from treatment contrasts to Helmert contrasts. The way you would do this is the following:

options(contrasts = c("contr.helmert", "contr.poly"))


(Note that "contr.poly" corresponds to ordered contrasts, and is unused here, but must be specified).

After setting this contrast option, try running your code again and I think you should get the right answers.

• The ezANOVA output is correct, the question is why doesn't the method below that come up with the same SS to match the main effect? I'm doing something wrong, but I don't believe it is related to specifying contrasts... – TeeVee Oct 12 '18 at 20:31
• I was trying to say that it's because of the way lm processes variables. lm creates a linear model where the coefficients are interpreted as I described, which is not how an ANOVA is set up, so when you remove a variable from the specification in lm, you're not capturing the effect of the variable as you would in ANOVA. This is a matter of contrasts. I was asking if after you set the contrasts as I specified, does the lm part of your code produce the correct output (i.e., that matches with ezANOVA)? – Noah Oct 14 '18 at 0:34
• OK, I think I see what you are getting at, although still the output doesn't match deviance differences when I include the contrast statement you mentioned... – TeeVee Oct 14 '18 at 18:19