Re: [R] Help with Mahalanobis

2005-07-13 Thread Thomas Petzoldt
Hello, a proposed solution of Bill Venables is archieved on the S-News mailing list: http://www.biostat.wustl.edu/archives/html/s-news/2001-07/msg00035.html and if I remember it correctly (and if the variance matrix is estimated from the data), another similar way is simply to use the Euclidean

Re: [R] Help with Mahalanobis

2005-07-10 Thread Jose Claudio Faria
I think we now have a very good R function here. Thanks for all Gabor! JCFaria Gabor Grothendieck wrote: > This one adds the labels: > > > D2Mah4 = function(y, x) { > > stopifnot(is.data.frame(y), !missing(x)) > stopifnot(dim(y)[1] != dim(x)[1]) > y= as.matrix(y) > x= as.factor(x)

Re: [R] Help with Mahalanobis

2005-07-10 Thread Gabor Grothendieck
This one adds the labels: D2Mah4 = function(y, x) { stopifnot(is.data.frame(y), !missing(x)) stopifnot(dim(y)[1] != dim(x)[1]) y= as.matrix(y) x= as.factor(x) man = manova(y ~ x) E= summary(man)$SS[2] #Matrix E S= as.matrix(E$Residuals)/man$df.residual InvS = solve(S) m

Re: [R] Help with Mahalanobis

2005-07-10 Thread Jose Claudio Faria
Indeed, it is very nice Gabor (as always)! So, a doubt: how to preserve the 'rowname' and 'colname' of D2, like in the first function? I think it is useful to posterior analyzes (as cluster, for example). Regards, # A small correction (reference to gtools was eliminated) D2Mah2 = function(y, x

Re: [R] Help with Mahalanobis

2005-07-10 Thread Gabor Grothendieck
And here is one more simplification using the buildin mahalanobis function: D2Mah3 = function(y, x) { stopifnot(is.data.frame(y), !missing(x)) stopifnot(dim(y)[1] != dim(x)[1]) y= as.matrix(y) x= as.factor(x) man = manova(y ~ x) E= summary(man)$SS[2] #Matrix E S= as.matrix

Re: [R] Help with Mahalanobis

2005-07-10 Thread Gabor Grothendieck
I think you could simplify this by replacing everything after the nObjects = nrow(mds) line with just these two statements. f <- function(a,b) mapply(function(a,b) (mds[a,] - mds[b,])%*%InvS%*%(mds[a,] - mds[b,]), a,b) D2 <- outer(seq(nObjects), seq(nObjects), f) This also eliminates dep

Re: [R] Help with Mahalanobis

2005-07-10 Thread Jose Claudio Faria
Well, as I did not get a satisfactory reply to the original question I tried to make a basic function that, I find, solve the question. I think it is not the better function, but it is working. So, perhaps it can be useful to other people. # # Calculate the matrix of Mahalanobis Distances betwe

Re: [R] Help with Mahalanobis

2005-07-09 Thread Jose Claudio Faria
Christian Hennig wrote: > Dear Jose, > > normal mixture clustering (mclust) operates on points times variables data > and not on a distance matrix. Therefore > it doesn't make sense to compute Mahalanobis distances before using > mclust. > Furthermore, cluster analysis based on distance matrices

Re: [R] Help with Mahalanobis

2005-07-08 Thread Christian Hennig
Dear Jose, normal mixture clustering (mclust) operates on points times variables data and not on a distance matrix. Therefore it doesn't make sense to compute Mahalanobis distances before using mclust. Furthermore, cluster analysis based on distance matrices (hclust or pam, say) operates on a poin

[R] Help with Mahalanobis

2005-07-08 Thread Jose Claudio Faria
Dear R list, I'm trying to calculate Mahalanobis distances for 'Species' of 'iris' data as obtained below: Squared Distance to Species From Species: Setosa Versicolor Virginica Setosa 0 89.86419 179.38471 Versicolor 89.86419 0 17.20107 Virginica 179.38471