[R] How to extract diagonals
Gavin Simpson
gavin.simpson at ucl.ac.uk
Wed Jun 20 18:02:53 CEST 2007
On Wed, 2007-06-20 at 15:09 +0200, Birgit Lemcke wrote:
> Hello Gavin and thanks for your answer.
>
> Your completely right I dont need the diagonal that is the bisecting
> line of the angle.
>
> I need another diagonal of the (now) matrix.
>
> A1 A2 A3 A4 B1 B2 B3 B4
> A1
> A2
> A3
> A4
> B1 X
> B2 X
> B3 X
> B4 X
>
Not easily, especially without knowing how many samples are in A or B,
although all that is really needed is some careful subsetting of the
dist object and a minor amount of programming - unfortunately after
close to two weeks intensive teaching my brain isn't up to doing that
just now.
One simple way to do this is to use the distance() function in my
analogue package (on CRAN). distance() can calculate the dissimilarities
between one group of samples and another. Here is a simple example using
some dummy data, from ?distance:
## simple example using dummy data
train <- data.frame(matrix(abs(runif(200)), ncol = 10))
rownames(train) <- LETTERS[1:20]
colnames(train) <- as.character(1:10)
fossil <- data.frame(matrix(abs(runif(100)), ncol = 10))
colnames(fossil) <- as.character(1:10)
rownames(fossil) <- letters[1:10]
## calculate distances/dissimilarities between train and fossil
## samples
test <- distance(train, fossil)
test is now a matrix, the diagonal elements of which are the values that
you appear to want:
diag(test)
if I'm reading your diagram correctly. Note that for this, you need to
be comparing row 1 from matrix A with row 1 from matrix B - if they are
in some other order, then this won't work.
distance() has a version of Gower's coefficient for mixed that allows
you to specify weights. The function is just about clever enough to
allow missing values if you use method = "mixed" in distance(). Be sure
to read up about Gower's mixed coefficient in his 1971 paper (Gower,
1971, Biometrics 23; 623--637) and the use that weights and the range
parameter Rj are put to, or see the relevant section in Legendre &
Legendre (1998).
> I need for example the diagonal that compares A1 with B1.
> Do you have an idea how I can handle this?
>
> What is the effect of this code?
>
> all.equal(diags, diag(as.matrix(dis.bc)))
This was showing you that the diagonals of the dissimilarity matrix are
just a vector of zeroes. all.equal tests equality of its arguments.
>
> Thanks a lot and sorry for my inability to solve my problems on my own.
You're welcome. Using R is a learning experience. You only need to
grovel and apologise if you have not done your homework before posting
and not read the FAQ, the documentation or searched the archives, or
followed the posting guide. Which is not the case here.
HTH
G
>
> Am 20.06.2007 um 14:11 schrieb Gavin Simpson:
>
> > On Wed, 2007-06-20 at 13:26 +0200, Birgit Lemcke wrote:
> >> Hello,
> >>
> >> I am using Mac OS X on a power book and R 2.5.0
> >>
> >> I try to extract a diagonal from a dissimilarity matrix made with
> >> dsvdis, with this code:
> >>
> >> diag(DiTestRR)
> >>
> >> But I get this error message:
> >>
> >> Fehler in array(0, c(n, p)) : 'dim' spezifiziert ein zu groes Array
> >>
> >> english:
> >>
> >> Error in array(0, c(n, p)) : 'dim' specifies a too big array.
> >>
> >> Is there a limit to extract diagonals?
> >
> > The returned object is not a matrix, but an object of class "dist"
> > which
> > doesn't store the diagonals or the upper triangle of the dissimilarity
> > matrix to save memory. You need to convert the dist object to a matrix
> > first, then extract the diagonal. But, as this shows:
> >
> >> require(labdsv)
> >> ?dsvdis
> >> data(bryceveg)
> >> ?dsvdis
> >> dis.bc <- dsvdis(bryceveg,index="bray/curtis")
> > Warning in symbol.For("dsvdis") : 'symbol.For' is not needed: please
> > remove it
> >> diag(as.matrix(dis.bc))
> >
> > This is meaningless as the diagonals are all zero, as they should be;
> > this is the distance between a site and itself.
> >
> >>
> >> I hope somebody will help me!
> >
> > So perhaps you could explain why you want the diagonal. It would be
> > easier to just do:
> >
> > diags <- rep(0, length = nrow(bryceveg))
> >
> > That will be without the sample labels, but that is easily rectified
> >
> >> names(diags) <- rownames(bryceveg)
> >> all.equal(diags, diag(as.matrix(dis.bc)))
> > [1] TRUE
> >
> > So you'll have to reformulate your question if this is not what you
> > wanted.
> >
> > A word of warning, do not do diag(dis.bc)) on the above as it
> > brought my
> > Linux box to it's knees trying to do something silly - easily
> > recoverable, but beware.
> >
> > HTH
> >
> > G
> >
> >>
> >> Greetings
> >>
> >> Birgit Lemcke
> >
> > --
> > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> > Gavin Simpson [t] +44 (0)20 7679 0522
> > ECRC, UCL Geography, [f] +44 (0)20 7679 0565
> > Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
> > Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
> > UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
> > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> >
>
> Birgit Lemcke
> Institut fr Systematische Botanik
> Zollikerstrasse 107
> CH-8008 Zrich
> Switzerland
> Ph: +41 (0)44 634 8351
> birgit.lemcke at systbot.uzh.ch
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson [t] +44 (0)20 7679 0522
ECRC, UCL Geography, [f] +44 (0)20 7679 0565
Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT. [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
More information about the R-help
mailing list