Logistic Regression
calculator.
Fit a yes or no outcome to your predictors, and read it as odds and probabilities.
- ROC AUC
- 0.895
- McFadden R²
- 0.421
- Accuracy at 0.5
- 80%
The 95% interval for the odds ratio leaves out 1, so this predictor has a clear link with the outcome. The model gives 60.7% at the values you chose. It crosses 50% at hours = 2.71.
The fitted probability curve
Drag along the curveThe S-shaped curve fitted to 20 cases, with each outcome drawn at 0 or 1 and the dotted line at the threshold, 0.5. At hours = 3 the curve reads 60.7%.
Predict a new case
- Predicted probability
- 60.7%
- Odds
- 1.547
- hours for 80%
- 3.6314
Coefficients, with Wald tests
| Term | β | SE | z | p | Odds ratio | OR 95% interval |
|---|---|---|---|---|---|---|
| Interceptβ −4.0777SE 1.761z −2.316 | −4.0777 | 1.761 | −2.316 | 0.0206 | 0.0170.001 to 0.535 | 0.001 to 0.535 |
| hoursβ 1.5046SE 0.6287z 2.393 | 1.5046 | 0.6287 | 2.393 | 0.0167 | 4.5031.313 to 15.439 | 1.313 to 15.439 |
Classify at a threshold
Confusion matrix
| Counts | Predicted 1 | Predicted 0 |
|---|---|---|
| Actual 1 | 8 | 2 |
| Actual 0 | 2 | 8 |
The same model at other thresholds
| Threshold | Accuracy | Recall | Specificity | Precision |
|---|---|---|---|---|
| 0.1 | 65%Precision 58.8% | 100%Specificity 30% | 30% | 58.8% |
| 0.2 | 75%Precision 69.2% | 90%Specificity 60% | 60% | 69.2% |
| 0.3 | 80%Precision 75% | 90%Specificity 70% | 70% | 75% |
| 0.4 | 75%Precision 72.7% | 80%Specificity 70% | 70% | 72.7% |
| 0.5 | 80%Precision 80% | 80%Specificity 80% | 80% | 80% |
| 0.6 | 75%Precision 77.8% | 70%Specificity 80% | 80% | 77.8% |
| 0.7 | 75%Precision 85.7% | 60%Specificity 90% | 90% | 85.7% |
| 0.8 | 80%Precision 100% | 60%Specificity 100% | 100% | 100% |
| 0.9 | 75%Precision 100% | 50%Specificity 100% | 100% | 100% |
A lower threshold catches more true 1s and raises more false alarms.
ROC curve
True positive rate against false positive rate across every threshold. The area under this curve is 0.895, and the dotted diagonal is a model with no skill. The marked point is your threshold, 0.5.
How well it separates
- 0.5 or less
- No better than chance
- 0.5 to 0.7
- Poor
- 0.7 to 0.8
- Acceptable
- 0.8 to 0.9
- Excellent
- Above 0.9
- Outstanding
Working
- 1Write the log odds as a straight linelnp1 − p= −4.0777+ 1.5046 × hours
- 2Fit by maximum likelihoodNewton-Raphson settled after 6 rounds, log-likelihood = −8.0299
- 3Fit the intercept alonep̄ =1020log-likelihood = −13.8629
- 4Compare the two fitsG = 2 × (−8.0299 − −13.8629) = 11.6661, on 1 df
- 5Raise e to each coefficient for the odds ratioe1.5046 = 4.5026 for hours
- 6Check: the fitted probabilities add up to the number of 1sSum of p = 10, and the data hold 10
How it's worked out
Logistic regression predicts the chance of an outcome that is either 0 or 1: pass or fail, stayed or left. It models the log of the odds as a straight line, which keeps every predicted probability between 0 and 1. A plain linear regression can give values outside that range. Odds are the chance of a 1 divided by the chance of a 0, so a probability of 0.8 means odds of 4 to 1.
Here p is the probability of a 1, each x is a predictor and each β is a coefficient. No closed-form answer exists, so the tool searches for the coefficients with the highest log-likelihood, using Newton-Raphson steps. Each standard error comes from the inverse of the information matrix at that peak, and a Wald z is the coefficient over its standard error.
The odds ratio is the factor by which the odds change for one extra unit of that predictor, with the others held fixed. The likelihood ratio test compares your model with a model that has only an intercept, on k degrees of freedom. McFadden's R² compares the same two log-likelihoods. Below, is the log-likelihood of your model and that of the intercept alone.
Worked examples
Twenty students, hours studied and passing
Twenty students studied from 0.5 to 5.5 hours, and 10 passed. The fit gives a slope of 1.5046 on hours and an intercept of −4.0777, in line with the published fit of this data set, about −4.1 and 1.5.
Each extra hour multiplies the odds of passing by 4.5 (95% interval 1.31 to 15.44, Wald p = 0.0167). The chance of passing reaches 50% at 2.71 hours and 80% at 3.63 hours.
| Hours | Probability | Odds |
|---|---|---|
| 1 | 7.1% | 0.076 |
| 2 | 25.6% | 0.344 |
| 3 | 60.7% | 1.547 |
| 4 | 87.4% | 6.965 |
| 5 | 96.9% | 31.359 |
The likelihood ratio test gives G = 11.67 on 1 degree of freedom (p = 0.0006), which is a stronger result than the Wald test on the same slope. McFadden's R² is 0.421 and the AUC is 0.895. As a check, the fitted probabilities add up to 10, the same as the number of passes.
At a threshold of 0.5 the model classifies 80% of these students correctly. Dropping to 0.3 lifts recall to 90% and cuts specificity to 70%. Raising it to 0.7 gives recall of 60% and specificity of 90%.
Treated and untreated, the 2 by 2 check
With one 0 or 1 predictor, logistic regression gives back the familiar 2 by 2 table. Here 3 of 10 untreated cases improved and 7 of 10 treated cases did.
The tool gives the same odds ratio from the slope, with a standard error of 0.9759. That matches Woolf's hand formula, the square root of the sum of one over each of the four counts. It is a useful test of any logistic regression software. The interval runs from 0.8 to 36.87, which includes 1, so twenty cases cannot settle this.
Adding practice tests as a second predictor
Give the same students a count of practice tests. The odds ratio for hours is now 5.6 with practice tests held fixed, and the odds ratio for each practice test is 4 (p = 0.112). The practice interval, 0.73 to 22.05, is the one to read before you call that effect real.
A student with 3 hours and 2 practice tests gets a predicted chance of 72.8%. With two predictors there is no single S-curve to draw, so the ROC curve takes its place, and the AUC is 0.94. AIC falls from 20.06 to 18.49; a lower AIC means a better fit for the number of terms.
Reading your result
Odds ratio. Above 1, higher values of the predictor raise the odds of a 1; below 1, they lower them. An interval that includes 1 is not significant at the 5% level. Remember that this is a ratio of odds, so the change in probability depends on where you start.
AUC. The chance that a randomly chosen 1 gets a higher predicted probability than a randomly chosen 0. Hosmer and Lemeshow's rule of thumb reads 0.5 to 0.7 as poor, 0.7 to 0.8 as acceptable, 0.8 to 0.9 as excellent and above 0.9 as outstanding, and 0.5 means no discrimination at all.
Confusion matrix. These counts depend on your threshold. A lower threshold catches more true 1s (higher recall) at the cost of more false alarms (lower precision).
McFadden's R². UCLA notes that it cannot be read like an ordinary R², and that it means something only against another value of the same type, on the same data and outcome. Use it to compare models, not to grade one.
Wald or likelihood ratio. Wald p-values come from each coefficient and its standard error. The likelihood ratio test refits the model, and it is the recommended method for judging the model as a whole.
Common mistakes
- Reading odds as probability. An odds ratio of 2 does not double the probability. Going from odds of 1 to 2 moves p from 50% to about 67%.
- Too few cases of the rarer outcome. Peduzzi's simulations point to at least 10 events for each predictor. Below that, odds ratios may be biased and their intervals unreliable.
- Ignoring separation. If a predictor splits the 0s and 1s perfectly, no finite estimate exists, because the likelihood keeps rising as the coefficient grows. This tool stops and says so.
- Grading accuracy on the same data. Accuracy and AUC on the rows used for the fit are optimistic. Judge the model on new cases before you rely on it.
Questions people ask
Sources
- Wikipedia, Logistic regression
- UCLA OARC, FAQ: How do I interpret odds ratios in logistic regression?
- UCLA OARC, FAQ: What are pseudo R-squareds?
- UCLA OARC, FAQ: Complete or quasi-complete separation in logistic regression
- van Domburg et al., EuroIntervention: How many variables are allowed in the logistic and Cox regression models?
- Statology, What is considered a good AUC score?