# creating interaction term for dummy variables and categorical variables

I want to create interaction term by using dummy variables and categorical variables.

For example, if I want to create interaction term by gender(0=male, 1=female) and education level(0=less than elementary, 1= middle and high school, 2= college or more)

Is it right to multiply these two terms? 0(male) x 0 (less than elementary) = 0 0(male) x 1 (middle and high) = 0 0(male) x 2 (college or more) = 0

Above examples get same variable ' 0'.

Can you give me a right way to create interaction term?

• the point about categorical variables is that they are not numbers, so they do not multiply! you form the cartesian product of the categorical variables so six possible combinations (male,less than elementary ,...etc) -> six dummy variables... [in fact you would typically use 5 - since one combination is represented as 5 0s] – seanv507 Dec 10 '14 at 19:58

The multiplication scheme only works if you want to treat the education variable as a continuous or ordinal variable that is linearly related to your dependent variable after controlling for sex. In most of the cases (from my experience), this linear assumption seldom holds as there are just too many heterogeneity within some of the educational categories.

If you treat education as a categorical variable, the computation of interaction terms is a bit tricky. Generally, if you have two categorical variables: $x_1$ with $j$ levels and $x_2$ with $k$ levels, to completely model their interactions you'll need $(j-1)\times (k-1)$ dummies. Here are the possible schemes: Variable $female$ has two levels and variable $education$ has three, so to model the interaction you'll need $(2-1)\times(3-1) = 2$ more dummies on top of the dummies used for main effects.

For instance, if people in college is your reference group, to model the main effect, you'd need $female, D_{Elementary}, D_{Middle}$. To further model the interaction, you'll then need add the products $female\times D_{Elementary}$ and $female\times D_{Middle}$, which is Scheme 1 in the table.

Alternately, if you use other levels in your education variable as reference group, you can change your scheme accordingly. But overall, you should have 5 binary independent variables. These five dummies and the intercept together will allow you to estimate all the 6 means (2 sexes by 3 education levels = 6 possible combinations).

In real setting, we rarely do that by hands. Most software packages allow us to assign the type of variables so that the regression analysis will handle the variable appropriately.

In SAS, look into class statement in proc glm; in SPSS, check the factor and covariate panel in glm module; in R, use factor() or as.factor() functions to change the variable's nature; in Stata, look into adding prefix i. before your independent variable.

The misunderstanding here is in how categorical variables are presented/coded for usage in analysis. You have two categorical variables (gender with 2 levels and education with 3 levels), and you need to dummy-code them in order to use them - note the distinction between the type of variable (categorical) and how you encode them (dummy).

In this case, you end up with a representation that looks something like

            isF
male        0
female      1

isH isC
elementary  0   0
high        1   0
college     0   1


And when you interact the variables together, you get sensible differentiation.

                    isF isH isC
male, elementary    0   0   0
male, high          0   1   0
male, college       0   0   1
female, elementary  1   0   0
female, high        1   1   0
female, college     1   0   1


And probably nearly all statistical packages takes care of this in the background, and also note that there are other ways of encoding categorical variables as well.

• Gender is a categorical variable, at least in the Western world. Education is usually ordinal, in that Higher>Elementary. When you encode Education with dummies you lose the ordinal relationship. – Aksakal Dec 10 '14 at 19:59
• @Aksakal That is true, but it is difficult to represent ordinal predictors (whereas ordinal outcomes are much more broadly covered). And outside the scope of the question. – Affine Dec 10 '14 at 20:06

What you have described is an ordinal, not really a categorical. In case of education it is probably not a good idea to multiply the ordinal variable. If you are using stat software, it can be told how to treat the variable. This way you don't manually multiply variables to create the interaction term, but let the stat software do it in a consistent way.