Tutorials › Exam PA - Predictive Analytics › Handling Missing Values Before Computing Distances

Exam PA · Clustering and Similarity Structure · Tutorial 590 of 966

Handling Missing Values Before Computing Distances

Compare three ways of handling missing predictors before calculating distances, and see how each can change which observations appear similar.

Intermediate 12 min read

What You'll Learn

  • Distinguish complete-case deletion, imputation, and pairwise handling of missing predictors
  • Calculate Euclidean distances after mean imputation
  • Apply the pairwise rescaling used for missing coordinates
  • Recognize when a pairwise distance is unsupported or misleading
  • Choose and document a missing-value strategy that fits the analysis

Why Missing Predictors Change a Distance Analysis

A distance between two observations summarizes how different they are across the predictors being compared. If a predictor is missing for one observation, its contribution to that comparison is unknown. The analyst must decide whether to omit affected observations, fill in missing values, or calculate each distance using only predictors observed for that pair. These choices can change both the number of observations in the analysis and the similarities represented in a distance matrix.

The methods below assume that the selected predictors are numeric and have already been prepared for distance calculation. In particular, when Euclidean distance is used, predictor scales should be appropriate to the analysis; missing-value handling does not correct a poor choice of scale.

Definition: Complete-case analysis retains only observations with observed values for every selected predictor. Imputation replaces a missing value with an estimated or chosen value so that a complete data matrix can be formed. Pairwise distance handling calculates the distance between two observations using the predictors observed for both, possibly adjusting for the number of shared predictors.

None of these methods is automatically best. Complete-case analysis discards information and may change which observations remain. Imputation creates values that were not observed. Pairwise handling preserves more observations, but different pairs may be compared on different subsets of predictors. The resulting distances therefore need to be interpreted in light of the method used.

Three Ways to Handle Missing Values

Suppose there are \(p\) selected numeric predictors. Under complete-case analysis, an observation is retained only if all \(p\) values are present. In R, `complete.cases()` can identify such rows, and `na.omit()` can remove rows containing missing values in the variables being analyzed. The retained set is easy to describe, but it may be small. It may also differ systematically from the original set if missingness is associated with observed characteristics.

With imputation, each missing value is replaced before distances are calculated. For example, a predictor's observed mean can be substituted for missing values in that predictor. This produces a complete matrix, but a filled-in value is not an additional observation. Mean imputation can make observations with missing values appear more average on that predictor and can reduce the apparent spread. Other imputation methods make different assumptions; the choice should be specified rather than treated as a neutral data-cleaning step.

For pairwise Euclidean distances, let \(M_{ij}\) be the set of predictors observed for both observations \(i\) and \(j\), and let \(m_{ij}\) be the number of such predictors. A common adjusted distance is

$$ d_{ij}=\sqrt{\frac{p}{m_{ij}}\sum_{k\in M_{ij}}(x_{ik}-x_{jk})^2}, \qquad m_{ij}>0. $$

Here \(p\) is the total number of selected predictors. The adjustment scales the observed sum of squared differences to account for predictors not shared by that pair. If there are no shared observed predictors, \(m_{ij}=0\), and this formula does not define a distance. In base R, the Euclidean method of `dist()` uses this type of adjustment when coordinates are missing. Always check the function's documented behavior rather than assuming that missing values are handled as intended.

Why Pairwise Rescaling Is Used

The adjustment has a simple justification under a particular idealized setup. Suppose the \(p\) coordinate-wise squared differences for a pair are viewed as comparable contributions, and the \(m_{ij}\) observed contributions are representative of all \(p\). The mean observed squared difference is their sum divided by \(m_{ij}\). Multiplying that average by \(p\) estimates the total squared difference across all \(p\) coordinates:

$$ p\left(\frac{1}{m_{ij}}\sum_{k\in M_{ij}}(x_{ik}-x_{jk})^2\right) = \frac{p}{m_{ij}}\sum_{k\in M_{ij}}(x_{ik}-x_{jk})^2. $$

Taking the square root gives the adjusted Euclidean distance. This explains the rescaling; it does not guarantee that the estimate is accurate. It is most defensible when the observed coordinates provide a reasonable representation of the unobserved ones. If the missing predictors are systematically different, or a pair shares very few predictors, the adjusted value can be a poor guide to overall similarity. Moreover, different pairs can be compared on different coordinates, so the resulting distances need not behave like ordinary Euclidean distances in one common complete space.

Worked Example: Comparing All Three Methods

Four hypothetical service regions are described by three prepared numeric predictors, \(x_1,x_2,x_3\). Which distances are available under complete-case analysis, mean imputation, and pairwise handling? For pairwise handling, use the rescaling formula above.

Region\(x_1\)\(x_2\)\(x_3\)
A00Missing
B204
C0Missing1
D232

Method 1: complete cases. B and D have values for all three predictors. A is incomplete because \(x_3\) is missing, and C is incomplete because \(x_2\) is missing. Thus, 2 of the 4 regions remain. Their distance is

$$ d_{BD}=\sqrt{(2-2)^2+(0-3)^2+(4-2)^2} =\sqrt{0+9+4} =\sqrt{13} \approx 3.606. $$

The method gives one pairwise distance among the retained regions; it gives no distances involving A or C.

Method 2: mean imputation. The observed mean of \(x_1\) is \((0+2+0+2)/4=1\), and \(x_1\) has no missing values. The observed mean of \(x_2\) is \((0+0+3)/3=1\), so replace C's missing \(x_2\) by 1. The observed mean of \(x_3\) is \((4+1+2)/3=7/3\), so replace A's missing \(x_3\) by \(7/3\). In particular, the imputed rows are A \((0,0,7/3)\) and C \((0,1,1)\).

The imputed distance between A and C is

$$ d_{AC} =\sqrt{(0-0)^2+(0-1)^2+(7/3-1)^2} =\sqrt{0+1+16/9} =\sqrt{25/9} =\frac{5}{3} \approx 1.667. $$

All four regions now have complete rows, but this distance depends on the imputed values. It is not a distance calculated solely from observed A–C measurements.

Method 3: pairwise handling. For A and C, the only shared predictor is \(x_1\). Their values match on that predictor, so the observed sum of squared differences is zero. With \(p=3\) and \(m_{AC}=1\), the adjusted distance is \(\sqrt{(3/1)\times 0}=0\). This zero does not establish that the regions are alike overall: the pair shares just one predictor. For a comparison with more observed difference, A and D share \(x_1\) and \(x_2\), with squared differences \(0\) and \(9\). Thus

$$ d_{AD} =\sqrt{\frac{3}{2}\bigl((0-2)^2+(0-3)^2\bigr)} =\sqrt{\frac{3}{2}(4+9)} =\sqrt{19.5} \approx 4.416. $$

B and D share all three predictors, so their pairwise distance is \(\sqrt{13}\approx3.606\), the same as in the complete-case calculation. The key check is that A and C share only the first predictor; the missing entries do not count as shared coordinates. The three methods retain or compare different information, so their resulting distance matrices are not interchangeable.

Worked Example: The Rescaling Trap

Two hypothetical facilities have records on four standardized predictors. The differences on their two shared observed predictors are \(1\) and \(-2\); the other two predictors are not observed for both facilities. Calculate the adjusted pairwise Euclidean distance and identify the error in omitting the adjustment.

There are \(p=4\) selected predictors and \(m=2\) shared observed predictors. The sum of squared observed differences is

$$ 1^2+(-2)^2=1+4=5. $$

Applying the rescaling gives

$$ d=\sqrt{\frac{4}{2}\times 5} =\sqrt{10} \approx 3.162. $$

The common mistake is to report \(\sqrt{5}\approx2.236\), which is the unadjusted distance using only the two shared coordinates. The adjusted answer is \(\sqrt{10}\), because the specified method multiplies the squared sum by \(p/m=2\) before taking the square root. As a check, if all four predictors were shared, the factor would be \(4/4=1\), and no adjustment would be needed.

Worked Example: Deciding What the R Workflow Retains

A hypothetical analyst has two prepared numeric measures for five districts. The goal is to describe their similarity, not to estimate a predictive model. The analyst wants to compare the consequences of complete cases, column-mean imputation, and pairwise distances.

DistrictMeasure 1Measure 2
U01
V2Missing
W13
XMissing2
Y34

Complete cases. U, W, and Y have both measures observed, so 3 of 5 districts remain. The distance between U and W is

$$ d_{UW}=\sqrt{(0-1)^2+(1-3)^2} =\sqrt{1+4} =\sqrt{5} \approx2.236. $$

V and X are excluded from a complete-case distance matrix based on these two measures.

Mean imputation. The observed mean of Measure 1 is \((0+2+1+3)/4=1.5\). The observed mean of Measure 2 is \((1+3+2+4)/4=2.5\). Thus V becomes \((2,2.5)\) and X becomes \((1.5,2)\). Their imputed distance is

$$ d_{VX}=\sqrt{(2-1.5)^2+(2.5-2)^2} =\sqrt{0.25+0.25} =\sqrt{0.5} \approx0.707. $$

This distance is fully calculable after imputation, but both coordinates include imputed values. It should not be described as a comparison based entirely on observed measurements.

Pairwise handling. U and V share only Measure 1, with a difference of \(0-2=-2\). Here \(p=2\), \(m=1\), and

$$ d_{UV}=\sqrt{\frac{2}{1}(0-2)^2} =\sqrt{8} \approx2.828. $$

As a check on the adjustment, U and W share both measures, so their pairwise distance remains \(\sqrt{5}\approx2.236\). Before using an R distance matrix, the analyst should identify which rows each method retains, confirm the missing-value behavior, and record whether imputation or pairwise adjustment was used. The numerical result alone does not reveal those choices.

Choosing and Reporting a Strategy

The choice should reflect the purpose of the analysis and the pattern of missingness, not just which method produces a convenient matrix. Complete-case analysis is straightforward when few rows are affected and the retained observations remain suitable for the intended exploration. If many rows are dropped, the resulting similarity structure describes only the complete subset. Imputation can retain all observations, but simple mean substitution may pull incomplete observations toward the center and create artificial similarity. Pairwise handling may retain more pair comparisons, but can compare one pair on many predictors and another on very few.

For pairwise distances, report the number of shared predictors as well as the calculated distance when overlap varies substantially. A small distance based on one shared coordinate should not be treated as equally informative as a small distance based on every coordinate. If a pair has no shared predictors, mark its distance as unavailable rather than inventing a value. Also check whether the chosen distance procedure produces a matrix suitable for the intended downstream method.

A concise analysis record should state which predictors were used, how incomplete rows or entries were handled, how any imputation values were calculated, and whether pairwise rescaling was applied. These details are part of the meaning of the distance matrix, not merely implementation notes.

Key takeaway: Missing-value handling determines which observations contribute to a distance analysis and which coordinates define each comparison. State the method, check the available overlap, and interpret distances in light of what was actually compared.

Check Your Understanding

For each problem, calculate the requested result and note which observations or predictors support it.

  1. There are five selected predictors, and two observations share three of them. Their differences on the shared predictors are \(1\), \(2\), and \(-2\). Using the adjusted pairwise Euclidean formula, calculate their distance. (Answer: \(\sqrt{15}\approx3.873\))
  2. Four records have two selected predictors: R1 is \((1,2)\), R2 is \((3,\text{missing})\), R3 is \((\text{missing},4)\), and R4 is \((5,6)\). Which records are retained by complete-case analysis, and how many remain? (Answer: R1 and R4; 2 of 4 records remain.)
  3. Two predictors have observed values \(2,4,6\) and \(1,3,5\), respectively, with one missing first-predictor value in a fourth record. The second predictor for that record is \(3\). Under column-mean imputation, what value replaces the missing first-predictor value, and what is the distance from the imputed record \((\text{imputed value},3)\) to the observed record \((4,3)\)? (Answer: The imputed value is \(4\), and the distance is \(0\).)
  4. There are four selected predictors. For a particular pair, only predictors 1 and 4 are observed for both, and their differences are \(3\) and \(0\). Calculate the adjusted distance, and state what unadjusted shared-coordinate distance would be. (Answer: Adjusted distance \(=\sqrt{18}\approx4.243\); unadjusted distance \(=3\).)
  5. Two observations have no predictors observed in common. Can the pairwise rescaling formula define their distance? What should be recorded instead? (Answer: No; \(m_{ij}=0\), so the formula is undefined. Record the distance as unavailable.)
Back to Tutorials
Clinical Biostats