# Is it appropriate to use categorical data with logit and/or linear regression?

If I have data set containing only categorical variables, so variables with only values of 0 and 1, do I violate any assumptions of the logit and standard linear regression if I was to use them? I checked and it doesn't seem to violate anything.

So is it wrong if I just went ahead and estimated a logit regression on two variables x,y in the following way in R?

regression = glm(y~x , family = binomial(logit))

Do I need to specify that x,y are categorical variables, why do I need that if so, aren't they just data?

• It makes no difference whether you teat them as categorical or numeric. (To make them categorical in R, you would as.factor them, and then the dummy for the second level of the factor would replicate the original 0-1 variable.) Commented Nov 11, 2013 at 4:00

Given that your response variable y is categorical, it would violate the assumptions of standard linear (OLS) regression, if you were to use that instead of logistic regression. Specifically, the residuals would not be normally distributed and the residual variance would not be constant (since the variance of a proportion is a function of the proportion itself).

Regarding logistic regression, the comment by @Glen_b is exactly correct. You should use logistic regression.

• what do you mean by the variance of a proportion is a function of the proportion itself? why would that effect the standard errors in a linear regression? Commented Nov 11, 2013 at 4:12
• If you have 2 variables, each of which is 0 / 1 only, then you are assessing how the proportion of 1s changes as you move from x=0 to x=1. Your response will be distributed as a binomial with variance = np(1-p); unless p(Y=1) is identical at both x=0 & x=1, the variance will not be constant. Thus the assumption of homogeneity of variance will be violated. Commented Nov 11, 2013 at 4:18

Leaving aside the appropriateness of using a 0-1 variable as the response (dependent variable) in a linear regression (about which see gung's answer):

It makes no difference whether you treat a 0-1 variable as categorical or numeric.

To make a numeric variable categorical in R, you would as.factor them, and then the dummy for the second level of the factor would replicate the original 0-1 variable. (That's the dummy that will be used in the regression by default, so the fits are the same.)

If we set up some data

 x <- c(0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L)
y <- c(1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L)


We can see that these are all equivalent (some lines omitted):

> glm(y~x,family=binomial)
Coefficients:
(Intercept)            x
0.6931       1.2528

Degrees of Freedom: 13 Total (i.e. Null);  12 Residual
Null Deviance:      14.55
Residual Deviance: 13.67    AIC: 17.67




> glm(y~as.factor(x),family=binomial)
Coefficients:
(Intercept)  as.factor(x)1
0.6931         1.2528

Degrees of Freedom: 13 Total (i.e. Null);  12 Residual
Null Deviance:      14.55
Residual Deviance: 13.67    AIC: 17.67




> glm(as.factor(y)~as.factor(x),family=binomial)
Coefficients:
(Intercept)  as.factor(x)1
0.6931         1.2528

Degrees of Freedom: 13 Total (i.e. Null);  12 Residual
Null Deviance:      14.55
Residual Deviance: 13.67    AIC: 17.67


Similarly, it makes no difference with linear regression except that if you try to have a factor as the response in a linear regression in R, it will generate some warnings. The estimates - and so the fit - are the same in each case.