From Selected Predictors to Cluster Profiles
A k-means result is useful only if the entire path from input data to interpretation is sound. The analysis begins with a deliberate selection of numeric predictors, not every column in a data frame. Those predictors are assembled into a matrix, checked and usually standardized, then used to fit k-means for candidate values of \(k\). The final step is to describe the fitted groups in units a reader can understand.
This workflow keeps two purposes separate. The matrix supplied to the algorithm determines distances and cluster assignments. A profile table describes what those assigned observations look like, preferably using the original units of the selected variables. A standardized centroid can help explain the geometry of a fit, but it is not a substitute for a substantive profile.
Prepare the Analysis Matrix
Each row of the matrix must represent one observation, and each column must represent one selected numeric predictor. Keep identifiers out of the matrix: a policy number or customer code is a label, not a meaningful distance dimension. Likewise, do not pass a character or factor column to k-means as though its encoded values had numeric distance meaning.
Before fitting, decide how incomplete records are handled. The code below uses complete cases for the selected predictors and then keeps the corresponding rows together. This is a choice, not a universal missing-data remedy: if excluding incomplete records would distort the target population, the missing-data strategy needs separate consideration. Also check for a predictor with zero standard deviation. It cannot be standardized by dividing by its standard deviation and contributes no distance variation.
R’s scale function centers each column and, by default, divides by its sample standard deviation. For predictor \(j\), the transformed value is
\(z_{ij}=(x_{ij}-\bar{x}_j)/s_j\), where \(\bar{x}_j\) is the sample mean and \(s_j\) is the sample standard deviation. After this transformation, each nonconstant column has mean zero and sample variance one.
This matters because k-means uses squared Euclidean distance. Without scaling, a predictor measured in large numerical units can dominate a predictor measured on a smaller scale, even when the smaller-scale variable is substantively important. Standardization gives the selected columns comparable marginal scales; it does not remove correlation or guarantee that every predictor deserves equal substantive importance.
Worked Example: Standardizing Two Predictors Before Fitting
A hypothetical analyst selects two numeric service measures for four offices: a monthly event count and average resolution time. The observed pairs are \((0,0)\), \((0,2)\), \((10,10)\), and \((10,12)\). Standardize both columns using the sample standard deviation, then calculate the WSS for the natural two-group assignment: the first two offices together and the last two together.
Method. Compute each column’s mean and sample standard deviation, transform the values, and calculate squared distances to the two standardized cluster means. This avoids comparing raw Euclidean distances when the predictors have different ranges.
For the event count, the mean is \((0+0+10+10)/4=5\). The sum of squared deviations is \(25+25+25+25=100\), so the sample variance is \(100/(4-1)=100/3\), and the sample standard deviation is \(10/\sqrt{3}\), approximately \(5.7735\). The standardized values are \(-5/(10/\sqrt{3})=-0.8660\), \(-0.8660\), \(0.8660\), and \(0.8660\).
For resolution time, the mean is \((0+2+10+12)/4=6\). The deviations are \(-6,-4,4,6\); their squares sum to \(36+16+16+36=104\). The sample variance is \(104/3\), and the sample standard deviation is \(\sqrt{104/3}\), approximately \(5.8878\). The standardized values are approximately \(-1.0191,-0.6794,0.6794,1.0191\).
The first cluster’s standardized centroid is \((-0.8660,(-1.0191-0.6794)/2)\), approximately \((-0.8660,-0.8492)\). The second centroid is approximately \((0.8660,0.8492)\). Within either pair, the first coordinate has no deviation from its cluster mean. The two resolution-time coordinates differ by \(2/\sqrt{104/3}\), so each is half that distance from the pair mean. Each pair’s WSS is therefore \(2(1/\sqrt{104/3})^2=2/(104/3)=3/52\), and the total WSS is \(2(3/52)=3/26\), approximately \(0.1154\). The check is that both clusters have equal within-pair variation, so the total is twice the contribution of one pair.
A typical R preparation for these selected variables is:
In code, use a deliberate column selection and verify that the chosen columns are numeric and nonconstant. The example’s standard deviations are both positive, so scaling is defined. A common mistake would be to compute distances from the raw values and let the larger numerical range decide the influence of each predictor.
Fit Candidate Values of \(k\) in R
In base R, kmeans returns assignments, cluster centers, and within-cluster sums of squares. The following pattern makes the data preparation and repeated fitting explicit. Replace the example column names with the selected predictors in the analysis data.
The expression is the total WSS reported by a fitted solution: each cluster contributes its sum of squared distances to its own center. A reproducible random seed makes the random initialization repeatable in a fixed R environment; multiple starts reduce dependence on one starting configuration but do not prove that the global minimum has been found.
Worked Example: Fitting and Comparing Candidate Cluster Counts
Suppose a prepared, standardized one-predictor matrix contains the values \(0,1,5,6\). Fit candidate values \(k=1,2,3,4\), and determine the minimum WSS for each. This small example makes the comparison transparent; a practical analysis would generally use several predictors.
Method. Use the cluster means to calculate WSS and compare the best partitions. The common mistake is to assume that a lower WSS by itself establishes that a larger \(k\) is preferable.
For \(k=1\), the mean is \((0+1+5+6)/4=3\). The WSS is \((0-3)^2+(1-3)^2+(5-3)^2+(6-3)^2=9+4+4+9=26\).
For \(k=2\), the split into \(\{0,1\}\) and \(\{5,6\}\) has means \(0.5\) and \(5.5\). Each pair contributes \((0.5)^2+(0.5)^2=0.5\), so the total is \(1\). To check it is the best two-cluster split, the other contiguous splits are \(\{0\},\{1,5,6\}\) and \(\{0,1,5\},\{6\}\). The three-point cluster in either alternative has mean \(4\) or \(2\), respectively, and WSS \(14\); each alternative therefore has total WSS \(14\), not less than \(1\). For one-dimensional squared-distance clustering, an optimal partition can be taken as contiguous groups in sorted order, so these are the possible split points.
For \(k=3\), one pair and two singletons give the lowest WSS by pairing adjacent values. The pair \(\{0,1\}\) has mean \(0.5\) and WSS \(0.5\); the two singleton clusters each contribute zero. Pairing \(\{5,6\}\) instead also gives \(0.5\). Thus the minimum is \(0.5\). For \(k=4\), every observation can be its own cluster, giving WSS zero.
The resulting minimum WSS values are \(26,1,0.5,0\) for \(k=1,2,3,4\), respectively. The decrease is expected: allowing more clusters cannot increase the minimum objective, since an existing cluster can be split without forcing a higher minimum WSS. But WSS will keep falling as \(k\) grows, reaching zero when every distinct observation has its own cluster. The numerical decrease alone is not a decision rule.
A practical R loop can retain the full fit for each candidate \(k\), then extract its total WSS for comparison:
Before this line, set a seed, for example set.seed(2026), and confirm that each proposed \(k\) is feasible for the number of retained observations and distinct rows. A typical preparation is:
In R, write these as ordinary code lines in a script; the displayed sequence highlights the order of operations. Then define, for example, ks <- 1:5, fit each value, and collect vapply(fits, function(f) f$tot.withinss, numeric(1)). Check that the retained rows, selected columns, and candidate cluster counts match the analysis plan. If observed WSS values are not decreasing across increasing \(k\), remember that these are algorithmic solutions from finite starts: the theoretical minimum is nonincreasing, but a heuristic fit may not attain each minimum.
Why the Cluster Mean Is the Center
K-means uses arithmetic means because they minimize the sum of squared distances within a cluster. Let the observations in one cluster be vectors \(z_1,\ldots,z_m\), and let \(\bar{z}\) be their coordinate-wise mean. For any proposed center \(c\), write \(z_i-c=(z_i-\bar{z})+(\bar{z}-c)\), square each Euclidean norm, and sum over observations. The cross term vanishes because \(\sum_i(z_i-\bar{z})=0\). Thus:
The second term is nonnegative and equals zero when \(c=\bar{z}\). Therefore the mean is the center that minimizes within-cluster squared distance. This result justifies both the center used by k-means and the calculation of WSS around that center.
Turn Assignments into Profiles
After selecting a candidate fit, inspect the size and practical meaning of each cluster. Cluster numbers are arbitrary labels: “cluster 1” is not inherently smaller, safer, or more important than “cluster 2.” For interpretation, calculate the means of the original variables for each fitted group and report the number of observations in each group. If the fit used standardized values, do not label standardized centers as though they were original units.
Worked Example: Reporting Profiles in Original Units
A hypothetical analyst clusters six service locations using monthly events per 1,000 accounts and average resolution days. A chosen \(k=2\) fit assigns locations A, B, and C to one cluster, and D, E, and F to the other. Their original measurements are A \((2,3)\), B \((4,5)\), C \((3,4)\), D \((8,9)\), E \((10,11)\), and F \((9,10)\). Compute and interpret the profiles.
Method. Group by the fitted assignment, calculate each predictor’s arithmetic mean, and report the group size. The common mistake is to present a cluster number as a meaningful rank or to report only standardized centers without translating back to the original measures.
For the first group, the event-rate mean is \((2+4+3)/3=3\) events per 1,000 accounts. Its resolution-time mean is \((3+5+4)/3=4\) days. The group contains three locations.
For the second group, the event-rate mean is \((8+10+9)/3=9\) events per 1,000 accounts. Its resolution-time mean is \((9+11+10)/3=10\) days. This group also contains three locations.
The profile is therefore: one group averages 3 events per 1,000 accounts and 4 days; the other averages 9 events per 1,000 accounts and 10 days. The check is that each reported mean lies between the corresponding observed minimum and maximum in its group. This describes the observed groups; it does not establish that cluster membership causes either measure to change.
In R, keep the rows in X aligned with the assignments from the selected fit. Then aggregate(as.data.frame(X), list(cluster=fit$cluster), mean) calculates original-unit means, and table(fit$cluster) gives cluster sizes. If the fitted assignments came from Z, they still identify the same rows as X; the profile calculation deliberately uses X, not Z.
Common Traps in the Workflow
- Scaling every column blindly. Select meaningful numeric predictors first. Identifiers and numeric category codes can create artificial distances.
- Ignoring scale. If one predictor’s numerical spread is much larger, it can dominate squared Euclidean distance. Standardization is a modeling choice, not a universal requirement; explain it and use a consistent transformation.
- Treating one initialization as definitive. K-means can converge to a local solution. Multiple starts improve the search but do not guarantee a global optimum.
- Choosing \(k\) by the smallest WSS. WSS cannot increase at the global minimum when clusters are added, so its raw minimum favors more clusters. Consider the practical purpose and other relevant evidence, not WSS alone.
- Interpreting cluster IDs as ordered categories. Cluster labels can be permuted without changing the partition. Describe groups by their profiles and sizes.
- Reporting standardized means as original measurements. A value in standard-deviation units is not a rate, count, or number of days. Compute a separate profile from the original selected predictors.
Check Your Understanding
Work through each problem and use the final answer to check the key calculation.
- A portfolio analyst selects two numeric predictors with sample standard deviations 4 and 20. An observation is 2 units above the first predictor’s mean and 10 units above the second predictor’s mean. After standardization, what are its two coordinates, and what is its squared distance from the origin? (Answer: \((0.5,0.5)\); squared distance \(0.5\).)
- A one-dimensional standardized data set is \(1,3,9,11\). For \(k=1\), calculate its WSS. For \(k=2\), use the split \(\{1,3\},\{9,11\}\) and calculate WSS. (Answer: \(k=1\) WSS \(=68\); \(k=2\) WSS \(=4\).)
- A fitted cluster contains three offices with event rates \(4,7,7\) per 1,000 accounts and mean resolution times \(2,5,8\) days. Give the cluster’s size and original-unit mean profile. (Answer: size \(3\); mean event rate \(6\) per 1,000 accounts and mean resolution time \(5\) days.)
- A candidate analysis has 120 complete observations and five selected predictors. One predictor has sample standard deviation zero. Explain whether standardization by the sample standard deviation is defined for that column, and give the appropriate workflow action before fitting. (Answer: it is undefined because the denominator is zero; remove the constant predictor from the distance matrix, then scale the remaining selected predictors.)
- Two candidate fits have total WSS 42 for \(k=3\) and 31 for \(k=4\). Is the \(k=4\) fit automatically preferable? State what the WSS comparison does establish and one additional consideration. (Answer: no; it establishes that the observed \(k=4\) fit has lower WSS, but does not alone establish preferable complexity; consider whether the added cluster yields useful, interpretable separation for the analysis purpose.)