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
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,
sigma2=1){
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)
out
}
########
hist(rnorm.mixture(1000, prob=0.6, mu1=-1, sigma1=0.5, mu2=2,
sigma2=0.5))
I hope it helps.
Best,
Dimitris
----
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/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- 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
>
For each variate, generate it from f1() with probability p1, and from f2() with probability p2. In other words, flip a p1-biased coin to decide which distribution, f1 or f2, to generate from. HTH, Giovanni> Date: Wed, 23 Mar 2005 13:53:10 +0000 > From: Vumani Dlamini <dvumani at hotmail.com> > Sender: r-help-bounces at stat.math.ethz.ch > Precedence: list > > 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 > >-- __________________________________________________ [ ] [ Giovanni Petris GPetris at uark.edu ] [ Department of Mathematical Sciences ] [ University of Arkansas - Fayetteville, AR 72701 ] [ Ph: (479) 575-6324, 575-8630 (fax) ] [ http://definetti.uark.edu/~gpetris/ ] [__________________________________________________]
Here's one possible way:
rmix2 <- function(n, p1, rF1, rF2, argF1=NULL, argF2=NULL) {
## n is the number of deviates to simulate
## p1 is the probability of a point coming from the 1st component
## rF1, rF2 are functions for generating random deviates
## from the two components
## argF1, argF2 are lists of arguments to rF1 and rF2
n1 <- rbinom(1, n, p1)
n2 <- n - n1
x1 <- do.call("rF1", c(list(n1), argF1))
x2 <- do.call("rF2", c(list(n2), argF2))
c(x1, x2)
}
To test:
x <- rmix2(1000, 0.3, rnorm, rnorm, list(mean=5))
hist(x)
HTH,
Andy
> From: Vumani Dlamini
>
> 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
>
>
>
> I would like to sample from a mixture distribution p1*f(x1)+p2*f(x2).***Surely*** you mean ``p1*f1(x)+p2*f2(x)'' !!!> 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?If you want a sample of size n, first generate n1 by n1 <- rbinom(1,n,p1) Then generate a vector x1 equal to n1 observations from the f1(x) distribution and a vector x2 equal to n2 = n-n1 observations from the f2(x) distribution. Finally combine the two vectors of observations into a single vector: x <- c(x1,x2) You can then shuffle the order of x x <- sample(x,n) if you want to be obsessive about it. cheers, Rolf Turner rolf at math.unb.ca