Re: [R] Mixture of Distributions

From: Ted Harding <>
Date: Thu, 21 Feb 2008 21:39:56 +0000 (GMT)

On 21-Feb-08 20:58:25, Evgenia wrote:
> Dear R users,
> I would like to sample from a mixture distribution
> p1*f1+p2*f2+p3*f3 with f1,f2,f3 three different forms
> of distributions. I know that in the case of two
> distributions I have to sample the mixture compoment
> membership.
> How can I weight my three distributions with their
> respective probabilities?
> Evgenia

There are several ways in which you could write code to do this, but all amount to the fact that each value sampled from such a mixture is obtained by a) choose between f1, f2, f3 at random according to the

   probabilities p1, p2, p3 (it is assumed p1+p2+p3=1). b) sample 1 value from whichever of f1, f2, f3 was chosen.


Suppose the functions rf1(n), rf2(n), rf3(n) respectively sample n values from f1, f2 and f3.

S0 <- cbind(rf1(n),rf2(n),rf3(n))
ix <- sample(c(1,2,3),n,3,prob=c(p1,p2,p3),replace=TRUE)

S <- S0[,ix]

will produce n values, each of which is a sample of size 1 from the mixture.


rf1 <- function(n){rnorm(n,0,1)} ## normal distribution with mean=0
rf2 <- function(n){rnorm(n,4,1)} ## normal distribution with mean=4
rf3 <- function(n){rnorm(n,9,1)} ## normal distribution with mean=9

p1 <- 0.2; p2 <- 0.3; p3 <-0.5
S0 <- cbind(rf1(500),rf2(500),rf3(500))

ix <- sample(c(1,2,3),500,prob=c(p1,p2,p3),replace=TRUE) S <- S0[,ix]

Hoping this helps.

E-Mail: (Ted Harding) <> Fax-to-email: +44 (0)870 094 0861
Date: 21-Feb-08                                       Time: 21:39:53
------------------------------ XFMail ------------------------------

______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 21 Feb 2008 - 21:42:03 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Feb 2008 - 23:30:15 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive