治疗组中的随机分配

时间:2019-08-21 05:10:47

标签: stata

我有1200名患者要分配到4个随机治疗组中。

我可以如下创建组:

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

但是,如何使这些组中的患者分配随机?

1 个答案:

答案 0 :(得分:1)

您只需要创建一个随机变量进行排序:

clear
set obs 1200

generate random = runiform()
sort random

然后,您可以使用seq()命令的egen功能:

egen groups = seq(), from(1) to(4)

tabulate groups

     groups |      Freq.     Percent        Cum.
------------+-----------------------------------
          1 |        300       25.00       25.00
          2 |        300       25.00       50.00
          3 |        300       25.00       75.00
          4 |        300       25.00      100.00
------------+-----------------------------------
      Total |      1,200      100.00

不太直观的cond()函数也可以使用:

generate groups = cond(_n <= 300, 1, cond(_n <= 600, 2, cond(_n <= 900, 3, 4)))