Skip to content
BI & Data ScienceLogistic regression, and how to judge a classifier

Formulas for this chapter

Logit

Z = b0 + b1*x1 + b2*x2 + ... + bn*xn

The first column of every logistic-regression sheet. Unbounded, and not yet a probability.

b0
Intercept, the value of Z when every predictor is zero
bi
Change in Z per one unit of predictor i, all else equal
xi
The value of predictor i for this row

Sigmoid (logistic function)

P(y=1) = e^Z / (1 + e^Z) Excel: =EXP(Z)/(1+EXP(Z))

Immediately after Z. Turns the score into a probability. Z = 0 gives 0.5.

Z
The logit for this row
P(y=1)
Predicted probability that the row belongs to class 1

Likelihood and log-likelihood

Likelihood = IF(y = 1, P, 1 - P) Objective = SUM( LN(likelihood) ) -> maximise with GRG Nonlinear

The fitting objective. Solver changes only the coefficient cells; every other column recalculates.

y
The actual class of the row, 0 or 1
P
The row's predicted probability of class 1
SUM(LN)
Log-likelihood of the whole training set, always negative

Classification cut-off

predicted class = IF( P(y=1) >= 0.5, 1, 0 )

On the holdout rows, once the coefficients are frozen. Change the 0.5 to trade precision against recall.

0.5
The cut-off, a business decision rather than a statistical one

The five classification metrics

Accuracy = (TP + TN) / (TP + TN + FP + FN) Precision = TP / (TP + FP) Recall = TP / (TP + FN) (Sensitivity) Specificity = TN / (TN + FP) F1 = 2 x (Precision x Recall) / (Precision + Recall)

On the test rows only, from the four COUNTIFS cells. Choose which one to report from the cost of each error.

TP
Actual 1, predicted 1
FN
Actual 1, predicted 0. Type II error, loss of opportunity
FP
Actual 0, predicted 1. Type I error, loss of resources
TN
Actual 0, predicted 0
Step 2 of 30
The real wordsTheory

Why a straight line will not do

The class insurance data has a column that is only ever 0 or 1. Fit a straight line to it and three things break.

  • The line runs off past 1 and below 0, so it predicts probabilities like 1.4 and -0.2
  • The errors cannot be normally distributed, because the actual value is only ever 0 or 1
  • The effect of one more year of age is forced to be the same everywhere, when in truth it must flatten out near 0 and near 1

So the outcome is bent onto a curve that can never leave the interval from 0 to 1.