# logistic regression log odds to probability issue in R

I have the following issue in R.

I perform logistic regression in R:

logitMod <- glm(dependent_var ~ var1, var2, varN, data=traindata, family=binomial())


Then, I run predict on the first record of the testdata set, to get the log odds:

>predict(logitMod, testdata)
-44.81362


Then I calculate the probability from the log odds:

> 1/(1+ exp(-predict(logitMod, testdata)))
3.449006e-20


Then, I check with the predict function what the built-in probability-conversion would yield, and the result is very different:

>predict(logitMod, testdata, type="response")
2.220446e-16


So my question is, what am I overlooking?

• Both are basically zero. Surely not "very different" in my book. (I haven't made an effort to check whether you got things right though.) – Lewian Jun 26 at 12:56
• I'm with Lewian on this. Sure, it looks like you're off by many orders of magnitude, but your numbers are so small that a tiny rounding difference could make for considerable differences. Maybe there's an issue, but try your predictions with testdata, 3, etc to see if you get matching results. Your probability calculation looks right. Maybe try $\dfrac{exp(x)}{1+exp(x)}$ and see if the numerical precision improves for testdata. – Dave Jun 26 at 13:04
• I see no error in your code and it indeed looks like predict(logitMod, testdata, type="response") produces erroneous predictions. But maybe there are good reasons for this behaviour of predict.glm. – Jarle Tufto Jun 26 at 13:07
• @JarleTufto Why is that prediction erroneous? – David Jun 26 at 13:11
• @David Well, there is certainly an unexpected numerical difference. While the difference may not have any practical consequences, it is worth asking why predict.glm was implemented to have this behaviour. Btw, 2.22e-16 is the same as .Machine\$double.eps. – Jarle Tufto Jun 26 at 13:32

## 1 Answer

Those two results are exactly the same: zero! Try to see if the problem persists when using a more "normal" number, i.e: predict(logitMod, testdata) in a range like $$(-2,2)$$ or $$(-3, 3)$$

• For testdata, the result of predict(logitMod, testdata, type="response") 6.653410e-01 = 0.665341. With the same manual formula as above, the results indeed seem to be matching. Thanks for this tip! In the end, even though the difference between those approximately zero numbers are 4 orders of magnitude, this issue is probably related to decimal conversion indeed. – itarill Jun 26 at 15:05