Skip to content

Mixture of Multivariate Gaussian Sample Data

2 messages · Feng Zhang, Ben Bolker

#
Hey, I am confused about how to generate the sample data from a mixture of
Multivariate Gaussian ditribution.
For example, there are 2 component Gaussian with prior
probability of 0.4 and 0.6,
the means and variances are
u1=[1 1]', Cov1=[1 0;0 1]
and
u2=[-1 -1]', Cov2=[1 0;0 1]
repectively.

So how can I generate a sample of 500 data from the above mixture
distribution?

Thanks.

Fred
#
Your problem is actually very simple since your variables are all
uncorrelated (diagonal covariance matrices), so you don't really need to
treat it as a multivariate problem, *but* -- assuming you will need to do 
this for correlated variables sometime ...
 
  You need the mvrnorm() command from the MASS library.
  This solution is slightly complicated in an attempt to come up with an 
efficient answer ...

library(MASS)
S1 <- matrix(c(1,0,0,1),nrow=2,byrow=TRUE)
mu1 <- c(1,1)
S2 <- matrix(c(1,0,0,1),nrow=2,byrow=TRUE)
mu2 <- c(-1,-1)

n <- 500
p1 <- 0.4
n1 <- rbinom(1,size=n,prob=p1)  ## how many from first distribution?
n2 <- n-n1
val1 <- mvrnorm(n1,mu=mu1,Sigma=S1)
val2 <- mvrnorm(n2,mu=mu2,Sigma=S2)
allval <- rbind(val1,val2)      ## combine
allval <- allval[sample(n,n),]  ## scramble order
On Wed, 4 Dec 2002, Feng Zhang wrote: