使用以下代码:http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Clustering/Hierarchical_Clustering
以下是如何生成树形图:
# import data
x <- read.table("data.txt")
# run AGNES
ag <- agnes (x, false, metric="euclidean", false, method ="single")
# print components of ag
print(ag)
# plot clusters
plot(ag, ask = FALSE, which.plots = NULL)
我在
上收到错误ag <- agnes (x, false, metric="euclidean", false, method ="single")
错误是:
Error in agnes(x, false, metric = "euclidean", false, method = "single") :
object 'false' not found
agnes的这种实现有效,但它为树形图生成编号标签:
ag <- agnes (data, metric="euclidean")
# plot clusters
plot(ag, ask = FALSE, which.plots = NULL)
树形图:
从http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Clustering/Hierarchical_Clustering
生成的树形图包括标签:
这是数据文件:
,ba,fi,mi,vo,rm,to
ba,0,662,877,255,412,996
fi,662,0,295,468,268,400
mi,877,295,0,754,564,138
vo,255,468,754,0,219,869
rm,412,268,564,219,0,669
to,996,400,138,869,669,0
如何基于以上数据生成标记的层次聚类,与上面标记的树状图相同?
更新:
在关注@sgibb建议之后我使用了这个:
ag <- agnes(data, FALSE, metric="euclidean", FALSE, method ="single")
# plot clusters
plot(ag, ask = FALSE, which.plots = NULL)
现在生成的树形图是:
这是一个不正确的结构。也许它因为我的数据集的列名称显示为1,2,3,4,5,6并且没有标记,如果是这样,我怎样才能修改import语句?
导入的数据集:
答案 0 :(得分:1)
我的问题是数据集中的额外逗号。
running help(read.table)
并注意到这一部分帮助我解决了这个问题:
If there is a header and the first row contains one fewer field than the number of columns, the first column in the input is used for the row names. Otherwise if row.names is missing, the rows are numbered.
这有效:
导入r gui的数据 要导入的数据:
BA,音响,MI,VO,RM,至 BA,0,662,877,255,412,996 网络连接,662,0,295,468,268,400 MI,877,295,0,754,564,138 VO,255,468,754,0,219,869 RM,412,268,564,219,0,669 于,996,400,138,869,669,0
运行命令:
ag&lt; - agnes(data,FALSE,metric =“euclidean”,FALSE,method =“single”)
plot(ag,ask = FALSE,which.plots = NULL)
这会生成这个看起来正确的树形图: