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)
df = read.table("http://personality-project.org/r/datasets/R.appendix2.data", header=T)
ezANOVA(data=df, between=.(Gender,Dosage),dv=Alertness, wid=Observation, type=3, detailed=TRUE)

# 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
deviance(lm(Alertness~male+dosea,data=df)) - deviance(lm(Alertness~male+dosea+intx, data=df))

# calculate SS for doseage by dropping dosea term on left side
# result is 2, doesn't match ezANOVA output (5.06)
deviance(lm(Alertness~male+intx,data=df)) - deviance(lm(Alertness~male+dosea+intx, data=df))

# calculate SS for gender by dropping male term on left side
# result is 36.125, doesn't match ezANOVA output (76.56)
deviance(lm(Alertness~dosea+intx,data=df)) - deviance(lm(Alertness~male+dosea+intx, data=df))

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.
 A: 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.
