Re: [R] Generating correlated data from uniform distribution

2005-07-05 Thread Greg Snow
Here is an approach using 'optim' and simulated annealing:

x - sort(runif(1000))
y - sort(runif(1000))

ord - 1:1000
target - function(ord){ ( cor(x, y[ord]) - 0.6 ) ^2 }
new.point - function(ord){
tmp - sample(length(ord), 2)
ord[tmp] - ord[rev(tmp)]
ord
}

new.point2 - function(ord){
tmp - sample(length(ord) -100, 1)
tmp2 - sample(100, 1)
ord[ c(tmp, tmp+tmp2) ] - ord[ c(tmp+tmp2, tmp) ]
ord
}

res - optim(ord, target, new.point, method=SANN,
control = list(maxit=6000, temp=2000, trace=TRUE))

res2 - optim(ord, target, new.point2, method=SANN,
control = list(maxit=6, temp=200, trace=TRUE))

y - y[res$par]

par(mfrow=c(2,2))
hist(x)
hist(y)
plot(x,y)
cor(x,y)


y - sort(y)[res2$par]

par(mfrow=c(2,2))
hist(x)
hist(y)
plot(x,y)
cor(x,y)

Hope this helps,

Greg Snow, Ph.D.
Statistical Data Center, LDS Hospital
Intermountain Health Care
[EMAIL PROTECTED]
(801) 408-8111

 Jim Brennan [EMAIL PROTECTED] 07/01/05 05:25PM 
OK now I am skeptical especially when you say in a weird way:-)
This may be OK but look at plot(x,y) and I am suspicious. Is it still
alright with this kind of relationship?

For large N it appears Spencer's method is returning slightly lower
correlation for the uniforms as compared to the normals so maybe there is a
problem!?!

Hope we are all learning something and Menghui gets/has what he wants . :-)

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter
Dalgaard
Sent: July 1, 2005 6:59 PM
To: Jim Brennan
Cc: 'Tony Plate'; 'Menghui Chen'; r-help@stat.math.ethz.ch 
Subject: Re: [R] Generating correlated data from uniform distribution

Jim Brennan [EMAIL PROTECTED] writes:

 Yes you are right I guess this works only for normal data. Free advice
 sometimes comes with too little consideration :-)

Worth every cent...

 Sorry about that and thanks to Spencer for the correct way.

Hmm, but is it? Or rather, what is the relation between the
correlation of the normals  and that of the transformed variables? 
Looks nontrivial to me.

Incidentally, here's a way that satisfies the criteria, but in a
rather weird way:

N - 1
rho - .6
x - runif(N, -.5,.5)
y - x * sample(c(1,-1), N, replace=T, prob=c((1+rho)/2,(1-rho)/2))

-- 
   O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
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

__
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


[R] Generating correlated data from uniform distribution

2005-07-02 Thread Ken Knoblauch
While you are looking at weird distributions, here is one that 
 we have used in experiments on noise masking to explore the 
 bandwidth of visual mechanisms 

D'Zmura, M.,  Knoblauch, K. (1998). Spectral bandwidths for the detection of 
color. 
Vision Research, 20, 3117-28 and
G. Monaci, G. Menegaz, S. Susstrunk and K. Knoblauch Chromatic Contrast 
Detection in Spatial 
Chromatic Noise Visual Neuroscience, Vol. 21, No 3, pp. 291-294, 2004

N - 1
x - runif(N, -.5,.5)
y - runif(N, -abs(x), abs(x))
plot(x,y)

y is not uniform but it is conditional on x.  The plot reveals
why we called this sectored noise.

HTH

ken


Jim Brennan jfbrennan at rogers.com writes:

 Yes you are right I guess this works only for normal data. Free advice
 sometimes comes with too little consideration :-)

Worth every cent...

 Sorry about that and thanks to Spencer for the correct way.

Hmm, but is it? Or rather, what is the relation between the
correlation of the normals  and that of the transformed variables? 
Looks nontrivial to me.

Incidentally, here's a way that satisfies the criteria, but in a
rather weird way:

N - 1
rho - .6
x - runif(N, -.5,.5)
y - x * sample(c(1,-1), N, replace=T, prob=c((1+rho)/2,(1-rho)/2))

-- 
   O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - (p.dalgaard at biostat.ku.dk)  FAX: 
(+45) 35327907


Ken Knoblauch
Inserm U371, Cerveau et Vision
Department of Cognitive Neurosciences
18 avenue du Doyen Lepine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: 06 84 10 64 10
http://www.lyon.inserm.fr/371/

__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-02 Thread Peter Dalgaard
Jim Brennan [EMAIL PROTECTED] writes:

 OK now I am skeptical especially when you say in a weird way:-)
 This may be OK but look at plot(x,y) and I am suspicious. Is it still
 alright with this kind of relationship?
...
 N - 1
 rho - .6
 x - runif(N, -.5,.5)
 y - x * sample(c(1,-1), N, replace=T, prob=c((1+rho)/2,(1-rho)/2))

Well, the covariance is (everything has mean zero, of course)

E(XY) = (1+rho)/2*EX^2 + (1-rho)/2*E(X*-X) = rho*EX^2 

The marginal distribution of Y is a mixture of two identical uniforms
(X and -X) so is uniform and in particular has the same variance as X.

In summary,  EXY/sqrt(EX^2EY^2) == rho

So as I said, it satisfies the formal requirements. X and Y are
uniformly distributed and their correlation is rho. 

If for nothing else, I suppose that this example is good for
demonstrating that independence and uncorrelatedness is not the same
thing. 

-- 
   O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-02 Thread Ted Harding
On 02-Jul-05 Peter Dalgaard wrote:
 Jim Brennan [EMAIL PROTECTED] writes:
 
 OK now I am skeptical especially when you say in a weird way:-)
 This may be OK but look at plot(x,y) and I am suspicious. Is it still
 alright with this kind of relationship?
 ...
 N - 1
 rho - .6
 x - runif(N, -.5,.5)
 y - x * sample(c(1,-1), N, replace=T, prob=c((1+rho)/2,(1-rho)/2))
 
 Well, the covariance is (everything has mean zero, of course)
 
 E(XY) = (1+rho)/2*EX^2 + (1-rho)/2*E(X*-X) = rho*EX^2 
 
 The marginal distribution of Y is a mixture of two identical uniforms
 (X and -X) so is uniform and in particular has the same variance as X.
 
 In summary,  EXY/sqrt(EX^2EY^2) == rho
 
 So as I said, it satisfies the formal requirements. X and Y are
 uniformly distributed and their correlation is rho. 
 
 If for nothing else, I suppose that this example is good for
 demonstrating that independence and uncorrelatedness is not the same
 thing.

That was a nice sneaky solution! I was toying with something similar,
but less sneaky, until I saw Peter's, on the lines of

  x-runif(2N, -0.5,0.5); ix-(N-k):(N+k); y-x; y[ix]-(-y[ix])

(which makes the same point about independence and correlation).
The larger k as a fraction of N, the more you swing from rho = 1
to rho = -1, but you cannot achieve, as Peter did, an arbitrary
correlation coefficient rho since the value depends on k which
can only take discrete values.

Another approach which leads to a less special joint distribution
is

  x-sort(runif(N, -0.5,0.5)); y-sort(runif(N, -0.5,0.5))

followed by a rho-dependent permutation of y. I'm still pondering
a way of choosing the permutation so as to get a desired rho.

The extremes are the identity, which for a given sample will
give as close as you can get to rho = +1, and reversal, which
gives as close as you can get to rho = -1.

However, the maximum theoretical rho which you can get (as opposed
to what is possible for particular samples, which may get arbitrarily
close to +1) depends on N. For instance, with N=3, it looks as
though the theoretical rho is about 0.9 with the identity
permutation (for N=1000, however, just about all samples give
rho  0.99).

I smell a source of interesting exam questions ...

Over to you!

Best wishes,
Ted.



E-Mail: (Ted Harding) [EMAIL PROTECTED]
Fax-to-email: +44 (0)870 094 0861
Date: 02-Jul-05   Time: 12:22:09
-- 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


[R] Generating correlated data from uniform distribution

2005-07-01 Thread Menghui Chen
Dear R users,

I want to generate two random variables (X1, X2) from uniform
distribution (-0.5, 0.5) with a specified correlation coefficient r.
Does anyone know how to do it in R?

Many thanks!

Menghui

__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-01 Thread Jim Brennan
 dat-matrix(runif(2000),2,1000)
 rho-.77
 R-matrix(c(1,rho,rho,1),2,2)
 dat2-t(ch)%*%dat
 cor(dat2[1,],dat2[2,])
[1] 0.7513892
 dat-matrix(runif(2),2,1)
 rho-.28
 R-matrix(c(1,rho,rho,1),2,2)
 ch-chol(R)
 dat2-t(ch)%*%dat
 cor(dat2[1,],dat2[2,])
[1] 0.2681669
 dat-matrix(runif(20),2,10)
 rho-.28
 R-matrix(c(1,rho,rho,1),2,2)
 ch-chol(R)
 dat2-t(ch)%*%dat
 cor(dat2[1,],dat2[2,])
[1] 0.2814035

See  ?choleski

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Menghui Chen
Sent: July 1, 2005 4:49 PM
To: r-help@stat.math.ethz.ch
Subject: [R] Generating correlated data from uniform distribution

Dear R users,

I want to generate two random variables (X1, X2) from uniform
distribution (-0.5, 0.5) with a specified correlation coefficient r.
Does anyone know how to do it in R?

Many thanks!

Menghui

__
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

__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-01 Thread Tony Plate
Isn't this a little trickier with non-normal variables?  It sounds like 
Menghui Chen wants variables that have uniform marginal distribution, 
and a specified correlation.

When I look at histograms (or just the quantiles) of the rows of dat2 in 
your example, I see something for dat2[2,] that does not look much like 
it comes from a uniform distribution.

  dat-matrix(runif(2000),2,1000)
  rho-.77
  R-matrix(c(1,rho,rho,1),2,2)
  ch-chol(R)
  dat2-t(ch)%*%dat
  cor(dat2[1,],dat2[2,])
[1] 0.7513892
  hist(dat2[1,])
  hist(dat2[2,])
 
  quantile(dat2[1,])
  0% 25% 50% 75%100%
0.000655829 0.246216035 0.507075912 0.745158441 0.16418
  quantile(dat2[2,])
0%   25%   50%   75%  100%
0.0393046 0.4980066 0.7150426 0.9208855 1.3864704
 

-- Tony Plate

Jim Brennan wrote:
 dat-matrix(runif(2000),2,1000)
 rho-.77
 R-matrix(c(1,rho,rho,1),2,2)
 ch-chol(R)
 dat2-t(ch)%*%dat
 cor(dat2[1,],dat2[2,])
[1] 0.7513892
 
dat-matrix(runif(2),2,1)
rho-.28
R-matrix(c(1,rho,rho,1),2,2)
ch-chol(R)
dat2-t(ch)%*%dat
cor(dat2[1,],dat2[2,])
 
 [1] 0.2681669
 
dat-matrix(runif(20),2,10)
rho-.28
R-matrix(c(1,rho,rho,1),2,2)
ch-chol(R)
dat2-t(ch)%*%dat
cor(dat2[1,],dat2[2,])
 
 [1] 0.2814035
 
 See  ?choleski
 
 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] On Behalf Of Menghui Chen
 Sent: July 1, 2005 4:49 PM
 To: r-help@stat.math.ethz.ch
 Subject: [R] Generating correlated data from uniform distribution
 
 Dear R users,
 
 I want to generate two random variables (X1, X2) from uniform
 distribution (-0.5, 0.5) with a specified correlation coefficient r.
 Does anyone know how to do it in R?
 
 Many thanks!
 
 Menghui
 
 __
 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
 
 __
 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


__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-01 Thread Spencer Graves
  How about tetrachoric correlations?  Generate correlated normal 
observations, then convert to uniform using pnorm:

rho - 0.9
Cor - array(c(1, rho, rho, 1), dim=c(2,2))

library(mvtnorm)

set.seed(1)
Y - rmvnorm(1, sigma=Cor)

X - pnorm(Y)-0.5
plot(X)
hist(X[,1])
hist(X[,2])
cor(X)

  Enjoy.
  spencer graves

Tony Plate wrote:

 Isn't this a little trickier with non-normal variables?  It sounds like 
 Menghui Chen wants variables that have uniform marginal distribution, 
 and a specified correlation.
 
 When I look at histograms (or just the quantiles) of the rows of dat2 in 
 your example, I see something for dat2[2,] that does not look much like 
 it comes from a uniform distribution.
 
   dat-matrix(runif(2000),2,1000)
   rho-.77
   R-matrix(c(1,rho,rho,1),2,2)
   ch-chol(R)
   dat2-t(ch)%*%dat
   cor(dat2[1,],dat2[2,])
 [1] 0.7513892
   hist(dat2[1,])
   hist(dat2[2,])
  
   quantile(dat2[1,])
   0% 25% 50% 75%100%
 0.000655829 0.246216035 0.507075912 0.745158441 0.16418
   quantile(dat2[2,])
 0%   25%   50%   75%  100%
 0.0393046 0.4980066 0.7150426 0.9208855 1.3864704
  
 
 -- Tony Plate
 
 Jim Brennan wrote:
 
dat-matrix(runif(2000),2,1000)
rho-.77
R-matrix(c(1,rho,rho,1),2,2)
ch-chol(R)
dat2-t(ch)%*%dat
cor(dat2[1,],dat2[2,])
 
 [1] 0.7513892
 
dat-matrix(runif(2),2,1)
rho-.28
R-matrix(c(1,rho,rho,1),2,2)
ch-chol(R)
dat2-t(ch)%*%dat
cor(dat2[1,],dat2[2,])

[1] 0.2681669


dat-matrix(runif(20),2,10)
rho-.28
R-matrix(c(1,rho,rho,1),2,2)
ch-chol(R)
dat2-t(ch)%*%dat
cor(dat2[1,],dat2[2,])

[1] 0.2814035

See  ?choleski

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Menghui Chen
Sent: July 1, 2005 4:49 PM
To: r-help@stat.math.ethz.ch
Subject: [R] Generating correlated data from uniform distribution

Dear R users,

I want to generate two random variables (X1, X2) from uniform
distribution (-0.5, 0.5) with a specified correlation coefficient r.
Does anyone know how to do it in R?

Many thanks!

Menghui

__
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

__
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

 
 
 __
 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

-- 
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

[EMAIL PROTECTED]
www.pdf.com http://www.pdf.com
Tel:  408-938-4420
Fax: 408-280-7915

__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-01 Thread Jim Brennan
Yes you are right I guess this works only for normal data. Free advice
sometimes comes with too little consideration :-)
Sorry about that and thanks to Spencer for the correct way.
-Original Message-
From: Tony Plate [mailto:[EMAIL PROTECTED] 
Sent: July 1, 2005 6:01 PM
To: Jim Brennan
Cc: 'Menghui Chen'; r-help@stat.math.ethz.ch
Subject: Re: [R] Generating correlated data from uniform distribution

Isn't this a little trickier with non-normal variables?  It sounds like 
Menghui Chen wants variables that have uniform marginal distribution, 
and a specified correlation.

When I look at histograms (or just the quantiles) of the rows of dat2 in 
your example, I see something for dat2[2,] that does not look much like 
it comes from a uniform distribution.

  dat-matrix(runif(2000),2,1000)
  rho-.77
  R-matrix(c(1,rho,rho,1),2,2)
  ch-chol(R)
  dat2-t(ch)%*%dat
  cor(dat2[1,],dat2[2,])
[1] 0.7513892
  hist(dat2[1,])
  hist(dat2[2,])
 
  quantile(dat2[1,])
  0% 25% 50% 75%100%
0.000655829 0.246216035 0.507075912 0.745158441 0.16418
  quantile(dat2[2,])
0%   25%   50%   75%  100%
0.0393046 0.4980066 0.7150426 0.9208855 1.3864704
 

-- Tony Plate

Jim Brennan wrote:
 dat-matrix(runif(2000),2,1000)
 rho-.77
 R-matrix(c(1,rho,rho,1),2,2)
 ch-chol(R)
 dat2-t(ch)%*%dat
 cor(dat2[1,],dat2[2,])
[1] 0.7513892
 
dat-matrix(runif(2),2,1)
rho-.28
R-matrix(c(1,rho,rho,1),2,2)
ch-chol(R)
dat2-t(ch)%*%dat
cor(dat2[1,],dat2[2,])
 
 [1] 0.2681669
 
dat-matrix(runif(20),2,10)
rho-.28
R-matrix(c(1,rho,rho,1),2,2)
ch-chol(R)
dat2-t(ch)%*%dat
cor(dat2[1,],dat2[2,])
 
 [1] 0.2814035
 
 See  ?choleski
 
 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] On Behalf Of Menghui Chen
 Sent: July 1, 2005 4:49 PM
 To: r-help@stat.math.ethz.ch
 Subject: [R] Generating correlated data from uniform distribution
 
 Dear R users,
 
 I want to generate two random variables (X1, X2) from uniform
 distribution (-0.5, 0.5) with a specified correlation coefficient r.
 Does anyone know how to do it in R?
 
 Many thanks!
 
 Menghui
 
 __
 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
 
 __
 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


__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-01 Thread Peter Dalgaard
Jim Brennan [EMAIL PROTECTED] writes:

 Yes you are right I guess this works only for normal data. Free advice
 sometimes comes with too little consideration :-)

Worth every cent...

 Sorry about that and thanks to Spencer for the correct way.

Hmm, but is it? Or rather, what is the relation between the
correlation of the normals  and that of the transformed variables? 
Looks nontrivial to me.

Incidentally, here's a way that satisfies the criteria, but in a
rather weird way:

N - 1
rho - .6
x - runif(N, -.5,.5)
y - x * sample(c(1,-1), N, replace=T, prob=c((1+rho)/2,(1-rho)/2))

-- 
   O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-01 Thread Jim Brennan
OK now I am skeptical especially when you say in a weird way:-)
This may be OK but look at plot(x,y) and I am suspicious. Is it still
alright with this kind of relationship?

For large N it appears Spencer's method is returning slightly lower
correlation for the uniforms as compared to the normals so maybe there is a
problem!?!

Hope we are all learning something and Menghui gets/has what he wants . :-)

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter
Dalgaard
Sent: July 1, 2005 6:59 PM
To: Jim Brennan
Cc: 'Tony Plate'; 'Menghui Chen'; r-help@stat.math.ethz.ch
Subject: Re: [R] Generating correlated data from uniform distribution

Jim Brennan [EMAIL PROTECTED] writes:

 Yes you are right I guess this works only for normal data. Free advice
 sometimes comes with too little consideration :-)

Worth every cent...

 Sorry about that and thanks to Spencer for the correct way.

Hmm, but is it? Or rather, what is the relation between the
correlation of the normals  and that of the transformed variables? 
Looks nontrivial to me.

Incidentally, here's a way that satisfies the criteria, but in a
rather weird way:

N - 1
rho - .6
x - runif(N, -.5,.5)
y - x * sample(c(1,-1), N, replace=T, prob=c((1+rho)/2,(1-rho)/2))

-- 
   O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph: (+45) 35327918
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-01 Thread Spencer Graves
  Peter is absolutely correct:  The correlation I used was for a 
hidden normal process, not for the resultant correlated uniforms.  This 
is similar to but different from tetrachoric corrrelations, about 
which there is a substantial literature (including an R package 
polycor).

  Why do you want correlated uniforms?  What do they represent 
physically?  Does it matter if you can match exactly a particular 
correlation coefficient, or is it enough to say that they are uniformily 
distributed random variables such that their normal scores have a 
specified correlation coefficient?  There is so much known about the 
multivariate normal distribution and so little about correlated uniforms 
that it might be more useful to know the correlations of latent normals, 
for which your uniforms are what are measured.

  spencer graves

Peter Dalgaard wrote:

 Jim Brennan [EMAIL PROTECTED] writes:
 
 
Yes you are right I guess this works only for normal data. Free advice
sometimes comes with too little consideration :-)
 
 
 Worth every cent...
 
 
Sorry about that and thanks to Spencer for the correct way.
 
 
 Hmm, but is it? Or rather, what is the relation between the
 correlation of the normals  and that of the transformed variables? 
 Looks nontrivial to me.
 
 Incidentally, here's a way that satisfies the criteria, but in a
 rather weird way:
 
 N - 1
 rho - .6
 x - runif(N, -.5,.5)
 y - x * sample(c(1,-1), N, replace=T, prob=c((1+rho)/2,(1-rho)/2))
 

-- 
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

[EMAIL PROTECTED]
www.pdf.com http://www.pdf.com
Tel:  408-938-4420
Fax: 408-280-7915

__
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


Re: [R] Generating correlated data from uniform distribution

2005-07-01 Thread alejandro munoz
Dear Menghui,

You may consider looking in Luc Devroye's Non-uniform Random Number
Generation. Despite its title, section XI.3.2 describes how to
generate bivariate uniforms. The book is out of print but Devroye
himself urges you to print it from his scanned PDFs(!):

http://cgm.cs.mcgill.ca/~luc/rnbookindex.html

Hope this helps,

alejandro

On 7/1/05, Menghui Chen [EMAIL PROTECTED] wrote:
 Dear R users,
 
 I want to generate two random variables (X1, X2) from uniform
 distribution (-0.5, 0.5) with a specified correlation coefficient r.
 Does anyone know how to do it in R?
 
 Many thanks!
 
 Menghui
 
 __
 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


__
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