治疗组中随机分组受阻

时间:2019-08-21 14:15:37

标签: stata

我有1200个患者,我想在4个随机组 5个区块中进行分配。

我可以如下创建组:

generate groups=1
replace groups=2 if id>300
replace groups=3 if id>600
replace groups=4 if id>900

但是,有没有一种方法可以使这些组成块随机出现?

1 个答案:

答案 0 :(得分:1)

您可以首先使用seq()命令的egen函数来生成组和块:

clear
set obs 1200

egen groups = seq(), from(1) to(4)
egen blocks = seq(), block(5)

然后,您需要在块内的随机变量上对数据进行排序:

generate random = runiform()
sort blocks (random)

结果(前十个观察结果):

list in 1/10

     +----------------------------+
     | groups   blocks     random |
     |----------------------------|
  1. |      3        1   .0454974 |
  2. |      4        1   .4462283 |
  3. |      1        1   .5405352 |
  4. |      1        1   .6910226 |
  5. |      2        1   .7799934 |
     |----------------------------|
  6. |      2        2   .0061428 |
  7. |      4        2   .0816054 |
  8. |      2        2   .3310352 |
  9. |      3        2   .4107759 |
 10. |      1        2   .6661102 |
     +----------------------------+

此处提供的解决方案是我在previous answer中提出的第一个解决方案的扩展。