Logistic regression, overfitting and validation
Introduction and exam tips
Provisional: written without the lecture of Fri 27-11-2026. It matches the course manual, the practice exam and the formulas, and will be supplemented after the lecture.
So far you predicted a number: revenue, satisfaction, salary. In module 6 you predict a yes or no. Does a customer buy? Does a customer cancel? Does someone fail to repay? Such an outcome is a : 1 for yes, 0 for no.
For such an outcome you use . In R you do it with glm(y ~ x1 + x2, family = binomial).
You can run OLS on a 0/1 outcome. That is called a linear probability model. It has two problems.
- The prediction can fall outside 0 and 1. A probability of 1.3 or -0.2 does not exist. In our simulated acceptance data (500 households), 16 predictions lie outside 0-1.
- A probability does not rise in a straight line with income or age. It flattens towards 0 and 1.
The model solves this with an S-shaped curve. The probability always ends up between 0 and 1.
You can also state a probability p as odds: p divided by 1 - p. At p = 0.20 the odds are 0.20/0.80 = 0.25. That is “1 to 4”.
The log-odds are the natural logarithm of the odds. The model is linear in those log-odds:
ln(p/(1 - p)) = b0 + b1 x1 + b2 x2 + …
To go from log-odds back to a probability, use p = 1/(1 + exp(-z)). At z = -1.5, p = 0.18.
This is where things often go wrong. A coefficient in a model is a change in the . It is not a change in the probability and not percentage points.
What do you read instead?
- The sign. Positive: more chance of yes. Negative: less chance.
- The significance. Look at
Pr(>|z|). If it is below 0.05, the effect is significant. - The . Take exp(b). That is the factor by which the change when the predictor rises by one unit, holding other variables equal.
Example. Koophuis (owner-occupied home) has 1.010. Then exp(1.010) = 2.75. The odds of acceptance are about 2.75 times as large for owners as for renters. Does that mean “2.75 times the chance”? No. It is about odds.
An odds ratio of 1 means no effect. If 1 lies in the confidence interval, the effect is not significant.
OLS minimises squares. A model uses . R looks for the b’s under which the data you saw are most probable. That happens step by step, not with one formula.
You see it in the output: z value instead of t value, and at the top null and residual deviance instead of R². At the bottom is the .
There are two kinds of measures.
For the whole model.
- The is 2k - 2 ln(L). Lower is better. The 2k penalises extra parameters.
- The is 1 - ln L(model)/ln L(null). In our data that is 1 - 527.77/577.20 = 0.086. It is not a “percentage of explained variance”.
For the classification. You choose a , often 0.5. If the predicted probability is higher, you say “yes”. Then you count right and wrong in a :
| Actual no | Actual yes | |
|---|---|---|
| Predicted no | TN | FN |
| Predicted yes | FP | TP |
These four numbers give the measures:
- Accuracy = (TP + TN)/n. How many right?
- Sensitivity = TP/(TP + FN). What share of the real yeses is found?
- Specificity = TN/(TN + FP). What share of the real noes is recognised?
- Precision = TP/(TP + FP). Of everything the model calls “yes”, what share is right?
Suppose 3% of customers do not pay. A model that always says “pays” has 97% accuracy. And it finds no defaulter at all. With imbalanced classes accuracy is therefore misleading. Then look at and , and compare with the baseline “always the majority class”.
In our acceptance data accuracy is 0.746 at 0.5, but sensitivity is only 0.129. The model finds 17 of the 132 acceptors.
0.5 is a habit, not a law. If you lower the threshold, the model finds more real yeses ( up) but also gives more false alarms ( down). Which way you go depends on the costs. If a missed customer costs a lot and an extra offer costs little, you choose a low threshold.
The ROC curve shows sensitivity against 1 - specificity for all thresholds. The is the area under it: 0.5 is guessing, 1 is perfect.
A model you estimate on certain data always fits that data well. That says nothing about new data. A model that is too flexible even learns the noise. That is overfitting.
Tap a part for the explanation from the summary.
The model is too simple and misses the pattern: poor on train and test.
The model captures the main pattern; test performance is still rising.
Test performance is at its highest: the model generalises well.
Train performance keeps rising, but test performance is already falling.
The model memorises the noise of the train set and generalises badly.
Test performance first rises with complexity and then falls again: that is overfitting. Check on new data.
That is why you split the data:
- A to estimate the model on.
- A that the model has never seen, to judge its performance.
If the model is much better on the training set than on the test set, it is overfit. In a fitness example (simulated), a large model with 29 parameters scored 0.85 on the training set and 0.58 on the test set. The small model scored 0.67 and 0.62. So the large model is worse, even though it looks better.
For a more stable estimate, use : the data goes into k parts, each part is the test part once, and you average the scores.
Behind all this sits the . A flexible model has little bias but a lot of variance. The test error first falls and then rises again.
Logistic regression, overfitting and validation - exam level
Introduction and exam tips
Provisional: written without the lecture of Fri 27-11-2026, based on the course manual, practice exam, formula sheet, Key Terms and R_Functions. It will be checked after the lecture. Numbers come from simulated data (n = 500,
set.seed(21)) unless stated otherwise.
Definition. A binary dependent variable has two outcomes, 0 or 1 (acceptance, churn, default). You model the probability p that Y = 1. The practice exam asks (6.5) why or is needed for this.
Why not OLS (linear probability model)?
- The relation between predictors and probability is non-linear (S-shape).
- Predictions can fall outside [0, 1]. In the data: 16 of the 500 fitted values.
- The variance of the error term depends on the predicted probability (heteroscedasticity).
R. glm(Accepteert ~ Inkomen + Koophuis + Leeftijd, family = binomial, data = d). Without family = binomial you get an ordinary linear regression.
Recognising it in output. Columns z value and Pr(>|z|), then null , residual deviance and . No R² and no F-statistic.
Pitfall. lm with a 0/1 outcome is not a logit; predict() without type = "response" gives log-odds, not probabilities.
Definition. odds = p/(1 - p). log-odds (logit) = ln(p/(1 - p)). The model (formula sheet):
ln(p/(1 - p)) = b0 + b1 x1 + … + bk xk = z, so p = 1/(1 + exp(-z)).
Calculating. z = -1.5 gives p = 1/(1 + exp(1.5)) = 0.18. p = 0.20 gives odds 0.25. p = 0.75 gives log-odds ln 3 = 1.10.
Pitfalls.
- Reading log-odds as a probability.
- Confusing odds and probability: 0.25 here is the odds, the probability is 0.20.
- Misreading the sign: a negative coefficient lowers the odds (and so the probability), a positive one raises them. How many percentage points the probability changes depends on the values of the other variables, not on b alone.
Definition. A coefficient is a change in the per unit. The odds ratio is exp(b): the factor by which the odds multiply, ceteris paribus.
Example from the data.
| | Estimate | exp(b) | Pr(>|z|) | |—|—|—|—| | Inkomen (per €1,000) | 0.0432 | 1.044 | 6.2e-06 | | Koophuis (1 = owner) | 1.0102 | 2.746 | 1.2e-05 | | Leeftijd (per year) | -0.0141 | 0.986 | 0.060 |
In business language. “At equal income and age, the odds of acceptance are about 2.75 times as large for owners as for renters.” Leeftijd is not significant at α = 0.05 (p = 0.06): no evidence of a relation, but also no evidence that it is zero.
Several units. Odds ratios multiply. For 10 units: exp(10 b), not 10 times the odds ratio. For €10,000 extra income: exp(0.43241) = 1.54.
Reference category. For a factor with three levels you get two dummy variables; each coefficient compares with the reference category.
Pitfalls.
- Saying “2.75 times the chance”. It is about odds; odds ratio and probability ratio are only close if the outcome is rare.
- Seeing the size of a logit coefficient as a marginal effect. R_Functions says “sign and significance only”, but the official rubric of 7.1 gives 1 point for direction and size (e^-0.0182 ≈ 0.982): the rubric wins. So also for a non-significant coefficient state direction and size (exp(b)).
- An odds ratio of 1 means no effect. If 1 is in the 95% interval, the effect is not significant.
- Reading the p-value as the probability of H0.
Definition. The coefficients come from maximum likelihood (MLE): choose the b’s under which the observed data have the largest likelihood. The is on the formula sheet: ℓ = Σ [y ln p + (1 - y) ln(1 - p)].
Properties (practice exam 6.4). As the sample grows, MLE is consistent, asymptotically normal and efficient. These are properties for large n, not for small samples. The z-test is therefore an approximation.
Recognising it. The iterations (Fisher scoring) are shown below the output; the is -2 times the log-likelihood.
Null and residual deviance. Null deviance (intercept-only model): 577.20 on 499 df. Residual deviance (full model): 527.77 on 496 df. A larger drop means the predictors add more.
AIC (formula sheet) = 2k - 2 ln(L). Lower is better. With k = 4 parameters, AIC = 2 × 4 + 527.77 = 535.77. The 2k penalises complexity: adding a variable always lowers the deviance a little, but not always the AIC. In the data the deviance drops by 0.33 for Stedelijk and the AIC rises from 535.77 to 537.44.
McFadden pseudo-R² (formula sheet) = 1 - ln L(model)/ln L(null). Here 1 - 527.77/577.20 = 0.086.
Comparing models. anova(m1, m2, test = "Chisq") tests whether the extra variable lowers the deviance significantly (p = 0.57: no).
Pitfalls. Pseudo-R² is not a percentage of explained variance. Compare AICs only on the same data.
Definition. You choose a threshold τ (often 0.5): predicted probability ≥ τ gives “positive”. The confusion matrix counts TN, FN, FP and TP.
Tap a part for the explanation from the summary.
Actually positive, predicted negative. Belongs to sensitivity = TP / (TP + FN).
Actually positive, predicted positive. Also in precision = TP / (TP + FP).
Actually negative, predicted negative. Belongs to specificity = TN / (TN + FP).
Actually negative, predicted positive. Lowers the precision.
First read what "positive" means in the case. Accuracy = (TP + TN) / n.
| Actual 0 | Actual 1 | |
|---|---|---|
| Predicted 0 | TN | FN |
| Predicted 1 | FP | TP |
Example (τ = 0.5): TN 356, FN 115, FP 12, TP 17.
Watch the order. R (table) puts 0 first, so TN is top left (table above). The practice exam 7.1 puts 1 first: then TP is top left. The same numbers in exam form:
| Actual 1 | Actual 0 | |
|---|---|---|
| Predicted 1 | 17 (TP) | 12 (FP) |
| Predicted 0 | 115 (FN) | 356 (TN) |
Always read the row and column headings first; if you expect TN top left you will point at the wrong cell.
Role of the threshold. A lower τ gives more positive predictions: up, down. At τ = 0.3 sensitivity is 0.58 against 0.13 at 0.5. If all predicted probabilities lie below τ (here max 0.66), there is no positive prediction at all and cannot be calculated (0/0). Choose τ on the costs of FN and FP; the coefficients and the do not change.
| Measure | Formula | Example |
|---|---|---|
| accuracy | (TP + TN)/n | 373/500 = 0.746 |
| sensitivity | TP/(TP + FN) | 17/132 = 0.129 |
| specificity | TN/(TN + FP) | 356/368 = 0.967 |
| precision | TP/(TP + FP) | 17/29 = 0.586 |
Question and measure. “What share of the real positives do you find?” is sensitivity. “What share of the alerts is right?” is precision. “What share of the real negatives do you recognise?” is specificity.
Imbalanced classes. Accuracy is misleading when one class is large (practice exam 7.1 part 3). A model that always says “no” has 97% accuracy at 3% fraud and sensitivity 0. Compare with the baseline (share of the majority class; here 0.736) and look at sensitivity and precision.
The ROC curve plots (vertical) against 1 - (horizontal) for all thresholds. The AUC is the area under it: the probability that a random positive observation gets a higher predicted probability than a random negative one. AUC 0.5 is guessing, 1 is perfect separation. In R: prediction() and performance() from ROCR.
In the data: AUC 0.689 (train) and 0.714 (test). A model with a higher AUC on the same separates the classes better (practice exam 6.3). A lower AUC is not in itself.
Pitfall. AUC is not the share classified correctly and does not depend on a single threshold.
Definition. Overfitting: the model fits the noise in the training data and predicts new data worse. You split to judge how well the model generalises to new data (practice exam 6.6).
Tap a part for the explanation from the summary.
The model is too simple and misses the pattern: poor on train and test.
The model captures the main pattern; test performance is still rising.
Test performance is at its highest: the model generalises well.
Train performance keeps rising, but test performance is already falling.
The model memorises the noise of the train set and generalises badly.
Test performance first rises with complexity and then falls again: that is overfitting. Check on new data.
Procedure. Draw training rows at random (sample()), estimate on the , judge on the .
Example. Acceptance model: 0.729 (train) and 0.793 (test). That does not point to overfitting; the test set is small (150) and the naive baseline is 0.76. Fitness example (simulated): large model train 0.85, test 0.58; small model train 0.67, test 0.62. The large model is overfit.
. k-fold: k parts, each part is the test once. LOOCV is k = n. A better estimate of the test error than one split.
Bias-variance trade-off. More flexible: less bias, more variance. The training error always falls, the test error is U-shaped.
Pitfalls. Using the test set to choose the model (and then reporting that same score). Too few predictors is called underfitting, not overfitting.
Question 7.1 (9 points, 200 words) gives glm output and a and asks three things:
- Coefficients in business language (about 4 points): sign, significance, , ceteris paribus.
- Calculation (about 2 points): and or precision, with the formula and the numbers.
- Why accuracy misleads (about 3 points): imbalanced outcome (baseline), low sensitivity, and the consequence for the company (which customers or cases you miss). Mention threshold or overfitting only if the case asks for it.
Write briefly: one sentence per point, numbers included.
Logistic regression, overfitting and validation - as the teacher writes it
Introduction and exam tips
Provisional: there is no transcript for module 6 yet, because the lecture of Fri 27-11-2026 has not taken place. Below are only written sources by the teacher (practice exam, formula sheet, Key Terms and R_Functions, which belong to the course). Harwin quotes from the lecture will follow after the lecture.
The sources below are written texts, not spoken words. Quotes are literal, in the original language, with page number. Each quote is followed by what it means for your exam. Use the practice exam as a model for question 6.x (multiple choice) and 7.1 (open).
[Oefententamen BM21MIM-P], p. 10
Waarom zijn voor een binaire afhankelijke variabele speciale modellen zoals of nodig?
What this says: the exam asks exactly this. The good answer says that the relation with the probability is non-linear and that the variance of the error term depends on the predicted probability. OLS can also give predictions outside 0 and 1. An answer that only says “because Y is a dummy variable” misses the reason.
[Oefententamen BM21MIM-P], p. 9
Een logitcoëfficiënt geeft een verandering in de log-odds weer, niet direct een verandering in de kans in procentpunten.
What this says: this is the core of every interpretation question. Never say “the probability rises by 1.01 percentage points”. Say: the log-odds rise by 1.01, or the odds become 2.75 times as large.
[Oefententamen BM21MIM-P], p. 9
worden de odds, ceteris paribus, vermenigvuldigd met ongeveer
What this says: the standard wording is “the odds are multiplied by exp(b)”. The words ceteris paribus (holding other variables equal) belong with it.
[R Functions in BM21MIM-P], p. 1
Sign and significance only (size is not a marginal effect)
What this says: R_Functions mentions only sign and significance for glm; the size of a coefficient is not a marginal effect. The official rubric of 7.1 goes further: 1 point for direction and size (e^-0.0182 ≈ 0.982, so about 1.8% lower odds) and 1 point for significance. The rubric wins over R_Functions: read the size as an odds ratio, not as a marginal effect.
[Oefententamen BM21MIM-P], p. 11
Interpreteer de coëfficiënten van Koophuis en Leeftijd in bedrijfstaal
What this says: in 7.1 you give four things per coefficient: the sign, the size ( exp(b)), whether the effect is significant (p-value against α), and the meaning for the company. A non-significant coefficient you do not interpret as “no effect”, but as “no evidence of an effect”.
[Oefententamen BM21MIM-P], p. 10
(maximum likelihood estimation, MLE)
What this says: you must know the properties of MLE (consistent, asymptotically normal, efficient) and know that they hold in larger samples.
[Formuleblad BM21MIM-P], p. 1
What this says: the log-likelihood is on the formula sheet. You need not know it by heart, but recognise and use it.
[Formuleblad BM21MIM-P], p. 1
Akaike-informatiecriterium ()
What this says: AIC = 2k - 2 ln(L) is on the formula sheet. Lower is better. You use it to compare models, not to judge one model on its own.
[Formuleblad BM21MIM-P], p. 1
Pseudo-R2 (McFadden)
What this says: the formula is 1 - ln L(model)/ln L(null). You can calculate it from the null and the residual deviance in the glm output.
[Oefententamen BM21MIM-P], p. 11
Tap a part for the explanation from the summary.
Actually positive, predicted negative. Belongs to sensitivity = TP / (TP + FN).
Actually positive, predicted positive. Also in precision = TP / (TP + FP).
Actually negative, predicted negative. Belongs to specificity = TN / (TN + FP).
Actually negative, predicted positive. Lowers the precision.
First read what "positive" means in the case. Accuracy = (TP + TN) / n.
Met een drempelwaarde () van 0,5
What this says: the matrix belongs to a threshold. Mention it when you explain a confusion matrix.
[Oefententamen BM21MIM-P], p. 11
Bereken de nauwkeurigheid () en de sensitiviteit () van het model.
What this says: this is a calculation question for your calculator. Write down the formula and fill in the numbers: accuracy = (TP + TN)/n, sensitivity = TP/(TP + FN).
[Oefententamen BM21MIM-P], p. 10
Welke maat beantwoordt deze vraag?
What this says: multiple-choice question 6.7 describes a business question and has you choose the measure. Learn the questions that go with the measures: “what share of the alerts is right” is , “what share of all real cases do I find” is sensitivity.
[Key Terms (English-Dutch)], p. 11
Sensitivity (true positive rate) Sensitiviteit Specificity Specificiteit Precision Precisie
What this says: the Dutch terms are in the list; in your answer you use the English term.
[Oefententamen BM21MIM-P], p. 11
Het management vindt het model goed omdat de nauwkeurigheid hoog is. Leg uit waarom dat oordeel misleidend is.
What this says: a fixed question in 7.1. The answer contains: the classes are imbalanced, a model that always predicts the majority class already gets a high accuracy, and the shows how many real cases are missed. Add a calculation with numbers from the matrix.
[Oefententamen BM21MIM-P], p. 10
Tap a part for the explanation from the summary.
The model is too simple and misses the pattern: poor on train and test.
The model captures the main pattern; test performance is still rising.
Test performance is at its highest: the model generalises well.
Train performance keeps rising, but test performance is already falling.
The model memorises the noise of the train set and generalises badly.
Test performance first rises with complexity and then falls again: that is overfitting. Check on new data.
Waarom splitsen we de data in een trainingsset en een testset bij een logistische regressie?
[Oefententamen BM21MIM-P], p. 10
Om te beoordelen hoe goed het model generaliseert naar nieuwe data
What this says: the good answer is generalisation. The fit on the training data is too favourable; the shows the real performance and detects overfitting.
[Oefententamen BM21MIM-P], p. 9
Het logitmodel onderscheidt wanbetalers en niet-wanbetalers het best.
What this says: in 6.3 the model with the highest on the same test set wins. A lower AUC points to weaker discrimination, not to overfitting.
[Key Terms (English-Dutch)], p. 11
Bias–variance trade-off Afweging tussen bias en variantie
What this says: the is in the Key Terms. The practice exam does not ask about it, but the term belongs with validation.
- After the lecture, recalculate the examples and compare with the worked solution in the exam-level summary.
- Do the five assignments of module 6 and the block 7 practice questions with the marking model next to them.
- If the lecture says something that is not here (for example a new topic or a different emphasis), the lecture wins. Those quotes will come here.
Simulated data (set.seed(21), n = 500): a provider wants to predict which household accepts an offer. Model: glm(Accepteert ~ Inkomen + Koophuis + Leeftijd, family = binomial), real output from m6-glm-summary.
- Coefficients: Inkomen 0.043241 (p < 0.001), Koophuis 1.010191 (p < 0.001), Leeftijd -0.014108 (p = 0.06, not significant at 0.05).
- Koophuis: exp(1.010191) = 2.75 (95% interval 1.75 to 4.32): at equal Inkomen and Leeftijd the for owners are 2.75 times as large as for renters.
- Model fit: null 577.20, residual deviance 527.77, AIC 535.77; = 1 - 527.77/577.20 = 0.0856.
- Confusion matrix at threshold 0.5: 356 TN, 115 FN, 12 FP, 17 TP. Accuracy 0.746, sensitivity 0.129, specificity 0.967, precision 0.586.
- The baseline ‘always no’ gets 368/500 = 0.736: the accuracy is barely better. At cutoff 0.3 the sensitivity rises to 0.576 and the specificity falls to 0.715.
- Test set (150): AUC 0.7142 against 0.6891 on the training set; no sign of overfitting.
Case: the acceptance data above.
- Coefficients (4 p): Koophuis is positive and significant (p < 0.001): the of acceptance are 2.75 times as large for owners as for renters, ceteris paribus. Leeftijd is negative but not significant (p = 0.06): no evidence of a relation. Do state direction and size (1 point in the rubric): per year the odds are about 1.4% lower (exp(-0.0141) = 0.986), indistinguishable from zero.
- Calculation (2 p): = (356 + 17)/500 = 0.746; = 17/(17 + 12) = 0.586.
- Assessment (3 p): the baseline ‘always no’ scores 0.736, so accuracy says little with imbalanced classes. The is only 0.129 (17 of the 132 acceptors); choose the threshold on the cost of missed acceptors versus unnecessary offers.