如何在给定索引数据框的情况下创建cclust对象

时间:2013-09-23 19:41:32

标签: r matlab interface cluster-analysis

我需要在R中访问cclust包的函数clustIndex。 该函数的原型如下:

clustIndex ( y, x, index = "all" )
y Object of class "cclust" returned by a clustering algorithm such as kmeans
x Data matrix where columns correspond to variables and rows to observations
index The indexes that are calculated "calinski", "cindex", "db", "hartigan",
      "ratkowsky", "scott", "marriot", "ball", "trcovw", "tracew", "friedman",
      "rubin", "ssi", "likelihood", and "all" for all the indexes. Abbreviations
      of these names are also accepted.

y是在同一个包中从函数cclust生成的对象,但我有一个用Matlab编码的聚类算法,并希望使用此函数clustIndex来使用matlab中算法生成的解决方案来计算索引。

我能想到的一种方法是创建一个cclust对象并使用我的solutuion填充其变量的值,然后使用它。这是正确/有效吗? 该软件包的文档可用here

还有其他想法吗?

1 个答案:

答案 0 :(得分:3)

无需创建对象,您只需创建如下列表:

  y = list(cluster = matlabObj$cluster , 
           centers = matlabObj$centers ,
           withins = matlabObj$withins,
           size = matlabObj$size)

这里有一个使用cclust的例子(你应该在这里使用你的matlab集群)来表明4个变量足以使用clustIndex函数:

x<- rbind(matrix(rnorm(100,sd=0.3),ncol=2),
         matrix(rnorm(100,mean=1,sd=0.3),ncol=2))
matlabObj <- cclust(x,2,20,verbose=TRUE,method="kmeans")
clustIndex(matlabObj,x, index="all")

y = list(cluster = matlabObj$cluster , 
         centers = matlabObj$centers ,
         withins = matlabObj$withins,
         size = matlabObj$size)

identical(clustIndex(y,x, index="all"),
          clustIndex(matlabObj,x, index="all"))

[1] TRUE