Missing data in depth
Last updated
Lesson 3.8 found missing values and dropped or filled them. Which is right depends on why the values are missing, and that changes what the answer means. In this lesson you'll simulate the three ways data goes missing, see what each does to an average, look for patterns in what's missing, and see what filling a typhoon week with the mean does to a month's total.
By the end of this lesson you can
- Name the three kinds of missingness and say how each biases an average
- Look for patterns in missing values by group
- Choose between dropping, filling and flagging, and say why
Three reasons data goes missing
Statisticians name three kinds of missing data (after Rubin, 1976). Step through them:
- Missing completely at random: why a value is missing has nothing to do with anything in the data. Dropping those rows costs you data but doesn't bias the answer.
- Missing at random: missingness depends on something you can see, like the day of the week. The answer is biased, but you can correct it using what you see.
- Missing not at random: missingness depends on the missing value itself, like slow days not being written down. The answer is biased, and the data alone can't show it.
Now the same three on 91 days of simulated sales. Run the cells.
The true mean is 48.5 kg. With 15 days lost completely at random, the mean is 48.4: hardly changed. With weekend days missing, it's 46.3, too low, because weekends sell more. With slow days missing, it's 50.4, too high.
The third cell finds the pattern: the share missing by day of the week is 0 on weekdays, and 0.69 and 0.62 on Saturday and Sunday. Always look at where the gaps are, by store, day, month and any other column, before deciding anything.
Because that pattern is visible, it can be corrected: the last cell takes the weekday and weekend means separately and weights them by 5 days and 2, giving 48.7, close to the truth. Slow days missing can't be fixed this way. Only knowing why the values are missing can tell you which kind you have; ask the people who collected the data.
Wrong way: a typhoon week filled with the mean
A typhoon closes a store for three days in July. Nobody records sales. Run the cells.
Sales fell to 18 kg on the 25th, are missing for three days, and recover from 12 kg on the 29th. Filling the gap with the mean puts 45.8 kg on each of the three days: an ordinary day's sales, in the middle of a typhoon. The month's total jumps from 779 kg (on the 17 days known) to 916.5 kg: 137.5 kg of rice that was never sold.
These values are missing not at random: they're missing because of what was happening. The mean of normal days says nothing about them.
Interpolating draws a straight line between the known days on each side: 16.5, 15 and 13.5 kg, a total of 824 kg. That's believable, but it's still a guess. The last cell keeps the estimate and adds an estimated column, so every later table and chart can show which values were invented.
Drop, fill or flag
There's no right answer for every gap, but there's a right way to decide:
| choice | good when | cost |
|---|---|---|
| Drop rows | few, and missing completely at random | less data; biased if not random |
| Fill with a fixed value | the value is known, like 0 for a day the store was closed | wrong if you're not sure |
Fill from neighbours (interpolate, ffill) | values change slowly, like stock or temperature | invents values; hides real drops |
| Fill with the mean or median | rarely: values missing completely at random, and a tool can't take gaps | makes every gap look ordinary, and the spread too small |
| Flag and leave | you don't know why they're missing | every later step must handle the gaps |
Whatever you choose: count the gaps, say what you did and why in a note, and keep a flag for every filled value. Report results both ways when it changes the answer.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Rubin, Inference and missing data, Biometrika 63(3), 1976: missing completely at random, at random, and not at random
- van Buuren, Flexible Imputation of Missing Data, 2nd edition (CRC Press, 2018): a book on handling missing data well; the author also publishes it free online
- pandas: Working with missing data: isna, fillna, interpolate