# 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'

1 vote
1.3k views

## Problem :

I want to perform a logistic regression but I am facing following error I am unable to find my mistake.

`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`

1 vote

## Solution :

The most important thing here is you can not have factor/categorical response variables.

For e.g.:

`> 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`

So if  you really want to do a logistic regression you must change them to 0 and 1OR FALSE and TRUE, and you must use family=binomial as follows:

For e.g.:

`d\$f=d\$f=="2"`
`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`

## Related questions

937 views
Problem : Suppose I have data.frame a And I am using following code m.fit <- lm(col2 ~ col3 * col4, na.action = na.exclude) Here the col2 has some NA values And col3,col4 have values less than 1. I am getting following error: Error in lm.fit ... because of the NAs. Problem with this warning is interpreted by every google results is pointing towards the NA values. Have I misinterpreted the error ?
114 views
Problem : I have done a lot of research on this extensively without finding any solution on it. I have tried cleaning my data set as follows: library("myraster") impute.mean <- function(l) replace(l, is.na(l) | is.nan(l) | is.infinite(l) , mean(l, na.rm = TRUE)) losses <- ... TRUE, na.rm=TRUE, nan.rm=TRUE) All my research says it should be NA's or Inf's or NaN's in the data but I don't have any
1 vote