# How to encode nominal variables for R program

I have a survey that includes many nominal variables (country, type of institution, etc.), a number of which have 10-16 possible levels. (For country alone, there are about 16.) I want to bring this into R in order to do a logistic regression. The dependent variable will be binary.

I have to recode almost all of those variables, obviously. But when I do a google search of recoding categorical or nominal variables, I get suggestions such as the following: Code a base level as all zeros, with ones in different places, as below:

I II  III

A 0 0 0

B 1 0 0

C 0 1 0

D 0 0 1

where I, II, and III are new variables, using k - 1 levels for the number of new variables as a rule of thumb. So if A, B, C, and D are the UK, France, Canada, and Germany respectively, I'm going to create 3 columns out of the 1 original column? (There are several hundred rows.) I'm not seeing how the UK would end up represented in the new data, since the zeros under I could represent A, C, or D.

Archie

I think you're confusing the the variables with each variable's levels (the values each variable it can take). Raw data with categorical variables is usually pretty concise, but usually when you transform this into data you can run a regression on, the data gets a lot "wider."

Say your country variable has 16 levels. For your country variable alone, you would add 15 regressors/predictors to your logistic regression. You would pick one country to be "baseline," or in other words to not have a predictor/column in the data matrix. And then you would add 15 predictors. For each observation/row in your data matrix, you would have a $1$ in the column that corresponds to the country that observation is from.

Say you want to include type of institution information into your model as well. Without loss of generality say this variable has 10 levels. Then you would add 9 more predictors. And so on and so forth. Your diagram there is better for demonstrating how to include one variable.

In your case you're going to have a lot of predictors, which might be a bad thing for a few reasons. You might consider shrinking the levels of each variable (maybe make country intro continent) or combining variables (united states farming, French telecommunications, etc.)

Also, it should be mentioned that a) there are many ways to code your categorical data, and b) sometimes the software takes care of this task of making a new data matrix for you.

First, R will do this for you, don't do it by hand (unless you really want to learn it better by reinventing the wheel (which can help sometimes)).

The key to understand is that when you fit your actual model, there will also be an intercept term of all 1's. So that means a member of group A will be represented as $\beta_0 + 0 \times \beta_1 + 0 \times \beta_2 + 0 \times \beta_3 + \epsilon$ and a member of group B will be $\beta_0 + 1 \times \beta_1 + 0 \times \beta_2 + 0 \times \beta_3 + \epsilon$ with similar expansion's for groups C, D, etc. Simplifying the above we get that group A will be represented by $\beta_0$ and group B by $\beta_0 + \beta_1$ so $\beta_0$ will be the mean of group A and $\beta_1$ will be the difference between the mean of group B and the mean of group A ($\beta_2 = C-A$ and $\beta_3 = D-A$). That is why group A is referred to as the baseline, the intercept represents that group and the other coefficients are comparisons to the baseline group.

For identification purpose you can't recover (estimate) the effect of each category, that's why you have to arbitrarily define one category as reference (e.g., UK) and then you will estimate effects of other categories relative to this reference level (e.g., France vs UK, Germany vs UK, etc.).

By constraint the effect for the reference category would be 0 (If you use dummy coding strategy). The choice of the reference category does not really matter. In the particular case where the categorical would take only two values (e.g., Country = {UK; FRance}), effect of UK relative to FR would the same as -1*(FR relative to UK).

An interesting alternative to dummy coding is effect coding (where the reference category takes the value "-1" instead of "0"). In brief, effect coding can be seen as mean centring for categorical variables and therefore you estimate effects relative to overall mean effect of the variable (e.g., Country). I find effect coding useful when I specify interaction effects between categorical variables. But at the end of the day the choice of either dummy or coding strategy does not really matter as these coding schemes are linearly equivalent - However the interpretation of the effects would change!