2016-01-20 145 views
0

hclust中,可以指定method = "average"以在聚类中使用平均链接。用于计算两组点之间平均链接距离的函数

我的情况是,我有两个固定的集群,我想计算这两个集群之间的平均关联。

在R中有这样的功能吗? hclust似乎使用Fortran代码来执行此操作。

样本数据:

structure(list(lon = c(106.0081819, 106.0621591, 106.0787142, 
105.9581624, 105.9982149, 105.9455287, 106.0726373, 106.12575, 
106.1110501, 106.060344, 106.0635147, 105.9575665, 105.9494248, 
106.0475363, 105.9564829, 105.9964291, 106.1037006, 105.9964291, 
106.1639749, 106.1110501), lat = c(21.1400879, 21.1766814, 21.1738006, 
21.202957, 21.1244525, 21.1101074, 21.1861204, 21.163438, 21.121444, 
21.169068, 21.1815923, 21.1085185, 21.0994022, 21.1688445, 21.1158848, 
21.1122605, 21.1988765, 21.1122605, 21.0178933, 21.121444), group = c("domestic", 
"foreign", "domestic", "domestic", "foreign", "domestic", "domestic", 
"foreign", "domestic", "domestic", "domestic", "domestic", "domestic", 
"domestic", "foreign", "domestic", "domestic", "foreign", "domestic", 
"domestic")), .Names = c("lon", "lat", "group"), class = c("tbl_df", 
"data.frame"), row.names = c(NA, -20L)) 

回答

1

也许

d <- dist(df[, 1:2]) 
idx <- as.matrix(expand.grid(which(df$group=="domestic"), which(df$group=="foreign"))) 
mean(as.matrix(d)[idx]) 
# [1] 0.09028491 

如果平均键是的平均距离(这里:欧几里得)在簇1中的每个点之间,并且在簇中的每个点2.

+0

我想出了同样的解决方案,尽管'as.matrix(d)[which(df $ group ==“domestic”),which(df $ group ==“foreign”)]''代替。这和'expand.grid'一样吗? – Heisenberg

+0

应该是。有时我觉得太复杂了。 - > – lukeA

相关问题