Missing values
Why values go missing (MCAR, MAR, MNAR) and what that means for how you fill them.
Lesson 8 of 6610 minBeginner
You will learn to
- Distinguish the three missingness mechanisms
- Measure missingness per column
- Decide between dropping, imputing and flagging
Before this lesson
Intuition
Real data has holes. A customer leaves a field blank, a sensor goes offline for an hour, a record from an old system never had that field. Before filling a hole, ask the question that matters most: why is it empty? The answer decides what is safe to do.
Think of a classroom survey on study hours. If a few forms are lost in the post, the gaps have nothing to do with the students: the remaining forms are still a fair sample. If the students who study the least skip the question out of embarrassment, the remaining forms describe a group that studies more than the real class. Same number of blanks, but the second case is a trap.
Statisticians describe three mechanisms:
- MCAR, missing completely at random. Whether a value is missing has nothing to do with any data, observed or not. Lost forms, a random sensor glitch.
- MAR, missing at random. The chance of a gap depends on other columns you can see, but not on the hidden value itself, once those are accounted for. For example, older customers are more likely to skip the "app usage" field.
- MNAR, missing not at random. The chance of a gap depends on the missing value itself. High earners skip the income field; very sick patients miss their appointments.
The names are confusing, so remember the test: can I explain the gaps using columns I have? If they are random noise, it is MCAR. If other columns explain them, it is MAR. If only the hidden value would explain them, it is MNAR.
Picture it
MCAR and MAR can often be recognised from the data: compare the other columns for rows with and without gaps. MNAR cannot be proven from the data alone, because the evidence is exactly what is missing. It is something you reason about from how the data was collected.
The math
Let be an indicator that a value is missing (1 if missing, 0 if observed), let be the observed columns and the values that are hidden. The three mechanisms are three statements about the probability of a gap:
Under MCAR, the rows you keep are a random subset, so averages computed on them are not biased (just less precise). Under MAR, they are biased unless you take the observed columns into account, which is what a good imputation does. Under MNAR, no method that uses only the data can fully remove the bias.
To measure missingness for a column with missing values among rows, use the rate:
and compute it for every column, and also for rows (how many values is each row missing?).
Worked example
Ten illustrative incomes (in thousands): 20, 25, 30, 35, 40, 45, 55, 70, 90 and 120. The true mean is .
MCAR. Three values vanish at random: 25, 45 and 90. The remaining seven sum to , and their mean is . Very close to the truth, and it would be close on average over many random draws.
MNAR. The three highest earners (70, 90 and 120) refuse to answer. The seven that remain are 20 to 55 and sum to 250, so the mean is . That is an underestimate by 17.3, or 33%. No amount of extra rows of the same kind would fix it, because the missing people are systematically different.
The same count of gaps led to a harmless result in one case and a large bias in the other. The count of missing values is a clue to look further; it is not the verdict.
What to do with the gaps. Three options, in order of how often they are appropriate:
- Impute and flag. Fill the gap (with the median, the most common value, or a model's estimate) and add a yes or no column saying that the value was missing. The flag lets the model use the fact of being missing, which is often informative, especially under MAR and MNAR.
- Drop the column. When most of a column is missing and it has no clear meaning, it may do more harm than good. A common rule of thumb is to consider this when more than half is missing, but let the context decide.
- Drop the rows. Reasonable when only a few rows are affected and the gaps look random. Dangerous otherwise, because the rows you remove may be different from those you keep.
The strategies for filling are covered in the lesson on imputation.
How mlflow does it
The data profile has a Missing tab. To open it, go to Data, choose a dataset and select Explore, then open Missing. It shows:
- Total cells, Missing cells, Missing % and Columns with missing.
- Missing rate by column, so the worst columns stand out.
The Overview tab lists the Top columns by missing, and the Columns tab shows each column's missing count and percentage; an expanded column also shows how many cells are filled. A dataset with no gaps shows "No missing values across any column. Clean dataset."
The profile tells you how much is missing, not why. For that you compare the rows with gaps against other columns, which you can do in a notebook or analysis. To handle the gaps, press Choose... on the column in a pipeline and pick one of the Fill missing values options; the preprocessing docs page lists the available methods.
Common mistakes
- Deleting rows with any gap. With many columns, almost every row has a gap somewhere, and you may be left with a small and unusual subset. Look at the rate per column first.
- Filling with the mean and moving on. The mean is a poor guess when the data is skewed, and a filled value looks like real data. Add a flag column when missing carries meaning.
- Treating a special code as a real value. Values such as -999, 0 or "N/A" often stand for missing. Convert them to real gaps, or they will distort averages and distributions.
- Assuming the gaps are random. Test it: compare other columns between rows with and without a gap. If they differ, the mechanism is not MCAR.
- Imputing before the split. Learn fill values (a median, say) from the training rows only and apply them to the rest, or you leak information from the test data. A pipeline does this for you.
- Forgetting that new data will have gaps too. The model in production will meet missing values. Make sure the imputation step is part of the pipeline so that predictions do not fail or change meaning. Studio pipelines also fill missing numeric and boolean values automatically at prediction time, but with generic values learned on the training rows, not a strategy you chose.
Try it in mlflow
Check the Missing tab
- Open Data, choose a dataset and select Explore.
- Open the Missing tab and read Missing cells, Missing % and Columns with missing.
- Look at the Missing rate by column and pick the worst column. Ask: is it random, related to another column, or related to its own value?