Stata - How to Generate Random Integers

为君一笑 提交于 2019-12-11 06:29:56

问题


I am learning Stata and want to know how to generate random integers (without replacement). If I had 10 total rows, I would want each row to have a unique integer from 1 to 10 assigned to it. In R, one could simply do:

sample(1:10, 10)

But it seems more difficult to do in Stata. From this Stata page, I saw:

generate ui = floor((b-a+1)*runiform() + a)

If I substitute a=1 and b=10, I get something close to what I want, but it samples with replacement.

After getting that part figured out, how would I handle the following wrinkle: my data come in pairs. For example, in the 10 observations, there are 5 groups of 2. Each group of 2 has a unique identifier. How would I arrange the groups (and not the observations) in random order? The data would look something like this:

obs   group  mem     value
1     A      x       9345
2     A      y       129
3     B      x       251
4     B      y       373
5     C      x       788
6     C      y       631
7     D      x       239
8     D      y       481
9     E      x       224
10    E      y       585  

obs is the observation number. group is the group the observation (row) belongs to. mem is the member identifier in the group. Each group has one x and one y in it.


回答1:


First question:

You could just shuffle observation identifiers.

set obs 10
gen y = _n 
gen rnd = runiform()
sort rnd 

Or in Mata

jumble(1::10)

Second question: Several ways. Here's one.

gen rnd = runiform() 
bysort group (rnd): replace rnd = rnd[1] 
sort rnd 

General comment: For reproducibility, set the random number seed beforehand.

set seed 2803 

or whatever.



来源:https://stackoverflow.com/questions/24147881/stata-how-to-generate-random-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!