# [R] What does the command diag(solve(matrix)) do?

Peter Alspach Peter.Alspach at plantandfood.co.nz
Wed Apr 13 07:06:45 CEST 2011

```Tena koe Tiago

?solve
?diag

will tell you what those R functions do.  Why the code wants the diagonal of the inverse of that particular correlation matrix is another matter.  If the matrix is the so-called numerator relationship matrix (i.e., twice the coancestry matrix) then its inverse is used in fitting the so-called animal model.  See Lynch, M. and B. Walsh (1998)  Genetics and Analysis of Quantitative Traits. Sunderland, MA, USA,  Sinauer Associates, Inc. in particular chapters 26 and 27 (I think) for more details.

HTH ....

Peter Alspach

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of tiago.pereira at mbe.bio.br
> Sent: Wednesday, 13 April 2011 12:44 p.m.
> To: r-help at r-project.org
> Subject: [R] What does the command diag(solve(matrix)) do?
>
> Hello, R users!
>
> As a geneticist, and new user of R are still find some basic questions
> hard
> to solve. For example, I am trying to understand a code, which uses,
> for
> instance, a matrix of correlations:
>
> >mymatrix
> [,1]      [,2]      [,3]      [,4]      [,5]
> [1,] 1.0000000 0.2500000 0.2357023 0.2182179 0.2721655
> [2,] 0.2500000 1.0000000 0.3535534 0.3273268 0.4082483
> [3,] 0.2357023 0.3535534 1.0000000 0.3086067 0.3849002
> [4,] 0.2182179 0.3273268 0.3086067 1.0000000 0.3563483
> [5,] 0.2721655 0.4082483 0.3849002 0.3563483 1.0000000
>
> So far, so good. I understand well what this matrix means. Then, the
> following line shows up:
>
>
>  solved<- diag(solve(mymatrix))
>
> > solved
> [1] 1.131429 1.325714 1.285714 1.240000 1.388571
>
> would it be possible to you please, to send me some comments on what
> that
> command does, and where I might learn the math behind it?
>
>
> Thank you very much in advance.
>
> All the best,
>
> Tiago
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/What-does-
> the-command-diag-solve-matrix-do-tp3445982p3445982.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.

The contents of this e-mail are confidential and may be subject to legal privilege.
If you are not the intended recipient you must not use, disseminate, distribute or
reproduce all or any part of this e-mail or attachments.  If you have received this
e-mail in error, please notify the sender and delete all material pertaining to this
e-mail.  Any opinion or views expressed in this e-mail are those of the individual
sender and may not represent those of The New Zealand Institute for Plant and
Food Research Limited.

```