Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: random uniform variable with unequal probabilities


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: random uniform variable with unequal probabilities
Date   Mon, 19 Nov 2012 12:24:38 +0000

You can use the Mata function -rdiscrete()-.

. sysuse auto, clear
(1978 Automobile Data)

. gen foo = .
(74 missing values generated)

. mata
------------------------------------------------- mata (type end to
exit) ------------------------------------------------------------------
: prob = J(10,1,.8/9)

: which = ceil(10 * runiform(1,1))

: which
  7

: prob[which] = .2

: st_store(., "foo", 9 :+ rdiscrete(st_numscalar("c(N)"), 1, prob))

: end
--------------------------------------------------------------------------------------------------------------------------------------------

. tab foo

        foo |      Freq.     Percent        Cum.
------------+-----------------------------------
         10 |          3        4.05        4.05
         11 |          6        8.11       12.16
         12 |          6        8.11       20.27
         13 |          5        6.76       27.03
         14 |          4        5.41       32.43
         15 |         11       14.86       47.30
         16 |         16       21.62       68.92
         17 |          6        8.11       77.03
         18 |          9       12.16       89.19
         19 |          8       10.81      100.00
------------+-----------------------------------
      Total |         74      100.00



On Mon, Nov 19, 2012 at 11:44 AM, Tomáš Houška <xbender@gmail.com> wrote:

> I need to generate a random variable from uniform distribution, but
> the probabilities are not not same for each option. The variable is
> discrete in interval [10,19] and is generated 10times. Each time one
> of the options has twice as high probability of occuring as the
> others. So in some sence, the draw is two-staged - in the first stage
> a random number (n) between 1 and 10 is generated and then that
> particular n-th option in the interval [10,19] has a probability 0.2
> of occuring, the other options have a probability 0.8/9 each.
>
> How can I generate such variable?

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


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