Error in glm() in R

You can’t have factor/categorical response variables.

Illustration:

> d=data.frame(f=factor(c(1,2,1,2,1,2)),x=runif(6))
> glm(f~x,data=d)
Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 0.351715633412823, 0.449422287056223,  : 
  NA/NaN/Inf in 'y'
In addition: Warning messages:
1: In Ops.factor(y, mu) : - not meaningful for factors
2: In Ops.factor(eta, offset) : - not meaningful for factors
3: In Ops.factor(y, mu) : - not meaningful for factors

If you really want to do a logistic regression you should change them to 0 and 1, or FALSE and TRUE, and use family=binomial:

# recode d$f==2 as TRUE, else FALSE
d$f=d$f=="2"
# fit
glm(f~x,data=d,family=binomial)

Call:  glm(formula = f ~ x, family = binomial, data = d)

Coefficients:
(Intercept)            x  
    -0.9066       1.8922  

Degrees of Freedom: 5 Total (i.e. Null);  4 Residual
Null Deviance:      8.318 
Residual Deviance: 8.092    AIC: 12.09

Leave a Comment