I will give you an example in R, using two contrast type, the default one and one that could be useful to you. The default contrast in R is to compare each level of a factor to the "reference", i.e. a baseline, the effect is estimated in the intercept term. Another set of contrasts is called "contr.sum", the intercept term now represents the mean intercept of all factor levels.
An example with mtcars toy dataset, predicting mpg
from gear
, which is a factor with 3 levels.
mtcars$gear=factor(mtcars$gear)
summary(lm(mpg~gear,data=mtcars))
which results in
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 16.107 1.216 13.250 7.87e-14 ***
gear4 8.427 1.823 4.621 7.26e-05 ***
gear5 5.273 2.431 2.169 0.0384 *
the intercept term has the mean score of the first level of gear (gear==3), we can check this by manually calculating the mean of mpg for gear3
mean(mtcars$mpg[mtcars$gear==3])
[1] 16.10667
Now we use the "contr.sum" contrast
summary(lm(mpg~gear,data=mtcars,contrasts=list(gear="contr.sum")))
which results in
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 20.6733 0.9284 22.267 < 2e-16 ***
gear1 -4.5667 1.1639 -3.924 0.000492 ***
gear2 3.8600 1.2156 3.175 0.003534 **
now the intercept term has the overall mean of each factor, we can check manually
tapply(mtcars$mpg,mtcars$gear,mean)
3 4 5
16.10667 24.53333 21.38000
and the overall mean
mean(tapply(mtcars$mpg,mtcars$gear,mean))
[1] 20.67333
The other terms are interpreted as deviations from the overall mean.