Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Making a sample of the specified size from the elements of a vector. What is the equivalent of R's sample() in Stata?

    Hello,

    I would like to make a sample of the specified size from the elements of a vector. Let's say that you have an abnormal die with six sides unequal. Sides 1 and 6, for example, are twice as wide as the other sides. It's easy to see that when you roll this die repeatedly, the probabilities of landing on each side are 2/8, 1/8, 1/8, 1/8, 1/8, and 2/8, respectively. Suppose that we roll this die a thousand times. How many times will each side appear? R has a simple function sample() that answers this question.

    set.seed(10101)
    X=sample(1:6, 1000, replace=TRUE, prob=c(2/8, 1/8, 1/8, 1/8. 1/8, 2/8))
    table (X)
    277 126 113 122 122 240

    As expected, 1 and 6 appear about 250 times, respectively, while the others appear about 125 times, respectively.

    I haven't found such a single function or command in Stata 15.1. How can I achieve the same or similar result in Stata?

    Sang-soo

  • #2
    There are various ways to accomplish what you want in Stata, and which one is most convenient would depend on what you want to do with the result. Presuming that what you want can be described as "generate a new variable in a Stata data set with values drawn from a multinomial random variable with range 1, ..., 6 and a specified list of probabilities," you could use the built-in -irecode- function to do this:
    Code:
    clear
    set obs 10000
    gen rand = runiform()
    gen x = 1 + irecode(rand, 2/8, 3/8, 4/8, 5/8, 6/8, 1)   
    tab x
    See -help irecode()-

    Stata lacks a multinomial random variable function, which I think is strange given its wide range of random variable functions. However, Stata's Mata matrix language does have such a function, -rdiscrete()-, and it also could be used here:
    Code:
    clear
    set obs 10000
    mata: x = rdiscrete(10000, 1, (2/8, 1/8, 1/8, 1/8, 1/8, 2/8))
    getmata x = x
    tab x
    If you truly want a vector, i.e., an N X 1 matrix, rather than a variable within a Stata data set, I'd use the Mata language approach.

    Comment


    • #3
      Mike, Much Thanks!

      Comment

      Working...
      X