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
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()