开发者

How to specify distance metric while for kmeans in R?

I'm doing kmeans clustering in R with two requirements:

  1. I need to specify my own distance function, now it's Pearson Coefficient.

  2. I want to do the clustering that uses average of group members as centroids, rather some actual member. The reason for this requirement is that I think using average as centroid makes more sense than using an actual member since the members are always not near the real centroid. Please correct me if I'm wrong about this.

First I tried the kmeans function in stat package, but this function doesn't allow custom distance meth开发者_JAVA百科od.

Then I found pam function in cluster package. The pam function does allow custom distance metric by taking a dist object as parameter, but it seems to me that by doing this it takes actual members as centroids, which is not what I expect. Since I don't think it can do all the distance computation with just a distance matrix.

So is there some easy way in R to do the kmeans clustering that satisfies both my requirements ?


Check the flexclust package:

The main function kcca implements a general framework for k-centroids cluster analysis supporting arbitrary distance measures and centroid computation.

The package also includes a function distCor:

R> flexclust::distCor
function (x, centers) 
{
    z <- matrix(0, nrow(x), ncol = nrow(centers))
    for (k in 1:nrow(centers)) {
        z[, k] <- 1 - .Internal(cor(t(x), centers[k, ], 1, 0))
    }
    z
}
<environment: namespace:flexclust>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜