Number of clusters 'k' must be in {1,2, .., n-1}; hence n >= 2", is displayed.

Hello all,
i am new in R and i am preparing cluster analysis and get this error:Number of clusters 'k' must be in {1,2, .., n-1}; hence n >= 2", is displayed. after executing this line : fviz_nbclust(dif,cluster::pam, method = "silhouette")+theme_classic() and i cant understand why it does not work and throws the error. Would be really thankful if someone could help to solve :slight_smile:

data is which is called "lentele"
age_15_25 age_25_35 age_35_45 age_45_55
Latvija 172 424 506 505
Vokietija 83 186 234 300
Lenkija 126 271 379 395
Ukraina 65 224 350 301
Estija 109 246 245 276
JungtinÄ— KaralystÄ— 52 147 145 153
Baltarusija 169 609 792 973

code is :
lentele = data.frame(
name = c("Latvija","Vokietija","Lenkija","Ukraina","Estija","JungtinÄ— KaralystÄ—","Baltarusija"),
age_15_25 = c(count_15_lv,count_15_d,count_15_pl,count_15_ua,count_15_ee,count_15_gb,count_15_b),
age_25_35 = c(count_25_lv,count_25_d,count_25_pl,count_25_ua,count_25_ee,count_25_gb,count_25_b),
age_35_45 = c(count_35_lv,count_35_d,count_35_pl,count_35_ua,count_35_ee,count_35_gb,count_35_b),
age_45_55 = c(count_45_lv,count_45_d,count_45_pl,count_45_ua,count_45_ee,count_45_gb,count_45_b),
age_55_60 = c(count_55_lv,count_55_d,count_55_pl,count_55_ua,count_55_ee,count_55_gb,count_55_b),
stringsAsFactors = FALSE

)
rownames(lentele) <- c("Latvija","Vokietija","Lenkija","Ukraina","Estija","JungtinÄ— KaralystÄ—","Baltarusija")

print(lentele)
atstumas.dist <- dist(lentele, method = "euclidean")
print(atstumas.dist)
as.matrix(atstumas.dist)[1:7, 1:7]
library("factoextra")
res.hc <- hclust(d = atstumas.dist, method = "ward.D2")

fviz_dend(res.hc, cex = 0.5)

grp <- cutree(res.hc,k=4)
head(grp,n=5)
fviz_dend(res.hc, cex = 0.6, k = 4, rect = TRUE)
library(cluster)
library(factoextra)
library(NbClust)
dif = lentele[,2:5]
print(dif)
fviz_nbclust(dif,cluster::pam, method = "silhouette")+theme_classic()

Please make your example reproducible. ( object 'count_15_lv' not found etc.)

You can test to see if it would be , by pasting what you propose to post into a blank new R session, and seeing that it does what you expect.

Here is general advice on how to make a reprex;

Does this represent your data correctly?

lentele = tibble::tribble(
~"region", ~"age_15_25", ~"age_25_35", ~"age_35_45", ~ "age_45_55",
"Latvija", 172, 424, 506, 505,
"Vokietija", 83, 186, 234, 300,
"Lenkija", 126, 271, 379, 395,
"Ukraina", 65, 224, 350, 301,
"Estija", 109, 246, 245, 276,
"JungtinÄ— KaralystÄ", 52, 147, 145, 153,
"Baltarusija", 169, 609, 792, 973) %>% 
  column_to_rownames("region") # move to rownames

print(lentele)
                    age_15_25 age_25_35 age_35_45 age_45_55
Latvija                   172       424       506       505
Vokietija                  83       186       234       300
Lenkija                   126       271       379       395
Ukraina                    65       224       350       301
Estija                    109       246       245       276
JungtinÄ— KaralystÄ        52       147       145       153
Baltarusija               169       609       792       973

By default the first 10 k are checked, however you only have a smaller number of readouts (regions). You need to set the k.max to a smaller number.

fviz_nbclust(lentele,cluster::pam,
             k.max = 6, 
             method = "wss") + theme_classic()

PS: Usually it's better to scale the numbers, to limit the effect of different dimensions in the data-collection, here in Baltarusija you have more readouts than in all the other regions.
Here a row-wise scaling might be useful - depending on what you want to find out.

  region              counts
  <chr>                <dbl>
1 JungtinÄ— KaralystÄ    497
2 Vokietija              803
3 Estija                 876
4 Ukraina                940
5 Lenkija               1171
6 Latvija               1607
7 Baltarusija           2543

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.