3
$\begingroup$

In logistic regression, the probability is obtained from $$ Pr = \frac{\exp(X\beta)}{1 + \exp(X\beta)} ~~~~ (1) $$ From the plot below, it is obvious that if $X\beta$ > 10, the probability approaches 1 enter image description here In my case (a penalized logistic regression, details are omitted), my estimate for $\beta$ (and therefore $X\beta$) is very large, > 2000, when plugging the equation (1), R gives out Inf.

(Updated details based on @whuber comments: I actually also have some negative values, say, <-2000, so the alternative Equation (2) is not working) $$ Pr = \frac{1}{1+\exp(-X\beta)} ~~~~~~~(2) $$

I am looking for some suggestions on how to deal with this situation, where the output is $Pr$.

Possible two approaches I can think of are:

  • Use the if-condition: If $X\beta$ > 10, then Pr = 1; to compute the probability and skip the equation (1).
  • Check my code on the estimation of $\beta$. (though I do not think my code has bugs)
  • (based on @whuber's comments) If $X\beta$ is positive, use Equation (2); if $X\beta$ is negative, use Equation (1).
  • (based on @BenBolker's comments) Use the R function plogis() to compute the $Pr$, which is the logistic of $X\beta$. The code is plogis(c(-2000,2000), lower.tail=TRUE). A related article is here.
$\endgroup$
11
  • 2
    $\begingroup$ it seems you know what to do already $\endgroup$
    – Aksakal
    Commented Nov 14, 2018 at 23:12
  • 3
    $\begingroup$ Why not just compute the algebraically equivalent $\frac{1}{1+\exp(-X\beta)}$? When $X\beta \gg 0,$ floating point arithmetic will automatically round the result to $1.$ $\endgroup$
    – whuber
    Commented Nov 14, 2018 at 23:24
  • $\begingroup$ @whuber Thanks for the quick response. I updated the question based on the comment. Basically, I also have $X\beta << 0$, so I come up with the third approach in the post. $\endgroup$
    – vtshen
    Commented Nov 14, 2018 at 23:53
  • $\begingroup$ @Aksakal Thanks for the quick response. I hope so, but still curious if there are other clever approaches $\endgroup$
    – vtshen
    Commented Nov 14, 2018 at 23:55
  • $\begingroup$ You could just report your result as the log odds (or, logit of the probability) and avoid computing the exponential altogether $\endgroup$ Commented Nov 15, 2018 at 0:44

1 Answer 1

2
$\begingroup$

If you use the built-in plogis() function (which returns the cumulative distribution function of the logistic distribution, which by definition is the same as the logistic/inverse-logit function) it will take care of your computational issues:

plogis(c(-2000,2000))
## 0 1

Even better, you can get a more precise answer by asking R to provide $\ln(\textrm{logistic}(X\beta))$ directly:

plogis(-2000,log.p=TRUE)
## -2000

In this case, because the logistic is approximately equal to the exponential function at its lower extent, $\ln(\textrm{logistic}(x)) \approx \ln(\exp(x)) = x$. If you want to convert this to base 10, $-2000/\ln(10)=-868.589$ tells you that $p \approx 10^{-868}$.

Since the logistic function is symmetric, the upper end works similarly: to compute $\ln(1-\textrm{logistic}(x))$, use

plogis(2000,log.p=TRUE,lower.tail=FALSE)
## -2000

So (as we could have figured out from symmetry), $\textrm{logistic}(2000) \approx 1-10^{-868}$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.