1
$\begingroup$

I'm running a linear regression in R.

If i have an independent variable Gender with only values 0 (for Male) and 1 (for Female), do i need to convert them to factor or character?

What is going to be the impact on my analysis?

$\endgroup$
  • $\begingroup$ If it's already coded as 0/1 there's no need to make it a factor but neither is there any harm in making it a factor. $\endgroup$ – Glen_b -Reinstate Monica Jun 3 '16 at 8:13
5
$\begingroup$

A factor variable with n levels, are represented as n - 1 binary variables. Thus if this categorical variable is already 0-1 binary, then there is no need to code it as factor variable.

The only subtle issue, is the meaning of 0 and 1. Consider the following example:

## raw binary variable
set.seed(0); x <- sample(0:1, 8, replace = TRUE)

Without coding it into a factor, we have a model matrix:

> model.matrix(~x)
  (Intercept) x
1           1 1
2           1 0
3           1 0
4           1 1
5           1 1
6           1 0
7           1 1
8           1 1

Now, if we code it into a factor, there are two ways of coding:

x1 <- factor(x, levels = c(0, 1))
x2 <- factor(x, levels = c(1, 0))

R will drop the first level for contrasting, so the model matrix generated are slightly different:

model.matrix(~ x1)
  (Intercept) x11  ## x11 means level 1 of variable x1
1           1   1
2           1   0
3           1   0
4           1   1
5           1   1
6           1   0
7           1   1
8           1   1

> model.matrix(~ x2)
  (Intercept) x20  ## x20 means level 0 of variable x1
1           1   0
2           1   1
3           1   1
4           1   0
5           1   0
6           1   1
7           1   0
8           1   0

Representations are equivalent, but interpretation of the resulting coefficients will be different.

$\endgroup$
  • $\begingroup$ I'm sorry @ZheyuanLi. "there is no need to code it as factor variable" is not technically correct. The regression needs to know if the variables are actual quantitative variables or categorical ones. The only way I know to mark the variable as categorical ones is to turn them into factors. $\endgroup$ – pbahr Jun 2 '16 at 16:56
  • 4
    $\begingroup$ @pbahr His categorical variable is already 0-1 binary. $\endgroup$ – Zheyuan Li Jun 2 '16 at 16:57
  • $\begingroup$ Does "For your information" add anything of value to this answer? If not I suggest removing it because it may be viewed as impolite. $\endgroup$ – Ista Jun 3 '16 at 0:31
  • $\begingroup$ @Ista thanks. I receive such kind of comment with "FYI" often, so thought it means no harm. Hehe, now I know it does mean something. Sorry English is not my first language. $\endgroup$ – Zheyuan Li Jun 3 '16 at 5:22
  • $\begingroup$ The first sentence says it is represented but I think it might be better to say it can be since there are other ways of considering contrasts. $\endgroup$ – mdewey Jun 3 '16 at 13:05
2
$\begingroup$

You need to turn the categorical variables into factor for the regression to deal with them as such. contrasts() can be used to check the way R thinks about the base level, against which other levels are compared. relevel() can be used to change the base level if needed.

$\endgroup$

Your Answer

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