Purpose

This workflow develops and evaluates descriptive student profiles using k-means clustering, hierarchical clustering, and k-medoids. It emphasizes decisions that older cluster tutorials often hide: variable selection, missingness, scaling, number of clusters, initialization, stability, profile naming, and external validation.

The included data are entirely synthetic. Clusters are algorithmic summaries, not naturally occurring types. Their meaning and usefulness must be supported by stability, replication, external evidence, and subject-matter judgment.

Questions to answer before clustering

  • Why are these variables appropriate for defining similarity?
  • Are any variables redundant, incomparable, or dominated by outliers?
  • How will missing values and scaling be handled?
  • What evidence will guide the number of clusters?
  • Can the solution be reproduced across starts, samples, or methods?
  • Is there an external variable that was not used to create the clusters?

User settings

Simulate and import the example

Rows: 520
Columns: 9
$ student_id      <chr> "C001", "C002", "C003", "C004", "C005", "C006", "C007"~
$ true_profile    <dbl> 1, 1, 2, 3, 3, 3, 4, 4, 1, 3, 4, 3, 1, 3, 3, 3, 2, 1, ~
$ study_hours     <dbl> 15.946442, 16.102213, 10.908850, 11.465247, 13.750164,~
$ sleep_hours     <dbl> 7.756082, 8.106259, 3.670323, 4.377750, 7.364323, 5.03~
$ academic_stress <dbl> 35.24278, 46.82493, 62.95744, 59.87518, 56.50946, 49.7~
$ engagement      <dbl> 83.87823, 83.00668, 48.44207, 69.26914, 68.62040, 65.9~
$ advisor_support <dbl> 81.02638, 73.92431, 38.72225, 53.01090, 50.25814, 60.6~
$ baseline_gpa    <dbl> 3.26, 2.93, 3.42, 2.86, 2.99, 3.64, 3.46, 3.28, 2.93, ~
$ persisted       <dbl> 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, ~

Data preparation

Missingness and analysis sample

stage n
Imported records 520
Complete cases for clustering 485
Excluded for missing clustering variables 35
variable missing_n missing_percent
student_id 0 0.0
true_profile 0 0.0
study_hours 7 1.3
sleep_hours 7 1.3
academic_stress 7 1.3
engagement 7 1.3
advisor_support 7 1.3
baseline_gpa 0 0.0
persisted 0 0.0

Complete-case analysis is used to make the example transparent. In applied work, examine the missingness process and consider a defensible imputation strategy. Do not impute cluster labels.

Scaling and correlation

study_hours sleep_hours academic_stress engagement advisor_support
study_hours 1.00 0.01 -0.04 0.68 0.36
sleep_hours 0.01 1.00 -0.31 0.11 0.29
academic_stress -0.04 -0.31 1.00 -0.31 -0.70
engagement 0.68 0.11 -0.31 1.00 0.54
advisor_support 0.36 0.29 -0.70 0.54 1.00

Standardization gives each selected variable equal variance. That is a substantive weighting decision, not a neutral default. Strongly redundant variables can still receive excessive combined weight.

Scaled distributions

Choose the number of clusters

k total_within_ss average_silhouette minimum_cluster_size
2 1616.642 0.315 132
3 1108.287 0.363 108
4 867.221 0.343 107
5 778.704 0.317 57
6 709.782 0.298 56
7 641.954 0.278 50

No index can discover a uniquely correct number of clusters. This worked example uses four clusters because the solution is interpretable, adequately separated, and matches the simulation design; real research cannot appeal to a hidden true label.

Final k-means solution

chosen_k average_silhouette smallest_cluster largest_cluster between_to_total_ss
4 0.343 107 132 0.642

Profile and name clusters

cluster study_hours sleep_hours academic_stress engagement advisor_support n
1 15.03 7.43 43.75 77.01 76.13 132
2 6.99 7.59 35.32 56.10 68.17 107
3 8.87 6.18 71.95 47.68 44.01 119
4 12.97 6.40 59.10 69.97 56.97 127

Visualize separation with principal components

Compare clustering methods

method average_silhouette smallest_cluster
K-means 0.343 107
Ward hierarchical 0.335 107
K-medoids 0.338 109

Different methods need not return the same memberships because they optimize different criteria. Material disagreement should temper claims that the profiles are robust.

Resampling stability

The following nonparametric bootstrap repeatedly resamples students, refits the chosen k-means solution, assigns every original record to the nearest bootstrap center, and calculates the best Jaccard overlap for each reference cluster. This directly evaluates membership reproducibility under case resampling. It does not prove that the retained solution is true or transportable to another population.

reference_cluster mean_jaccard median_jaccard lower_10 successful_replications interpretation
1 0.986 0.985 0.977 100 highly stable
2 0.999 1.000 0.999 100 highly stable
3 0.995 1.000 0.975 100 highly stable
4 0.980 0.984 0.962 100 highly stable

External validation

Persistence was not used to create the clusters, so it can provide a limited external description. It does not validate the clusters as causal entities.

cluster n persisted_n persistence_rate
1 132 53 0.402
2 107 29 0.271
3 119 14 0.118
4 127 35 0.276
term estimate std.error statistic p.value conf.low conf.high
(Intercept) 0.353 1.057 -0.984 0.325 0.044 2.785
cluster2 0.557 0.281 -2.085 0.037 0.318 0.960
cluster3 0.198 0.336 -4.830 0.000 0.099 0.373
cluster4 0.569 0.267 -2.113 0.035 0.336 0.957
baseline_gpa 1.212 0.313 0.616 0.538 0.657 2.246

Avoid significance tests that merely compare clusters on the same variables used to define them; those differences are built into the algorithm.

Dynamic results template

K-means clustering was conducted on 485 complete synthetic records using 5 standardized variables. Candidate solutions from 2 to 7 clusters were reviewed using within-cluster sum of squares, average silhouette width, cluster size, interpretability, and method comparisons. The largest average silhouette among the candidates occurred at k = 3 (0.36); a 4-cluster solution was retained for the worked example.

The retained solution had an average silhouette width of 0.34 and explained 64.2% of the standardized total sum of squares. Cluster sizes were 132, 107, 119, 127. Mean bootstrap Jaccard overlap ranged from 0.98 to 1 across reference clusters; cluster-specific stability should be reported rather than replaced by a single label. Profile descriptions should be based on the standardized pattern rather than a single defining variable. Persistence rates, treated as external descriptions, ranged from 11.8% to 40.2% across clusters. These profiles require independent replication and should not be treated as fixed student types.

Reporting checklist

  • Justify the variables, transformations, scaling, distance, and clustering algorithm.
  • Report missing-data handling, exclusions, candidate solutions, and selection evidence.
  • State the number of random starts and random seed.
  • Provide cluster sizes, standardized profiles, separation metrics, and visualizations.
  • Examine stability across starts, samples, and plausible methods.
  • Use external variables not included in clustering when assessing usefulness.
  • Avoid deterministic labels, causal claims, and significance tests that restate the clustering inputs.

Exports

file
synthetic_data_with_clusters.csv
cluster_selection_metrics.csv
cluster_profiles_raw.csv
cluster_profiles_standardized.csv
method_comparison.csv
bootstrap_stability_replications.csv
bootstrap_stability_summary.csv
external_validation_summary.csv

References

  • Everitt, B. S., Landau, S., Leese, M., & Stahl, D. (2011). Cluster Analysis (5th ed.). Wiley.
  • Hennig, C., Meila, M., Murtagh, F., & Rocci, R. (Eds.). (2016). Handbook of Cluster Analysis. CRC Press.
Paid version: The download includes all editable R code, the synthetic-data generator, documented outputs, and complete software-version details.