In my course about machine learning I'm studying multiple linear regression and we talked about dummy variable trap. I have a data set which contains country, height, weight, gender of every person where country is encoded with letters such as us, uk, fr, ge for united states, united kingdom, france and germany respectively and genders are encoded with M F. When I convert these categorical variables into numeric ones (with one hot encoder) I get confused about the following.

When we encode M and F with two different columns if we don't drop one, we fall into dummy variable trap since a "1" on male column would obviously mean a "0" on female column therefore we only have one degree of freedom so the other is redundant, no problem here.

However with country column we can for example say a person is french if all other columns have "0" for them therefore I think that 3 columns are enough for specifying 4 countries and if we have 4 columns we would fall into dummy variable trap but all worked examples state otherwise.

Why is it so? Why 2 variables can be represented with 1 columns if two of them can't be true at the same time but 4 columns cannot be represented by 3 columns if any two columns cannot be true at the same time? Thanks in advance

  • $\begingroup$ "...but all worked examples state otherwise." These are examples in your course? Does every person have one of the four countries listed? $\endgroup$ – Ben Reiniger Apr 20 '20 at 2:27
  • $\begingroup$ As far as i know to avoid dummy variable trap, if you have m level you should keep m-1 dummy variables only as you said. Otherwise you will face an issue of multicollinearity due to high correlation between columns. $\endgroup$ – Ayush Ranjan Apr 20 '20 at 12:12
  • $\begingroup$ @BenReiniger yes, speaking for the examples in my course only. And yes, every person has one of the four countries listed. $\endgroup$ – Alper Karasuer Apr 20 '20 at 18:23
  • $\begingroup$ @AyushRanjan exactly, that is what I understood from other sources as well. Alas I don't know how to tell that if I fell to the trap. Are there any exact way to say it just by looking at results or if it is the case I will see only gibberish so it is the only way to know? $\endgroup$ – Alper Karasuer Apr 20 '20 at 18:28
  • $\begingroup$ i am not sure but go through this theanalysisfactor.com/eight-ways-to-detect-multicollinearity and see if it helps $\endgroup$ – Ayush Ranjan Apr 20 '20 at 23:44

It can be.
But with country, people don't assume the list to be exhaustive i.e. new country might come to the list. With gender, this is very less likely.

But please, don't give too much attention to the dummy variable trap.
It just creates an extra correlated variable. Correlation doesn't impact model performance. just some extra computation.
Anyway, if var count become too high, you will need dimensionality reduction techniques.

Only place I have seen importance to Dummy Var trap is A-Z Udemy ML course.

  • $\begingroup$ Not following that exact course but when I checked I thought my course is somehow similar in terms of course materials but translated to my language. So I guess that might be the case. Thanks for the answer. $\endgroup$ – Alper Karasuer Apr 21 '20 at 14:40
  • $\begingroup$ Please accept answer it it answered the question. It's OK if it didn't $\endgroup$ – 10xAI Apr 21 '20 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.