How to use 'hclust' as function call in R

﹥>﹥吖頭↗ 提交于 2019-12-02 20:42:51

Do read the help for functions you use. ?hclust is pretty clear that the first argument d is a dissimilarity object, not a matrix:

Arguments:

       d: a dissimilarity structure as produced by ‘dist’.

Update

As the OP has now updated their question, what is need is

hclustfunc <- function(x) hclust(x, method="complete")
distfunc <- function(x) as.dist((1-cor(t(x)))/2)
d <- distfunc(mydata)
fit <- hclustfunc(d)

Original

What you want is

hclustfunc <- function(x, method = "complete", dmeth = "euclidean") {    
    hclust(dist(x, method = dmeth), method = method)
}

and then

fit <- hclustfunc(mydata)

works as expected. Note you can now pass in the dissimilarity coefficient method as dmeth and the clustering method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!