DataTile Knowledge Base

Cluster Analysis: the maths behind the module

Every calculation of the cluster analysis module, with formulas: how answers become columns, principal components with varimax rotation, KMO and Bartlett's test, k-means, the silhouette and the recommendation rule, and the profile metrics.

This page is the method reference for Cluster Analysis in DataTile. It is written for analysts who need to explain, check or repeat a result. How to read the numbers is covered in Reading the results and best practice.

The calculation at a glance

  1. Respondents. Keep the respondents in the filter who have a usable answer to every selected variable and a usable weight.

  2. Columns. Turn each variable into one or more numeric columns and standardise them.

  3. Factors. Extract principal components from the correlation matrix, rotate them by varimax, and compute a factor score for every respondent.

  4. Clusters. Run k-means on the factor scores, together with the columns of the variables that enter directly.

  5. Quality. Repeat the clustering for every number of clusters from 2 to 20, score each solution and recommend a number.

  6. Profiles. Describe each cluster on the original variables.

Notation used below:

Symbol

Meaning

Respondents used

Weight of respondent i, and the sum of the weights. Without a weight, every is 1 and

Value of respondent in column

Number of columns in the factor step

Number of factors kept

Number of clusters

Weights are treated as frequency weights throughout: a respondent with weight 2 counts as two identical respondents.

1. Respondents and columns

Missing answers

Missing answers are handled listwise. A respondent is used only when every selected variable gives a usable value. With a weight, a respondent whose weight is empty, 0 or negative is left out as well. Step 1 reports the respondents in the filter and the respondents used. Both are unweighted counts.

How a variable becomes columns

Type

Columns

Value

The respondent is left out when

Numeric

1

The value

The value is empty

Single choice, scored

1

The code of the chosen category

No answer, or the chosen category is not in use or has no score

Multiple response

1 per category in use

1 when the category is chosen, else 0

None of the chosen categories is in use

Multiple response, scored

1

The mean of the scores of the chosen categories

None of the chosen categories has a score

Single choice

1 per category in use

1 when the category is chosen, else 0

No answer, or the chosen category is not in use

The first four types form the factor part. Single-choice variables without scores are direct: they skip the factors and enter the clusters as they are.

Standardisation

Every column of the factor part is standardised to mean 0 and standard deviation 1, with the weights:

A column in which every respondent has the same value cannot be standardised. It is left out of the factors, and step 2 names it.

Scaling of a direct variable

A single-choice variable with q categories in use would weigh more the more categories it has. To prevent this, its columns are scaled so that the whole variable has a total variance of 1, the same as one factor. For category c with (weighted) share and indicator

The column of category c then has variance , and the variances of the variable's columns add up to 1.

2. Factors

The factor step is a principal component analysis of the correlation matrix, followed by a varimax rotation with Kaiser normalization. It is the method SPSS runs with FACTOR /EXTRACTION PC /ROTATION VARIMAX.

Correlation matrix

With Z the matrix of standardised columns, the Pearson correlation matrix is

Eigenvalues and the suggested number of factors

R is decomposed into eigenvalues and eigenvectors:

An eigenvalue is the variance a factor explains, counted in variables. The share of variance of factor j is . The scree plot draws the eigenvalues in order.

The suggested number of factors is the number of eigenvalues above 1 (the Kaiser criterion). You may keep any number from 1 up to the upper end of the range shown beside the field. The kept factors together explain

In the example of the guide p = 12 and the first three eigenvalues are 3.251, 1.249 and 1.131, so three factors explain 5.631 / 12 = 46.9%.

Loadings before rotation

holds the first m eigenvectors. A loading is the correlation between variable i and factor j.

Varimax rotation

The rotation turns the m factors, keeping them uncorrelated, so that each variable loads high on few factors and each factor has few high loadings. This makes the factors easier to name. It does not change how much variance the m factors explain together, only how that variance is shared between them.

  1. Kaiser normalization. Each row of A is divided by its length, so that variables with a low communality count as much as the others: with

  2. Criterion. Find the orthogonal matrix T that maximises the variance of the squared loadings within each factor, summed over the factors:

  3. Algorithm. Kaiser's method: each pair of factors is turned in its plane by the angle that maximises the criterion there, and the pairs are swept repeatedly until no pair turns by more than 10⁻⁸ radians.

  4. Rotated loadings. These are the values of the table "Rotated factor loadings".

  5. Orientation. The sign of a factor is arbitrary. Each factor is signed so that its largest loading is positive.

After the rotation the variance of factor is the sum of its squared loadings, , shown as "Rotated sum of squared loadings", and its share is that sum divided by p. In the example the three rotated factors hold 19.8%, 17.3% and 9.8%, which again add up to 46.9%.

If the rotation does not settle, the step reports that it did not converge and asks for fewer factors or fewer variables.

Factor scores

Every respondent gets a score on every kept factor:

For principal components this equals the regression method, , and it needs no inverse of R. Each factor score has a mean of 0 and a standard deviation of 1 over the respondents used, and the scores of different factors are uncorrelated. A score of 1 is one standard deviation above the sample mean of the factor.

KMO

The Kaiser-Meyer-Olkin measure compares the correlations with the partial correlations:

is the correlation between variables i and j once the other variables are held constant. When the variables share common factors, the partial correlations are small and KMO approaches 1. The module asks for 0.5 or more.

Bartlett's test of sphericity

The test asks whether R could be an identity matrix, that is, whether the variables are unrelated:

is the sum of the logarithms of the eigenvalues. N is the number of respondents used or, with a weight, the sum of their weights. Sig is the upper tail of the distribution. The module asks for a value below 0.05. In the example with 66 degrees of freedom.

When the tests cannot be computed

KMO and Bartlett's test need the inverse and the determinant of R. When some columns are determined by others, R is not positive definite (its smallest eigenvalue is 0), and neither exists. The factors and the scores are still computed, and step 2 says that the two tests are not available.

Special cases

  • Only direct variables. There is no factor part and no factor step.

  • No eigenvalue above 1. The variables are not correlated enough for a common factor. Without direct variables, the step stops and asks for related variables. Besides direct variables, the standardised columns of the factor part enter the clusters directly instead.

  • Fewer than two columns that vary. Factors need at least two. The same two outcomes apply.

3. Clusters

Input

The input of the clustering has one row per respondent used. Its columns are the m factor scores, followed by the scaled columns of the direct variables. Every factor has a variance of 1 and every direct variable a total variance of 1, so each counts equally.

K-means

For a number of clusters k, k-means looks for the partition that minimises the within-cluster sum of squares:

is the centre of cluster c, its weighted size, and distance is Euclidean.

  1. Start. The first centres are chosen by k-means++: the first at random, each next one with a probability proportional to its squared distance from the nearest centre already chosen. With a weight the probabilities are multiplied by the weights.

  2. Iterate. Assign every respondent to the nearest centre, move each centre to the mean of its respondents, and repeat until nothing changes, at most 300 times.

  3. Restart. The whole run is repeated from 100 different starts. The solution with the lowest WCSS is kept.

K-means can end in a local optimum, which is why it is restarted. The random draws come from a generator with a fixed seed stored with the analysis, so the same analysis on the same data returns the same clusters on every run. The seed, the number of restarts and the iteration limit are listed on the Settings sheet of the report.

Clusters are numbered by size: Cluster 1 is the largest. K-means needs more respondents than clusters.

The clusters do not depend on the rotation or on the signs of the factors: turning the factor axes changes no distance between respondents.

The clustering is computed for every k from 2 to 20 (fewer when there are fewer respondents), and each solution gets three measures.

Silhouette

For respondent i, let a(i) be the mean distance to the other members of its own cluster, and b(i) the smallest mean distance to the members of any other cluster:

s(i) is near 1 when the respondent sits well inside its cluster, near 0 on the border between two, and negative when another cluster is closer. A respondent alone in its cluster scores 0. The silhouette of a solution is the mean of s(i).

The silhouette is computed on a sample of up to 1,000 respondents, drawn once with the seed of the analysis and used for every k, so the numbers are comparable. With a weight, the mean distances and the final mean are weighted.

Explained variance

μ is the centre of all respondents. This is the share of the total variance that lies between the clusters. It grows with every added cluster.

Smallest cluster

The recommendation

  1. A number is eligible when its smallest cluster holds at least 5% of the respondents.

  2. Among the eligible numbers, take the one with the highest silhouette. On a tie, take the smaller number.

  3. It is recommended when its silhouette is at least 0.25. Otherwise no number is recommended, and the step says that the data shows no clear cluster structure.

Until you choose a number yourself, the analysis uses the number from the second point, recommended or not, or 2 when no number is eligible.

In the example, 8 to 20 clusters all score a higher silhouette than 7 (up to 0.47), but each has a cluster under 5%. Among 2 to 7, the highest silhouette is 0.38 at 7 clusters, which is above 0.25, so 7 is recommended.

The threshold of 0.25 follows Kaufman and Rousseeuw's reading of the silhouette. With only two or three dimensions, data without real groups can still score above it, which is why the step states this beside the number.

5. Profile metrics

The profile table describes the clusters on the original columns: the value of a numeric variable, the score of a scored variable, and for each category a 0/1 column. All metrics use the respondents in the analysis and their weights. is the mean in cluster c and the mean in the total.

Metric

Formula

Notes

Average

For a category this is the share who chose it, shown as a percent

Standard deviation

The sample standard deviation. "-" when the cluster has a weighted size of 1 or less.

Standardized score

0 is the total, 1 is one standard deviation above it

Index

100 is the total. "-" when the variable has a negative value or a total mean of 0.

(eta squared)

The share of the variable's variance that lies between the clusters, 0% to 100%. Rank orders the rows by it.

Cells are shaded on the value as displayed: under Index at 150 and 120 or more and at 80 and 67 or less, under Standardised score at 1 and 0.5 or more and at −0.5 and −1 or less.

Cluster sizes are weighted sizes, and the shares are rounded so that they add up to 100%.

6. The map and the cluster centres

The map plots the factor scores of the respondents on two factors. The centre of a cluster on a factor is the weighted mean score of its respondents. Since every factor has mean 0 and standard deviation 1, a centre of +1 lies one standard deviation above the sample mean. In the table of centres, the bold value in a row is the factor on which the cluster lies furthest from 0.

The map warns when every cluster centre lies within 0.25 of 0 on every factor: the clusters then differ on the direct variables, not on the factors.

7. The saved variable

Saving writes a single-choice variable with one category per cluster. A respondent belongs to the category of their cluster. Respondents who were left out of the analysis are in no category. The variable is a snapshot: when the clusters or the data change, it is marked out of date until it is updated.

Repeating a result elsewhere

The Settings sheet of the report lists what is needed: filter, weight, variables with their excluded categories, numbers of factors and clusters, and the seed.

  • Factors. Any principal component analysis of the correlation matrix with varimax rotation and Kaiser normalization gives the same loadings and scores. The order and the signs of the factors may differ between programs.

  • Clusters. K-means with k-means++ starts and many restarts on the factor scores finds the same or a nearly identical solution. Small differences in cluster sizes are possible, because other software draws different starting points.

  • Silhouette. Computed on all respondents instead of a sample of 1,000, it differs slightly.

References

  • Kaiser, H. F. (1958). The varimax criterion for analytic rotation in factor analysis. Psychometrika, 23, 187–200.

  • Kaiser, H. F. (1960). The application of electronic computers to factor analysis. Educational and Psychological Measurement, 20, 141–151.

  • Kaiser, H. F. (1974). An index of factorial simplicity. Psychometrika, 39, 31–36.

  • Bartlett, M. S. (1954). A note on the multiplying factors for various χ² approximations. Journal of the Royal Statistical Society, Series B, 16, 296–298.

  • Lloyd, S. P. (1982). Least squares quantization in PCM. IEEE Transactions on Information Theory, 28, 129–137.

  • Arthur, D., and Vassilvitskii, S. (2007). k-means++: the advantages of careful seeding. Proceedings of the 18th ACM-SIAM Symposium on Discrete Algorithms, 1027–1035.

  • Rousseeuw, P. J. (1987). Silhouettes: a graphical aid to the interpretation and validation of cluster analysis. Journal of Computational and Applied Mathematics, 20, 53–65.

  • Kaufman, L., and Rousseeuw, P. J. (1990). Finding Groups in Data: An Introduction to Cluster Analysis. Wiley.