Jika pertanyaan Anda adalah how can I determine how many clusters are appropriate for a kmeans analysis of my data?, maka berikut adalah beberapa opsi. The artikel wikipedia pada penentuan jumlah cluster memiliki review yang baik tentang beberapa metode ini.
Pertama, beberapa data yang dapat direproduksi (data dalam Q itu ... tidak jelas bagi saya):
n = 100
g = 6
set.seed(g)
d <- data.frame(x = unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))),
y = unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))))
plot(d)

Satu . Cari tikungan atau siku dalam jumlah plot scree jumlah kuadrat (SSE). Lihat http://www.statmethods.net/advstats/cluster.html & http://www.mattpeeples.net/kmeans.html untuk informasi lebih lanjut. Lokasi siku di plot yang dihasilkan menunjukkan jumlah cluster yang cocok untuk kmeans:
mydata <- d
wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var))
for (i in 2:15) wss[i] <- sum(kmeans(mydata,
centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")
Kami dapat menyimpulkan bahwa 4 cluster akan ditunjukkan dengan metode ini:

Dua . Anda dapat melakukan partisi di sekitar medoid untuk memperkirakan jumlah cluster menggunakan pamkfungsi dalam paket fpc.
library(fpc)
pamk.best <- pamk(d)
cat("number of clusters estimated by optimum average silhouette width:", pamk.best$nc, "\n")
plot(pam(d, pamk.best$nc))

# we could also do:
library(fpc)
asw <- numeric(20)
for (k in 2:20)
asw[[k]] <- pam(d, k) $ silinfo $ avg.width
k.best <- which.max(asw)
cat("silhouette-optimal number of clusters:", k.best, "\n")
# still 4
Tiga . Kriteria Calinsky: Pendekatan lain untuk mendiagnosis berapa banyak cluster yang sesuai dengan data. Dalam hal ini kami mencoba 1 hingga 10 grup.
require(vegan)
fit <- cascadeKM(scale(d, center = TRUE, scale = TRUE), 1, 10, iter = 1000)
plot(fit, sortg = TRUE, grpmts.plot = TRUE)
calinski.best <- as.numeric(which.max(fit$results[2,]))
cat("Calinski criterion optimal number of clusters:", calinski.best, "\n")
# 5 clusters!

Empat . Tentukan model yang optimal dan jumlah cluster berdasarkan Kriteria Informasi Bayesian untuk maksimalisasi-harapan, diinisialisasi oleh pengelompokan hierarkis untuk model campuran Gaussian berparameterisasi
# See http://www.jstatsoft.org/v18/i06/paper
# http://www.stat.washington.edu/research/reports/2006/tr504.pdf
#
library(mclust)
# Run the function to see how many clusters
# it finds to be optimal, set it to search for
# at least 1 model and up 20.
d_clust <- Mclust(as.matrix(d), G=1:20)
m.best <- dim(d_clust$z)[2]
cat("model-based optimal number of clusters:", m.best, "\n")
# 4 clusters
plot(d_clust)

Lima . Pengelompokan afinitas (AP), lihat http://dx.doi.org/10.1126/science.1136800
library(apcluster)
d.apclus <- apcluster(negDistMat(r=2), d)
cat("affinity propogation optimal number of clusters:", length(d.apclus@clusters), "\n")
# 4
heatmap(d.apclus)
plot(d.apclus, d)

Enam . Statistik Gap untuk Memperkirakan Jumlah Cluster. Lihat juga beberapa kode untuk output grafis yang bagus . Mencoba 2-10 cluster di sini:
library(cluster)
clusGap(d, kmeans, 10, B = 100, verbose = interactive())
Clustering k = 1,2,..., K.max (= 10): .. done
Bootstrapping, b = 1,2,..., B (= 100) [one "." per sample]:
.................................................. 50
.................................................. 100
Clustering Gap statistic ["clusGap"].
B=100 simulated reference sets, k = 1..10
--> Number of clusters (method 'firstSEmax', SE.factor=1): 4
logW E.logW gap SE.sim
[1,] 5.991701 5.970454 -0.0212471 0.04388506
[2,] 5.152666 5.367256 0.2145907 0.04057451
[3,] 4.557779 5.069601 0.5118225 0.03215540
[4,] 3.928959 4.880453 0.9514943 0.04630399
[5,] 3.789319 4.766903 0.9775842 0.04826191
[6,] 3.747539 4.670100 0.9225607 0.03898850
[7,] 3.582373 4.590136 1.0077628 0.04892236
[8,] 3.528791 4.509247 0.9804556 0.04701930
[9,] 3.442481 4.433200 0.9907197 0.04935647
[10,] 3.445291 4.369232 0.9239414 0.05055486
Inilah output dari implementasi statistik gap Edwin Chen:

Tujuh . Anda juga mungkin merasa berguna untuk mengeksplorasi data Anda dengan clustergram untuk memvisualisasikan penetapan cluster, lihat http://www.r-statistics.com/2010/06/clustergram-visualization-and-diagnostics-for-cluster-analysis-r- kode / untuk lebih jelasnya.
Delapan . The NbClust paket menyediakan 30 indeks untuk menentukan jumlah cluster dalam kumpulan data.
library(NbClust)
nb <- NbClust(d, diss=NULL, distance = "euclidean",
method = "kmeans", min.nc=2, max.nc=15,
index = "alllong", alphaBeale = 0.1)
hist(nb$Best.nc[1,], breaks = max(na.omit(nb$Best.nc[1,])))
# Looks like 3 is the most frequently determined number of clusters
# and curiously, four clusters is not in the output at all!

Jika pertanyaan Anda adalah how can I produce a dendrogram to visualize the results of my cluster analysis, maka Anda harus mulai dengan ini:
http://www.statmethods.net/advstats/cluster.html
http://www.r-tutor.com/gpu-computing/clustering/hierarchical-cluster-analysis
http://gastonsanchez.wordpress.com/2012/10/03/7-ways-to-plot-dendrograms-in-r/ Dan lihat di sini untuk metode yang lebih eksotis: http://cran.r-project.org/ web / views / Cluster.html
Berikut ini beberapa contoh:
d_dist <- dist(as.matrix(d)) # find distance matrix
plot(hclust(d_dist)) # apply hirarchical clustering and plot

# a Bayesian clustering method, good for high-dimension data, more details:
# http://vahid.probstat.ca/paper/2012-bclust.pdf
install.packages("bclust")
library(bclust)
x <- as.matrix(d)
d.bclus <- bclust(x, transformed.par = c(0, -50, log(16), 0, 0, 0))
viplot(imp(d.bclus)$var); plot(d.bclus); ditplot(d.bclus)
dptplot(d.bclus, scale = 20, horizbar.plot = TRUE,varimp = imp(d.bclus)$var, horizbar.distance = 0, dendrogram.lwd = 2)
# I just include the dendrogram here

Juga untuk data dimensi tinggi adalah pvclustpustaka yang menghitung nilai-p untuk pengelompokan hierarkis melalui resampling bootstrap multiskala. Inilah contoh dari dokumentasi (tidak akan bekerja pada data dimensi rendah seperti pada contoh saya):
library(pvclust)
library(MASS)
data(Boston)
boston.pv <- pvclust(Boston)
plot(boston.pv)

Apakah ada yang membantu?
fpcpaket. Memang benar, Anda kemudian harus menetapkan dua parameter ... tetapi saya telah menemukan bahwafpc::dbscankemudian melakukan pekerjaan yang cukup baik secara otomatis menentukan jumlah cluster yang baik. Plus itu sebenarnya dapat menghasilkan satu cluster jika data itu memberi tahu Anda - beberapa metode dalam jawaban luar biasa @ Ben tidak akan membantu Anda menentukan apakah k = 1 sebenarnya yang terbaik.