# Re: [R] Generate Random Samples

From: Moshe Olshansky <m_olshansky_at_yahoo.com>
Date: Thu, 12 Jun 2008 16:31:15 -0700 (PDT)

To answer your specific question, you can use mvrnorm (from MASS, i.e. library(MASS)) to generate each component. To generate a mixture with three components (Prob(1 st component) = p1, Prob(2nd component) = p2, Prob(3rd component) = p3, p1+p2+p3=1), you can generate a uniformly distributed variable X in (0,1) and then generate the 1st component if X < p1, the 2nd one if p1 <= X < p1+p2 and the 3rd one if X >=p1+p2.

• On Fri, 13/6/08, Peng Jiang <jp021_at_sjtu.edu.cn> wrote:

> From: Peng Jiang <jp021_at_sjtu.edu.cn>
> Subject: [R] Generate Random Samples
> To: r-help@r-project.org
> Received: Friday, 13 June, 2008, 1:24 AM
> Hi,
>
> I am a newbie to R and I am working with a Mac.
>
> Is there any package that I can use to generate random
> samples from a
> user defined distribution ? That is , I define a
> distribution
> function ( maybe multi dimension ) and I want some random
> samples
> generated from my this distribution.
>
> Or, there is a more specific problem . If I have a three
> component
> mixture with each of them being normal distribution( say 3
>
> dimension ) , is there any package that I can use to
> generate random
> samples from this mixture . I know I can generate random
> samples from
> each individual component. However, can I just add them
> directly????
>
> Thanks.
>
>
>
>
>
>
>
>
>
>
> --------------------------
> Peng Jiang
> 江鹏
> Ph.D. Candidate
>
> Antai College of Economics & Management
> 安泰经济管理学院
> Department of Mathematics
> 数学系
> Shanghai Jiaotong University (Minhang Campus)
> 200240 Shanghai
> P. R. China
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help