Unit 1, lesson 7

Missing Data and Reliable Imputation

How missing values, missingness indicators and the mechanisms MCAR, MAR and MNAR shape a data treatment, how to fit imputation on training data only, and why complete cases and missing targets need care.

  • Video 7
  • Data Quality and Feature Preparation

A temperature sensor sends no reading for one row of a training table. Filling the blank with 0 would record a freezing day that never happened, and deleting the row quietly changes which conditions the model learns from. Which treatment is reasonable depends on why the reading is missing and on what the model will see when it is used. This lesson separates the possible reasons for missingness, fits a simple imputer without letting held-out data in, and shows why a missing target needs different treatment from a missing feature.

Key terms

Missing value and missingness indicator

Definition A missing value is an entry of a data table for which the intended quantity was not observed or not recorded for that record. It is not zero and not any other number, and software marks it with a special code such as NaN or None. For record ii and feature jj, the missingness indicator is

Rij={1,observed,0,missing.R_{ij} = \begin{cases} 1, & \text{observed}, \\ 0, & \text{missing}. \end{cases}

This page uses 1 for observed. Some libraries use the opposite convention, so a pipeline should state which one it uses. The indicator can be added to the model inputs next to a filled-in version of the feature, so that the model receives both a number and the fact of whether that number was measured. Like any feature, it must be available at prediction time, and it carries the same meaning there only if values go missing in the same way.

In plain words A blank cell says that a value is unknown, not that it is small. The indicator keeps the fact that a reading was missing separate from whatever number is later put in the cell. That fact can itself be informative, for example when sensors tend to fail under unusual conditions, but the relationship belongs to the collection process and can change when the device or the logging policy changes.

Example A constructed training column holds the temperatures 10, 14, missing and 22 degrees Celsius, and a held-out group has the readings 18 and missing. The indicators are (1,1,0,1)(1, 1, 0, 1) for training and (1,0)(1, 0) for the held-out records. Entering 0 for the blanks would add two readings of 0 degrees Celsius, real temperatures that nobody measured.

Missing completely at random

Definition Take one feature that can be missing, with value vv and indicator RR, and let w\mathbf{w} hold the other variables of the analysis, assumed to be recorded for every record. The missingness mechanism is the process that decides which values are absent, described by the probability P(R=1∣v,w)P(R = 1 \mid v, \mathbf{w}) of observing the feature. The values are missing completely at random (MCAR) if

P(R=1∣v,w)=P(R=1)\begin{aligned} &P(R = 1 \mid v, \mathbf{w}) \\[0.4em] &\quad = P(R = 1) \end{aligned}

for all values vv and w\mathbf{w}. The chance of observing the feature then depends neither on the value that went missing nor on anything recorded. If P(R=1)>0P(R = 1) > 0, the records with an observed value have the same distribution of all the variables as the full set of records. A mechanism is an assumption about how the data were produced. Observed data can give evidence against MCAR, for example a clear difference in missingness rates between groups of a recorded variable, but they cannot confirm it.

In plain words Under MCAR the blanks fall like a lottery that ignores everything in the analysis. The observed values are fewer, but they are not selected by their size. This is a much stronger statement than the everyday word random. A sensor that fails at unpredictable moments, but more often on hot afternoons, does not meet MCAR, because its failures depend on the temperature.

Example Suppose a radio link loses a transmission with the same chance on every record, whatever the temperature, the demand or the device. Then the observed training readings 10, 14 and 22 are a random subset of the four readings, and summaries of them estimate the same quantities as summaries of complete data, only from fewer values. If missing readings turned out to be much more common on one recorded device type, that would be evidence against this account.

Missing at random and missing not at random

Definition In the same setting, the values are missing at random (MAR) given w\mathbf{w} if

P(R=1∣v,w)=P(R=1∣w)\begin{aligned} &P(R = 1 \mid v, \mathbf{w}) \\[0.4em] &\quad = P(R = 1 \mid \mathbf{w}) \end{aligned}

for all values vv and w\mathbf{w}. Once the recorded variables are known, the chance of observing the feature does not depend further on its value. Then among records with the same w\mathbf{w}, the observed and the missing values have the same distribution, whenever both kinds of record occur there. The values are missing not at random (MNAR) if MAR fails, so that for some w\mathbf{w} the chance of observing still depends on vv. MCAR is the special case of MAR in which the chance does not depend on w\mathbf{w} either. The observed data never show the distribution of the values that are missing, so the same observed table is generally compatible with MAR and with many MNAR mechanisms. Telling them apart needs knowledge of the collection process, extra measurements or explicit assumptions, and conclusions should be checked under plausible alternatives in a sensitivity analysis.

In plain words Under MAR the recorded information explains who is missing. Under MNAR the missing value itself still matters after everything recorded is taken into account. MAR does not mean that the blanks show no pattern, and it does not make one pooled median adequate, because the observed values can still overrepresent some groups.

Example Suppose sensor type is recorded, older devices lose more transmissions, and within each type the chance of a loss does not depend on temperature. That is MAR given type. If the older devices sit in cooler rooms, the pooled observed readings overrepresent the warmer rooms of the newer devices, and filling each blank with the median of the observed training readings of the same type fits the assumption better than one pooled median. Now suppose instead that a probe stops recording above a temperature threshold and nothing recorded marks the heat. That is MNAR. The visible column 10, 14, missing, 22 could hide an ordinary 18 lost in transmission or a 40 from a failed probe. With 40, the four readings have mean 21.5, while the observed readings average about 15.3 and a median imputer still fills 14. No rule fitted only to the recorded temperatures can learn the hidden hot tail. Finding that blanks concentrate on older devices argues against MCAR, but it does not prove MAR, since within each type failures could still depend on temperature.

Imputation

Definition Imputation replaces each missing entry by a value computed by a rule, so that the model receives a complete input. The rule is fitted on the training data only, the numbers it learns are saved, and the same rule is then applied unchanged to training, validation, test and future records. In cross-validation it is fitted again on the training part of every fold. Median imputation of a numerical feature fills every blank with the median of the observed training values, the middle value of the sorted list when the count is odd and the average of the two middle values when it is even. If a training column has no observed values, the median is undefined, and a declared policy is needed, such as dropping the feature, obtaining measurements or using a constant justified by knowledge of the domain. Held-out values must not be used to rescue it. An imputed value is a model input, not a recovered measurement.

In plain words Fit once on training data, save the number and reuse it everywhere. Held-out records exist to judge the whole procedure, the imputer included, so they must not shape the fill value. Filling several blanks with one number also makes those records look more alike than their true values probably were, which can weaken relationships and hide uncertainty. Rules that use other features, or that produce several plausible completions, still rest on assumptions about the mechanism.

Example The observed training readings sorted are 10, 14 and 22, so the saved median is 14. The training column becomes 10, 14, 14, 22, the held-out 18 stays 18 and the held-out blank becomes 14. Pooling the observed readings of both groups gives 10, 14, 18, 22 with median (14+18)/2=16(14 + 18)/2 = 16, a fill value shaped by held-out data, which leaks information into the evaluation. In the filled training column the measured 14 and the imputed 14 are equal inputs, and only their indicators, 1 and 0, tell them apart. In code the fit and the application are two separate steps.

import numpy as np

train = np.array([10.0, 14.0, np.nan, 22.0])
held_out = np.array([18.0, np.nan])

fill = np.nanmedian(train)               # 14.0, fitted on training data only
r_train = (~np.isnan(train)).astype(int) # 1 observed, 0 missing
train_filled = np.where(np.isnan(train), fill, train)
held_filled = np.where(np.isnan(held_out), fill, held_out)

Complete-case analysis

Definition Complete-case analysis keeps only the records whose values are observed on every variable the analysis requires and discards the others. If the missingness of the required variables, taken together, is MCAR, the complete cases are a random subset of the records, so deleting the others does not by itself shift the distribution and only leaves less data. Under MAR or MNAR the complete cases can differ systematically from all records. For a prediction model, deleting incomplete training records does not remove missing values at prediction time. The deployed procedure still needs a rule for incomplete records, and its evaluation should include them.

In plain words Deleting rows is a decision about which records the model learns from and speaks for. Whether that is defensible depends on the question and on the mechanism, not on a general rule that blanks should be removed.

Example For an analysis that needs the temperature, complete-case analysis keeps rows 1, 2 and 4 of the training column, three of the four rows. If the missing row came from the probe that fails in heat, every retained row comes from cooler conditions. A model trained on them has never seen the conditions in which the probe fails, yet at prediction time the probe will fail again in exactly those conditions.

Missing target

Definition A missing target is an outcome value that is not observed for a record. A common cause is label delay, where the outcome is defined over a window that has not ended yet, such as a product return within 30 days of purchase, so the label becomes available only later. A pending label is unknown, not 0, and filling it with a fixed value changes the question the model learns to answer. Ordinary supervised training uses the records whose labels have matured by the training date, or a method designed for delayed or censored outcomes, which brings its own assumptions. Pending records are kept for later labeling. A historical evaluation uses only labels that were available at the simulated training date, so the time at which each label became available is stored separately from the observation time and the prediction time.

In plain words A missing feature is an input the model lacks, while a missing target is part of the answer the model is trying to learn. Filling pending labels with 0 teaches the model that unresolved cases are confirmed negatives. Feature imputation cannot repair labels that are unknown, or labels from the future that leak into the past.

Example Four constructed orders will eventually have the return outcomes 1, 0, 1 and 0, where 1 means returned within 30 days, but today only the first two labels have matured. The observed rate among mature labels is 1/21/2. Filling both pending labels with 0 gives 1/41/4, recording two unresolved orders as confirmed nonreturns. The eventual rate, known only later, is 2/4=1/22/4 = 1/2. The mature labels matched it here, but older orders can differ from recent ones, so that agreement is not guaranteed.

Check yourself

  1. Constructed training readings are 4, 8, missing and 12 degrees Celsius, and the validation readings are 20 and missing. Fit a median imputer and write the filled training and validation columns with their indicators, using 1 for observed.
Show the answer

The observed training readings sorted are 4, 8 and 12, so the saved median is 8. The filled training column is 4, 8, 8, 12 with indicators (1,1,0,1)(1, 1, 0, 1). The validation column becomes 20 and 8 with indicators (1,0)(1, 0). The validation reading 20 plays no part in the fill value, and the indicator records that the second validation input was imputed, not measured.

  1. A colleague computes one median from the observed readings of both groups in the first question and fills every blank with it, then runs 5-fold cross-validation on the training data with that filled column. What goes wrong, which validation inputs are correct, and how should the pipeline be repaired?
Show the answer

The pooled observed readings 4, 8, 12 and 20 have median (8+12)/2=10(8 + 12)/2 = 10, so the colleague’s validation inputs are 20 and 10. That fill value was shaped by the validation reading 20, so the validation score judges a procedure that has already seen part of the data it is scored on. The correct validation inputs are 20 and 8, from the training median alone. In cross-validation the column must not be filled in advance. The imputer is fitted on the training part of each fold and applied to that fold’s held-out part, which a pipeline that fits the imputer and the model together does automatically. When the final model is fitted, the imputer is fitted on the same training data and saved with it.

  1. Five constructed orders are labeled 1 if returned within 30 days of purchase. Three orders have mature labels 1, 1 and 0, and two were placed this week. A colleague fills the two pending labels with 0. Compute the return rate among mature labels and after the fill, give the range of possible eventual rates for all five orders, and explain why the fill changes the task.
Show the answer

The mature labels give 2/3≈0.672/3 \approx 0.67. After the fill the rate is 2/5=0.42/5 = 0.4. The eventual rate for all five orders lies between 2/52/5, if neither recent order is returned, and 4/54/5, if both are, so the fill silently picks the lowest possible value. A model trained on the filled labels learns whether a return had been recorded by the day the data were extracted, which depends on how recently the order was placed, instead of whether the order is returned within 30 days. The repair keeps the pending labels unknown, trains on orders whose 30-day window has closed or uses a method built for delayed outcomes, and keeps the recent orders for labeling once their window ends.

How this lesson connects