Use the predictor variable to perfectly predict the response variable
When there is perfect separability in the given data, then it’s easy to find the result of the response variable by the predictor variable. The data we considered in this article has clear separability and for every negative predictor variable the response is 0 always and for every positive predictor variable, the response is 1. So we can perfectly predict the response variable using the predictor variable.
Example:
Below is the code that predicts the response variable using the predictor variable with the help of predict method.
R
# create random data which consists of # 5 numbers x < - rnorm (5) # create data with five 1's y < - rep (1, 5) # if x value is less than 0 the at that index # replace 1 with 0 in y y[x < 0] < - 0 # create dataframe data1 < - data.frame (x, y) data1 # create a linear model model < - glm (y ~ x, data1, family= "binomial" ) # predicting response variables predict (model, newdata= data.frame (y= c (0, 0, 1, 1, 1))) |
Output
x y 1 -0.4057154 0 2 1.9408241 1 3 -0.2419725 0 4 0.2374463 1 5 -1.6208003 0 Warning message: glm.fit: fitted probabilities numerically 0 or 1 occurred 1 2 3 4 5 -39.25575 189.68953 23.27980 23.49574 -157.80817 [Execution complete with exit code 0]
How to Fix in R: glm.fit: algorithm did not converge
In this article, we will discuss how to fix the “glm.fit: algorithm did not converge” error in the R programming language.
glm.fit: algorithm did not converge is a warning in R that encounters in a few cases while fitting a logistic regression model in R. It encounters when a predictor variable perfectly separates the response variable. To get a better understanding let’s look into the code in which variable x is considered as the predictor variable and y is considered as the response variable. To produce the warning, let’s create the data in such a way that the data is perfectly separable.
Code that produces a warning:
The below code doesn’t produce any error as the exit code of the program is 0 but a few warnings are encountered in which one of the warnings is glm.fit: algorithm did not converge. This was due to the perfect separation of data. From the data used in the above code, for every negative x value, the y value is 0 and for every positive x, the y value is 1.
R
# create random data which consists # of 50 numbers x < - rnorm (50) # create data with fifty 1's y < - rep (1, 50) # if x value is less than 0 the at that # index replace 1 with 0 in y y[x < 0] < - 0 # create dataframe data < - data.frame (x, y) # first 6 rows head (data) # fitting logistic regression model glm (y ~ x, data, family= "binomial" ) |
Output
x y
1 1.3295285 1
2 -0.9738028 0
3 0.6963700 1
4 -1.1586337 0
5 -1.1001865 0
6 -0.6252191 0
Call: glm(formula = y ~ x, family = “binomial”, data = data)
Coefficients:
(Intercept) x
-13.42 273.54
Degrees of Freedom: 49 Total (i.e. Null); 48 Residual
Null Deviance: 68.03
Residual Deviance: 1.436e-08 AIC: 4
Warning messages:
1: glm.fit: algorithm did not converge
2: glm.fit: fitted probabilities numerically 0 or 1 occurred
[Execution complete with exit code 0]