Tutorials › Exam PA - Predictive Analytics › Reproducible Clustering In R

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

Reproducible Clustering In R

Build a reproducible clustering record in R, from prepared input data and algorithm settings to saved assignments and meaningful comparisons.

Intermediate 12 min read

What You'll Learn

  • Identify the data, preprocessing, distance, algorithm, and random-number details needed to repeat a clustering fit.
  • Use R seeds and repeated starts deliberately, and understand what a seed does not guarantee.
  • Preserve row identifiers and link cluster assignments back to the records they describe.
  • Distinguish a change in cluster labels from a change in the underlying partition.
  • Organize clustering objects and analysis metadata for reliable reruns and comparisons.

Why Reproducibility Matters in Clustering

Clustering is often exploratory: an analyst changes a feature set, a distance measure, or an algorithm setting and examines the result. That flexibility is useful, but it makes it easy to lose track of what produced a particular set of clusters. A saved plot or a column of cluster labels is not enough to reproduce the analysis or explain why a later run differs.

Reproducibility means being able to reconstruct the analytical choices and, where the software and computational environment permit, obtain the same result. In clustering, that requires more than a random seed. The observations, row order, selected features, preprocessing, distance definition, algorithm, tuning settings, and software environment may all matter. A reproducible record is therefore a small, organized account of the entire fit.

Definition: A clustering analysis is reproducible when its input data and analytical choices are recorded sufficiently to repeat the procedure and interpret any resulting assignments. Exact repetition of a stochastic fit additionally requires controlling its random-number sequence and using a compatible computational environment.

This is aligned with the SOA Exam PA (Predictive Analytics) syllabus. The practical emphasis is not on memorizing a particular R command; it is on identifying which choices must be fixed and recorded before cluster results can be interpreted or compared.

What to Record

A useful record separates the analysis into decisions that can otherwise become mixed together. For each fit, retain the data identity and order, the variables used, all preprocessing rules, the dissimilarity or distance choice, the clustering method and settings, and the random-number setup when relevant. Record the resulting assignments with observation identifiers, not just row numbers.

Part of the analysisWhat to preserveWhy it matters
Input dataData version or source, selected observations, row identifiers, and row orderChanging membership or order can change results, especially when initialization or tie handling depends on order
PreprocessingMissing-value rule, variables retained, transformations, and fitted centering or scaling valuesDifferent transformed inputs represent different analyses
SimilarityDistance or dissimilarity measure and any feature weightsThese choices define which observations count as close
AlgorithmMethod, requested number of clusters, initialization settings, iteration limits, and other optionsDifferent settings can produce different partitions
ComputationRandom seed, random-number generator settings when relevant, R version, and package versionsThese help explain whether a stochastic computation can be repeated exactly
ResultFit object, assignments keyed by observation ID, and a concise analysis manifestThese allow later review without guessing which labels belong to which records

Preprocessing should be recorded as an operation and as fitted values. For example, “standardize each variable” is incomplete if the means and standard deviations used are not available. If the analysis is intended for future observations, estimate those quantities on the original analysis data and apply the same saved quantities to new observations. Recomputing them on a new batch changes the transformation.

A constant feature needs explicit handling: its standard deviation is zero, so division by that standard deviation is undefined. The analyst must decide whether to remove the feature or use another defensible transformation, and record that decision. Similarly, a missing-value rule should state whether rows were omitted, values imputed, or another method used. Do not leave these choices implicit in an undocumented data-preparation step.

Seeds, Random Starts, and the Limits of Exact Repetition

Some clustering procedures use randomness, including random initialization in k-means. In R, set.seed() initializes the random-number generator so that a procedure using that generator can follow the same random sequence when the rest of the setup is unchanged. For example, set.seed(814) immediately before a k-means call makes the intended seed visible next to the fit.

A seed does not specify the clustering analysis by itself. The data and their order, the chosen variables, preprocessing, requested number of clusters, algorithm, number of starts, and software implementation still need to match. A larger number of random starts can reduce dependence on a poor initialization by allowing the method to try more starting configurations, but it does not make the analysis deterministic in the absence of controlled computation. Record the number of starts as well as the seed.

$$ \text{fit} = g(X,\theta,U) $$

Here \(X\) denotes the prepared data, \(\theta\) denotes fixed analysis choices such as the algorithm and its settings, and \(U\) denotes the random sequence used by a stochastic procedure. This notation highlights why recording only a seed is insufficient: changing \(X\) or \(\theta\) can change the fit even when the random sequence is repeated.

Justification: If the same prepared input \(X\), fixed settings \(\theta\), and random sequence \(U\) are supplied to the same deterministic implementation, then the procedure applies the same operations to the same inputs and returns the same fit. A seed can reproduce \(U\) under compatible random-number settings, but software changes, different implementations, and altered input order may change the computation. Therefore, exact repeatability is conditional on the full setup, not guaranteed by the seed alone.

In R, a practical pattern is to set the seed immediately before fitting, store the returned object, and save a manifest alongside it. For example, record the equivalent of set.seed(814), kmeans(x_scaled, centers = 3, nstart = 25, iter.max = 100), the feature names in x_scaled, and the scaling values. The values shown here are example settings, not universal recommendations.

Some procedures are deterministic for fixed inputs and settings, so a seed may have no effect. Even then, documentation matters: a repeated run on a different feature set or with a different distance is not a repetition of the same analysis. If a procedure has ties or implementation-specific behavior, preserving the data order and software versions can also help explain differences.

Worked Example: Recording a Standardization Step

A hypothetical portfolio team has three records with two numeric features: annual service events \(x\) and a second measurement \(z\). The observed rows are \((2,10)\), \((4,10)\), and \((6,10)\). The analyst wants to standardize nonconstant features using the sample standard deviation before a later clustering fit. Show what should be recorded and identify the issue with \(z\).

Method. Calculate the mean and sample standard deviation for each feature, then calculate standardized values using \(z_i=(x_i-\bar{x})/s\). The same calculations must be saved with the fit so that the transformed data can be reconstructed.

For \(x\), the mean is \(\bar{x}=(2+4+6)/3=4\). The squared deviations are \((2-4)^2=4\), \((4-4)^2=0\), and \((6-4)^2=4\). Thus the sample variance is \((4+0+4)/(3-1)=4\), and \(s_x=\sqrt{4}=2\). The standardized values are \((2-4)/2=-1\), \((4-4)/2=0\), and \((6-4)/2=1\).

For \(z\), the mean is \((10+10+10)/3=10\). Every deviation is zero, so its sample variance is \(0/(3-1)=0\) and its standard deviation is \(0\). Standardization would require dividing by zero, so it is undefined. The analyst must omit \(z\) from this standardization-based fit or use a separately justified alternative, and record which choice was made.

The reproducibility record should include the three record IDs in their order, the feature decision, \(\bar{x}=4\), \(s_x=2\), and the treatment of \(z\). The transformed \(x\) values are \(-1,0,1\). Check: their mean is \(0\), and their sample standard deviation is \(\sqrt{(1+0+1)/2}=1\), as required. The common mistake avoided is trying to standardize a constant feature or saving only the instruction “scale the data” without its fitted values.

Organizing R Results for Reuse

A robust workflow preserves both the fitted object and a simple assignment table. Keep the original observation identifier in a separate column and bind it to the cluster assignment returned by the fit. Do not rely on row position alone: filtering, sorting, or joining data later can silently attach a valid cluster label to the wrong record.

1
Freeze the analysis input.
Choose the records and features, retain their IDs, and record the row order and data version.
2
Save preprocessing choices.
Record missing-value handling, transformations, and any fitted centering, scaling, or feature-weight values.
3
Specify the fit completely.
Record the distance or dissimilarity, algorithm, requested cluster count, algorithm options, and seed when randomness is used.
4
Save fit and assignments together.
Keep the fitted R object, a table of ID and cluster assignment, and a manifest with software and analysis settings.
5
Compare partitions, not label numbers.
Check whether the groups of observations match, allowing for a permutation of cluster labels.

Cluster labels are names assigned to groups, not measurements with an inherent order. If one run assigns records A and B to cluster 1 and C and D to cluster 2, a second run assigning A and B to cluster 2 and C and D to cluster 1 has the same partition. Comparing the numeric labels row by row would incorrectly suggest that every assignment changed. Compare group membership or use a partition comparison measure that is invariant to label renaming.

Worked Example: Repeating and Comparing Two Fits

Suppose an analyst fits k-means with two clusters to four identified records A, B, C, and D. For illustration, assume a saved run with a fixed seed and settings produces assignments A:1, B:1, C:2, D:2. A repeat run with the same recorded setup gives A:1, B:1, C:2, D:2. A separate fit gives A:2, B:2, C:1, D:1. Determine which comparison is a reproducible repeat and whether the separate fit has a different partition.

Method. First compare the two same-setup assignment tables by ID. Each of A and B has label 1 in both, and each of C and D has label 2 in both; this is an exact repeat of the stored assignments. Next compare group membership without treating the numbers 1 and 2 as meaningful names. In the separate fit, A and B remain together, C and D remain together, and the two groups remain distinct. Its labels are swapped, but its partition is identical.

A quick check uses the six record pairs. The within-group pairs are A-B and C-D in both partitions. The four cross-group pairs, A-C, A-D, B-C, and B-D, are separated in both. Thus all six pairwise same-group or different-group relationships agree. The separate fit does not represent a different partition; it represents a label permutation.

In R, save each assignment alongside the ID rather than comparing the bare assignment vector after an undocumented sort. Keep the seed and settings with the fit object. Common mistake: counting every difference between label 1 and label 2 as a changed cluster. The correction is to align labels or compare the underlying membership relationships. The example is a stated hypothetical output; it does not claim that these assignments arise from a particular numerical data set.

Distance Choices Are Part of the Reproducible Result

Recording the distance is essential because distances determine what “similar” means for the selected features. A feature measured in large numerical units can dominate an unscaled Euclidean distance even if it is not more important to the analysis. Standardizing, weighting, or choosing a different dissimilarity changes the geometry and can change the resulting clusters. The method name alone does not make two analyses comparable.

Worked Example: How a Saved Scaling Choice Changes Distances

A hypothetical service study records three branches using \(x\), annual event count, and \(y\), a severity measure in thousands of dollars. Their values are A: \((1,2)\), B: \((2,4)\), and C: \((5,3)\). Compare the Euclidean distances before scaling with distances after standardizing each feature by its sample standard deviation. State why the choice belongs in the analysis record.

Unscaled distances. For A and B, the coordinate differences are \(1\) and \(2\), so \(d(A,B)=\sqrt{1^2+2^2}=\sqrt{5}\), approximately \(2.236\). For A and C, the differences are \(4\) and \(1\), so \(d(A,C)=\sqrt{4^2+1^2}=\sqrt{17}\), approximately \(4.123\). For B and C, the differences are \(3\) and \(-1\), so \(d(B,C)=\sqrt{3^2+(-1)^2}=\sqrt{10}\), approximately \(3.162\). The closest pair is A and B.

Scaling values. The mean of \(x\) is \((1+2+5)/3=8/3\). Its deviations are \(-5/3,-2/3,7/3\), with squared deviations summing to \((25+4+49)/9=78/9=26/3\). The sample variance is \((26/3)/2=13/3\), so \(s_x=\sqrt{13/3}\). The mean of \(y\) is \((2+4+3)/3=3\); its deviations are \(-1,1,0\), so the sample variance is \(2/2=1\) and \(s_y=1\).

After standardization, the distance between two observations is calculated from their original differences divided by the corresponding standard deviations. Thus \(d(A,B)^2=1^2/(13/3)+2^2/1^2=3/13+4=55/13\), giving \(d(A,B)=\sqrt{55/13}\), approximately \(2.057\). Similarly, \(d(A,C)^2=4^2/(13/3)+1^2=48/13+1=61/13\), giving approximately \(2.166\). Finally, \(d(B,C)^2=3^2/(13/3)+(-1)^2=27/13+1=40/13\), giving approximately \(1.754\). The closest pair is now B and C.

The ordering changed because standardization altered the relative contribution of the two features. The record should state that Euclidean distance was used and that each feature was divided by its sample standard deviation, along with the means and standard deviations. Check: the standardized distances are approximately \(2.057\), \(2.166\), and \(1.754\), so B and C are closest. The common mistake avoided is treating a distance choice as an incidental software default rather than a substantive part of the analysis.

Common Reproducibility Traps

  • Setting a seed but changing the data. A seed cannot compensate for changed records, features, row order, or preprocessing. Preserve the input specification as well as the seed.
  • Saving labels without IDs. A label vector is meaningful only in relation to its observation order. Store assignments keyed by stable identifiers.
  • Comparing cluster numbers literally. Cluster labels can be permuted without changing the partition. Compare membership, not label names.
  • Recording an instruction but not its fitted values. “Standardize” is not enough to recreate a transformation; retain the means, scales, and feature order.
  • Assuming a seed guarantees exact results forever. Reproducibility can depend on the R version, package version, random-number settings, and implementation. Record these when exact repetition matters.
  • Confusing repeated starts with repeated analyses. Multiple starts are settings within one fit; changing the seed, features, or distance defines a different computation or analysis that should be documented separately.
Key takeaway: A reproducible clustering result is a package of input data, preprocessing values, distance and algorithm choices, computational settings, fit object, and ID-linked assignments. A seed is useful, but only as one part of that record.

Check Your Understanding

For each problem, identify the reproducibility decision that matters most and check your reasoning against the final answer.

  1. A k-means fit uses a fixed seed and returns a vector of 80 labels. The analyst sorts the original data frame by a new variable before attaching those labels. What must be done to avoid assigning labels to the wrong records? (Answer: Preserve a stable ID with each row and join assignments to the data by ID, not by row position.)
  2. A numeric feature has observed values 7, 7, and 7. The analyst plans to standardize using its sample standard deviation. What is the standard deviation, and can the usual standardization be applied? (Answer: The sample standard deviation is 0; division by zero makes the usual standardization undefined, so the feature needs an explicit alternative treatment or omission.)
  3. Two runs assign P and Q to cluster 1 and R and S to cluster 2 in the first run, then assign P and Q to cluster 2 and R and S to cluster 1 in the second. Did the partition change? (Answer: No. The membership groups are identical and only the cluster labels were permuted.)
  4. An analyst records set.seed(52) but omits the distance choice and scaling values. Is that enough to reproduce a clustering fit? Name two missing items. (Answer: No. The distance choice and fitted preprocessing values, such as feature means and standard deviations, are missing; data, algorithm settings, and computational environment should also be recorded.)
  5. A fit uses three features with sample standard deviations 2, 1, and 4. For two observations, their raw feature differences are 2, 1, and 4. What is their Euclidean distance after each difference is divided by its feature's standard deviation? (Answer: Each standardized difference is 1, so the distance is \(\sqrt{1^2+1^2+1^2}=\sqrt{3}\), approximately 1.732.)
Clinical Biostats