# [R] simulating correlated vars

From: Andrej Kveder (andrejk@zrc-sazu.si)
Date: Thu 20 Feb 2003 - 04:48:38 EST

```Message-id: <FHEEJBDDCNPPNJEACDJACEIECJAA.andrejk@zrc-sazu.si>

```

Hi!

I'm running monte carlo simulations and I'm thus creating pseudo samples of
an imaginary population. Of course vars in this population are correlated.
There is no problem as far as they are purely numeric (normal or nonnormal).
I use the chol() transformation of the correlation matrix to get the things
done. Does anybody have any experience in including a dichotomous (binomial)
variable to the show? I tried it in noumerous ways using all sorts of
different starting distributions, however none seem to work... either I
don't get the wanted proportion in the final modification or I ruin the
correlation with the final transformation. I would be very glad of any
insight how to solve the problem...

I give an example using binomial distribution as a start... I also tried
skewed and nonskewed normal, and various beta distributions and none worked.
n<-1000 #sample size
p<-0.3 #proportion
r12<-0.4 #correlation
x1<-rbinom(n,1,p)
x1<-(x1-p)/sqrt(p*(1-p))
x2<-rnorm(n,0,1)
r<-array(1,c(2,2))
r[1,2]<-r12
r[2,1]<-r12
r.ch<-chol(r)
d.1<-cbind(x1,x2)
d.1<-d%*%r.ch
d.1[,1]<-round((d.1[,1]*sqrt(p*(1-p)))+p)
#d.1[,1]<-round(d.1[,1]+p)*sqrt(p*(1-p))
cor(d.1)
mean(d.1[,1])

Andrej

_________
Andrej Kveder, M.A.
researcher
Institute of Medical Sciences SRS SASA; Novi trg 2, SI-1000 Ljubljana,
Slovenia
phone: +386 1 47 06 440 fax: +386 1 42 61 493

[[alternate HTML version deleted]]

______________________________________________
R-help@stat.math.ethz.ch mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help

This archive was generated by hypermail 2.1.3 : Tue 01 Jul 2003 - 09:11:22 EST