[R] simulate correlated binary, categorical and continuous variable

David Duffy David.Duffy at qimr.edu.au
Sun Apr 1 00:45:03 CEST 2012


Burak Aydin <burak235813 at hotmail.com> asked:

> Lets say I know Pearson covariance matrix.
> When I use rmvnorm to simulate 9 variables and then 
> dichotomize/categorize
> them, I cant retrieve the population covariance matrix.

library(polycor)
sim1 <- function(thresh=0.5, r=0.3) {
  x <- rmvnorm(1000,c(0,0),matrix(c(1,r,r,1), nr=2))
  x[x>thresh] <- 2
  x[x<2] <- 1
  polychor(x[,1], x[,2])
}

tr <- double(100)
for(i in 1:100) tr[i] <- sim1()
summary(tr)
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  0.1571  0.2703  0.3041  0.3010  0.3328  0.4062



-- 
| David Duffy (MBBS PhD)                                         ,-_|\
| email: davidD at qimr.edu.au  ph: INT+61+7+3362-0217 fax: -0101  /     *
| Epidemiology Unit, Queensland Institute of Medical Research   \_,-._/
| 300 Herston Rd, Brisbane, Queensland 4029, Australia  GPG 4D0B994A v



More information about the R-help mailing list