Last week I asked: Compare clusters quality (internal validity) after and before dimensionality reduction by PCA

I've been trying to calculate internal validity for clusters after PCA using the iris example, but I couldn't.

So far I have this:

iris.scaled <- scale(iris[, -5])
# k-means group number of each observation
km.res <- eclust(iris.scaled, "kmeans", k = 3, nstart = 2, graph = FALSE)
# Visualize k-means clusters
fviz_cluster(km.res, geom = "point", frame.type = "norm")

# Compute pairwise-distance matrix
dd <- dist(iris.scaled, method ="euclidean")
# Statistics for k-means clustering
km_stats <- cluster.stats(dd,  km.res$cluster)

And I get the following result:

        1         2         3 
0.6363162 0.3473922 0.3933772 

[1] 0.4599482

In order to try to use cluster.stats after PCA I run this:


iris.pca <- PCA(iris.scaled, graph = FALSE)
iris.hc <- HCPC(iris.pca)

#Here are the individuals and the dimensions

#here are the clusters

dis <- dist(iris.pca$ind$contrib, method ="euclidean")

#I need clusters to be numeric
p<- as.numeric(iris.hc$data.clust[,5])

km_stats <- cluster.stats(dis, p)

But I get this result:

          1           2           3 
 0.39043588 -0.01734689 -0.15697508 

[1] 0.07483054
  • 1
    $\begingroup$ Do you have a specific question? What is wrong with these results? $\endgroup$
    – OTStats
    Aug 7, 2019 at 13:56
  • $\begingroup$ The result is wrong, the silhouette of the clusters after PCA should be higher, in one of the answers I got (in the link at the begining of my question) I was told that silhouette was higher after PCA using the iris dataset.. and it makes sense, clusters at least "look" better after PCA $\endgroup$
    – Ana
    Aug 7, 2019 at 14:15

2 Answers 2


You didn't run k-means on the output of PCA.

PCA substantially changes the weights of features. It distorts the data heavily. Also, $ind$contrib sounds wrong to me. You don't want to project to contributions.

Hence, a result computed on the original data, but evaluated after PCA is expected to score quite badly.


I think this one works:

distanc <- dist(iris.pca$ind$coord[,1:2], method="euclidean" ) #distances of the first two components
iris.hc$data.clust[,5]  #here are the clusters
clusters<- as.numeric(iris.hc$data.clust[,5])
km_stats <- cluster.stats(distanc, clusters)

Now I have:

        1         2         3 
0.6540650 0.4418605 0.4309233 

[1] 0.509168

I'm using the coords of the individuals, is this ok?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.