在组内随机分配R中的整数而无需替换 [英] Randomly Assign Integers in R within groups without replacement

查看:197
本文介绍了在组内随机分配R中的整数而无需替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行两项实验的实验:experiment_1和experiment_2。每个实验具有5种不同的处理(即1,2,3,4,5)。我们试图在组内随机分配治疗。

I am running an experiment with two experiments: experiment_1 and experiment_2. Each experiment has 5 different treatments (i.e. 1, 2, 3, 4, 5). We are trying to randomly assign the treatments within groups.

我们希望通过抽样来实现这一点,而不是在每个组内迭代替换。我们希望这样做是为了确保我们在治疗中获得尽可能平衡的样本(例如,我们不希望第1组中的4名受试者被分配到治疗2而没有人获得治疗1)。因此,如果一个群体有23个受试者,我们希望将受访者分成5个子群体和3个子群体。然后我们想要在5个第一个子群体中随机抽样而不进行替换,因此每个人都被分配了1个治疗,为第二,第三和第四小组5做同样的事情,并为最终小组3随机抽样而不替换。因此,我们保证每项治疗分配至少4个科目,3个科目分配给该组中的5个科目。我们希望为实验中的所有组和两种治疗方法都这样做。结果输出看起来像这样......

We would like to do this via sampling without replacement iteratively within each group. We want to do this to insure that we get as a balanced a sample as possible in the treatment (e.g. we don't want to end up with 4 subjects in group 1 getting assigned to treatment 2 and no one getting treatment 1). So if a group has 23 subjects, we want to split the respondent into 4 subgroups of 5, and 1 subgroup of 3. We then want to randomly sample without replacement across the first subgroup of 5, so everyone gets assigned 1 of the treatments, do the same things for the the second, third and 4th subgroup of 5, and for the final subgroup of 3 randomly sample without replacement. So we would guarantee that every treatment is assigned to at least 4 subjects, and 3 are assigned to 5 subjects within this group. We would like to do this for all the groups in the experiment and for both treatments. The resultant output would look something like this...

         group   experiment_1   experiment_2
    [1,]     1           5             3
    [2,]     1           3             2
    [3,]     1           4             4
    [4,]     1           1             5
    [5,]     1           2             1
    [6,]     1           2             3
    [7,]     1           4             1
    [8,]     1           3             2
    [9,]     2           5             5
   [10,]     2           1             4
   [11,]     2           3             4
   [12,]     2           1             5
   [13,]     2           2             1
      .      .           .             .
      .      .           .             .
      .      .           .             .

我知道如何使用示例函数,但我不确定如何在每组内没有替换的情况下进行采样,以便我们的输出符合上述程序。任何帮助将不胜感激。

I know how to use the sample function, but am unsure how to sample without replacement within each group, so that our output corresponds to above described procedure. Any help would be appreciated.

推荐答案

这个功能怎么样:

f <- function(n,m) {sample( c( rep(1:m,n%/%m), sample(1:m,n%%m) ), n )}

n是组大小,m是治疗次数。
每组治疗必须至少含有n%/%m次。
剩余的n %% m组成员的治疗数字是
任意分配而不重复。
向量c(rep(1:m,n%/%m),样本(1:m,n %% m))包含这些处理编号。最后,样本函数
扰乱了这些数字。

"n" is the group size, "m" the number of treatments. Each treatment must be containt at least "n %/% m" times in the group. The treatment numbers of the remaining "n %% m" group members are assigned arbitrarily without repetition. The vector "c( rep(1:m,n%/%m), sample(1:m,n%%m) )" contains these treatment numbers. Finally the "sample" function perturbes these numbers.

> f(8,5)
[1] 5 3 1 5 4 2 2 1
> f(8,5)
[1] 4 5 3 4 2 2 1 1
> f(8,5)
[1] 4 2 1 5 3 5 2 3

这是一个使用上述函数创建数据帧的函数:

Here is a function that creates a dataframe, using the above function:

Plan <- function( groupSizes, numExp=2, numTreatment=5 )
{
  numGroups <- length(groupSizes)
  df <- data.frame( group = rep(1:numGroups,groupSizes) )

  for ( e in 1:numExp )
  {
    df <- cbind(df,unlist(lapply(groupSizes,function(n){f(n,numTreatment)})))
    colnames(df)[e+1] <- sprintf("Exp_%i", e)
  }
  return(df)
}

示例:

> P <- Plan(c(8,23,13,19))
> P
   group Exp_1 Exp_2
1      1     4     1
2      1     1     4
3      1     2     2
4      1     2     1
5      1     3     5
6      1     5     5
7      1     1     2
8      1     3     3
9      2     5     1
10     2     2     1
11     2     5     2
12     2     1     2
13     2     2     1
14     2     1     4
15     2     3     5
16     2     5     3
17     2     2     4
18     2     5     4
19     2     2     5
20     2     1     1
21     2     4     2
22     2     3     3
23     2     4     3
24     2     2     5
25     2     3     3
26     2     5     2
27     2     1     5
28     2     3     4
29     2     4     4
30     2     4     2
31     2     4     3
32     3     2     5
33     3     5     3
34     3     5     1
35     3     5     1
36     3     2     5
37     3     4     4
38     3     1     4
39     3     3     2
40     3     3     2
41     3     3     3
42     3     1     1
43     3     4     2
44     3     4     4
45     4     5     1
46     4     3     1
47     4     1     2
48     4     1     5
49     4     3     3
50     4     3     1
51     4     4     5
52     4     2     4
53     4     5     3
54     4     2     1
55     4     4     2
56     4     2     5
57     4     4     4
58     4     5     3
59     4     5     4
60     4     1     2
61     4     2     5
62     4     3     2
63     4     4     4

检查分配:

> with(P,table(group,Exp_1))
     Exp_1
group 1 2 3 4 5
    1 2 2 2 1 1
    2 4 5 4 5 5
    3 2 2 3 3 3
    4 3 4 4 4 4
> with(P,table(group,Exp_2))
     Exp_2
group 1 2 3 4 5
    1 2 2 1 1 2
    2 4 5 5 5 4
    3 3 3 2 3 2
    4 4 4 3 4 4
> 

这篇关于在组内随机分配R中的整数而无需替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆