Is one-hot encoding required for a binary categorical variable? We are performing multiple linear regression.
Dataset:

*

*let's call the response Y and the predictors X1, X2 and X3,

*where Y, X1, X2 are continuous, whereas X3 is binary categorical variable (i.e. it can take either two values, 1 or 2).

Now, we have two choices

*

*Choice #1: Without one-hot encoding of X3

*

*i.e. Linear regression on raw variables lm(Y ~ X1 + X2 + X3)



*Choice #2: With one-hot encoding of X3

*

*i.e. X3 is one-hot encoding in [X3a, X3b]

*e.g. Possible values: [1, 0] and [0, 1]

*Hence, the linear regression model is lm(Y ~ X1 + X2 + X3a + X3b)
Question:
Since X3 is a binary variable, whether or not we one-hot encode (i.e. Choice#1 or Choice#2), will it make any difference (especially in terms of the model's loss)?
 A: There's a better approach than both. With dummy coding, you would end up with a simpler model that is easier to interpret.
In the first case, you are forcing $\beta_3 X_3$ to be twice higher (or lower) for group coded 2 vs 1. To adapt to a specific difference between groups, the parameter, and intercept need to adapt. It's much easier to interpret the parameters using 0 and 1 codes, so $\beta_3 \times 1$ (vs $0$) serves as an additional intercept for the group coded as 1.
In the second case $X_{3a}$ and $X_{3b}$ are colinear and cannot be used. What you need to do is to encode the categories as 0 and 1, so you encode the presence of one category that has an additive effect $\beta_3 X_3 = \beta_3 1 = \beta_3$.
A: @Tim,
I think both would give same performance because:

*

*Rationale#1:Mathematically, there's enough room for parameter to adjust (to give same y value).


*Rationale#2: Computationally, we get the same explanatory power (i.e. R^2 value). Minimal reproducible example:

# Read Data
data("mtcars")
data <- mtcars
attach(data)

# Model 1: My Model
am_new <- am+1
model_1_my <- lm(mpg ~ am_new)
print(summary(model_1_my))        # Output: R-squared:  0.3598

# Model 2: Tim's Model
model_2_tim <- lm(mpg ~ am)
print(summary(model_2_tim))       # Output: R-squared:  0.3598   (again)

