Re: [R] generating symmetric matrices

2007-07-31 Thread Michael Dewey
At 16:29 30/07/2007, Gregory Gentlemen wrote:


Douglas Bates [EMAIL PROTECTED] wrote: On 7/27/07, 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.

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?

Yes, my goal is to generate correlation matrices whose entries have 
been sampled independently from a normal with a specified mean and variance.

Would it sufficient to use one of the results of
RSiteSearch(random multivariate normal, restrict = functions)
or have I completely misunderstood what you want? (I appreciate this 
is not exactly what you say you want.)

Thanks for the help.

Greg


-

 [[alternative HTML version deleted]]

Michael Dewey
http://www.aghmed.fsnet.co.uk

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread Benilton Carvalho
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@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.

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread Douglas Bates
On 7/27/07, Gregory Gentlemen [EMAIL PROTECTED] 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?

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread John Logsdon
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. -0.2941807  0.03784626 -0.6431474 -0.5743853
[2,] -0.29418072  1.000  0.65284419 -0.3410521 -0.5921743
[3,]  0.03784626  0.6528442  1. -0.2502129 -0.7101451
[4,] -0.64314741 -0.3410521 -0.25021285  1.000  0.7593021
[5,] -0.57438527 -0.5921743 -0.71014507  0.7593021  1.000

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@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
[EMAIL PROTECTED]  [EMAIL PROTECTED]
+44(0)161 445 4951/G:+44(0)7717758675   www.quantex-research.com

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread Gregory Gentlemen


Douglas Bates [EMAIL PROTECTED] wrote: On 7/27/07, 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.

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?

Yes, my goal is to generate correlation matrices whose entries have been 
sampled independently from a normal with a specified mean and variance.

Thanks for the help.

Greg

   
-

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread Charles C. Berry
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@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:[EMAIL PROTECTED]  UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread Douglas Bates
On 7/30/07, Gregory Gentlemen [EMAIL PROTECTED] wrote:


 Douglas Bates [EMAIL PROTECTED] wrote:
  On 7/27/07, 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.

 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?

 Yes, my goal is to generate correlation matrices whose entries have been
 sampled independently from a normal with a specified mean and variance.

I think that will be difficult.  For one thing correlations are
constrained to be in [-1,1].  Also, when you get into dimensions
greater than 2 the set of allowable correlation matrices is
constrained with difficult constraints.

Maybe you should reconsider what you are trying to do.

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread Bert Gunter
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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Gregory Gentlemen
Sent: Friday, July 27, 2007 8:28 PM
To: r-help@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@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.

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread Cleber Borges

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.

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread Ted Harding
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.
 
 Gregory 

Would 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) [EMAIL PROTECTED]
Fax-to-email: +44 (0)870 094 0861
Date: 30-Jul-07   Time: 20:00:55
-- XFMail --

__
R-help@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.


Re: [R] generating symmetric matrices

2007-07-30 Thread Ben Bolker
Gregory Gentlemen gregory_gentlemen at yahoo.ca writes:

 
 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:

 
 [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.

__
R-help@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.