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?
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?
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
-------------+------------------------------ Adj R-squared = 0.1430
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
-------------+------------------------------ Adj R-squared = 0.1430
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.
if modeled as continuous, the reference group is
Pardon me, how can a continuous (scale, numeric) variable have a reference group at all?
$\endgroup$
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.