0

In R I can sample from a multivariate normal distribution as follows:

n         <- 100 # sample size
mu        <- c(1, 5) # mean for each distribution           
Va        <- c(0.2, 1) # variation for each distribution        
CORa      <- 0 # correlation between the two                     
Cova      <- CORa*(sqrt(Va[1])*Va[2])  # covariance between the two
VCVmatrix <- matrix(c(Va[1], Cova, Cova, Va[2]), 2, 2) # variance-covariance matrix
MVNData   <- mvrnorm(n, mu, VCVmatrix) # function for multivariate randome normal sampling     

But I want to be able to replicate this without using the mvrnorm function (I've to create these data in a different language that doesn't have this function).

Someone at stackoverflow suggested this but it's not working out exactly right:

rho <- 0
dim1 <- rnorm(100, 1, 0.2)
dim2 <- rho * dim1 + sqrt(1 - rho ^ 2) * rnorm(100, 5, 1)

If I compare the histograms of the distributions from the two methods, dim1 seems to have a much smaller variation than MVNData[,1].

hist(MVNData[,1], col='blue')
hist(dim1, col='red',add=T)

hist(MVNData[,2], col='blue')
hist(dim2, col='red',add=T)

Hope you can help.

adkane
  • 646
  • 7
  • 18
  • Please clarify: in what way is it not working out? – mikeTronix Jun 27 '18 at 21:52
  • @mikeTronix ,I've clarified now, it's difference in variation between MVNData[,1] and dim1 – adkane Jun 27 '18 at 22:00
  • Well *of course* they have different amounts of variation, because you *specify* different amounts in your calls to `rnorm`! Set the two sd arguments (which currently are 0.2 and 1) to the same value and try again. – whuber Jun 27 '18 at 23:45
  • See this post to simulate by hand, the code inside ``mvrnorm`` is similar https://stat.ethz.ch/pipermail/r-help/2007-April/128925.html – Robert Jun 28 '18 at 02:59
  • @whuber but I thought for the multivariate case I could sample from two different normals. In both approaches one dist has mean 1 sd 0.2 and the other mean 5 sd 1 – adkane Jun 28 '18 at 07:58

0 Answers0