# Will one-hot encoding for categorical predictor violate the independence assumption of linear regression?

we know that for non-numeric categorical features, such as country, color, they can be transform into numeric value by one-hot encoding.

For instance, suppose there are 7 color options for a car, red will then be encoded as [0 0 0 1 0 0 0]. We can therefore include these 7 numeric values into a logistic regression model.

However, one of linear regression's assumptions is that, predictors are independent. One hot encoding obviously violates this assumption.

Probably I miss understood something here. Any thoughts?

PS: as answered by Dave and Gordon below: the trick is to use 6, instead of 7, new features. None of these 6 could be a linear combination of the other 5.

• How does "logistic regression" enter in to this question? Are you treating the 0/1 coded values as x-variables or as the y-variable? If they are predictors (x-variables) then you seem to be doing standard anova rather than logistic regression. If they are the y-variable, then you would indeed have an independence problem. – Gordon Smyth Sep 29 '19 at 8:26
• the color is a predictor, i.e., it is part of X, not y. – eight3 Sep 30 '19 at 7:12
• Then this a standard encoding that has been used in statistics for 100 years. You avoid any collinearity problem by inserting 6 numeric values into the regression model, not 7. The color level left out becomes the reference level and is absorbed into the intercept term. If you did incorrectly insert all 7 variables into the regression model, then most regression software tools will just remove the last one automatically to avoid the problem. – Gordon Smyth Oct 1 '19 at 2:25
• If you are using R, just include +color in the linear model formula and the correct number of coding variables will be created automatically. You never need to create the coding variables yourself. – Gordon Smyth Oct 1 '19 at 8:25
• @GordonSmyth thanks! – eight3 Oct 1 '19 at 8:29

There is no such assumption about predictors. This is an easy mistake to make, because of the Gauss-Markov theorem requiring uncorrelated errors. Another way to make this mistake is by confusing independence of predictors and independence of predictions.

I've made this mistake. Probably most of us have made this mistake. Welcome to the club of learning that it's false. :)

I do want to comment on using all seven colors in the model. The typical way to do it would be to include six color variables. The seventh gets sucked into the intercept term. Not doing it this way means that your model matrix can add up all of the color columns and get the intercept column of 1s, which makes your model matrix not have full rank.

Red: [1 0 0 0 0 0]$$^T$$

Orange: [0 1 0 0 0 0]$$^T$$

Yellow: [0 0 1 0 0 0]$$^T$$

Green: [0 0 0 1 0 0]$$^T$$

Blue: [0 0 0 0 1 0]$$^T$$

Indigo: [0 0 0 0 0 1]$$^T$$

Violet: [0 0 0 0 0 0]$$^T$$

• thanks for the answer. However, as per statisticssolutions.com/assumptions-of-linear-regression . multicollinearity surely violates the assumption of linear regression. Do you mean 'dependency' is not necessarily co-linearity? – eight3 Sep 30 '19 at 7:09
• It depends on what you're doing. Multicollinearity will affect parameter inference. However, if you only include those seven colors in your regression (e.g. ANOVA), there is no issue of multicollinearity. No linear combination of six can given the seventh. – Dave Sep 30 '19 at 12:48