2015-06-22 97 views
1

我们有七次暴露和24组。我们希望将七次曝光中的五次随机分配到组中,同时还要确保每次曝光的结果都是一致的,这意味着每次曝光最终都会暴露相同的次数。我写了一些代码,但我无法控制每次曝光显示的次数。例如:使用R分配治疗组

exposures <- c("A", "B", "C", "D", "E", "F", "G") 
groups <- c(1:24) 

table <- c() 

for (i in 1:24){ 
    draw <- sample(exposures, size=5, replace=F) 
    table <- rbind(table, draw) 
} 

table(table) 

所以计数结束了一些接近,但有什么我可以做,以确保每次曝光的最低限度?谢谢!

编辑此外,我们需要每个曝光组每次只出现一次。

+0

如果组的数量乘以每组曝光次数不是总曝光次数的整数倍(在这种情况下不是这种情况下...... 120不是7的整数倍) – Bridgeburners

+1

@Bridgeburners我看到“大约相同的次数”和“确保每个参数的最小值”。 – Frank

回答

1

根据未使用的两次曝光而非五次曝光,可以更容易地想到它。让我们限制时间的曝光可以排除数量:

draw_exc <- function(exposures,nexp,ng,max_excluded = 10){ 

    nexc <- length(exposures)-nexp 

    exp_rem <- exposures 
    exc  <- matrix(,ng,nexc) 
    for (i in 1:ng){ 
    pool <- combn(exp_rem,nexc) 
    draw <- pool[,sample(1:ncol(pool), 1)] 

    exc[i,] <- draw 

    tab  <- table(exc) 
    exp_rem <- setdiff(exp_rem, names(tab[tab > max_excluded])) 
    } 
    exc 
} 

这里有一个例证:

set.seed(1) 
exc  <- draw_exc(exposures,5,24,10) 
assignment <- apply(exc,1,function(x) setdiff(exposures,x)) 

table(exc) 
# exc 
# A B C D E F G 
# 7 4 6 6 8 10 7 

table(assignment) 
# assignment 
# A B C D E F G 
# 17 20 18 18 16 14 17 

所以,用24组,排除的最大数目等于24减去出场的最小数量。这个循环效率不高,但似乎完成了这项工作。

+0

谢谢弗兰克!这是最有帮助的。 – armbreaker284