user5071089
user5071089

Reputation:

Error in glm() in R

I would like to perform a logistic regression but get errors - don't know where the mistake might be.

The structure of my data:

'data.frame':   3911 obs. of  29 variables:
 $ vn1              : Factor w/ 2 levels "maennlich","weiblich": 1 1 2 1 1 2 1 1 1 1 ...
 $ vn2c             : int  1976 1943 1927 1949 1965 1977 1986 1976 1944 1994 ...
 $ vn35             : Factor w/ 7 levels "keine Angabe",..: 6 4 5 3 3 5 7 6 5 5 ...
 $ v39              : Factor w/ 8 levels "keine Angabe",..: 8 4 5 8 7 7 5 6 6 6 ...
 $ n39              : Factor w/ 9 levels "keine Angabe",..: 4 4 4 4 4 4 4 4 4 4 ...
 $ v41              : Factor w/ 7 levels "keine Angabe",..: 6 5 5 2 7 7 5 5 6 6 ...
 $ n41              : Factor w/ 7 levels "keine Angabe",..: 4 4 4 4 4 4 4 4 4 4 ...
 $ vn42a            : Factor w/ 8 levels "keine Angabe",..: 8 4 8 8 5 5 6 6 6 4 ...
 $ vn42b            : Factor w/ 8 levels "keine Angabe",..: 5 4 7 5 5 5 6 7 6 5 ...
 $ vn43a            : Factor w/ 8 levels "keine Angabe",..: 7 5 8 6 2 6 6 2 7 7 ...
 $ vn43b            : Factor w/ 8 levels "keine Angabe",..: 7 4 6 4 4 7 6 2 6 5 ...
 $ vn62             : Factor w/ 14 levels "keine Angabe",..: 8 11 9 2 3 3 8 6 5 7 ...
 $ vn119a           : Factor w/ 15 levels "keine Angabe",..: 6 3 8 14 10 8 14 8 6 6 ...
 $ ostwest          : Factor w/ 2 levels "Ost","West": 2 2 2 2 2 2 2 2 2 2 ...
 $ prefmerkel       : Factor w/ 2 levels "Steinbrueck",..: 1 2 2 NA NA NA 2 2 1 1 ...
 $ angst            : num  1 3 2 4 4 2 0 1 2 2 ...
 $ crisismerkel     : num  0 4 3 0 1 1 3 2 2 2 ...
 $ leadership42     : Factor w/ 5 levels "trifft ueberhaupt nicht zu",..: 5 1 5 5 2 2 3 3 3 1 ...
 $ leadership43     : Factor w/ 5 levels "trifft ueberhaupt nicht zu",..: 4 2 5 3 NA 3 3 NA 4 4 ...
 $ leadership       : num  1 -1 0 2 NA -1 0 NA -1 -3 ...
 $ trustworthiness42: Factor w/ 5 levels "trifft ueberhaupt nicht zu",..: 2 1 4 2 2 2 3 4 3 2 ...
 $ trustworthiness43: Factor w/ 5 levels "trifft ueberhaupt nicht zu",..: 4 1 3 1 1 4 3 NA 3 2 ...
 $ trustworthiness  : num  -2 0 1 1 1 -2 0 NA 0 0 ...
 $ ideology         : num  5 8 6 NA NA NA 5 3 2 4 ...
 $ pid              : Factor w/ 10 levels "none","CDU/CSU",..: 3 2 5 1 7 5 1 5 3 3 ...
 $ age              : num  37 70 86 64 48 36 27 37 69 19 ...
 $ agegroups        : Factor w/ 7 levels "bis 25 Jahre",..: 3 6 7 5 4 3 2 3 6 1 ...
 $ gender           : Factor w/ 2 levels "male","female": 1 1 2 1 1 2 1 1 1 1 ...
 $ region           : Factor w/ 2 levels "west","east": 1 1 1 1 1 1 1 1 1 1 ...

The regression command returns the following error:

summary(glm(prefmerkel~angst+crisismerkel+leadership+trustworthiness+ideology+pid+agegroups+gender+region,data=gles))

Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  : 
  NA/NaN/Inf in 'y'

In addition: Warning messages:
1: In Ops.factor(y, mu) : ‘-’ nicht sinnvoll für Faktoren
2: In Ops.factor(eta, offset) : ‘-’ nicht sinnvoll für Faktoren
3: In Ops.factor(y, mu) : ‘-’ nicht sinnvoll für Faktoren

Upvotes: 7

Views: 59588

Answers (4)

Peter Smittenaar
Peter Smittenaar

Reputation: 331

This page is showing up high on search for this error, so want to add another reason not to do with linear vs logistic regression. I had the same issue as Yulia, where I had log-transformed some of my predictors resulting in the error discussed here.

The reason for the error is that if you have any rows with value 0 before log-transforming, these become -Inf which results in the regression throwing an error. The solution is to refrain from log-transforming such variables, or to make sure you have no 0-valued rows (see e.g. discussion on stack exchange).

Upvotes: 9

Yulia Smirnova
Yulia Smirnova

Reputation: 21

In my case it was none of the above. I have log-transformed on of the right-skewed variables and when I used it, log-regression resulted in this error. When I used the original (untransformed) variant - it worked perfect.

Upvotes: 2

Nidhin Chandrasekhar
Nidhin Chandrasekhar

Reputation: 321

If your variable is binomial mention 'family=binomial'. This will solve the problem

Upvotes: 10

Spacedman
Spacedman

Reputation: 94307

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

Upvotes: 16

Related Questions