Greetings, I have a seemingly simple task which I have not been able to solve today. I want to construct a symmetric matrix of arbtriray size w/o using loops. The following I thought would do it: p <- 6 Rmat <- diag(p) dat.cor <- rnorm(p*(p-1)/2) Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor However, the problem is that the matrix is filled by column and so the resulting matrix is not symmetric. I'd be grateful for any adive and/or solutions. Gregory --------------------------------- [[alternative HTML version deleted]]
after dat.cor use: Rmat[lower.tri(Rmat)] <- dat.cor Rmat <- t(Rmat) Rmat[lower.tri(Rmat)] <- dat.cor b On Jul 27, 2007, at 11:28 PM, Gregory Gentlemen wrote:> Greetings, > > I have a seemingly simple task which I have not been able to solve > today. I want to construct a symmetric matrix of arbtriray size w/o > using loops. The following I thought would do it: > > p <- 6 > Rmat <- diag(p) > dat.cor <- rnorm(p*(p-1)/2) > Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor > > However, the problem is that the matrix is filled by column and so > the resulting matrix is not symmetric. > > I'd be grateful for any adive and/or solutions. > > Gregory > > > > > --------------------------------- > > > > > [[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.
Since a symmetric matrix must be square, the following code does it:> X<-5 > MAT<-matrix(rnorm(X*X),X,X) > MAT<-MAT+t(MAT) > MAT[,1] [,2] [,3] [,4] [,5] [1,] 0.1084372 -1.7867366 -0.9620313 -1.0925719 -0.5902326 [2,] -1.7867366 -0.0462097 -1.2707656 0.6112664 1.8673785 [3,] -0.9620313 -1.2707656 -0.3248162 1.5458446 0.7641865 [4,] -1.0925719 0.6112664 1.5458446 -0.1621192 -1.1381366 [5,] -0.5902326 1.8673785 0.7641865 -1.1381366 -2.8668220 If you want to rescale it, for example to make a correlation matrix with the diagonal=1, just use cor():> cor(MAT)[,1] [,2] [,3] [,4] [,5] [1,] 1.00000000 -0.2941807 0.03784626 -0.6431474 -0.5743853 [2,] -0.29418072 1.0000000 0.65284419 -0.3410521 -0.5921743 [3,] 0.03784626 0.6528442 1.00000000 -0.2502129 -0.7101451 [4,] -0.64314741 -0.3410521 -0.25021285 1.0000000 0.7593021 [5,] -0.57438527 -0.5921743 -0.71014507 0.7593021 1.0000000 The essential trick is to add matrix to transpose. On Saturday 28 July 2007 04:28:25 Gregory Gentlemen wrote:> Greetings, > > I have a seemingly simple task which I have not been able to solve today. I > want to construct a symmetric matrix of arbtriray size w/o using loops. The > following I thought would do it: > > p <- 6 > Rmat <- diag(p) > dat.cor <- rnorm(p*(p-1)/2) > Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor > > However, the problem is that the matrix is filled by column and so the > resulting matrix is not symmetric. > > I'd be grateful for any adive and/or solutions. > > Gregory > > > > > --------------------------------- > > > > > [[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.-- Best wishes John John Logsdon "Try to make things as simple Quantex Research Ltd, Manchester UK as possible but not simpler" j.logsdon at quantex-research.com a.einstein at relativity.org +44(0)161 445 4951/G:+44(0)7717758675 www.quantex-research.com
On 7/27/07, Gregory Gentlemen <gregory_gentlemen at yahoo.ca> wrote:> Greetings,> I have a seemingly simple task which I have not been able to solve today. I want to construct a symmetric matrix of arbtriray size w/o using loops. The following I thought would do it:> p <- 6 > Rmat <- diag(p) > dat.cor <- rnorm(p*(p-1)/2) > Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor> However, the problem is that the matrix is filled by column and so the resulting matrix is not symmetric.Could you provide more detail on the properties of the symmetric matrices that you would like to generate? It seems that you are trying to generate correlation matrices. Is that the case? Do you wish the matrices to be a random sample from a specific distribution. If so, what distribution?
On Fri, 27 Jul 2007, Gregory Gentlemen wrote:> Greetings, > > I have a seemingly simple task which I have not been able to solve > today. I want to construct a symmetric matrix of arbtriray size w/o > using loops. The following I thought would do it: > > p <- 6 > Rmat <- diag(p) > dat.cor <- rnorm(p*(p-1)/2) > Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor > > However, the problem is that the matrix is filled by column and so the resulting matrix is not symmetric. > > I'd be grateful for any adive and/or solutions.Depends on the order of elements in dat.cor. Either Rmat <- diag(p) Rmat[lower.tri(Rmat)] <- dat.cor Rmat[upper.tri(Rmat)] <- t( Rmat )[upper.tri(Rmat)] or swap 'upper' for 'lower'.> > Gregory > > > > > --------------------------------- > > > > > [[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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
See ?dist for an object oriented approach that may be better. Directly, you can do something like (see ?row ?col): x <- matrix(NA, 10,10) ## Lower triangular : x[row(x) >= col(x) ] <- rnorm(55) x[row(x) < col(x)] <- x[row(x) > col(x)] ## or you could have saved the random vector and re-used it. Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Gregory Gentlemen Sent: Friday, July 27, 2007 8:28 PM To: r-help at stat.math.ethz.ch Subject: [R] generating symmetric matrices Greetings, I have a seemingly simple task which I have not been able to solve today. I want to construct a symmetric matrix of arbtriray size w/o using loops. The following I thought would do it: p <- 6 Rmat <- diag(p) dat.cor <- rnorm(p*(p-1)/2) Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor However, the problem is that the matrix is filled by column and so the resulting matrix is not symmetric. I'd be grateful for any adive and/or solutions. Gregory --------------------------------- [[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.
hello try ?upper.tri ############# example p <- 6 Rmat <- diag(p) dat.cor <- rnorm(p*(p-1)/2) Rmat[upper.tri(Rmat)]<- dat.cor Rmat[lower.tri(Rmat)]<- dat.cor Cleber Borges> Greetings, > > I have a seemingly simple task which I have not been able to solve today. I want to construct a symmetric matrix of arbtriray size w/o using loops. The following I thought would do it: > > p <- 6 > Rmat <- diag(p) > dat.cor <- rnorm(p*(p-1)/2) > Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor > > However, the problem is that the matrix is filled by column and so the resulting matrix is not symmetric. > > I'd be grateful for any adive and/or solutions. > > Gregory > >_______________________________________________________ Experimente j? e veja as novidades.
On 28-Jul-07 03:28:25, Gregory Gentlemen wrote:> Greetings, > > I have a seemingly simple task which I have not been able to solve > today. I want to construct a symmetric matrix of arbtriray size w/o > using loops. The following I thought would do it: > > p <- 6 > Rmat <- diag(p) > dat.cor <- rnorm(p*(p-1)/2) > Rmat[outer(1:p, 1:p, "<")] <- Rmat[outer(1:p, 1:p, ">")] <- dat.cor > > However, the problem is that the matrix is filled by column and so the > resulting matrix is not symmetric. > > I'd be grateful for any adive and/or solutions. > > GregoryWould the fact that A + t(A) is symmetric be useful here? E.g. p <- 6 A <- matrix(rnorm(p^2),ncol=p) A <- (A + t(A))/sqrt(2) diag(A) <- rep(1,p) round(A,digits=2) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1.00 0.53 -0.20 1.27 0.34 0.83 [2,] 0.53 1.00 -0.99 -0.72 0.68 -1.21 [3,] -0.20 -0.99 1.00 -0.62 -0.36 -0.87 [4,] 1.27 -0.72 -0.62 1.00 2.40 0.33 [5,] 0.34 0.68 -0.36 2.40 1.00 0.20 [6,] 0.83 -1.21 -0.87 0.33 0.20 1.00 (Here, because each off-diagonal element of A is the sum of 2 independent N(0,1)s, divided by sqrt(2), the result is also N(0,1)). However, whether this is reallyu seful for you depends on what you want the elements of A to be! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 30-Jul-07 Time: 20:00:55 ------------------------------ XFMail ------------------------------
Gregory Gentlemen <gregory_gentlemen <at> yahoo.ca> writes:> > Greetings, > > I have a seemingly simple task which I have not been able to solve today. Iwant to construct a symmetric> matrix of arbtriray size w/o using loops. The following I thought would do it: >[snip] p <- 6 Rmat <- diag(p) vals <- rnorm(p*(p-1)/2) Rmat[lower.tri(Rmat)] <- vals Rmat[upper.tri(Rmat)] <- t(Rmat)[upper.tri(Rmat)] appears to work.