[R] Mixture of Multivariate Gaussian Sample Data

Ben Bolker ben at zoo.ufl.edu
Wed Dec 4 20:49:03 CET 2002


  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:

> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 

-- 
318 Carr Hall                                bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704




More information about the R-help mailing list