Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: from normal to bimodal distribution


From   Maarten buis <maartenbuis@yahoo.co.uk>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: from normal to bimodal distribution
Date   Mon, 17 Nov 2008 10:44:57 +0000 (GMT)

--- Linn Renée Naper <linn.naper@ecgroup.no> wrote:
> How do I tell Stata to draw random variables or generate a stochastic
> variable with a bimodal distribution? Can the rnd-command by Hilde be
> used?

You can create a mixture of Gaussian (normal) distributions, like in
the example below: the local p represents the probability of belonging
to group 1, the locals mu1 and mu2 represents the means of group 1 and
2 respectively, and the locals sd1 and sd2 the standard deviations in
group 1 and group 2. 

*--------------- begin example -----------------
drop _all
set obs 10000

local p = .5

local sd1 = .75
local sd2 = 1.5

local mu1 = -2
local mu2 = 2

gen u = uniform()
gen e = invnorm(uniform()) * ///
        cond(u < `p', `sd1', `sd2') + ///
        cond(u < `p', `mu1', `mu2')

hist e
*---------------- end example ---------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten


-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room N515

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index