Summarize variable variations across clusters (k-means) using R
I have a df that I got after implementing k-value clustering in my original dataset. Here I have 4 different clusters and what I would like to know is how much the 4 variables (V1 to V4) vary in each cluster. In other words, what variation of these 4 variables causes cluster separation.
fit <- kmeans(df, 4, iter.max=1000, nstart=25)
palette(alpha(brewer.pal(9,'Set1'), 0.5))
plot(df, col=fit$clust, pch=16)
aggregate(df, by=list(fit$cluster), FUN=mean)
clust.out <- fit$cluster
df1 <- data.frame(df, fit$cluster)
Here is my df1 after k-means
+-------+-------+-------+--------+--------+-------------+
| ID | V1 | V2 | V3 | V4 | fit.cluster |
+-------+-------+-------+--------+--------+-------------+
| DJ123 | 0.5 | 0.7 | -0.4 | -0.1 | 1 |
| DJ123 | 0.46 | 0.68 | -0.39 | -0.09 | 1 |
| DJ123 | 0.77 | 0.9 | -0.4 | -0.4 | 2 |
| DJ123 | 11.23 | 11.11 | -11.21 | -11.21 | 4 |
| DJ123 | 1.5 | 1.7 | -1.4 | -5.1 | 3 |
| DJ123 | 0.76 | 0.9 | -0.4 | -0.4 | 2 |
| DJ123 | 1.5 | 2.7 | -1.4 | -4.1 | 3 |
+-------+-------+-------+--------+--------+-------------+
Could you please provide some sample code to get summary statistics on clusters? Hope my question was clear.
+3
source to share
1 answer
You can use ddply
from plyr
to make it easy.
library(plyr)
ddply(df,.(cluster),summarise,variance1 = var(V1),variance2 = var(V2),mean1 = mean(V1),...)
You can also do this,
ddply(df,.(cluster),function(x){
res = c(as.numeric(colwise(var)(x)),as.numeric(colwise(mean)(x)))
names(res) = paste0(rep(c('Var','Mean'),each = 4),rep(1:4,2))
res
})
+3
source to share