0
votes

I started by trying to validate my kmeans clustering using clv and/or clValid, however I'm not getting much help from clValid/clv docs or google. This site claims that clValid will run a bunch of different clustering methods and tell me the best one to use, but I'm not sure if this is the same clValid that's used in R 3.2.2. Or if R has that capability.

Up to this point I've been using a homemade loop to go through a bunch of different kmeans scenarios (of varying k and nstart params) and print the between_ss/tot_ss ratio of each. I learned that this ratio may not be the best determinant of cluster accuracy, which is why I started looking into clv / clValid.

How can I use clv/clValid to find the best clustering params and/or algorithm OR find a better measurement of accuracy than bss/tss?


Here's a dput of kmeans(d,2,nstart = 30,iter.max = 100):

structure(list(cluster = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), centers = structure(c(0.818413368421053, 0.432375465838509, -0.0337968947368421, 0.136620745341615, 0.858298052631579, 0.297504347826087, 0.0318997368421053, 0.215006770186335, 0.836031578947368, 0.212654472049689, -0.0298043684210526, 0.295393291925466, 0.815911789473685, 0.153015155279503, 0.0194942631578947, 0.372349813664596, 0.822618210526316, 0.067345652173913, 0.043492, 0.28668149068323, 0.82810552631579, -0.10347099378882, 0.00272705263157895, 0.200435155279503, 0.831215631578948, -0.23062801242236, 0.00693610526315788, 0.146891863354037, 0.790275263157895, -0.0999267080745341, -0.0197452631578947, 0.0154167701863354, 0.763873052631579, -0.117951118012422, -0.0115862631578947, -0.0387036024844721, 0.768319631578948, -0.172674658385093, 0.0672504736842106, -0.0612778260869565, 0.743664368421053, -0.0873747204968944, -0.0343678947368421, -0.0845932919254659, 0.705352947368421, 0.0312186956521739, -0.0237621578947368, -0.127153913043478, 0.739496263157895, 0.308146770186335, -0.0075103157894737, -0.14273751552795, 0.677577894736842, 0.0254314285714286, -0.00874694736842107, -0.050518447204969, 0.669764631578948, -0.0218817391304348, -0.00890131578947369, 0.00223378881987576, 0.645276631578947, 0.000149565217391298, -0.0168468421052632, 0.0514498136645963), .Dim = c(2L, 32L), .Dimnames = list(    c("1", "2"), c("a03", "a04", "a05", "a06", "a07", "a08",     "a09", "a10", "a11", "a12", "a13", "a14", "a15", "a16", "a17",     "a18", "a19", "a20", "a21", "a22", "a23", "a24", "a25", "a26",     "a27", "a28", "a29", "a30", "a31", "a32", "a33", "a34"))),     totss = 3209.21698006201, withinss = c(711.344542655624,     1675.94716875112), tot.withinss = 2387.29171140674, betweenss = 821.925268655267,     size = c(190L, 161L), iter = 1L, ifault = 0L), .Names = c("cluster", "centers", "totss", "withinss", "tot.withinss", "betweenss", "size", "iter", "ifault"), class = "kmeans")

Here's a dput of the cleaned data head(df,50):

structure(list(a03 = c(0.99539, 1, 1, 1, 1, 0.02337, 0.97588, 0, 0.96355, -0.01864, 1, 1, 1, 1, 1, 0.50932, 0.99645, 0, 0.67065, 1, 1, 1, 0.96071, -1, 1, 1, 1, -1, 1, -1, 1, 1, 0.88208, 0.71253, 1, 0.66161, 1, 1, 0.91241, 1, 1, -1, 0.90932, -0.64286, 0.99025, 0, 1, 0.94598, 0.98166, 0), a04 = c(-0.05889, -0.18829, -0.03365, -0.45161, -0.02401, -0.00592, -0.10602, 0, -0.07198, -0.08459, 0.06655, -0.5421, -0.16316, -0.86701, 0.0738, -0.93996, 0.06468, 0, 0.02528, -1, -0.00612, 1, 0.07088, 1, -0.06182, 0.5782, -0.08714, -1, 0.0838, -1, -0.14236, -1, -0.14639, -0.02595, -0.15899, -1, 0.00433, 1, 0.04347, 0.02461, 0.06538, -1, 0.08791, -1, -0.05785, 0, -0.0373, -0.02685, 0.00874, 0), a05 = c(0.85243, 0.93035, 1, 1, 0.9414, -0.09924, 0.94601, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, -1, 0.66626, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0.93408, 0.41287, 0.72314, 1, 1, 1, 0.94191, 0.99672, 1, 1, 0.86528, 1, 0.99793, 0, 1, -1, 0.98103, 1), a06 = c(0.02306, -0.36156, 0.00485, 1, 0.06531, -0.11949, -0.208, 0, -0.14333, 0, -0.18388, -1, -0.10169, 0.2228, 0.0342, 0.26708, -0.01236, -1, 0.05031, 0, -0.09834, 0, 0.04296, 0, 0.02942, -1, -0.17263, 0, 0.17387, 1, -0.16256, 1, -0.11057, -0.23067, 0.27686, 1, -0.01209, -1, 0.0228, 0.04861, 0.20746, 1, 0.16888, 0.82857, -0.13009, 0, -0.07383, 0.26131, -0.03818, 0.51724), a07 = c(0.83398, -0.10868, 1, 0.71216, 0.92106, -0.00763, 0.92806, 1, 1, 0, 1, 1, 0.99999, 0.85492, 1, -0.0352, 0.97811, 1, 0.57197, 0, 1, 0, 1, 0, 1, 1, 0.86635, -1, 1, 1, 1, 1, 0.921, 0.98019, 0.83443, 1, 1, 0, 0.94705, 0.97545, 1, 1, 1, 1, 0.98663, 0, 0.99601, -0.36393, 0.97565, 0), a08 = c(-0.37708, -0.93597, -0.12062, -1, -0.23255, -0.11824, -0.2835, -1, -0.21313, 0, -0.2732, -1, -0.15197, -0.39896, -0.05563, -1, 0.02498, 1, 0.18761, 0, -0.07649, 0, 0.09313, 0, -0.05131, -1, -0.81779, 1, -0.13308, -1, -0.23656, -1, -0.1645, -0.09473, -0.58388, -0.67321, -0.0296, 0, 0.05345, 0.07143, 0.26281, -0.14375, 0.16598, -1, -0.1943, 0, -0.11039, 0.35639, -0.05699, 0), a09 = c(1, 1, 0.88965, 0, 0.77152, 0.14706, 0.85996, 0, 1, 0.1147, 1, 1, 1, 1, 1, 1, 0.96112, -1, 0.08776, 1, 1, -1, 0.90169, -1, 1, 1, 0.94817, 1, 0.98172, -1, 1, 1, 0.88307, 0.99709, 1, 0.80893, 1, 0, 0.93582, 0.61745, 0.93051, 0, 0.55187, 1, 0.99374, 1, 0.99838, 0.69258, 0.95947, 0.10991), a10 = c(0.0376, -0.04549, 0.01198, 0, -0.16399, 0.06637, -0.27342, 0, -0.36174, -0.2681, -0.43107, 0.36217, -0.19277, -0.1209, 0.08764, -1, 0.02312, 1, 0.34081, 1, -0.10605, -1, -0.05144, 1, -0.01707, -1, 0.61053, -0.375, 0.6452, 1, -0.07514, -1, -0.17036, -0.10236, -0.28207, -0.40446, -0.07014, 0, 0.01321, -1, 0.32213, 0, 0.68154, -0.23393, -0.25843, 1, -0.09931, -0.63427, -0.06971, -1), a11 = c(0.85243, 0.50874, 0.73082, 0, 0.52798, 0.03786, 0.79766, -1, 0.9257, -0.45663, 1, 1, 0.94055, 1, 1, 0.43685, 0.99274, -1, 0.63621, 1, 1, 0, 0.89263, 1, 1, 1, 0.95473, 0, 1, 1, 1, 1, 0.88462, 1, 1, 1, 0.97839, -1, 0.91911, 0.91036, 0.86773, -1, 0.70207, 1, 0.92738, 1, 0.98941, 1, 0.99004, 0),     a12 = c(-0.17755, -0.67743, 0.05346, 0, -0.20275, -0.06302,     -0.47929, -1, -0.43569, -0.38172, -0.41349, -0.41119, -0.35151,     0.35147, 0.19651, -1, 0.07808, 1, 0.12131, -1, -0.11073,     0, 0.0258, 1, -0.11726, -1, -0.41382, 0, 0.47904, -1, -0.2501,     -1, -0.31809, -0.10951, -0.49863, -1, -0.06256, -1, 0.06348,     0.11147, 0.39039, 1, 0.36719, 0.96161, -0.3013, 1, -0.13814,     -0.03353, -0.04507, 0), a13 = c(0.59755, 0.34432, 0.85443,     0, 0.56409, 0, 0.78225, 0, 0.9451, 0, 0.96232, 1, 0.95735,     1, 1, 0, 0.89323, 1, 0.62099, -0.71875, 1, 0, 0.8325, 0,     0.84493, 1, 0.88486, 0, 1, -1, 1, 1, 0.85269, 0.58965, 0.79962,     1, 1, 0, 0.92766, 0.88462, 0.75474, 1, 0.16286, 1, 0.92651,     0, 0.96674, -0.2902, 0.94713, 0), a14 = c(-0.44945, -0.69707,     0.00827, 0, -0.00712, 0, -0.50764, 0, -0.40668, 0, -0.51874,     1, -0.29785, 0.07772, 0.20328, 0, 0.10346, -1, 0.14285, 1,     -0.39489, 0, -0.06142, 0, -0.05202, -1, -0.31736, 0, 0.59113,     -1, -0.26161, -0.0184, -0.31463, 1, -0.12527, -0.89375, -0.06544,     0, 0.12067, 0.5364, 0.50082, 1, 0.42739, -0.37679, -0.37965,     0, -0.21695, -0.0055, -0.11102, 0), a15 = c(0.60536, -0.51685,     0.54591, -1, 0.34395, -0.04572, 0.74628, 0, 0.90392, -0.33656,     0.90711, 1, 0.93719, 1, 1, -1, 0.94212, 1, 0.78637, 0, 1,     -1, 0.87534, 0, 0.93392, 1, 0.87937, 0, 1, 0, 1, 1, 0.82116,     0.83726, 0.76837, 1, 0.97261, 0, 0.92048, 0.82077, 0.79555,     0.17917, 0.5762, 1, 0.89812, 0, 0.95288, -0.54852, 0.93369,     -1), a16 = c(-0.38223, -0.97515, 0.00299, 0.14516, -0.27457,     -0.1554, -0.61436, 0, -0.46381, 0.38602, -0.59017, -1, -0.34412,     -0.14767, 0.12785, -0.34265, 0.05269, 1, 0.10976, 0, -0.15616,     -1, 0.09831, 0, -0.06598, -1, -0.23433, 0, 0.70758, 0, -0.21975,     -1, -0.35924, -1, 0.14638, 0.73393, -0.07917, 0, 0.06211,     0.14137, 0.52321, -1, 0.46086, -1, -0.43796, 0, -0.25099,     0.15452, -0.1279, -0.22414), a17 = c(0.84356, 0.05499, 0.83775,     0.54094, 0.5294, -0.00343, 0.57945, 1, 0.98305, -0.37133,     0.8923, 1, 0.94486, 1, 1, -0.37681, 0.88809, -1, 0.58373,     -1, 0.92124, -1, 0.76544, 1, 0.6917, 1, 0.81051, 1, 1, 1,     1, 1, 0.80681, 0.8227, 1, 0.17589, 0.92561, -1, 0.88899,     0.76929, 0.65954, -1, 0.51067, 1, 0.84922, -0.375, 0.91236,     0.91921, 0.94217, -0.55711), a18 = c(-0.38542, -0.62237,     -0.13644, -0.3933, -0.2178, -0.10196, -0.68086, 1, -0.35257,     0.15018, -0.66474, -0.29354, -0.28106, -1, 0.10561, 0.03623,     0.1112, -1, 0.18151, 1, -0.31884, -1, 0.0028, -1, -0.07379,     -1, -0.6218, -1, 0.82777, 1, -0.38606, 1, -0.33632, -0.17863,     0.39337, 0.70982, -0.13665, 1, 0.12722, 0.15189, 0.60756,     -1, 0.49618, 0.13839, -0.52064, -1, -0.344, -0.4627, -0.11583,     -0.83297), a19 = c(0.58212, 0.33109, 0.75535, -1, 0.45107,     -0.11575, 0.37852, -1, 0.84537, 0.63728, 0.69876, 1, 0.90137,     1, 1, 1, 0.86104, -1, 0.14395, 1, 0.86473, -1, 0.75206, -1,     0.65731, 1, 0.12245, -1, 1, -1, 1, 1, 0.75243, 0.8076, 1,     1, 0.94184, 1, 0.83744, 1, 0.57619, 0.0875, 0.31639, 1, 0.87433,     -1, 0.90581, 1, 0.79682, 0.7694), a20 = c(-0.32192, -1, -0.0854,     -0.54467, -0.17813, -0.05414, -0.73641, -1, -0.6602, 0.22115,     -0.70997, -0.93599, -0.43383, -1, 0.27087, -1, 0.08631, 1,     0.41224, 1, -0.34534, 1, -0.05295, 1, -0.20367, -1, -1, -1,     0.95099, -1, -0.46162, -0.85583, -0.47022, -0.28257, 0.2659,     0.78036, -0.14327, 1, 0.14439, 0.41003, 0.62999, -1, 0.12967,     -1, -0.57075, -1, -0.32152, -0.50424, -0.192, 0.63147), a21 = c(0.56971,     -0.13151, 0.70887, -0.69975, 0.05982, 0.01838, 0.36324, 0,     0.75346, 0, 0.70645, 1, 0.86043, 0.61831, 1, 0, 0.81633,     1, 0.53888, -1, 0.91693, -1, 0.65961, 1, 0.9491, 1, 0.90284,     1, 1, 1, 1, 1, 0.70555, -0.25914, 0.96354, 1, 0.99589, -1,     0.80983, 0.6585, 0.47807, 1, 0.37824, 1, 0.79016, 0, 0.89991,     -0.29735, 0.88274, 0), a22 = c(-0.29674, -0.453, -0.27502,     1, -0.35575, 0.03669, -0.76562, 0, -0.60589, 0, -0.7632,     1, -0.47308, 0.15803, 0.44758, 0, 0.1183, -1, 0.21326, 1,     -0.44072, 1, -0.07905, 1, -0.31558, -0.62796, 0.11053, -1,     1, -1, -0.35519, 1, -0.47153, 0.9273, -0.01891, 0.85268,     -0.14248, 1, 0.11849, 0.16371, 0.67135, -1, 0.54462, -0.03393,     -0.59839, 0, -0.34691, -0.31454, -0.17387, 0), a23 = c(0.36946,     -0.18056, 0.43385, 0, 0.02309, 0.01519, 0.31898, 0, 0.69637,     0, 0.63081, 1, 0.82987, 1, 1, 0, 0.83668, -1, 0.5142, 1,     0.9606, 0, 0.64158, 0, 0.80852, 1, 0.62357, 0, 0.98042, 1,     1, 1, 0.6615, 0.51933, 0.92599, 1, 0.94815, -1, 0.77041,     0.60138, 0.40553, -1, 0.31274, -0.84286, 0.74725, 0, 0.87874,     -0.73864, 0.86257, 0.53448), a24 = c(-0.47357, -0.35734,     -0.12062, 0, -0.52879, 0.00888, -0.79753, 0, -0.64225, 0,     -0.80544, 1, -0.5122, 0.62349, 0.4175, 0, 0.14442, 1, 0.22625,     0.5625, -0.46866, 0, -0.05929, 0, -0.31654, -1, -0.78547,     0, 1, -1, -0.59661, -1, -0.50085, 0.05456, -0.91338, -1,     -0.13565, 1, 0.14222, 0.16516, 0.6884, 1, 0.55826, 1, -0.64615,     0, -0.37643, 0.37361, -0.18739, 0.35668), a25 = c(0.56811,     -0.20332, 0.57528, 1, 0.03286, 0.03513, 0.22792, 1, 0.85106,     -0.14803, 0.55867, 1, 0.8408, 1, 1, -0.16253, 0.81329, -1,     0.48838, -1, 0.81874, 0, 0.55677, -1, 0.84932, 1, 0.55389,     1, 0.91624, 1, 1, 0, 0.61297, 0.65493, 1, 1, 0.89469, 1,     0.75755, 0.54446, 0.34384, -1, 0.24856, 0.5375, 0.68282,     -1, 0.86213, 0.83872, 0.88487, -0.90302), a26 = c(-0.51171,     -0.26569, -0.4022, 0.90695, -0.65158, -0.01535, -0.81634,     1, -0.6544, -0.01326, -0.89128, -0.40888, -0.47137, -0.17012,     0.20033, 0.92236, 0.13412, 1, 0.23724, 1, -0.40372, 0, -0.07705,     -1, -0.34838, -1, -0.82868, -1, 1, 1, -0.47643, 0, -0.48086,     -0.20392, 0.14803, 0.85357, -0.20851, -1, 0.11299, 0.1639,     0.72082, -1, 0.56527, 0.85714, -0.68479, -1, -0.4299, -0.46734,     -0.19689, 0.44828), a27 = c(0.41078, -0.20468, 0.58984, 0.51613,     0.1329, -0.0324, 0.13659, 1, 0.57577, 0.20645, 0.47211, 1,     0.76224, 1, 1, 0.39752, 0.79476, 1, 0.46167, 1, 0.82681,     1, 0.58051, 1, 0.72529, 1, 0.48136, -1, 0.83899, 1, 0.9882,     1, 0.56804, 0.93124, 1, 1, 0.89067, 1, 0.7355, 0.48867, 0.27712,     1, 0.18626, 1, 0.65247, -1, 0.83172, 0.52208, 0.81813, 1),     a28 = c(-0.46168, -0.18401, -0.22145, 1, -0.53206, 0.09223,     -0.8251, 1, -0.69712, -0.02294, -0.865, -0.62745, -0.5837,     0.35924, 0.36743, 0.26501, 0.13638, -1, 0.24618, 1, -0.42231,     -1, -0.02205, -1, -0.29174, -1, -0.86583, 1, 1, -1, -0.49687,     1, -0.54629, -0.41307, -0.11582, -0.08571, -0.17909, 1, 0.13282,     0.16019, 0.72386, -1, 0.56605, 1, -0.73174, -1, -0.43122,     -0.5813, -0.21136, -1), a29 = c(0.21266, -0.1904, 0.431,     1, 0.02431, -0.07859, 0.04606, 0, 0.25435, 0, 0.40303, 1,     0.65723, 1, 0.95603, 0, 0.7911, -1, 0.43433, 1, 0.75784,     -1, 0.49664, 1, 0.73094, 1, 0.4065, -1, 0.74822, 1, 1, 1,     0.50179, 0.63811, 1, 0.95982, 0.85644, -1, 0.66387, 0.43481,     0.19296, -1, 0.12635, 1, 0.6101, -1, 0.81433, 1, 0.78546,     -1), a30 = c(-0.3409, -0.11593, -0.17365, -0.20099, -0.62197,     0.00732, -0.82395, 0, -0.63919, 0, -0.83675, -1, -0.68794,     -0.66494, 0.48641, 0, 0.15379, 1, 0.25306, -1, -0.38231,     1, -0.01251, 1, -0.38576, -1, -0.89674, -1, 1, -1, -0.7582,     -0.79141, -0.59854, -0.21901, -0.11129, -0.3625, -0.18552,     1, 0.153, 0.15436, 0.70682, -1, 0.56101, -1, -0.75353, 1,     -0.42593, -0.61393, -0.23864, 0.81573), a31 = c(0.42267,     -0.16626, 0.60436, 0.25682, -0.05707, 0, -0.04262, 1, 0.45114,     0.16595, 0.30996, 1, 0.68714, 1, 1, 1, 0.77122, -1, 0.40663,     1, 0.80448, -1, 0.5131, -1, 0.54356, 1, 0.32984, 0, 0.64358,     -1, 1, 1, 0.47075, 0.86136, 1, 1, 0.83777, 1, 0.70925, 0.38352,     0.11372, 1, 0.06927, 1, 0.54752, 1, 0.77919, -0.09634, 0.76911,     0), a32 = c(-0.54487, -0.06288, -0.2418, 1, -0.59573, 0,     -0.81318, 1, -0.72779, 0.24086, -0.89093, -1, -0.64537, 0.88428,     0.32492, 0.23188, 0.1593, -1, 0.25792, 1, -0.40575, 1, -0.00015,     1, -0.26284, -1, -0.92128, 0, 1, 1, -0.75761, 1, -0.57377,     -0.87354, 0.53372, 0.65268, -0.20101, 1, 0.10754, 0.14677,     0.72688, 1, 0.55061, -1, -0.80278, 0, -0.47977, 0.20477,     -0.23095, 0), a33 = c(0.18641, -0.13738, 0.56045, -0.32382,     -0.04608, -0.00039, -0.13832, 0, 0.38895, -0.08208, 0.22995,     1, 0.64727, 1, 1, 0, 0.70941, 1, 1, 1, 0.74354, -1, 0.52099,     0, 0.64207, 1, -0.13341, -1, 0.52479, 1, 1, 1, 0.42189, -0.23186,     1, 1, 0.83867, 0, 0.65258, 1, 0.0699, 0, 0.12137, 1, 0.49195,     0, 0.75115, -0.06117, 0.74323, 0), a34 = c(-0.453, -0.02447,     -0.38238, 1, -0.65697, 0.12011, -0.80975, 0, -0.7342, 0.38065,     -0.89158, -1, -0.67226, -0.18826, 0.46712, 0, 0.12015, -1,     0.33036, 1, -0.45039, 1, -0.00182, 0, -0.39487, -1, -1, 1,     1, -1, -0.84437, 1, -0.58086, -1, -0.57758, 0.34732, -0.20766,     0, 0.11447, 1, 0.71444, 0, 0.67739, -1, -0.83245, 0, -0.50152,     0.41913, -0.23902, 0)), .Names = c("a03", "a04", "a05", "a06", "a07", "a08", "a09", "a10", "a11", "a12", "a13", "a14", "a15", "a16", "a17", "a18", "a19", "a20", "a21", "a22", "a23", "a24", "a25", "a26", "a27", "a28", "a29", "a30", "a31", "a32", "a33", "a34"), row.names = c(NA, 50L), class = "data.frame")

And a dput of the top 50 original data (with dependent varb, 'class'):

structure(list(a01 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L), a02 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), a03 = c(0.99539, 1, 1, 1, 1, 0.02337, 0.97588, 0, 0.96355, -0.01864, 1, 1, 1, 1, 1, 0.50932, 0.99645, 0, 0.67065, 1, 1, 1, 0.96071, -1, 1, 1, 1, -1, 1, -1, 1, 1, 0.88208, 0.71253, 1, 0.66161, 1, 1, 0.91241, 1, 1, -1, 0.90932, -0.64286, 0.99025, 0, 1, 0.94598, 0.98166, 0), a04 = c(-0.05889, -0.18829, -0.03365, -0.45161, -0.02401, -0.00592, -0.10602, 0, -0.07198, -0.08459, 0.06655, -0.5421, -0.16316, -0.86701, 0.0738, -0.93996, 0.06468, 0, 0.02528, -1, -0.00612, 1, 0.07088, 1, -0.06182, 0.5782, -0.08714, -1, 0.0838, -1, -0.14236, -1, -0.14639, -0.02595, -0.15899, -1, 0.00433, 1, 0.04347, 0.02461, 0.06538, -1, 0.08791, -1, -0.05785, 0, -0.0373, -0.02685, 0.00874, 0), a05 = c(0.85243, 0.93035, 1, 1, 0.9414, -0.09924, 0.94601, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, -1, 0.66626, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0.93408, 0.41287, 0.72314, 1, 1, 1, 0.94191, 0.99672, 1, 1, 0.86528, 1, 0.99793, 0, 1, -1, 0.98103, 1), a06 = c(0.02306, -0.36156, 0.00485, 1, 0.06531, -0.11949, -0.208, 0, -0.14333, 0, -0.18388, -1, -0.10169, 0.2228, 0.0342, 0.26708, -0.01236, -1, 0.05031, 0, -0.09834, 0, 0.04296, 0, 0.02942, -1, -0.17263, 0, 0.17387, 1, -0.16256, 1, -0.11057, -0.23067, 0.27686, 1, -0.01209, -1, 0.0228, 0.04861, 0.20746, 1, 0.16888, 0.82857, -0.13009, 0, -0.07383, 0.26131, -0.03818, 0.51724), a07 = c(0.83398, -0.10868, 1, 0.71216, 0.92106, -0.00763, 0.92806, 1, 1, 0, 1, 1, 0.99999, 0.85492, 1, -0.0352, 0.97811, 1, 0.57197, 0, 1, 0, 1, 0, 1, 1, 0.86635, -1, 1, 1, 1, 1, 0.921, 0.98019, 0.83443, 1, 1, 0, 0.94705, 0.97545, 1, 1, 1, 1, 0.98663, 0, 0.99601, -0.36393, 0.97565, 0), a08 = c(-0.37708, -0.93597, -0.12062, -1, -0.23255, -0.11824, -0.2835, -1, -0.21313, 0, -0.2732, -1, -0.15197, -0.39896, -0.05563, -1, 0.02498, 1, 0.18761, 0, -0.07649, 0, 0.09313, 0, -0.05131, -1, -0.81779, 1, -0.13308, -1, -0.23656, -1, -0.1645, -0.09473, -0.58388, -0.67321, -0.0296, 0, 0.05345, 0.07143, 0.26281, -0.14375, 0.16598, -1, -0.1943, 0, -0.11039, 0.35639, -0.05699, 0), a09 = c(1, 1, 0.88965, 0, 0.77152, 0.14706, 0.85996, 0, 1, 0.1147, 1, 1, 1, 1, 1, 1, 0.96112, -1, 0.08776, 1, 1, -1, 0.90169, -1, 1, 1, 0.94817, 1, 0.98172, -1, 1, 1, 0.88307, 0.99709, 1, 0.80893, 1, 0, 0.93582, 0.61745, 0.93051, 0, 0.55187, 1, 0.99374, 1, 0.99838, 0.69258, 0.95947, 0.10991), a10 = c(0.0376, -0.04549, 0.01198, 0, -0.16399, 0.06637, -0.27342, 0, -0.36174, -0.2681, -0.43107, 0.36217, -0.19277, -0.1209, 0.08764, -1, 0.02312, 1, 0.34081, 1, -0.10605, -1, -0.05144, 1, -0.01707, -1, 0.61053, -0.375, 0.6452, 1, -0.07514, -1, -0.17036, -0.10236, -0.28207, -0.40446, -0.07014, 0, 0.01321, -1, 0.32213, 0, 0.68154, -0.23393, -0.25843, 1, -0.09931, -0.63427, -0.06971, -1), a11 = c(0.85243, 0.50874, 0.73082, 0, 0.52798, 0.03786, 0.79766, -1, 0.9257, -0.45663, 1, 1, 0.94055, 1, 1, 0.43685, 0.99274, -1, 0.63621, 1, 1, 0, 0.89263, 1, 1, 1, 0.95473, 0, 1, 1, 1, 1, 0.88462, 1, 1, 1, 0.97839, -1, 0.91911, 0.91036, 0.86773, -1, 0.70207, 1, 0.92738, 1, 0.98941, 1, 0.99004, 0),     a12 = c(-0.17755, -0.67743, 0.05346, 0, -0.20275, -0.06302,     -0.47929, -1, -0.43569, -0.38172, -0.41349, -0.41119, -0.35151,     0.35147, 0.19651, -1, 0.07808, 1, 0.12131, -1, -0.11073,     0, 0.0258, 1, -0.11726, -1, -0.41382, 0, 0.47904, -1, -0.2501,     -1, -0.31809, -0.10951, -0.49863, -1, -0.06256, -1, 0.06348,     0.11147, 0.39039, 1, 0.36719, 0.96161, -0.3013, 1, -0.13814,     -0.03353, -0.04507, 0), a13 = c(0.59755, 0.34432, 0.85443,     0, 0.56409, 0, 0.78225, 0, 0.9451, 0, 0.96232, 1, 0.95735,     1, 1, 0, 0.89323, 1, 0.62099, -0.71875, 1, 0, 0.8325, 0,     0.84493, 1, 0.88486, 0, 1, -1, 1, 1, 0.85269, 0.58965, 0.79962,     1, 1, 0, 0.92766, 0.88462, 0.75474, 1, 0.16286, 1, 0.92651,     0, 0.96674, -0.2902, 0.94713, 0), a14 = c(-0.44945, -0.69707,     0.00827, 0, -0.00712, 0, -0.50764, 0, -0.40668, 0, -0.51874,     1, -0.29785, 0.07772, 0.20328, 0, 0.10346, -1, 0.14285, 1,     -0.39489, 0, -0.06142, 0, -0.05202, -1, -0.31736, 0, 0.59113,     -1, -0.26161, -0.0184, -0.31463, 1, -0.12527, -0.89375, -0.06544,     0, 0.12067, 0.5364, 0.50082, 1, 0.42739, -0.37679, -0.37965,     0, -0.21695, -0.0055, -0.11102, 0), a15 = c(0.60536, -0.51685,     0.54591, -1, 0.34395, -0.04572, 0.74628, 0, 0.90392, -0.33656,     0.90711, 1, 0.93719, 1, 1, -1, 0.94212, 1, 0.78637, 0, 1,     -1, 0.87534, 0, 0.93392, 1, 0.87937, 0, 1, 0, 1, 1, 0.82116,     0.83726, 0.76837, 1, 0.97261, 0, 0.92048, 0.82077, 0.79555,     0.17917, 0.5762, 1, 0.89812, 0, 0.95288, -0.54852, 0.93369,     -1), a16 = c(-0.38223, -0.97515, 0.00299, 0.14516, -0.27457,     -0.1554, -0.61436, 0, -0.46381, 0.38602, -0.59017, -1, -0.34412,     -0.14767, 0.12785, -0.34265, 0.05269, 1, 0.10976, 0, -0.15616,     -1, 0.09831, 0, -0.06598, -1, -0.23433, 0, 0.70758, 0, -0.21975,     -1, -0.35924, -1, 0.14638, 0.73393, -0.07917, 0, 0.06211,     0.14137, 0.52321, -1, 0.46086, -1, -0.43796, 0, -0.25099,     0.15452, -0.1279, -0.22414), a17 = c(0.84356, 0.05499, 0.83775,     0.54094, 0.5294, -0.00343, 0.57945, 1, 0.98305, -0.37133,     0.8923, 1, 0.94486, 1, 1, -0.37681, 0.88809, -1, 0.58373,     -1, 0.92124, -1, 0.76544, 1, 0.6917, 1, 0.81051, 1, 1, 1,     1, 1, 0.80681, 0.8227, 1, 0.17589, 0.92561, -1, 0.88899,     0.76929, 0.65954, -1, 0.51067, 1, 0.84922, -0.375, 0.91236,     0.91921, 0.94217, -0.55711), a18 = c(-0.38542, -0.62237,     -0.13644, -0.3933, -0.2178, -0.10196, -0.68086, 1, -0.35257,     0.15018, -0.66474, -0.29354, -0.28106, -1, 0.10561, 0.03623,     0.1112, -1, 0.18151, 1, -0.31884, -1, 0.0028, -1, -0.07379,     -1, -0.6218, -1, 0.82777, 1, -0.38606, 1, -0.33632, -0.17863,     0.39337, 0.70982, -0.13665, 1, 0.12722, 0.15189, 0.60756,     -1, 0.49618, 0.13839, -0.52064, -1, -0.344, -0.4627, -0.11583,     -0.83297), a19 = c(0.58212, 0.33109, 0.75535, -1, 0.45107,     -0.11575, 0.37852, -1, 0.84537, 0.63728, 0.69876, 1, 0.90137,     1, 1, 1, 0.86104, -1, 0.14395, 1, 0.86473, -1, 0.75206, -1,     0.65731, 1, 0.12245, -1, 1, -1, 1, 1, 0.75243, 0.8076, 1,     1, 0.94184, 1, 0.83744, 1, 0.57619, 0.0875, 0.31639, 1, 0.87433,     -1, 0.90581, 1, 0.79682, 0.7694), a20 = c(-0.32192, -1, -0.0854,     -0.54467, -0.17813, -0.05414, -0.73641, -1, -0.6602, 0.22115,     -0.70997, -0.93599, -0.43383, -1, 0.27087, -1, 0.08631, 1,     0.41224, 1, -0.34534, 1, -0.05295, 1, -0.20367, -1, -1, -1,     0.95099, -1, -0.46162, -0.85583, -0.47022, -0.28257, 0.2659,     0.78036, -0.14327, 1, 0.14439, 0.41003, 0.62999, -1, 0.12967,     -1, -0.57075, -1, -0.32152, -0.50424, -0.192, 0.63147), a21 = c(0.56971,     -0.13151, 0.70887, -0.69975, 0.05982, 0.01838, 0.36324, 0,     0.75346, 0, 0.70645, 1, 0.86043, 0.61831, 1, 0, 0.81633,     1, 0.53888, -1, 0.91693, -1, 0.65961, 1, 0.9491, 1, 0.90284,     1, 1, 1, 1, 1, 0.70555, -0.25914, 0.96354, 1, 0.99589, -1,     0.80983, 0.6585, 0.47807, 1, 0.37824, 1, 0.79016, 0, 0.89991,     -0.29735, 0.88274, 0), a22 = c(-0.29674, -0.453, -0.27502,     1, -0.35575, 0.03669, -0.76562, 0, -0.60589, 0, -0.7632,     1, -0.47308, 0.15803, 0.44758, 0, 0.1183, -1, 0.21326, 1,     -0.44072, 1, -0.07905, 1, -0.31558, -0.62796, 0.11053, -1,     1, -1, -0.35519, 1, -0.47153, 0.9273, -0.01891, 0.85268,     -0.14248, 1, 0.11849, 0.16371, 0.67135, -1, 0.54462, -0.03393,     -0.59839, 0, -0.34691, -0.31454, -0.17387, 0), a23 = c(0.36946,     -0.18056, 0.43385, 0, 0.02309, 0.01519, 0.31898, 0, 0.69637,     0, 0.63081, 1, 0.82987, 1, 1, 0, 0.83668, -1, 0.5142, 1,     0.9606, 0, 0.64158, 0, 0.80852, 1, 0.62357, 0, 0.98042, 1,     1, 1, 0.6615, 0.51933, 0.92599, 1, 0.94815, -1, 0.77041,     0.60138, 0.40553, -1, 0.31274, -0.84286, 0.74725, 0, 0.87874,     -0.73864, 0.86257, 0.53448), a24 = c(-0.47357, -0.35734,     -0.12062, 0, -0.52879, 0.00888, -0.79753, 0, -0.64225, 0,     -0.80544, 1, -0.5122, 0.62349, 0.4175, 0, 0.14442, 1, 0.22625,     0.5625, -0.46866, 0, -0.05929, 0, -0.31654, -1, -0.78547,     0, 1, -1, -0.59661, -1, -0.50085, 0.05456, -0.91338, -1,     -0.13565, 1, 0.14222, 0.16516, 0.6884, 1, 0.55826, 1, -0.64615,     0, -0.37643, 0.37361, -0.18739, 0.35668), a25 = c(0.56811,     -0.20332, 0.57528, 1, 0.03286, 0.03513, 0.22792, 1, 0.85106,     -0.14803, 0.55867, 1, 0.8408, 1, 1, -0.16253, 0.81329, -1,     0.48838, -1, 0.81874, 0, 0.55677, -1, 0.84932, 1, 0.55389,     1, 0.91624, 1, 1, 0, 0.61297, 0.65493, 1, 1, 0.89469, 1,     0.75755, 0.54446, 0.34384, -1, 0.24856, 0.5375, 0.68282,     -1, 0.86213, 0.83872, 0.88487, -0.90302), a26 = c(-0.51171,     -0.26569, -0.4022, 0.90695, -0.65158, -0.01535, -0.81634,     1, -0.6544, -0.01326, -0.89128, -0.40888, -0.47137, -0.17012,     0.20033, 0.92236, 0.13412, 1, 0.23724, 1, -0.40372, 0, -0.07705,     -1, -0.34838, -1, -0.82868, -1, 1, 1, -0.47643, 0, -0.48086,     -0.20392, 0.14803, 0.85357, -0.20851, -1, 0.11299, 0.1639,     0.72082, -1, 0.56527, 0.85714, -0.68479, -1, -0.4299, -0.46734,     -0.19689, 0.44828), a27 = c(0.41078, -0.20468, 0.58984, 0.51613,     0.1329, -0.0324, 0.13659, 1, 0.57577, 0.20645, 0.47211, 1,     0.76224, 1, 1, 0.39752, 0.79476, 1, 0.46167, 1, 0.82681,     1, 0.58051, 1, 0.72529, 1, 0.48136, -1, 0.83899, 1, 0.9882,     1, 0.56804, 0.93124, 1, 1, 0.89067, 1, 0.7355, 0.48867, 0.27712,     1, 0.18626, 1, 0.65247, -1, 0.83172, 0.52208, 0.81813, 1),     a28 = c(-0.46168, -0.18401, -0.22145, 1, -0.53206, 0.09223,     -0.8251, 1, -0.69712, -0.02294, -0.865, -0.62745, -0.5837,     0.35924, 0.36743, 0.26501, 0.13638, -1, 0.24618, 1, -0.42231,     -1, -0.02205, -1, -0.29174, -1, -0.86583, 1, 1, -1, -0.49687,     1, -0.54629, -0.41307, -0.11582, -0.08571, -0.17909, 1, 0.13282,     0.16019, 0.72386, -1, 0.56605, 1, -0.73174, -1, -0.43122,     -0.5813, -0.21136, -1), a29 = c(0.21266, -0.1904, 0.431,     1, 0.02431, -0.07859, 0.04606, 0, 0.25435, 0, 0.40303, 1,     0.65723, 1, 0.95603, 0, 0.7911, -1, 0.43433, 1, 0.75784,     -1, 0.49664, 1, 0.73094, 1, 0.4065, -1, 0.74822, 1, 1, 1,     0.50179, 0.63811, 1, 0.95982, 0.85644, -1, 0.66387, 0.43481,     0.19296, -1, 0.12635, 1, 0.6101, -1, 0.81433, 1, 0.78546,     -1), a30 = c(-0.3409, -0.11593, -0.17365, -0.20099, -0.62197,     0.00732, -0.82395, 0, -0.63919, 0, -0.83675, -1, -0.68794,     -0.66494, 0.48641, 0, 0.15379, 1, 0.25306, -1, -0.38231,     1, -0.01251, 1, -0.38576, -1, -0.89674, -1, 1, -1, -0.7582,     -0.79141, -0.59854, -0.21901, -0.11129, -0.3625, -0.18552,     1, 0.153, 0.15436, 0.70682, -1, 0.56101, -1, -0.75353, 1,     -0.42593, -0.61393, -0.23864, 0.81573), a31 = c(0.42267,     -0.16626, 0.60436, 0.25682, -0.05707, 0, -0.04262, 1, 0.45114,     0.16595, 0.30996, 1, 0.68714, 1, 1, 1, 0.77122, -1, 0.40663,     1, 0.80448, -1, 0.5131, -1, 0.54356, 1, 0.32984, 0, 0.64358,     -1, 1, 1, 0.47075, 0.86136, 1, 1, 0.83777, 1, 0.70925, 0.38352,     0.11372, 1, 0.06927, 1, 0.54752, 1, 0.77919, -0.09634, 0.76911,     0), a32 = c(-0.54487, -0.06288, -0.2418, 1, -0.59573, 0,     -0.81318, 1, -0.72779, 0.24086, -0.89093, -1, -0.64537, 0.88428,     0.32492, 0.23188, 0.1593, -1, 0.25792, 1, -0.40575, 1, -0.00015,     1, -0.26284, -1, -0.92128, 0, 1, 1, -0.75761, 1, -0.57377,     -0.87354, 0.53372, 0.65268, -0.20101, 1, 0.10754, 0.14677,     0.72688, 1, 0.55061, -1, -0.80278, 0, -0.47977, 0.20477,     -0.23095, 0), a33 = c(0.18641, -0.13738, 0.56045, -0.32382,     -0.04608, -0.00039, -0.13832, 0, 0.38895, -0.08208, 0.22995,     1, 0.64727, 1, 1, 0, 0.70941, 1, 1, 1, 0.74354, -1, 0.52099,     0, 0.64207, 1, -0.13341, -1, 0.52479, 1, 1, 1, 0.42189, -0.23186,     1, 1, 0.83867, 0, 0.65258, 1, 0.0699, 0, 0.12137, 1, 0.49195,     0, 0.75115, -0.06117, 0.74323, 0), a34 = c(-0.453, -0.02447,     -0.38238, 1, -0.65697, 0.12011, -0.80975, 0, -0.7342, 0.38065,     -0.89158, -1, -0.67226, -0.18826, 0.46712, 0, 0.12015, -1,     0.33036, 1, -0.45039, 1, -0.00182, 0, -0.39487, -1, -1, 1,     1, -1, -0.84437, 1, -0.58086, -1, -0.57758, 0.34732, -0.20766,     0, 0.11447, 1, 0.71444, 0, 0.67739, -1, -0.83245, 0, -0.50152,     0.41913, -0.23902, 0), class = structure(c(2L, 1L, 2L, 1L,     2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,     1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,     2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,     1L), .Label = c("b", "g"), class = "factor")), .Names = c("a01", "a02", "a03", "a04", "a05", "a06", "a07", "a08", "a09", "a10", "a11", "a12", "a13", "a14", "a15", "a16", "a17", "a18", "a19", "a20", "a21", "a22", "a23", "a24", "a25", "a26", "a27", "a28", "a29", "a30", "a31", "a32", "a33", "a34", "class"), row.names = c(NA, 50L), class = "data.frame")
1

1 Answers

1
votes

The short answer:

You don't run clValid on an existing k-means model. You run clValid on a bunch of different scenarios at once, and it gives you different measurements to decide which is best.

# run kmeans on k=2 thru 6
v <- clValid(d,2:6, clMethods = "kmeans", validation = "internal")

Also, you can theoretically get the Rand Index and VI score by using cluster.stats somehow, but I haven't figured that out yet. Here's an article about it. Except it uses the iris data set (Soapbox time, please ignore: Iris is the worst data set to use as an example, but everyone uses it. Why don't we use something you don't have to be a botanist to understand like cars or movies? Perhaps it's to illustrate the importance of understanding the data... Still, I hate it.)


The long answer:

The data has an outcome of g or b, which is given in the class variable.

# Install and load these packages
# "fpc","clValid","RankAggreg","kohonen","plyr","clv","cluster","stats","caret","party","partykit"

# prepare the data
a<-read.csv("ionoshpere.csv",T,",")
d<-a
d$class = NULL
d<-scale(d)

# run kmeans on k=2 thru 6
v <- clValid(d,2:6, clMethods = "kmeans", validation = "internal")

# find the best cluster number to use
r1 <- getRanksWeights(v)
ag<-RankAggreg(x=r1$ranks, k=2, weights=r1$weights, seed = 1234, verbose=FALSE, rho=.115)

print(c("Best k's to use, according to Rank: ",ag$top.list[1]),"b")

p<-pamk(d)  
print(c("Best k's to use, according to Rank: ",p$nc))

# get the predicted clustering, assuming k=2 is the best (output of p$nc)
pc <- clV1@clusterObjs$kmeans$'2'$cluster

# build a confusion matrix with the best cluster:
table(a$class,pc)

# build a confusionMatrix object with the best cluster (requires "caret" package)
# put your outcomes in cluster terms:
# since cluster 1 has more 'g's than 'b's we will pretend the algorithm is guessing 'g's in cluster 1 and 'b's in cluster 2.
d1<-a
d1$class<-revalue(d1$class,c("g"=1,"b"=2))    # requires "plyr" package

# build the confusion matrix
cm<-confusionMatrix(pc,d1$class)

# output all the confusion matrix accuracy / error ratios...
print(cm)