# Factor codings for models in R

I am holding an exercise on generalised models these days. Preparing a task on factor coding in generalised linear models, I realised that the help on the internet on that is not so easy to understand. At least what I found. So in order to help people who find this topic confusing, I want to help out a little here.

Consider the following data

Now we want to model the mean of y given x using the lm() function with the following codings: dummy-coding, treatment-coding (where the reference category is 5), effect-coding and split-coding.

To make the theory more general, we have a categorical variable $$X$$ with $$K$$ categories $$(a_1, \dots, a_K)$$

## Dummy-coding

Look at each level separately: $E(Y|X=a_{k}) = \beta_k, \quad k=1,\dots,K$

## Treatment-coding

Compare each category to the dummy-category $$a_d$$:

$E(Y|X=a_{d}) = \beta_0$ and $E(Y|X=a_k) = \beta_0 + \beta_k, \quad k\neq d$

## Effect-coding

Compare each category to the mean:

$E(Y|X=a_k) = \beta_0 + \beta_k, \quad k=1,\dots,K-1$ and $E(Y|X=a_K) = \beta_0 - \sum\limits_{j=1}^{K-1} \beta_j$

## Split-coding

Compare each category to the previous category (for ordered categories):

$E(Y|X=a_1) = \beta_0$ and $E(Y|X=a_k) = \beta_0 + \sum\limits_{j=1}^{k-1} \beta_j, \quad k=2,\dots,K$