Hello! Welcome to the next lesson in your journey through Categorical Data Analysis.
In our last session, we explored goodness-of-fit tests, like the Hosmer-Lemeshow test, to determine if our logistic regression model's structure was appropriate for the data. We made a key distinction between assessing a model's fit and its predictive performance. A model can fit the data well but still not be very useful for making accurate predictions.
Today, we shift our focus to that second question: How good is our model at predicting the outcome?
To answer this, we will introduce the single most important tool for evaluating a classification model's performance: the confusion matrix. By the end of this lesson, you will be able to define a confusion matrix and use it to calculate and interpret two fundamental performance metrics: sensitivity and specificity.
1. The Confusion Matrix: Organizing Predictions
After we fit a logistic regression model, it gives us a predicted probability for each observation (e.g., a 75% probability of having the disease). To make a concrete prediction, we apply a cut-off threshold (typically 0.5). If the probability is above the threshold, we predict "Yes" (positive); otherwise, we predict "No" (negative).
The confusion matrix is a simple but powerful table that summarizes how our predictions stack up against the actual, true outcomes. It shows us not just how many predictions were right or wrong, but also the types of errors the model made.
To get a clear, intuitive introduction to the confusion matrix, please watch the following video from StatQuest.
Machine Learning Fundamentals: The Confusion Matrix
This video, 'Machine Learning Fundamentals: The Confusion Matrix', provides a clear, step-by-step walkthrough of what a confusion matrix is and how to build one.
Please watch from 01:21 to 03:36. Pay close attention to the definitions of the four cells: True Positives, True Negatives, False Positives, and False Negatives.
As the video explained, a 2x2 confusion matrix has four essential components. The layout is crucial: typically, predictions are on one axis (rows) and the actual truth is on the other (columns).

Let's formally define these terms, which are the bedrock of classification performance evaluation:
- True Positive (TP): The model correctly predicted the positive class. (e.g., a patient with the disease is correctly identified as having the disease).
- True Negative (TN): The model correctly predicted the negative class. (e.g., a healthy patient is correctly identified as healthy).
- False Positive (FP): The model incorrectly predicted the positive class. This is also known as a Type I Error. (e.g., a healthy patient is incorrectly told they have the disease).
- False Negative (FN): The model incorrectly predicted the negative class. This is also known as a Type II Error. (e.g., a patient with the disease is incorrectly told they are healthy).
The "positive" class is the outcome of interest. This could be the presence of a disease, a fraudulent transaction, or a customer who churns. The choice is context-dependent.
Sensitivity and Specificity: A Complete Guide
For a slightly different example and a clear textual summary, please read the following section from a DataCamp tutorial. It uses a credit card fraud detection scenario, which is a classic application.
Read the subsection titled 'Confusion Matrix'. This will reinforce the definitions of TP, TN, FP, and FN in a different context.
2. Sensitivity and Specificity: Beyond Raw Counts
The counts in the confusion matrix are insightful, but to compare models or understand performance in a standardized way, we need to convert them into rates or proportions. Two of the most important metrics derived from the confusion matrix are sensitivity and specificity.
Machine Learning Fundamentals: Sensitivity and Specificity
The next StatQuest video is a direct follow-up and explains exactly how to calculate and interpret sensitivity and specificity from the confusion matrix.
Please watch from 01:59 to 03:52. Focus on how each metric is calculated and, more importantly, what it tells you about the model's performance.
Let's formalize those definitions:
Sensitivity (True Positive Rate or Recall)
Sensitivity measures how well the model identifies actual positive cases.
- Interpretation: "Of all the people who actually have the disease, what percentage did our model correctly flag?"
- A model with high sensitivity is good at finding the positive cases. It has a low false negative rate.
Specificity (True Negative Rate)
Specificity measures how well the model identifies actual negative cases.
- Interpretation: "Of all the people who are actually healthy, what percentage did our model correctly clear?"
- A model with high specificity is good at avoiding false alarms. It has a low false positive rate.
3. The Sensitivity vs. Specificity Trade-Off: A Functional Understanding
This is one of the most critical concepts in classification. Rarely can you maximize both sensitivity and specificity simultaneously. Improving one often comes at the cost of the other. The "best" model depends entirely on the real-world consequences of its errors.
Consider two scenarios:
-
Airport Security Screening:
- Positive Case: A weapon is present.
- Cost of a False Negative (FN): Catastrophic. A weapon gets onto a plane.
- Cost of a False Positive (FP): Annoying but manageable. A benign object (like a water bottle) is flagged, leading to a manual bag check.
- Priority: We need extremely high sensitivity. We are willing to tolerate low specificity (many false alarms) to ensure we don't miss a true threat.
-
Spam Email Filtering:
- Positive Case: An email is spam.
- Cost of a False Negative (FN): Annoying. A spam email lands in your inbox.
- Cost of a False Positive (FP): Potentially very high. An important email (e.g., a job offer) is sent to the spam folder and missed.
- Priority: We need very high specificity. We want to be absolutely sure that when we call an email "not spam," it's truly not spam. We are willing to tolerate lower sensitivity (letting some spam through) to avoid this critical error.
This trade-off is not just a theoretical concept; it's something you actively control. In logistic regression, you control it by changing the probability cut-off.
- A low cut-off (e.g., predict "disease" if probability > 0.10) makes it easy to classify as positive. This will increase sensitivity (you'll catch more true positives) but decrease specificity (you'll generate more false positives).
- A high cut-off (e.g., predict "disease" if probability > 0.90) makes it hard to classify as positive. This will increase specificity (fewer false alarms) but decrease sensitivity (you'll miss more true positives).
Sensitivity, Specificity, and Accuracy - Analytics Vidhya
To see this trade-off in action, please look at the graph in the following article from Analytics Vidhya. You don't need to read the code, just focus on the concepts and the plot.
Scroll down to the section 'Step 7: Variation with Cut-Off'. Look at the plot titled 'Sensitivity ,Specificity and Accuracy vs Probability'. Notice how as sensitivity goes up, specificity goes down, and vice versa. The point where they cross is often called the 'optimal' cut-off if you value both errors equally.
Conclusion
In this lesson, we moved from assessing model fit to evaluating predictive performance. You now have a solid functional understanding of the tools used to measure how well a classification model performs its primary task.
Key Takeaways:
- The confusion matrix is the starting point for evaluating classification performance, organizing predictions into True Positives (TP), True Negatives (TN), False Positives (FP), and False Negatives (FN).
- Sensitivity measures the model's ability to identify true positives (). It answers: "How many of the actual sick people did we find?"
- Specificity measures the model's ability to identify true negatives (). It answers: "How many of the actual healthy people did we clear?"
- There is a fundamental trade-off between sensitivity and specificity, controlled by the probability cut-off. Prioritizing one over the other depends entirely on the real-world costs of false positive versus false negative errors.
Preview of the Next Lesson:
Choosing a single cut-off to calculate one sensitivity/specificity pair gives a narrow view of the model's performance. What if we wanted to see how the model performs across all possible cut-offs? In the next lesson, we will introduce the Receiver Operating Characteristic (ROC) curve and the Area Under the Curve (AUC), which do exactly that, providing a comprehensive view of a model's discriminative power.