[R] sampling from a mixture distribution

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.ac.be
Wed Mar 23 15:14:44 CET 2005

you have also to sample the mixture compoment membership; check this 
for a mixtrue of two normals:

rnorm.mixture <- function(n, prob=0.5, mu1=0, sigma1=1, mu2=0, 
    u <- runif(n)
    out <- numeric(n)
    for(i in 1:n) out[i] <- if(u[i] < prob) rnorm(1, mu1, sigma1) else 
rnorm(1, mu2, sigma2)
hist(rnorm.mixture(1000, prob=0.6, mu1=-1, sigma1=0.5, mu2=2, 

I hope it helps.


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/

----- Original Message ----- 
From: "Vumani Dlamini" <dvumani at hotmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, March 23, 2005 2:53 PM
Subject: [R] sampling from a mixture distribution

> Dear R users,
> I would like to sample from a mixture distribution 
> p1*f(x1)+p2*f(x2). I usually sample variates from both distributions 
> and weight them with their respective probabilities, but someone 
> told me that was wrong. What is the correct way?
> Vumani
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html

More information about the R-help mailing list