0

我有一个具有不同特征的品牌数据集,如卡路里、糖含量、纤维含量等,例如 例子

使用dput()

structure(list(Row = 1:30, Brands = structure(c(1L, 112L, 223L,  242L,
253L, 264L, 275L, 286L, 297L, 2L, 13L, 24L, 35L, 46L, 57L,  68L, 79L,
90L, 101L, 113L, 124L, 135L, 146L, 157L, 168L, 179L,  190L, 201L,
212L, 224L), .Label = c("Brand 1", "Brand 10", "Brand 100",  "Brand
101", "Brand 102", "Brand 103", "Brand 104", "Brand 105",  "Brand
106", "Brand 107", "Brand 108", "Brand 109", "Brand 11",  "Brand 110",
"Brand 111", "Brand 112", "Brand 113", "Brand 114",  "Brand 115",
"Brand 116", "Brand 117", "Brand 118", "Brand 119",  "Brand 12",
"Brand 120", "Brand 121", "Brand 122", "Brand 123",  "Brand 124",
"Brand 125", "Brand 126", "Brand 127", "Brand 128",  "Brand 129",
"Brand 13", "Brand 130", "Brand 131", "Brand 132",  "Brand 133",
"Brand 134", "Brand 135", "Brand 136", "Brand 137",  "Brand 138",
"Brand 139", "Brand 14", "Brand 140", "Brand 141",  "Brand 142",
"Brand 143", "Brand 144", "Brand 145", "Brand 146",  "Brand 147",
"Brand 148", "Brand 149", "Brand 15", "Brand 150",  "Brand 151",
"Brand 152", "Brand 153", "Brand 154", "Brand 155",  "Brand 156",
"Brand 157", "Brand 158", "Brand 159", "Brand 16",  "Brand 160",
"Brand 161", "Brand 162", "Brand 163", "Brand 164",  "Brand 165",
"Brand 166", "Brand 167", "Brand 168", "Brand 169",  "Brand 17",
"Brand 170", "Brand 171", "Brand 172", "Brand 173",  "Brand 174",
"Brand 175", "Brand 176", "Brand 177", "Brand 178",  "Brand 179",
"Brand 18", "Brand 180", "Brand 181", "Brand 182",  "Brand 183",
"Brand 184", "Brand 185", "Brand 186", "Brand 187",  "Brand 188",
"Brand 189", "Brand 19", "Brand 190", "Brand 191",  "Brand 192",
"Brand 193", "Brand 194", "Brand 195", "Brand 196",  "Brand 197",
"Brand 198", "Brand 199", "Brand 2", "Brand 20",  "Brand 200", "Brand
201", "Brand 202", "Brand 203", "Brand 204",  "Brand 205", "Brand
206", "Brand 207", "Brand 208", "Brand 209",  "Brand 21", "Brand 210",
"Brand 211", "Brand 212", "Brand 213",  "Brand 214", "Brand 215",
"Brand 216", "Brand 217", "Brand 218",  "Brand 219", "Brand 22",
"Brand 220", "Brand 221", "Brand 222",  "Brand 223", "Brand 224",
"Brand 225", "Brand 226", "Brand 227",  "Brand 228", "Brand 229",
"Brand 23", "Brand 230", "Brand 231",  "Brand 232", "Brand 233",
"Brand 234", "Brand 235", "Brand 236",  "Brand 237", "Brand 238",
"Brand 239", "Brand 24", "Brand 240",  "Brand 241", "Brand 242",
"Brand 243", "Brand 244", "Brand 245",  "Brand 246", "Brand 247",
"Brand 248", "Brand 249", "Brand 25",  "Brand 250", "Brand 251",
"Brand 252", "Brand 253", "Brand 254",  "Brand 255", "Brand 256",
"Brand 257", "Brand 258", "Brand 259",  "Brand 26", "Brand 260",
"Brand 261", "Brand 262", "Brand 263",  "Brand 264", "Brand 265",
"Brand 266", "Brand 267", "Brand 268",  "Brand 269", "Brand 27",
"Brand 270", "Brand 271", "Brand 272",  "Brand 273", "Brand 274",
"Brand 275", "Brand 276", "Brand 277",  "Brand 278", "Brand 279",
"Brand 28", "Brand 280", "Brand 281",  "Brand 282", "Brand 283",
"Brand 284", "Brand 285", "Brand 286",  "Brand 287", "Brand 288",
"Brand 289", "Brand 29", "Brand 290",  "Brand 291", "Brand 292",
"Brand 293", "Brand 294", "Brand 295",  "Brand 296", "Brand 297",
"Brand 298", "Brand 299", "Brand 3",  "Brand 30", "Brand 300", "Brand
301", "Brand 302", "Brand 303",  "Brand 304", "Brand 305", "Brand
306", "Brand 307", "Brand 31",  "Brand 32", "Brand 33", "Brand 34",
"Brand 35", "Brand 36", "Brand 37",  "Brand 38", "Brand 39", "Brand
4", "Brand 40", "Brand 41", "Brand 42",  "Brand 43", "Brand 44",
"Brand 45", "Brand 46", "Brand 47", "Brand 48",  "Brand 49", "Brand
5", "Brand 50", "Brand 51", "Brand 52", "Brand 53",  "Brand 54",
"Brand 55", "Brand 56", "Brand 57", "Brand 58", "Brand 59",  "Brand
6", "Brand 60", "Brand 61", "Brand 62", "Brand 63", "Brand 64", 
"Brand 65", "Brand 66", "Brand 67", "Brand 68", "Brand 69", "Brand 7",
"Brand 70", "Brand 71", "Brand 72", "Brand 73", "Brand 74", "Brand
75",  "Brand 76", "Brand 77", "Brand 78", "Brand 79", "Brand 8",
"Brand 80",  "Brand 81", "Brand 82", "Brand 83", "Brand 84", "Brand
85", "Brand 86",  "Brand 87", "Brand 88", "Brand 89", "Brand 9",
"Brand 90", "Brand 91",  "Brand 92", "Brand 93", "Brand 94", "Brand
95", "Brand 96", "Brand 97",  "Brand 98", "Brand 99"), class =
"factor"), Fiber = c(82L, 36L,  51L, 86L, 26L, 98L, 91L, 28L, 1L, 88L,
35L, 84L, 27L, 58L, 9L,  43L, 49L, 56L, 66L, 43L, 62L, 73L, 20L, 33L,
17L, 88L, 57L, 45L,  89L, 16L), Sugar = c(77L, 87L, 40L, 69L, 9L, 1L,
54L, 64L, 24L,  52L, 29L, 14L, 76L, 24L, 39L, 54L, 18L, 72L, 54L, 9L,
45L, 65L,  43L, 90L, 40L, 93L, 75L, 50L, 1L, 44L), Calories = c(94L,
14L,  36L, 34L, 40L, 91L, 58L, 82L, 91L, 19L, 60L, 79L, 44L, 60L, 80L,
27L, 17L, 5L, 10L, 89L, 63L, 43L, 29L, 99L, 92L, 19L, 9L, 38L,  43L,
9L), Feature.4 = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L,  0L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L,  0L, 1L, 0L,
1L), Feature.5 = c(1L, 0L, 0L, 1L, 0L, 1L, 1L, 1L,  1L, 0L, 0L, 0L,
1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L,  1L, 0L, 1L, 1L, 1L,
0L), Feature.6 = c(7L, 11L, 45L, 45L, 35L,  28L, 56L, 52L, 1L, 49L,
28L, 68L, 99L, 70L, 62L, 73L, 97L, 2L,  41L, 14L, 68L, 84L, 76L, 2L,
53L, 38L, 3L, 52L, 12L, 70L), Feature.7 = c(54L,  22L, 11L, 67L, 22L,
67L, 69L, 67L, 89L, 24L, 32L, 25L, 90L, 62L,  82L, 100L, 53L, 50L,
75L, 79L, 53L, 4L, 31L, 96L, 55L, 35L, 69L,  74L, 88L, 9L)), row.names
= c(NA, 30L), class = "data.frame")

我想从中挑选 5 个品牌,比如品牌 1、2、3、4 和 5,然后形成与这 5 个品牌中的每一个具有相似特征的品牌集群或组,并保留所有其余不相似的品牌一个单独的集群。因此,我将有 1 个集群品牌 1,品牌 2 为 1,品牌 3 和品牌 4 和 5 类似。然后将有 1 个与这 5 个品牌中的任何一个都不相似的品牌集群。该特征可以是虚拟的或连续的。

我认为这应该很容易,但是,我在“R”中找不到任何包。

4

1 回答 1

0

这是一个简单的例子,它使用了我正在调用的数据dta。首先,我们计算除二分法之外的值的 z 分数:

library(fields)
dta.zscores <- dta
dta.zscores[, c(3:5, 8:9)] <- scale(dta[, c(3:5, 8:9)])

现在dta.zscores包含原始数据的 z 分数,以便每个变量的权重相等。接下来,我们使用第 3 到 9 列计算每行到第 1 到 5 行(品牌 1 到 5)的距离:

dta.dist <- rdist(dta.zscores[1:5, 3:9], dta.zscores[, 3:9])
dta.mindist <- apply(dta.dist, 2, min)
dta.brand <- apply(dta.dist, 2, which.min)
quantile(dta.mindist[-c(1:5)])
#       0%      25%      50%      75%     100% 
# 1.131532 1.952891 2.383079 2.908602 3.475676 
table(dta.brand)
# dta.brand
#  1  2  3  4  5 
#  4  2  7 11  6 

该矩阵dta.dist为 30 列(每个观察值)乘 5 行(到每个品牌的距离)。向量dta.mindist是每次观察的最小距离。对于前 5 个观测值,这将为 0。该向量dta.brand表示 5 个品牌中的哪一个与该观察结果最接近。四分位函数显示在排除用于定义组的前 5 个品牌后到最近品牌的距离范围。最后,该表显示了分配给每个品牌的观察次数。

您仍然需要确定将观察值分配给品牌并将这些观察值移动到另一个集群(例如集群 6)的距离太远。基于分位数,25% 的距离大于 2.9。probs=您可以使用in 中的参数指定其他分位数quantile(),例如 .90 或 .95,具体取决于您希望在集群 6 中的观测数。

于 2020-12-01T16:35:55.920 回答