-1

使用以下代码: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 并且没有标记,如果是这样,我该如何修改导入语句?

导入的数据集:

在此处输入图像描述

4

1 回答 1

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.

这有效:

  1. 导入数据vai r gui数据导入:

    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

  2. 运行命令:

ag <- agnes(data, FALSE, metric="euclidean", FALSE, method="single")

情节(ag,ask = FALSE,which.plots = NULL)

这会生成这个看起来正确的树状图:

在此处输入图像描述

于 2013-09-15T21:35:23.463 回答