Tutorials › Exam PA - Predictive Analytics › Clustering Mixed Data With Gower Distance

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

Clustering Mixed Data With Gower Distance

Calculate and interpret Gower dissimilarities for mixed predictors, then use them appropriately when clustering profiles.

Intermediate 12 min read

What You'll Learn

  • Calculate normalized contributions for numeric and ordinal predictors
  • Distinguish nominal, symmetric binary, and asymmetric binary comparisons
  • Combine variable contributions using weights and pair-specific missingness
  • Interpret a Gower dissimilarity and identify what drives it
  • Use a mixed-data dissimilarity matrix for medoid or hierarchical clustering
  • Recognize common calculation and method-selection traps

Why Mixed Data Need a Different Distance

A clustering data set may contain measurements such as age, ordered categories such as risk bands, and labels such as sales channel. Euclidean distance requires numerical coordinates on compatible scales; assigning arbitrary numbers to categories does not make their differences meaningful. Gower distance instead calculates a contribution for each variable according to its type, then combines the usable contributions into an overall dissimilarity.

The result is a pairwise distance matrix that can be used with methods accepting dissimilarities, including partitioning around medoids (PAM) and hierarchical clustering. The central modeling choices are how each variable is encoded, how much influence it receives, and which comparisons are excluded.

Definition: For observations \(i\) and \(j\), Gower dissimilarity is a weighted average of variable-level dissimilarities over the variables that can be compared for that pair. Write \(d_{ijk}\) for the dissimilarity on variable \(k\), \(\delta_{ijk}=1\) when that comparison is eligible and observed (and \(0\) otherwise), and \(w_k\geq 0\) for its chosen weight. Then
$$ D_{ij} = \frac{\sum_{k=1}^{p} w_k\,\delta_{ijk}\,d_{ijk}} {\sum_{k=1}^{p} w_k\,\delta_{ijk}}, \qquad \sum_{k=1}^{p} w_k\delta_{ijk}>0. $$

With equal weights, set \(w_k=1\) for every variable. The eligibility indicator prevents a missing value or an inapplicable comparison from being treated as a match or mismatch. If no variable is comparable for a pair, its Gower dissimilarity is undefined. Each included variable-level dissimilarity is between 0 and 1, so the weighted average is also between 0 and 1.

How Each Variable Contributes

A numeric variable is scaled by its observed range. If \(x_{ik}\) and \(x_{jk}\) are the two values and \(R_k=\max(x_k)-\min(x_k)>0\), its contribution is the absolute difference divided by that range. Thus a difference of 10 has less influence on a variable spanning 1,000 units than on one spanning 20 units.

$$ d_{ijk}=\frac{|x_{ik}-x_{jk}|}{R_k} \quad\text{for a numeric variable with } R_k>0. $$

For a nominal variable, the comparison is 0 when the categories match and 1 when they differ. No ordering or numerical spacing between labels is presumed. For an ordinal variable with \(M\) ordered categories, assign ranks \(r=1,\ldots,M\), convert each rank to \((r-1)/(M-1)\), and compare the converted scores by absolute difference. This preserves the order and gives equal steps between adjacent ranks; it does not claim that the underlying real-world gaps are equal.

For a binary variable, the coding and the meaning of absence determine how it is handled. A symmetric binary variable treats both values as informative: a match contributes 0 and a mismatch contributes 1. For an asymmetric binary variable, a shared absence is not informative, so a pair with 0 for both observations is excluded for that variable. A pair with one 1 and one 0 contributes 1, while a pair with two 1s contributes 0. The choice between symmetric and asymmetric is substantive: it should reflect whether joint absence carries meaningful similarity.

Variable typeVariable-level dissimilarityKey modeling choice
NumericAbsolute difference divided by the observed rangeWhich range is appropriate for the analysis
Nominal0 for a match; 1 for a mismatchCategories are labels, not ordered values
OrdinalAbsolute difference between normalized ranksSet the category order before assigning ranks
Symmetric binary0 for a match; 1 for a mismatchBoth joint presence and joint absence matter
Asymmetric binarySame as symmetric binary except joint zeros are excludedJoint absence is not considered evidence of similarity

Deriving the Weighted Average

The formula combines contributions by adding their weighted dissimilarities and dividing by the total weight of eligible comparisons. This denominator matters: an excluded comparison must not increase the amount of evidence that the pair is similar. If every variable is observed and eligible, the formula reduces to an ordinary weighted mean. If all weights are equal, it reduces further to the arithmetic mean of the variable-level dissimilarities.

$$ \text{weighted mean} = \frac{\text{sum of weight}\times\text{contribution}} {\text{sum of weights}} = \frac{\sum_k w_k\delta_{ijk}d_{ijk}} {\sum_k w_k\delta_{ijk}}. $$

Because each included \(d_{ijk}\) lies in \([0,1]\), the numerator is a sum of nonnegative weighted values no larger than their total weight. Dividing by that positive total weight therefore gives \(0\leq D_{ij}\leq 1\). A value of 0 means that all included comparisons match exactly; a value of 1 means that all included comparisons have maximal dissimilarity. Intermediate values summarize the average discrepancy under the chosen variable definitions and weights.

Worked Example: Combining Four Predictor Types

Suppose a hypothetical insurer wants to cluster four customer profiles using age, annual premium, ordered service tier, and home region. For a particular pair, the ages are 40 and 55, and the full data set has ages from 20 to 60. Premiums are $600 and $900, with a data-set range from $400 to $1,200. The service tiers are Moderate and Very high, ordered across four levels: Low, Moderate, High, Very high. The regions differ. Use equal weights, and treat all variables as observed and eligible.

Method. Calculate each variable’s contribution on the 0-to-1 scale, then take their equal-weight mean. This avoids comparing unscaled dollar differences directly with category labels.

Age contribution. The observed range is \(60-20=40\) years. The pair differs by \(55-40=15\) years, so the contribution is \(15/40=0.375\).

Premium contribution. The range is \(1200-400=800\) dollars. The difference is \(900-600=300\) dollars, giving \(300/800=0.375\).

Service-tier contribution. Moderate has rank 2 and Very high has rank 4. With \(M=4\), their normalized scores are \((2-1)/(4-1)=1/3\) and \((4-1)/(4-1)=1\). The contribution is \(|1-1/3|=2/3\).

Region contribution. The nominal categories differ, so the contribution is 1.

The numerator is \(0.375+0.375+2/3+1=2.4167\) to four decimal places. The denominator is 4. Thus \(D_{ij}=2.4167/4=0.6042\), rounded to four decimal places. As a check, the answer is between 0 and 1, and the differing region makes a full contribution of 1 without pretending that regions have a numerical order.

Common mistake avoided. Dividing the age difference by the largest age, 60, would give \(15/60\), not the range-based contribution. Gower’s numeric scaling uses the range, \(60-20\), so that the smallest and largest observed values are separated by 1.

Weights, Missing Values, and the Choice of Range

Equal weights give each variable the same maximum direct influence, not necessarily the same practical influence. A nominal mismatch contributes 1, while a numeric pair may differ by only a small fraction of its range. If a concept is represented by several related variables, those variables may collectively receive more influence than a concept represented once. Choose weights to reflect the intended contribution of predictors, and consider whether redundant variables are effectively counting the same information more than once.

For missing values, the pairwise denominator includes only eligible, observed comparisons. This is not the same as imputing a missing value: the set of variables contributing to one pair may differ from the set contributing to another. Consequently, pairwise distances can be based on different amounts of information, and their comparability may be weakened when missingness is extensive or systematic. Inspect the missingness pattern and use a deliberate strategy rather than assuming the resulting distances are equally reliable.

The numeric range is determined by the analysis data or by a prespecified meaningful range. A range of zero means a variable is constant in the analyzed data; its numeric contribution is undefined by the range formula and it provides no separation, so it should be excluded. Ranges are also sensitive to extreme observations: a very large range can make ordinary numeric differences contribute very little. Review potential outliers and the intended analysis population before calculating distances. Changing the analysis data can change the range, and therefore can change distances even when a particular pair’s values remain the same.

Worked Example: Asymmetric Binary Data and Missingness

Consider two hypothetical service profiles compared on four predictors. Utilization is 20% for profile A and 50% for profile B, on a scale whose observed range is 0% to 100%. The nominal channel differs. An indicator for a rare event is 0 for both profiles and has been designated asymmetric. An ordered support class has four levels; A is at level 1 and B at level 3. Assign weights 2 to utilization and 1 to each other predictor. All values are observed.

Method. Calculate the four possible contributions, exclude the joint-zero asymmetric comparison, and divide by the total weight of eligible comparisons. This avoids counting the shared absence as evidence of similarity.

Utilization contributes \(|50-20|/(100-0)=0.30\). Its weighted numerator contribution is \(2(0.30)=0.60\), and its denominator contribution is 2. The differing nominal channel contributes 1, adding 1 to both numerator and denominator. The asymmetric indicator is 0 for both, so it contributes nothing and adds no denominator weight. For support class, the normalized scores are \((1-1)/3=0\) and \((3-1)/3=2/3\); the contribution is \(2/3\), adding \(2/3\) to the numerator and 1 to the denominator.

The total numerator is \(0.60+1+2/3=2.2667\). The total denominator is \(2+1+1=4\). Therefore \(D_{AB}=2.2667/4=0.5667\), rounded to four decimal places. The result is within \([0,1]\), and the asymmetric joint zero has correctly been left out.

Common mistake avoided. If the joint zero were incorrectly counted as a matching variable, the numerator would remain 2.2667 but the denominator would become 5, producing \(0.4533\). That calculation falsely lowers the dissimilarity by treating the absence of the rare event as informative similarity.

From Gower Dissimilarities to Clusters

Once all pairwise Gower dissimilarities have been calculated, store them in a symmetric dissimilarity matrix with zeroes on the diagonal. PAM can use this matrix directly: it chooses observed records as medoids and assigns records to their nearest medoid. Hierarchical methods that operate on a dissimilarity matrix can also use it. Interpret the resulting clusters in terms of the variables and weights that constructed the distances; the clustering cannot recover distinctions that the chosen representation excluded.

The matrix may not have all the geometric properties of Euclidean distance. In particular, pairwise missingness can make different distances average over different variable sets, and may undermine familiar distance properties such as the triangle inequality. Use a method that accepts the intended dissimilarity input, and do not assume that a mixed-data dissimilarity automatically supports every method designed for Euclidean coordinates.

Worked Example: Selecting a Medoid From Mixed-Data Distances

A hypothetical analysis compares three profiles, P, Q, and R, using a numeric utilization score and a nominal service channel. Their utilization values are 10, 20, and 40, respectively; the observed numeric range is 10 to 40. P and Q use channel A, while R uses channel B. Use equal weights and consider one cluster, so the medoid is the observed profile with the smallest total dissimilarity to the others.

Method. Calculate the three pairwise Gower dissimilarities and sum each profile’s distances to the other profiles. This is the one-cluster PAM objective: choose an observed profile minimizing the sum of distances.

For P and Q, the numeric contribution is \(|20-10|/30=1/3\); the channel matches, contributing 0. Thus \(D_{PQ}=(1/3+0)/2=1/6=0.1667\). For P and R, the numeric contribution is \(|40-10|/30=1\), and the channel differs, contributing 1. Thus \(D_{PR}=(1+1)/2=1\). For Q and R, the numeric contribution is \(|40-20|/30=2/3\), and the channel differs, so \(D_{QR}=(2/3+1)/2=5/6=0.8333\).

The total for P is \(D_{PQ}+D_{PR}=1/6+1=7/6=1.1667\). The total for Q is \(D_{PQ}+D_{QR}=1/6+5/6=1\). The total for R is \(D_{PR}+D_{QR}=1+5/6=11/6=1.8333\). Q has the smallest total and is the medoid. As a check, Q is closer to P than R is, while R is maximally dissimilar from P on both variables.

Common mistake avoided. A cluster center for this method must be an observed profile, not an average of the numeric value and a fabricated average channel. The medoid is the observed record minimizing total dissimilarity.

Common Traps and Practical Checks

  • Using raw numeric differences. Calculate range-scaled differences for numeric variables; otherwise units and scales can dominate.
  • Encoding nominal categories as ordered numbers. A mismatch between two labels contributes 1, regardless of the arbitrary codes assigned to them.
  • Treating every binary variable alike. Decide whether joint zeros are informative before choosing symmetric or asymmetric treatment.
  • Keeping excluded comparisons in the denominator. Missing or ineligible comparisons do not contribute weight to the pair’s average.
  • Overlooking the influence of variable count. Equal weights per column may still give a concept greater total influence if it is represented by several columns.
  • Interpreting a distance without its construction. A Gower value is conditional on the variables, ranges, weights, category orders, and eligibility rules used.

When reporting a clustering based on Gower dissimilarity, state these choices along with the clustering method. This makes the analysis reproducible and clarifies what “similar” means for the profiles. The treatment of mixed predictors and dissimilarity matrices is aligned with the SOA Exam PA (Predictive Analytics) syllabus.

Check Your Understanding

For each problem, calculate or interpret the requested quantity using the stated variable treatment.

  1. A numeric predictor has observed range 5 to 25. Two records have values 8 and 18. What is their numeric Gower contribution? (Answer: 0.5000)
  2. An ordinal predictor has five ordered levels. Two records are at levels 2 and 5. What is their normalized-rank dissimilarity? (Answer: 0.7500)
  3. Two records differ on a nominal variable, match on a numeric variable with contribution 0.20, and have a shared zero on an asymmetric binary variable. With equal weights, what is their Gower dissimilarity? (Answer: 0.6000)
  4. Two records have contributions 0.25 and 1 on two observed variables with weights 2 and 1. A third, weight-1 asymmetric variable is zero for both and is excluded. Find the weighted Gower dissimilarity. (Answer: 0.5000)
  5. For three profiles, the pairwise dissimilarities are \(D_{AB}=0.30\), \(D_{AC}=0.70\), and \(D_{BC}=0.50\). If one observed profile must be selected as the one-cluster medoid, which is it, and what is its total distance? (Answer: B, with total distance 0.80)
Clinical Biostats