sil.obj[ 1:3] 中的错误:剪影方法的维数不正确?

Error in sil.obj[, 1:3] : incorrect number of dimensions for silhouette method?

我正在尝试应用剪影方法来获得我的最佳聚类数。但是我收到以下错误:Error in sil.obj[, 1:3] : incorrect number of dimensions

这是一个假数据集:

structure(list(co = c("a", "d1", "d2", "he", "hy", "ki", "li", 
"lu", "ob"), c_p = c(6.60985975350616, 1.35161744022503, 3.56536502546689, 
4.3010752688172, 4.7482646922721, 9, 8.89473684210526, 8.26600985221675, 
4.93449595916631), ch = c(2.2694432639184, 1.37552742616034, 
5.9592529711375, 1.505376344086, 1.4173993521518, 2.8, 2.6917293233083, 
1.4729064039409, 9.0238196512122), co = c(2.7964300892478, 1.6033755274262, 
4.5195246179966, 0.3225806451613, 2.8505321610366, 4.4, 8.4962406015038, 
2.7093596059113, 4.6201616333475), di = c(2.311942201445, 0.65682137834037, 
1.062818336163, 9.8924731182796, 2.1064322073114, 2.4, 1.0451127819549, 
8.7192118226601, 2.5861335601872), fa = c(5.7161070973226, 10.478199718706, 
46.3497453310696, 50.8064516129032, 47.5474317445627, 51.6, 51.1278195488722, 
50.4926108374384, 4.0646533390047), he = c(5.7437314067148, 8.64978902953587, 
8.7096774193548, 4.1182795698925, 3.1744562702453, 48, 4.3684210526316, 
4.6108374384236, 4.9472564866014), its = c(13.4296642583935, 
1.40646976090014, 9.08319185059423, 1.7096774193548, 12.1008792225821, 
1.4, 1.781954887218, 10.3448275862069, 1.8553806890685), jo = c(6.1198470038249, 
0.843881856540084, 4.15959252971138, 3.76344086021505, 5.25219805645535, 
5.2, 8.27067669172932, 5.78817733990148, 4.26414291790727), loss_a = c(7.3523161920952, 
1.75808720112518, 6.7911714770798, 8.06451612903226, 6.87181860249884, 
8, 9.02255639097744, 5.29556650246305, 7.60314759676733), loss_s = c(27.2843178920527, 
5.62587904360056, 29.6264855687606, 30.1075268817204, 2.8857010643221, 
30.8, 25.187969924812, 2.6453201970443, 3.9642705231816), mu = c(40.3739906502337, 
7.31364275668073, 3.46519524618, 41.1290322580645, 3.1124479407682, 
46.4, 5.4887218045113, 32.7586206896552, 3.8987664823479), na = c(39.8215044623884, 
5.76652601969058, 29.7113752122241, 3.9032258064516, 3.9888940305414, 
39.2, 3.203007518797, 36.6995073891626, 4.0893236920459), nau = c(6.50233744156396, 
2.0393811533052, 5.51782682512733, 6.45161290322581, 4.90513651087459, 
5.2, 6.01503759398496, 6.2807881773399, 4.96597192683964), shor = c(31.4067148321292, 
3.44585091420534, 1.9405772495756, 18.5483870967742, 16.658954187876, 
2.8, 20.3007518796992, 31.1576354679803, 16.5142492556359), sor = c(44.4963875903102, 
7.73558368495077, 6.3327674023769, 43.5483870967742, 41.8093475242943, 
41.2, 46.6165413533835, 3.4088669950739, 43.1412165036155), spu = c(22.3969400764981, 
3.65682137834037, 17.062818336163, 1.010752688172, 16.3350300786673, 
17.6, 1.4210526315789, 2.0935960591133, 16.3866439812846), temp = c(25.116872078198, 
9.28270042194093, 37.6910016977929, 24.7311827956989, 2.0245256825544, 
3.6, 3.4511278195489, 25.9852216748768, 26.1697150148873)), row.names = c(NA, 
-9L), groups = structure(list(com = c("a", "d1", "d2", "he", 
"hy", "ki", "li", "lu", "ob"), .rows = structure(list(1L, 2L, 
    3L, 4L, 5L, 6L, 7L, 8L, 9L), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, 9L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

这是我的代码我是 运行:

fviz_nbclust(dt_scaled, FUN = hcut, method = "silhouette")

这是我正在使用的图书馆:

library(NbClust)

在 R 中尝试使用剪影方法获得最佳簇数时,有人遇到过同样的情况吗?

问题是 fviz_nbclustk.max 的默认值是 10,这比您的数据(9 行)多一组。第一个变量也是字符而不是数字。试试这个

fviz_nbclust(dt_scaled, FUN = hcut, method = "silhouette", k.max=5)

或小于数据中观察值数量的任何 k.max 值。