# How to code binary (0/1) predictor variables in regression? Numeric versus factor

I am developing a regression model and most of my variables are 0/1 variables.

Should these variables be treated as factor variables in the model or can they just be left as numeric 0,1?

-
Do you need intercept in the model ? Is intermediate value between 0 and 1 possible ? Is 0 < 1 or does not matter - they are just categories ? – rdorlearn Jun 24 '14 at 20:45
It won't matter if they're factors or numeric. The most that might change is the reference category. – Glen_b Jun 25 '14 at 2:56

In linear regression, if they are independent variables and 1 and 0 are the only possible outcomes, then either way is fine.

Modeled as binary, but specified it as if it's continuous (data and syntax are of Stata 12):

. sysuse auto
. reg mpg foreign

Source |       SS       df       MS              Number of obs =      74
-------------+------------------------------           F(  1,    72) =   13.18
Model |  378.153515     1  378.153515           Prob > F      =  0.0005
Residual |  2065.30594    72  28.6848048           R-squared     =  0.1548
Total |  2443.45946    73  33.4720474           Root MSE      =  5.3558

------------------------------------------------------------------------------
mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
foreign |   4.945804   1.362162     3.63   0.001     2.230384    7.661225
_cons |   19.82692   .7427186    26.70   0.000     18.34634    21.30751
------------------------------------------------------------------------------


Modeled as factors:

. reg mpg i.foreign

Source |       SS       df       MS              Number of obs =      74
-------------+------------------------------           F(  1,    72) =   13.18
Model |  378.153515     1  378.153515           Prob > F      =  0.0005
Residual |  2065.30594    72  28.6848048           R-squared     =  0.1548
Total |  2443.45946    73  33.4720474           Root MSE      =  5.3558

------------------------------------------------------------------------------
mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
1.foreign |   4.945804   1.362162     3.63   0.001     2.230384    7.661225
_cons |   19.82692   .7427186    26.70   0.000     18.34634    21.30751
------------------------------------------------------------------------------


It's worth noticing that if modeled as a continuous variable (though bear in mind it's actually binary), the reference group is always whatever coded as 0. In some statistical software, however, binary variables modeled as factors may have its reference group swapped to whatever = 1. The ANOVA and F statistics will not be affected but the regression coefficients can change (due to reference group being reassigned.) Check the output carefully.

-
For those puzzled, specific syntax here is Stata syntax. Underlying principle is generic – Nick Cox Jun 24 '14 at 21:24
Thanks @Nick, sorry for being sloppy. I edited the answer. – Penguin_Knight Jun 24 '14 at 21:26
if modeled as continuous, the reference group is Pardon me, how can a continuous (scale, numeric) variable have a reference group at all? – ttnphns Jun 25 '14 at 6:25
@ttnphns, thanks and that's true. I meant to say model that binary variable in the software without specifying it as a factor. I guess the "statistical continuous" and "software configuration continuous" got mixed up. I have slightly revised the answer. – Penguin_Knight Jun 25 '14 at 12:25

In R, it doesn't matter if they are factors or numeric variables. But be sure to indicate that you're doing a logistic regression by indicating family=binomial in, for example, a general linear model or mixed effects model.

Without indicating this, the assumed variance of the distribution will differ. In a binomial family, the variance (dispersion parameter) is taken to be 1, unlike in gaussian family.

-
Thanks, I was referring to a linear regression model (failed to specify), are there still variance effects? – PT83 Jun 25 '14 at 13:39
The variance assumptions are more flexible with general linear models vs linear models. Almost all linear models will have a corresponding general linear model implementation. So, since you are specifically predicting binary variables, you want to specify this and therefore should use GLM vs LM. – Amyunimus Jun 25 '14 at 15:40
Sorry did not specify this but my target variable is not binary it is continuous (modeling cost), most of my independent variables are binary – PT83 Jun 25 '14 at 17:16

## protected by whuber♦Aug 12 '14 at 17:12

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).