繁体   English   中英

计算两组点之间的平均链接距离的函数

[英]Function to calculate average linkage distance between two groups of points

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

也许

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中每个点之间的平均距离(此处为欧几里得)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM