James Milks
2007-Jun-12 20:42 UTC
[R] Generating artificial datasets with a specific correlation coefficient.
I need to create artificial datasets with specific correlation coefficients (i.e. a dataset that returns r = 0.30, etc.) as examples for a lab I am teaching this summer. Is there a way to do that in R? Thanks. Jim Milks Graduate Student Environmental Sciences Ph.D. Program 136 Biological Sciences Wright State University 3640 Colonel Glenn Hwy Dayton, OH 45435 [[alternative HTML version deleted]]
Ken Knoblauch
2007-Jun-12 20:54 UTC
[R] Generating artificial datasets with a specific correlationcoefficient.
see mvrnorm in MASS and especially the empirical argument James Milks <james.milks <at> wright.edu> writes:> I need to create artificial datasets with specific correlation > coefficients (i.e. a dataset that returns r = 0.30, etc.) as examples > for a lab I am teaching this summer. Is there a way to do that in R? > > Thanks. > > Jim Milks
(Ted Harding)
2007-Jun-12 23:36 UTC
[R] Generating artificial datasets with a specific correlati
On 12-Jun-07 20:54:05, Ken Knoblauch wrote:> see mvrnorm in MASS and especially the empirical argument > > James Milks <james.milks <at> wright.edu> writes: > > >> I need to create artificial datasets with specific correlation >> coefficients (i.e. a dataset that returns r = 0.30, etc.) as examples >> for a lab I am teaching this summer. Is there a way to do that in R? >> >> Thanks. >> >> Jim MilksAlternatively, if you would prefer your datasets to have non-nomal distributions, consider the fact that if X and Y are independent, each with mean 0 and variance 1, then the correlation coefficient between (X + a*Y) and (X - a*Y) is (1 - a^2)/(1 + a^2) so if you choose a = sqrt((1 - r)/(1 + r)) then these will have correlation coefficient r. So generate X and Y as you please, and then continue as above. Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 13-Jun-07 Time: 00:35:59 ------------------------------ XFMail ------------------------------
John Vokey
2007-Jun-14 03:14 UTC
[R] Generating artificial datasets with a specific correlation coefficient.
Jim, Try here: <http://www.sitmo.com/doc/ Generating_Correlated_Random_Numbers>; the example code is in matlab, but it is trivial to translate to R. Note, if you use principal components as the source vectors (thanks to Jason Tangen for the suggestion), the correlations between the resulting vectors will be exactly the specified r-values. On 13-Jun-07, at 4:00 AM, r-help-request at stat.math.ethz.ch wrote:> I need to create artificial datasets with specific correlation > coefficients (i.e. a dataset that returns r = 0.30, etc.) as examples > for a lab I am teaching this summer. Is there a way to do that in R?-- Please avoid sending me Word or PowerPoint attachments. See <http://www.gnu.org/philosophy/no-word-attachments.html> -Dr. John R. Vokey
James R. Milks
2007-Jun-14 11:00 UTC
[R] Generating artificial datasets with a specific correlation coefficient.
Thanks to all who responded to my request. I'll probably use the corgen function in ecodist, as suggested by Sarah Goslee (below), as I just need examples of correlated vectors. Thanks again. Jim Milks Graduate Student Environmental Sciences Ph.D. Program 136 Biological Sciences Wright State University 3640 Colonel Glenn Hwy Dayton, OH 45435 Begin forwarded message:> From: Sarah Goslee <sarah.goslee@gmail.com> > Date: June 12, 2007 5:37:36 PM EDT > To: James Milks <james.milks@wright.edu> > Subject: Re: [R] Generating artificial datasets with a specific > correlation coefficient. > > If you don't need anything fancy, just two vectors x and y > such that cor(x, y) = 0.30 then the corgen function in > ecodist (current version available from CRAN) does just that. > > Sarah > > On 6/12/07, James Milks <james.milks@wright.edu> wrote: >> I need to create artificial datasets with specific correlation >> coefficients (i.e. a dataset that returns r = 0.30, etc.) as examples >> for a lab I am teaching this summer. Is there a way to do that in R? >> >> Thanks. >> >> Jim Milks >> >> Graduate Student >> Environmental Sciences Ph.D. Program >> 136 Biological Sciences >> Wright State University >> 3640 Colonel Glenn Hwy >> Dayton, OH 45435 >> >> >> > > -- > Sarah Goslee > http://www.functionaldiversity.org[[alternative HTML version deleted]]