# Warning message: glm.fit: algorithm did not converge

#1

Hello

While running logistic regression on titanic data set in R, I’m facing this warning, because of which I’m stuck at this.

``````Warning message: glm.fit: algorithm did not converge
``````

This is the structure of my data set:

str(trainnew1)

``````'data.frame':	800 obs. of  11 variables:
\$ PassengerId: int  1 2 3 4 5 6 7 8 9 10 ...
\$ Survived   : int  0 1 1 1 0 0 0 0 1 1 ...
\$ Pclass     : int  3 1 3 1 3 3 1 3 3 2 ...
\$ Name       : Factor w/ 885 levels "Abbing, Mr. Anthony",..: 108 189 356 275 16 555 516 625 414 577 ...
\$ Sex        : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
\$ Age        : num  22 38 26 35 35 6.36 54 2 27 14 ...
\$ SibSp      : int  1 1 0 1 0 0 0 3 0 1 ...
\$ Parch      : int  0 0 0 0 0 0 0 1 2 0 ...
\$ Ticket     : Factor w/ 678 levels "110152","110413",..: 522 595 667 49 471 274 85 394 343 132 ...
\$ Fare       : num  7.25 71.28 7.92 53.1 8.05 ...
\$ Embarked   : Factor w/ 3 levels "C","Q","S": 3 1 3 3 3 2 3 3 3 1 ...
``````

How can I solve this?

#2

GLm could not solve the likelihood. Check which family you used, perhaps the problem is there.

Alain

#3

Hi Manish,

In glm() there is a parameter called ‘maxit’. Set this to 100 or more you may get convergence.

Example: If your using glm for logistic regression following example my work for you.

fit <- glm(y ~., data = mydata, family = binomial(“logit”), maxit = 100)

Thanks,
Naveen

#4

thanks @Kaveti_Naveen_Kumar

#5

Is this solved ?

#6

Hi All,
When I am applying above solution getting below error message, Could you please help me on this

fit <- glm(y ~., data = mydata, family = binomial(“logit”), maxit = 100)
Error in model.frame.default(formula = y ~ ., data = mydata, drop.unused.levels = TRUE) :
variable lengths differ (found for ‘x’)

Regards,
Nikhil

#7

Remove all NA values and check length of all variables.

#8

Remove PassengerId, Cabin, Name and Ticket from the original dataset.