# hclust, R and Euclidean distances: weird stuff

I have a table of similarities expressed through cosines and am trying to do some cluster analysis in R, using hclust and method=ward.

First I need to turn cosines into squared Euclidean distances, knowing that $d=2(1-\cos)$. No problem. I turned myData into myDataDist.

But then when I use hclust (myDataDist, method=ward) it gives me an error:

must have n >= 2 objects to cluster


The craziest thing is that if I turn the table of cosines into Euclidean distances with the dist function: myDataDist <- dist(myData, method = "euclidean") it works just fine, but then the dendrogram plotted by hclust is wrong. (I know because I tried with another clustering program.)

Has anybody checked the code of dist or ward methods in R? Why doesn't hclust work as it should, with Euclidean squared distances computed manually as $d=2(1-\cos)$?

• The error is likely at your side. Did you read the documentation of hclust? It says about the arguments d a dissimilarity structure as produced by dist. Also how do you know the dendrogramm is wrong? – Momo Jul 24 '14 at 21:29
• hum.I am not quite sure about this. I read the documentation, yes, of course. That's the point. The function dist produces a dissimilarity structure that looks different from the one you would get by computing the squared euclidean distances manually, with the geometric formula d=2(1−cos). (you need euclidean squared distances in order to apply the ward method to a hierarchical clustering). Also, I believe that it is wrong because I plotted the same data with Multidendrograms (another program that works with cosines as well as with distances) and it comes out a different graph. – mariannaBol Jul 25 '14 at 7:57
• Does it work with other methods than ward? – Anony-Mousse Jul 25 '14 at 8:29
• yes it does, there is complete linkage, single linkage etc, I haven't checked which distance metrics they need, in any case but I need the Ward method – mariannaBol Jul 25 '14 at 8:48
• @mariannabol Your example is not reproducible but likely (and why I pointed you to the documentation) your $d$ is not an object that hclust recognizes (as it is not of class dist). Also, if you have a result you are happy with from Multidendrogramm, why do you need another one? – Momo Jul 25 '14 at 11:03

You have not given a lot of information. Two things to check are this. First, dist uses the rows of a matrix as units, not the columns (as is more customary in R analyses). Second, if you produce your own distance you have to pass it to hclust as as.dist(myDataDist).