[R] generating a random signal with a known correlation

Yasir Kaheil kaheil at gmail.com
Tue Aug 12 06:14:49 CEST 2008


OK, here is the code for those who are interested:
x<-rnorm(1000, mean=20, sd=100);
x.s<-scale(x)
xp<-pnorm(x.s);
xix<- sort(xp,index.return=TRUE)$ix
norm.cop <- normalCopula(0.6) #correlation of 0.6, other libraries can be
used instead of copula
y <- rcopula(norm.cop, length(x))
yp<-pnorm(y)
ypix<- sort(yp[,1],index.return=TRUE)$ix
yp<-yp[ypix,];
yps<-yp[,1];# dummy initialization
yps[xix]<-yp[,2];
#adjust mean and sd here.
yqs<-qnorm(yps,mean=mean(x),sd=sd(x));

cor(x,yqs)




Yasir Kaheil wrote:
> 
> Hi,
> How can I generate a random signal that's correlated with a given signal
> at a given correlation (say 0.7)?
> I've been looking at rmvnorm etc but don't seem to figure it out. Thanks
> 
> 


-----
Yasir H. Kaheil
Columbia University
-- 
View this message in context: http://www.nabble.com/generating-a-random-signal-with-a-known-correlation-tp18932541p18937754.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list