[R] Simulate data based on correlation coefficient

joris.dewolf at cropdesign.com joris.dewolf at cropdesign.com
Sun Oct 7 10:04:55 CEST 2007



Try this:

rnormcor <- function(x,rho) rnorm(1,rho*x,sqrt(1-rho^2))

a <- rnorm(1000,0,1)
b <- sapply(a, rnormcor, rho = 0.7)

cor(a,b)


Joris










                                                                           
             "Gustavo"                                                     
             <gugarl1980 at yahoo                                             
             .com.br>                                                   To 
             Sent by:                  <r-help at r-project.org>              
             r-help-bounces at r-                                          cc 
             project.org                                                   
                                                                   Subject 
                                       [R] Simulate data based on          
             06/10/2007 04:48          correlation coefficient             
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




Hello all,



I have a vector with 1000 values and I would like to generate other
correlated vector, but with different correlation coefficient (for example,

r = 0.7).

Any ideas how can I do this?



Regards,

Gustavo.






_______________________________________________________

Experimente já e veja as novidades.

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



More information about the R-help mailing list