# Logistic Regression for Prediction [duplicate]

Suppose we have a data set with a binary outcome variable $y$. The predictor variables are $x,w$ and $z$. This is the training data set. We obtain a logistic regression model from this training data set. Now suppose we have a test data set and want to predict $y$. In R, we use the  predict  function to do this. But we end up with predicted probabilities instead of $1$ or $0$. How would we convert the probabilities to either a $1$ or $0$?

## marked as duplicate by kjetil b halvorsen, mdewey, gung♦, Momo, hxd1011May 11 '17 at 1:16

• Why not choose a limit as cut-off (if your aim is information loss)? – Michael M Jul 1 '14 at 20:18
• You have $y$ predicting itself, but your predictor variables don't seem to matter anyway. Do you have any prior info on the probability of the outcomes, or is this model just an attempt to improve on a coin flip prediction? – Nick Stauner Jul 1 '14 at 20:20
• As Michael points out, you usually use a cutoff on the predicted probability. However, the issue lies in choosing a good cutoff (0.5 may not always be ideal). One method is to use a variety of cutoffs and use cross-validation to select the "best" cutoff. Then make your predictions in 27th testing set based on this optimal cutoff. – ved Jul 1 '14 at 20:24
• @NickStauner: was a typo. I have edited it. – thoms Jul 1 '14 at 20:26
• My question was more important than the typo. As I said, the predictors don't seem to matter. – Nick Stauner Jul 1 '14 at 20:37

If you want to convert to 0/1 (or other pairs) based on a cutoff, then you can just use the ifelse function:

ifelse( preditedProb > cutoff, 1, 0 )


But of course this throws away a bunch of information.

• "Throws away a bunch of information." +1. Unless you desperately need to make some kind of executive call on predicted classes, it's almost always better to express your answers in probabilistic terms. After all -- are you sure you've classed that point correctly, or is it just the most probable class? What if your estimated class probabilities are 0.4 and 0.6, are you really willing to say it's "in" the second class? – shadowtalker Jul 1 '14 at 22:58
• Note that the decision cutoff could well be case specific, as an example, if estimate Prob is probability of some disease given symptoms, then cutoff for deciding some treatment could maybe depend on age. – kjetil b halvorsen Mar 4 '17 at 21:48
• Since people are apparently still reading this question/answer, this recent blog post may be of additional interest: fharrell.com/2017/03/damage-caused-by-classification.html – Greg Snow Mar 6 '17 at 18:15

predict() returns P(Class|features), this is the inference step that R handles for you. Next comes the decision step, where you make optimal decisions given these conditional class probabilities.

Imposing a cut-off value (e.g. in the most naive case: 0.5) is an option, where you do:

threshold = 0.5
if P(C1|features) > threshold:
class= C1
else:
class= C2


Note that for cases where class probabilities are close to each other, you may choose to reject to make a classification, such as:

reject_threshold = 0.6
if max(P(C1|features),P(C2|features) ) < reject_threshold:
print: "We are unsure and opt not to classify"
else:
class = max(P(C1|features),P(C2|features) )


Overall, based on the thresholds you use, your confusion matrix is going to vary. You need to make use of cost/utility functions (which may e.g. more heavily penalise False Positives compared to False Negatives), take into account class priors, and any other application-specific idiosyncracies.

Summary: Converting the probabilities to either 1 or 0 is your decision step and hence application specific.