Tutorials › Propensity Score Matching and Instrumental Variables › Distances for Matching Mixed-Scale Covariates

Matching Methods · Tutorial 186 of 699

Distances for Matching Mixed-Scale Covariates

Construct distances for continuous and categorical covariates, and see why changing scales or weights can change candidate-pair rankings.

Intermediate 12 min read

What You'll Learn

  • Calculate raw and standardized Euclidean distances for continuous covariates.
  • Construct a Gower-style distance for mixed continuous and categorical data.
  • Explain how units, scale factors, and attribute weights change distance rankings.
  • Compare standardized Euclidean and Mahalanobis distances when covariates are correlated.
  • Identify practical diagnostics and limitations of common distance choices.

Why the Distance Definition Matters

Matching requires a way to describe how different two units are on observed covariates. That description is a distance, but it is not automatically determined by the data: the analyst chooses how to scale each covariate and how to combine differences across covariates. With mixed-scale data, those choices can change which control appears most similar to a treated unit.

A distance is a design tool, not evidence that two units are equivalent in every respect. It summarizes differences in the recorded covariates under a specified rule. The measured covariates should be pretreatment variables relevant to the intended comparison. As in other matching methods, a distance alone does not establish that all confounding has been removed or that a causal effect is identified.

Definition: Let \(X_i=(X_{i1},\ldots,X_{ip})\) denote the \(p\) measured covariates for unit \(i\), and let \(D_i=1\) indicate treatment and \(D_i=0\) indicate control. A covariate distance \(d(i,k)\) is a nonnegative numerical summary of the differences between \(X_i\) and \(X_k\), defined by a stated scaling and weighting rule. A smaller value means closer under that rule; it does not necessarily mean closer in every covariate.

A distance is meaningful only alongside its construction. In particular, state which variables are included, how each is scaled, how categorical values are compared, and whether attributes receive equal or different weights. These choices should be made deliberately rather than selected after seeing which candidate pair is preferred.

Distances for Continuous Covariates

For \(p\) continuous covariates, raw Euclidean distance is the square root of the sum of squared differences:

$$ d_{\mathrm{raw}}(i,k)=\sqrt{\sum_{j=1}^{p}(X_{ij}-X_{kj})^2}. $$

This treats a one-unit difference as equally important on every variable. That can be unreasonable when the units or spreads differ. A difference of one year in age and one dollar in income both contribute a squared difference of one, although the dollar difference is usually negligible. Changing a variable from thousands of dollars to dollars can therefore change distances and rankings without changing anyone's underlying characteristics.

Standardized Euclidean distance divides each difference by a scale \(s_j\), commonly the sample standard deviation of covariate \(j\) in the relevant study sample:

$$ d_{\mathrm{std}}(i,k)=\sqrt{\sum_{j=1}^{p}\left(\frac{X_{ij}-X_{kj}}{s_j}\right)^2}. $$

This expresses differences in units of the chosen scale. If \(s_j\) is a standard deviation, a one-standard-deviation difference contributes one squared unit. Standardization makes the result invariant to a simple change of measurement units, provided the scale factor is converted as well. It does not make all covariates equally important in a substantive sense: equal standardized contributions are a modeling choice.

Worked Example: Unit Changes Reverse a Raw-Distance Ranking

Suppose a hypothetical treated unit is 50 years old with annual income of $60,000. Two hypothetical controls have these values: control A is 52 years old with income $62,000; control B is 55 years old with income $60,000. First measure income in thousands of dollars. The raw Euclidean distances are

$$ d_{\mathrm{raw}}(T,A)=\sqrt{(52-50)^2+(62-60)^2} =\sqrt{4+4}=2.828, $$
$$ d_{\mathrm{raw}}(T,B)=\sqrt{(55-50)^2+(60-60)^2} =\sqrt{25+0}=5.000. $$

On this scale, A is closer. Now record income in dollars rather than thousands of dollars. The same raw-distance calculations become

$$ d_{\mathrm{raw}}(T,A)=\sqrt{2^2+2000^2}=2000.001, \qquad d_{\mathrm{raw}}(T,B)=\sqrt{5^2+0^2}=5.000. $$

The ranking reverses: B is now much closer by raw distance, solely because the income unit changed. To illustrate standardization, suppose the reference sample has \(s_{\mathrm{age}}=10\) years and \(s_{\mathrm{income}}=20\) thousand dollars. Then

$$ d_{\mathrm{std}}(T,A)=\sqrt{(2/10)^2+(2/20)^2}=0.2236, \qquad d_{\mathrm{std}}(T,B)=\sqrt{(5/10)^2+0^2}=0.5000. $$

A is closer under these specified scales. The standard deviations are hypothetical reference values, not facts implied by the three units. In an actual analysis, the analyst must state how the scales were obtained and whether they are appropriate for the intended comparison.

Combining Continuous and Categorical Covariates

A mixed dataset needs a rule for categorical differences as well as numerical ones. A common construction is a Gower-style distance: scale each variable-specific difference to a comparable range, then take a weighted average. For a continuous variable, a standard contribution is the absolute difference divided by its observed range. For a nominal variable, the contribution is zero for a match and one for a mismatch.

Definition: For units \(i\) and \(k\), let \(\delta_{ikj}=1\) when covariate \(j\) is observed and usable for that pair, and \(0\) otherwise. Let \(w_j\geq 0\) be its prespecified weight, and let \(g_{ikj}\in[0,1]\) be its variable-specific dissimilarity. The weighted mixed distance is $$ d_{\mathrm{mix}}(i,k)= \frac{\sum_{j=1}^{p}\delta_{ikj}w_jg_{ikj}} {\sum_{j=1}^{p}\delta_{ikj}w_j}, $$ provided the denominator is positive. For a continuous covariate with nonzero range \(R_j\), one usual choice is \(g_{ikj}=|X_{ij}-X_{kj}|/R_j\). For a nominal covariate, use \(g_{ikj}=0\) if the categories agree and \(1\) otherwise.

With equal weights and no missing values, this is the average of the variable-specific dissimilarities, so it lies between zero and one. The range scaling gives a difference equal to the full observed range a contribution of one; it can be sensitive to outliers because an extreme value enlarges the range and shrinks other differences. An alternative scale may be justified, but it must be stated.

For ordinal variables, the categories have an order, so treating every mismatch as equally large may discard information. One option is to map ordered categories to prespecified scores and scale the absolute score difference. The scores and their interpretation should be defensible; assigning consecutive integers implicitly treats adjacent category steps as equally spaced. For binary variables, a simple mismatch contribution is often clear. In settings where sharing an absence is uninformative, a specialized asymmetric-binary rule may be appropriate, but that choice also needs to be explicit.

Worked Example: A Mixed-Scale Distance and a Weighting Reversal

Consider a hypothetical treated unit with age 50 years, income $60,000, smoker status “yes,” and region “urban.” Two hypothetical controls are compared. A has age 52, income $62,000, smoker status “yes,” and region “rural.” B has age 55, income $60,000, smoker status “no,” and region “urban.” Suppose the observed ranges are 40 years for age and $40,000 for income. Use equal weights and the Gower-style contributions described above.

For A, the four contributions are \(2/40=0.0500\) for age, \(2000/40000=0.0500\) for income, \(0\) for smoking, and \(1\) for region. Thus

$$ d_{\mathrm{mix}}(T,A)=\frac{0.0500+0.0500+0+1}{4}=0.2750. $$

For B, the contributions are \(5/40=0.1250\), \(0/40000=0\), \(1\) for smoking, and \(0\) for region. Therefore

$$ d_{\mathrm{mix}}(T,B)=\frac{0.1250+0+1+0}{4}=0.2813. $$

With equal attribute weights, A is slightly closer. Now suppose the analyst gives region weight 4 while retaining weight 1 for each other covariate. The total weight is 7, so

$$ d_{\mathrm{mix},w}(T,A)=\frac{0.0500+0.0500+0+4(1)}{7}=0.5857, \qquad d_{\mathrm{mix},w}(T,B)=\frac{0.1250+0+1+4(0)}{7}=0.1607. $$

The ranking reverses, and B is now closer. The arithmetic is valid under either rule; the substantive question is whether giving region that much weight is justified. A distance ranking is conditional on the chosen weights, not a discovery of a uniquely correct notion of similarity.

Accounting for Correlation: Mahalanobis Distance

Standardized Euclidean distance accounts for different marginal scales but treats covariates as uncorrelated. If two continuous covariates are strongly correlated, that can count a common direction of variation as more unusual than it is. Mahalanobis distance uses the covariance matrix \(S\) of the covariates in a stated reference sample. For a difference vector \(\Delta_{ik}=X_i-X_k\), its squared distance is \(\Delta_{ik}^{\mathsf T}S^{-1}\Delta_{ik}\), when \(S\) is invertible.

The covariance adjustment can change rankings because it measures differences relative to the joint pattern of variation, not just each variable's separate spread. The matrix must be estimated from an appropriate sample and be nonsingular. Exact linear dependence, too many covariates relative to the sample, or near-collinearity can make inversion impossible or unstable. A generalized inverse or regularization changes the method and must be specified. For categorical covariates, blindly expanding categories into all indicator columns can produce a singular covariance matrix; a mixed-data distance or carefully chosen representation is often preferable.

Worked Example: Correlation Changes Which Pair Is Closer

Suppose two hypothetical continuous covariates have covariance matrix

$$ S=\begin{pmatrix}100&90\\90&100\end{pmatrix}. $$

Both standard deviations are 10 and the correlation is 0.90. Relative to a treated unit, let hypothetical control A differ by \(\Delta_A=(10,10)^{\mathsf T}\), and control B differ by \(\Delta_B=(4,-4)^{\mathsf T}\). Standardized Euclidean distances are

$$ d_{\mathrm{std}}(T,A)=\sqrt{(10/10)^2+(10/10)^2}=1.414, \qquad d_{\mathrm{std}}(T,B)=\sqrt{(4/10)^2+(-4/10)^2}=0.5657. $$

Thus standardized Euclidean distance ranks B closer. For Mahalanobis distance, the determinant of \(S\) is \(100(100)-90(90)=1900\), giving

$$ S^{-1}=\frac{1}{1900}\begin{pmatrix}100&-90\\-90&100\end{pmatrix}. $$

For a vector \((a,b)^{\mathsf T}\), the squared Mahalanobis distance is \((100a^2-180ab+100b^2)/1900\). Therefore

$$ d_M^2(T,A)=\frac{100(10)^2-180(10)(10)+100(10)^2}{1900} =\frac{2000}{1900}=1.053, $$
$$ d_M^2(T,B)=\frac{100(4)^2-180(4)(-4)+100(-4)^2}{1900} =\frac{6080}{1900}=3.200. $$

Taking square roots gives \(d_M(T,A)=1.026\) and \(d_M(T,B)=1.789\), so Mahalanobis distance ranks A closer. The positive correlation makes a simultaneous increase in both variables a common direction, while an increase in one and decrease in the other is less consistent with the observed joint variation. This comparison depends on the stated covariance matrix; it is not a universal claim that Mahalanobis distance is always preferable.

Choosing and Checking a Distance

The main alternatives answer different questions. Raw Euclidean distance is simple but unit-dependent. Standardized Euclidean distance removes unit dependence using marginal scales, but ignores correlations. Mahalanobis distance incorporates correlations among continuous covariates but requires a stable covariance estimate. A Gower-style distance handles mixed variable types through scaled, variable-specific contributions, but its ranges, category rules, weights, and missing-data treatment affect the result.

DistanceUseful featureImportant limitation
Raw EuclideanSimple when variables have comparable units and meaningful scalesRankings can change under unit conversion
Standardized EuclideanPlaces continuous differences on selected common scalesDoes not adjust for correlation; results depend on scale estimates
MahalanobisAccounts for covariance among continuous covariatesCan be unstable or undefined with singular or poorly estimated covariance
Gower-style mixed distanceCombines continuous, nominal, and suitably scored ordinal covariatesVariable scaling, weights, and missingness rules determine the result

Before using distances to form matches, inspect the variable-specific contributions and the resulting candidate rankings. Check for variables with extreme ranges, near-zero variation, outliers, rare categories, and highly correlated covariates. Recompute distances under defensible alternative scales or weights and note whether candidate rankings change substantially. Sensitivity of rankings is useful information: it shows that the selected pairs depend on the distance specification.

Missing values need special care. The formula for a mixed distance can omit a variable when it is unavailable for a particular pair and renormalize over the remaining weights. Then two pairwise distances may be averages over different sets of covariates and may not be directly comparable. The missingness strategy should be chosen and reported before interpreting the ranking. A zero-range variable also cannot be range-scaled; it contains no observed variation in the reference data and ordinarily contributes no ability to distinguish units.

A common mistake is to standardize every variable and assume that all scale problems have disappeared. Standardization addresses units or marginal spread according to the selected \(s_j\); it does not decide which variables matter, accommodate categorical values automatically, or account for correlations. Another mistake is to call the smallest computed distance the objectively best match. The correction is to state the metric and its assumptions, justify its scales and weights, and assess whether the resulting comparisons are substantively credible.

Distance construction complements, rather than replaces, choosing a matching design. The earlier material on propensity score matching with Mahalanobis refinement provides context for using covariate distances alongside propensity-score information. Regardless of the design, a close distance on measured covariates cannot guarantee similarity on unmeasured characteristics, and a favorable ranking is not by itself a causal justification.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. Why can converting income from thousands of dollars to dollars change a raw Euclidean ranking but not a standardized Euclidean ranking when the scale factor is converted consistently?
  2. For a nominal covariate, what contributions does a Gower-style distance assign to a matching pair and a mismatching pair?
  3. Two standardized Euclidean distances tie, but the covariates are strongly correlated. Which distance could distinguish the pairs using that correlation, and what additional quantity does it require?
  4. Suppose one pairwise mixed distance omits two missing covariates and another omits none. Why should their values be compared cautiously?
  5. In the mixed-scale worked example, what happens to the ranking when region receives weight 4? What does that result establish?

Solutions. (1) Raw Euclidean distance uses the numerical differences directly, so multiplying the income differences by 1,000 can dominate the sum of squares. Standardized distance divides by the corresponding scale, which also multiplies by 1,000, leaving the standardized contribution unchanged. (2) The nominal contribution is 0 for agreement and 1 for disagreement. (3) Mahalanobis distance can distinguish them because it uses the covariance matrix \(S\); that matrix must be estimated and invertible for the ordinary formula. (4) The pairwise averages may summarize different covariates, so equal numerical values need not represent equal evidence of similarity. (5) B becomes closer: its weighted distance is 0.1607 versus 0.5857 for A. This establishes that the ranking depends on the specified weights, not that the region-heavy weighting is necessarily preferable.

Clinical Biostats