# [R] cluster a distance(analogue)-object using agnes(cluster)

Martin Maechler maechler at stat.math.ethz.ch
Thu Sep 4 11:28:20 CEST 2008

```>>>>> "B" == Birgitle  <birgit.lemcke at systbot.uzh.ch>
>>>>>     on Tue, 2 Sep 2008 03:02:31 -0700 (PDT) writes:

B> I try to perform a clustering using an existing dissimilarity matrix that I
B> calculated using distance (analogue)
B> I tried two different things. One of them worked and one not and I don`t
B> understand why.
B> Here the code:

B> not working example

B> library(cluster)
B> library(analogue)

B> iris2 <- as.data.frame(iris)

why that?     After the above,  iris2  is identical() to iris !

B> str(iris2)
B> 'data.frame':	150 obs. of  5 variables:
B> \$ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
B> \$ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
B> \$ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
B> \$ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
B> \$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1
B> 1 1 1 ...

B> Test.Gower <- distance(iris2, method ="mixed")

why not just
daisy(iris2, metric = "gower")

daisy() is in cluster which has been a recommended R package
"forever".

So the solution (here, not in general!)
is to stay with package 'cluster' and use
daisy() before agnes().

Regards,
Martin Maechler, ETH Zurich
{same city!  feel free to phone me..}

B> Test.Gower.agnes<-agnes(Test.Gower, diss=T)
B> Fehler in agnes(Test.Gower, diss = T) :
B> (list) Objekt kann nicht nach 'logical' umgewandelt werden
B> Error in agnes(Test.Gower, diss=T).
B> (list) object can`t be transformed to "logical"

B> working example only numerics used:

B> library(cluster)
B> library(analogue)

B> irisPart<-subset(iris, select= Sepal.Length:Petal.Width)
B> Dist.Gower <- distance(irisPart, method ="mixed")
B> AgnesA <- agnes(Dist.Gower, method="average", diss=TRUE)

B> Would be great if somebody could help me.
B> The dataset that I would like to use for the clustering also contains
B> factors.
B> and gives me the same Error message as in the not working example.