Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Handling Missing Data Patterns

Recommended resources

In our last lesson, you learned how to identify the presence of critical data quality issues, including missing values. You can now use pandas and visualization tools to tell if and where data is missing. Now, we'll build on that foundation to understand why data might be missing and how that reason should guide our response.

This lesson focuses on the patterns behind missing data and the strategic thinking required to handle them. You will learn to classify missing data into three main types and, based on that classification, determine which handling strategies are most appropriate. This is a crucial step that moves beyond simple detection towards a more nuanced and effective approach to data cleaning.

Understanding the "Why": Patterns of Missing Data

Not all missing values are created equal. The underlying reason for a value's absence is known as the missing data mechanism. Understanding this mechanism is the key to choosing a treatment that doesn't inadvertently introduce bias into your analysis or model. The three primary mechanisms were first described by the statistician Donald Rubin.

To get a clear and concise introduction to these concepts, the following video from the ritvikmath channel provides excellent intuitive explanations.

Missing Data Mechanisms

This video breaks down the three core missing data mechanisms using a simple, relatable example.

Watch the entire video. Pay close attention to the library book example as the presenter uses it to explain: Missing Completely at Random (MCAR), where the missingness is just noise. Missing at Random (MAR), where the missingness is explainable by other observed variables. Missing Not at Random (MNAR), the most complex case where the missingness relates to the value itself. Finally, take note of the practical advice on how to differentiate MCAR from MAR.

To solidify these concepts, let's summarize them. The following image provides a great visual representation of the three patterns.

A visual summary of the three missing data patterns. 1) MCAR shows values missing randomly across the grid. 2) MAR shows that the missingness in one feature can be explained by the observed data in another. 3) MNAR shows that the missingness is related to the unobserved values themselves or other unobserved features.

Here is a breakdown of the three mechanisms:

  1. Missing Completely at Random (MCAR): The probability of a value being missing is the same for all observations. The fact that the data is missing is independent of both the observed data and the missing data itself.

    • Example: A lab technician accidentally skips a few measurements due to a distraction. A survey respondent misses a question by pure chance.
    • Implication: This is the ideal, though often unrealistic, scenario. The observed data is still a random subsample of the full data.
  2. Missing at Random (MAR): The probability of a value being missing is related to other observed information in the dataset, but not to the missing value itself.

    • Example: In a survey, men are less likely to answer questions about mental health than women. The missingness in the "mental health score" variable depends on the observed "gender" variable.
    • Implication: If you don't account for the relationship (e.g., with gender), your analysis could be biased.
  3. Missing Not at Random (MNAR): The probability of a value being missing is related to the value of the missing data itself.

    • Example: People with very high incomes are less likely to disclose their income on a survey. The missingness in the "income" variable depends on the unobserved income itself.
    • Implication: This is the most problematic case, as the reason for the missingness is unknown. Handling it often requires domain expertise or advanced statistical models.

For a more detailed exploration of these mechanisms with code examples, the following article provides an excellent deep dive.

Missing Data Demystified: The Absolute Primer for Data Scientists

This article from Towards Data Science provides an in-depth look at missing data mechanisms, complete with helpful diagrams and a simulated study example.

Focus on the section titled Missing Data Mechanisms. Read from the beginning of that section through the three subsections, ending with the MNAR example. This will reinforce the concepts from the video with another clear example.


Visualizing Patterns with missingno

While it's impossible to definitively prove the missing data mechanism without knowing the true data-generating process, we can use visualization to find clues. In the previous lesson, you used a simple heatmap. A more specialized library, missingno, offers a suite of plots designed specifically for this task.

The following video provides a practical guide to using missingno to understand the structure of missing data in your dataset.

Missingno Python Library | Visualising Missing Values in Data Prior to Machine Learning

This video by Andy McDonald is a tutorial on using the missingno library to visualize missing values.

Watch the following segments to learn about the key plots missingno offers: The Bar Plot: A quick overview of data completeness per column. The Matrix Plot: Visualizes where missing data occurs in the dataset. This is excellent for spotting if missingness is concentrated in particular rows or sections of your data. The Heatmap: This is crucial. It shows the nullity correlation. A strong positive correlation between two columns suggests that if a value is missing in one, it's likely missing in the other. This is a strong clue for a MAR pattern. The Dendrogram: This plot groups columns with similar nullity patterns, providing a hierarchical view of the relationships you see in the heatmap.

Using the missingno heatmap and dendrogram is your most practical tool for hypothesizing a MAR pattern. If you see that missing values in an income column are highly correlated with the missing values in an age column, it suggests the reason for missing income might be related to age, a classic MAR scenario.


From Patterns to Handling Strategies

Once you have a hypothesis about the missing data mechanism, you can choose an appropriate handling strategy. The choice is critical, as a mismatched strategy can introduce more problems than it solves.

This flowchart gives a high-level overview of the available techniques, which are broadly categorized into Deletions and Imputations.

A flowchart illustrating different methods for handling missing data, starting with the main branches of Deletions and Imputations, and breaking down into specific techniques for each.

The following table, adapted from the DataCamp article we'll reference below, provides a clear guide for matching the mechanism to the method.

Type of Missing Data Recommended Strategy
MCAR Deletion (if dataset is large and missingness is low <5%).
Simple Imputation (Mean, Median, Mode).
MAR Imputation is preferred. Simple methods can work, but more advanced methods like Multiple Imputation or Regression Imputation that leverage other columns are better. Deletion can introduce bias.
MNAR Requires careful consideration. May need domain-specific methods (e.g., if "0" means "not applicable"), or advanced modeling techniques. Simple imputation is likely to be biased.

Let's briefly introduce the two main families of techniques.

1. Deletion Methods

This involves removing data.

  • Listwise Deletion (Complete Case Analysis): Delete any row that has at least one missing value. This is the default for many Python libraries.
  • Pairwise Deletion: When calculating a statistic like correlation between two columns, you only use the rows where both columns have data. This is less common in ML model training.
  • Dropping Columns: If a column is mostly empty and not critical, you might drop the entire feature.

Pros: Simple, fast. If data is truly MCAR, it produces unbiased estimates.
Cons: Significant loss of information. Can drastically reduce your sample size. If data is not MCAR, it can introduce serious bias.

2. Imputation Methods

This involves filling in the missing values.

  • Simple Imputation (Univariate): Use a summary statistic from the same column to fill the gap.
    • Mean/Median: For numerical data. Median is preferred for skewed distributions as it's robust to outliers.
    • Mode (Most Frequent): For categorical data.
  • Advanced Imputation (Multivariate): Use other columns in the dataset to predict the missing value.
    • Examples: K-Nearest Neighbors (KNN) imputation, MICE (Multiple Imputation by Chained Equations). These assume a MAR pattern.

Pros: Retains all data and observations. Can be more accurate than deletion.
Cons: Can distort the natural distribution of the variable (e.g., mean imputation reduces variance). More complex to implement. Makes assumptions about the data that may not be true.

The following article offers a great summary of these strategies, their pros, and their cons.

Top Techniques to Handle Missing Values Every Data ...

This DataCamp article provides a comprehensive overview of handling strategies with code examples.

Read the section on Handling Missing Data. Focus on understanding the conceptual explanations, pros, and cons of: Data Dropping Mean/Median Imputation We will cover the implementation details and more advanced techniques in subsequent lessons. For now, focus on the "what" and "why" for these foundational methods.


Conclusion

In this lesson, you've moved from simply detecting missing values to strategically analyzing their patterns. This deeper understanding is essential for robust data preprocessing.

Here are your key takeaways:

  • Missing Data Mechanisms: Data can be Missing Completely at Random (MCAR), Missing at Random (MAR), or Missing Not at Random (MNAR).
  • Pattern Determines Strategy: The underlying mechanism dictates the best way to handle missing values. A poor choice can introduce bias and weaken your model.
  • Visualizing Patterns: The missingno library, particularly its heatmap and dendrogram plots, is a powerful tool for investigating nullity correlations and hypothesizing MAR patterns.
  • Handling Strategies: The main approaches are Deletion (removing data) and Imputation (filling data). Deletion is simple but can lose information, while simple imputation can distort statistical properties of your data.

You now have the theoretical framework to make informed decisions about missing data. In our next lesson, we will put this into practice by applying the most common foundational techniques—deletion and simple mean/median/mode imputation—using pandas on a real dataset.