MathsStatistics & Probability

Logistic Regression
calculator.

Fit a yes or no outcome to your predictors, and read it as odds and probabilities.

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.

ln⁡p1−p\displaystyle \ln\frac{p}{1 - p}=β0+β1x1+⋯+βkxk\displaystyle {}= \beta_0 + \beta_1 x_1 + \cdots + \beta_k x_k

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.

ℓ\displaystyle \ell=∑yln⁡p+∑(1−y)ln⁡(1−p)\displaystyle {}= \sum y \ln p + \sum (1 - y) \ln(1 - p)
OR\displaystyle \text{OR}=eβ\displaystyle {}= e^{\beta}

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, ℓ\ell is the log-likelihood of your model and ℓ0\ell_0 that of the intercept alone.

G\displaystyle G=2(ℓ−ℓ0)\displaystyle {}= 2(\ell - \ell_0)
RMcF2\displaystyle R^2_{\text{McF}}=1−ℓℓ0\displaystyle {}= 1 - \frac{\ell}{\ell_0}

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.

OR\displaystyle \text{OR}=e1.505\displaystyle {}= e^{1.505}=4.5\displaystyle {}= 4.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.

Predicted chance of passing
HoursProbabilityOdds
17.1%0.076
225.6%0.344
360.7%1.547
487.4%6.965
596.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%.

Open this example in the tool

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.

OR\displaystyle \text{OR}=7337\displaystyle {}= \frac{\frac{7}{3}}{\frac{3}{7}}=5.444\displaystyle {}= 5.444

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.

Open this example in the tool

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.

Open this example in the tool

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

It is the factor by which the odds change for a one-unit rise in the predictor, with the others held constant. An odds ratio of 1.5 means 50 percent higher odds, and 0.8 means 20 percent lower odds. Its size depends on the unit, so a per-year figure and a per-decade figure differ a lot.

Sources